TweetFollow Us on Twitter

Dec 98 Online

Volume Number: 14 (1998)
Issue Number: 12
Column Tag: MacTech Online

Advanced C++ Algorithms

by Jeff Clites, online@mactech.com

Algorithms are at the very heart of programming. Fortunately, the C++ standards committee saw fit to include an algorithms library to work in conjunction with the container classes of the STL. Although immensely useful, this library really covers only the most commonly used, general purpose tasks, and for specialized applications you have to look elsewhere. This month we are going to take a tour of some powerful algorithm libraries that pick up where the STL leaves off.

Algorithms in General

There are a number of printed references for learning about algorithms and the theory behind them. Algorithms in C++, 3rd ed., by Robert Sedgewick, is a new version of a classic reference, full of lucid explanations and copious illustrations, and there is a freeware program, MacBalsa, which animates many of the algorithms from the book. The Art of Computer Programming series by Donald E. Knuth is the definitive work on almost all things computing, and it is referenced by just about any writing which mentions the word "algorithm." Numerical Recipes in C by William H. Press, et al, is another classic, with an emphasis on practicality, and the full text is available in pdf form for preview on the web. On the other hand, a page at JPL warns against using the code from Numerical Recipes as-is, so you might want to check out these sites in tandem. For your general algorithm needs, check out Netlib, Codepage, and Programmer's Oasis - they have descriptions and links to a plethora of code sources.

MacBALSA
http://www.eg.bucknell.edu/~zaccone/MacBALSA/MacBALSA.html
Don Knuth's Home Page
http://www-cs-staff.stanford.edu/~knuth/
Numerical Recipes Home Page
http://www.nr.com/
Why not use Numerical Recipes?
http://math.jpl.nasa.gov/nr/
Netlib
http://www.netlib.org/c++/
{Codepage 2.2}
http://www.iro.umontreal.ca/~ratib/code/
Programmer's Oasis: Algorithms, Data Structures
http://www.utu.fi/~sisasa/oasis/oasis-algo.html

Finite-State Machines

Finite-State Machines (or FSMs for short) are based on a mechanical model of computing, originating with the Turing Machine. Although conceptually simple, they are the natural way to express many computational processes, such as string searching or parsing. Object Mentor has code available for an FSM compiler, which allows you to define an FSM with a simple notation and then compile it into code for a set of C++ classes. (Also be sure to check out the cool "Game of Life" applet, Wator, on their home page.) Conveniently, Michael Schürig has done the work of preparing a Mac version of the compiler as a droplet, and it is posted on his web site.

Object Mentor Freeware by Email
http://www.oma.com/Offerings/catalog.html
Michael Schürig's Home Page
http://www.schuerig.de/michael/

Genetic Algorithms

Genetic algorithms are a class of algorithms for solving minimization or best-fit problems, and are especially useful for situations in which classic optimization techniques fail. They are so-named because their basic strategy mimics biology: to find the optimal solution, generate a pool of candidate solutions, take a selection of the best ones and "combine" these to generate a new pool of candidates, and repeat. Unlike other optimization techniques, their successful use often depends heavily on finding the right way to encode the problem, but if used correctly they can succeed despite the presence of local minima which plague other approaches. MIT's GAlib makes it easy to implement a variety of different genetic algorithm strategies, and TimGA is a program which graphically demonstrates the optimization process and lets you experiment with how various parameters effect performance. TimGA is PowerPlant-based, and the code is available online.

GAlib: Matthew's C++ Genetic Algorithms Library
http://lancet.mit.edu/ga/
TimGA Source Code
http://hyperarchive.lcs.mit.edu/cgi-bin/NewSearch?key=TimGA

Parsing

A surprisingly difficult task in compiled languages is to allow the user to input a mathematical expression in text form and then evaluate it. This is a trivial one-liner in interpreted languages such as Perl, Python, and Tcl, but C and C++ don't have access to their own parsers and compilers at runtime, so if you want to do this sort of thing you have to write an interpreter yourself. A helpful starting point is the Chipmunk Basic Home Page, which has a large collection of links to source code. In particular, check out interp2num, an expression evaluator which gives you a choice of allowing C-like or BASIC-like syntax for the expressions it evaluates. Also, the STL-Compliant Software Components Collection page has a tokenizer which can help if you want to start from scratch.

Chipmunk Basic Home Page
http://www.rahul.net/rhn/cbas.page.html
STL-Compliant Software Components Collection
http://corp.metabyte.com/~fbp/stl/components.html

Linear Algebra and Matrix Manipulation

Fortran is famous for high-performance computation, and there a number of well-known libraries for numerical and scientific applications. Although many of these may be available to C and C++ programmers as compiled libraries or by way of the Fortran-to-C compiler, you probably prefer to find true C++ implementations. These allow you to peek under the hood when necessary, and they make it easier to integrate with the rest of your application by providing an object-based interface.

Most of these libraries fall under the general heading of linear algebra, since many problems are naturally expressed in terms of matrices. NIST has developed a library called TNT, the Template Numerical Toolkit, which tackles the difficult problem of developing an efficient yet elegant library for matrix math. One of its design criteria is compatibility with the STL, so that its matrices can act as generic container classes. Oleg Kiselyov's library, called simply LinAlg, is worth a look as well. Even if you do not use it directly, it uses a number of clever programming techniques, such as lazy evaluation and specialized constructors, which are instructive in their own right. Finally, Newmat, by Robert Davies, is a large and well-documented library, and is suitable for working with larger matrices.

Mac F2C
http://www.alumni.caltech.edu/~igormt/Mac_F2C.html
TNT Home Page
http://math.nist.gov/tnt/
Oleg Kiselyov's LinAlg Library
http://hyperarchive.lcs.mit.edu/cgi-bin/NewSearch?key=LinAlg
Newmat09: C++ matrix library
http://webnz.com/robert/nzc_nm09.html

(Pseudo) Random Numbers

It's pretty common for programs to use random number generators, and some are more serious about it than others. (Are you designing a solitaire game or trying to predict tornadoes?) If you need something a little more random than rand() or QuickDraw's Random(), there are alternatives out there. It's ironic, if you think about it, that the better implementations call themselves pseudorandom rather than random, to explicitly acknowledge that you can't really generate truly random numbers on a computer without some extra hardware making quantum measurements. UltraLib is a fast random-number generator, with implementations available in 68K and PPC assembly. It claims to be random even at the bit level, and to have an extremely long period ( 10^356). Agner Fog has another implementation of the same algorithm, as well as an algorithm of his own invention, on his page. Robert Davies, mentioned above, has a random number class library, Newran, which can generate sequences following a number of distributions. Donald E. Knuth's page, also mentioned above, has a public domain random number generator, with code in C or Fortran for single or double precision.

UltraLib
http://hyperarchive.lcs.mit.edu/cgi-bin/NewSearch?key=RandomNumberLib
Agner Fog's Pseudo random number generators
http://announce.com/agner/random/
Newran02A: C++ random number generator library
http://webnz.com/robert/nr02doc.htm
Knuth: Programs
http://www-cs-staff.stanford.edu/~knuth/programs.html

Cryptography

I could certainly write an entire column (or several) on the topic of cryptography, so I will just give a brief pointer here. Bruce Schneier's Applied Cryptography is the definitive reference on the subject, and his Counterpane web site has information on two freely available encryption schemes which he invented, as well as a pseudorandom number generator.

Counterpane Homepage
http://www.counterpane.com/

None of these libraries are Mac-specific; some are fully cross-platform, some have Mac versions available, and some may require tweaking (although less tweaking, now that CodeWarrior has support for member templates). In any case, I hope they are useful starting points.

These and armfuls of other links are available from the MacTech Online web pages at www.mactech.com/online/.

 
AAPL
$561.16
Apple Inc.
-4.16
MSFT
$29.11
Microsoft Corpora
+0.04
GOOG
$590.76
Google Inc.
-12.90
MacTech Search:
Community Search:

Autumn Dynasty Review
Autumn Dynasty Review By Kevin Stout on May 25th, 2012 Our Rating: :: NEARLY FLAWLESSiPad Only App - Designed for the iPad Autumn Dynasty is an oriental-themed real-time strategy game.   | Read more »
Our Annual “Holy Cow It’s Memorial Day A...
So, it’s that time of year again! BBQs, lawn chairs, beer, and the ability to finally wear shorts with sandals without fear of frostbite. Tan those legs and check out all the huge sales that are going on across the App Store below. We’ll try and... | Read more »
FREEday 5/25/12 – “They Call Me FREE but...
Another week of freebies, this time with very little in the way of “Big Name” titles. No need to panic, it’s intentional. Anyone browsing the App Store will no doubt see the more popular games anyway. | Read more »
Shoot the Zombirds Review
Shoot the Zombirds Review By Kevin Stout on May 25th, 2012 Our Rating: :: ADDICTINGUniversal App - Designed for iPhone and iPad Shoot the Zombirds is an archery game where the player shoots arrows at avian zombies.   | Read more »
Apple Debuts Free App of the Week Promot...
Apple has made a couple of changes to their weekly app features that pop up in the Featured tab of the App Store. While “App of the Week” and “Game of the Week” appear to be just rebranded as “Editors’ Choice,” there’s a new feature: the Free Game... | Read more »
Gun Runner Review
Gun Runner Review By Jason Wadsworth on May 25th, 2012 Our Rating: :: RUN AND GUNUniversal App - Designed for iPhone and iPad The name says it all. This clever homage to classic side-scrolling shooters is easy to enjoy but hard to... | Read more »
Five For Friday: Week Of May 25
This week’s Five for Friday is pretty heavy on the apps front but that’s not to say it’s all seriousness here. We’ve got a fun selection of ways to entertain the kids, a powerful web development tool, a companion app for avid golfers and an... | Read more »

Price Scanner via MacPrices.net

Apple Maintains Leading Mobile Device Manufacturer...
Milennial Media says Apple continued to be the number one mobile device manufacturer on their platform in Q1, representing 28% of the top manufacturers impression share. Apple iPhone accounted for 15... Read more
Asustek To Launch Three New ZenBook Ultrabook Mode...
Digitimes’ Rebecca Kuo and Steve Shen report that PC-maker Asustek Computer will launch three new models to its ZenBook Prime Ultrabook lineup – the UX21A, UX31A and UX32VD – in June, featuring full... Read more
Yahoo! Introduces Axis Search Browser For Mobile D...
Yahoo! has announced the availability of Yahoo! Axis, a new Web browser tool that it claims will re-imagine how people search and browse on the web, Axis offering a faster, smarter search with... Read more
Android- and iOS-Powered Smartphones Expand Market...
Smartphones powered by Android and iOS mobile operating systems accounted for more than eight out of ten smartphones shipped in the first quarter of 2012 (1Q12), according to the International Data... Read more
Roundup of Memorial Day Weekend MacBook Pro sales,...
 Apple resellers have MacBook Pros on sale for up to $240 off MSRP this Holiday weekend. Here is a roundup of the best prices available from any reseller: (1) B&H Photo has MacBook Pros on sale... Read more
iPad wait times down to 1-3 days at The Apple Stor...
The Apple Store Online is now reporting a 1-3 business day wait on all iPad orders, as it appears that Apple is clearing out their backlog. The iPad is available in Wi-Fi or Wi-Fi + Cellular... Read more
Roundup of Memorial Day Weekend MacBook Air sales,...
 Apple resellers have MacBook Airs on sale for up to $101 off MSRP this Holiday weekend. Here is a roundup of the best prices available from any reseller: (1) B&H Photo has 11-inch and 13-inch... Read more
13″ 2.8GHz MacBook Pro on sale for $100 off MSRP
Adorama has lowered their price on the 13″ 2.8GHz MacBook Pro to $1399 including free shipping plus NY/NJ sales tax only. Their price is $100 off MSRP, and it’s the lowest price for this model from... Read more

Jobs Board

*Apple* Retail - Manager - Natick Colle...
Much more than just a place for amazing products, the Apple Retail Store serves a dazzling range of needs for its customers. Not only can users get hands-on experience Read more
XML image iPhone App at Elance.com (Uppe...
I want a similar iphone app like the following App below: /us/app/hd-tattoo-designs-catalog/id524766650?mt=8 I want a ... can tell who knows the expertise and who outsources the project to others.... Read more
iPhone Modem DSP Firmware Engineer at Ap...
Firmware Engineer to help develop our next generation of iPhone products. This position requires directly related ... to deliver high performance best in class modem for iPhone products. Strong... Read more
iPhone Developer at Third Eye Consulting...
Third Eye is looking for an iPhone Developer.The ideal candidate will have the following:3-6 years experience in iOS design and developmentknowledge of iPhone Native AppsKnowledge of Java and... Read more
iPhone Mobile Developer at Mapmyfitness...
About MapMyFitness, Inc.: We're a well-funded and fast growing start-up. We're building the future of fitness applications on both the web and mobile. MapMyFitness is consistently ranked among the... Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.