You keep reading about the famous graph algorithms, BFS, DFS, Shortest
Path First, etc. -- used as well for network routing as for games
theory -- without ever really understanding them ?
Now, with the help of the new
HotJava
browser, watch them run live on this web page !
I just loved the sorting algorithms demo found in the HotJava alpha 2 release: it is so intuitive, you really are immediately convinced that the QuickSort is in fact better than any other common sorting algorithm.
Having played a bit with Sun's wonderful browser, I was looking for something easy and funny to code in their new Java language. I could have implemented other sorting algorithms such as ShellSort and so re-use James Gosling's framework, but this wouldn't have been any fun. After the sorting algorithms, the next step had to be of course... the graph algorithms.
So here they are, the well-known graph algorithms, implemented in the new object-oriented, multithreaded, distributed, secure, portable, ... Java language. !!!
I would like to extend this page with other algorithms, such as Shortest Path Search (Dijkstra and Bellmann-Ford), Spanning Trees (Kruskal and Prim), etc., but of course I have time constraints... Maybe next week !
If you're interested in contributing to create a Graph Algorithms Site, please mail me!
Search a graph (directed or not) in breadth first; this is done by using a queue where the found vertices are stored.
Here is a brief description of the BFS algorithm:
bfs (Graph G) { all vertices of G are first painted white the graph root is painted gray and put in a queue while the queue is not empty { a vertex u is removed from the queue for all white successors v of u { v is painted gray v is added to the queue } u is painted black } }
And now watch it run -- click the applet to start/stop the search.
Have a look at the Java code; it happens to be pretty clean ! :-).
Basically the idea is the same, but we are now using a stack instead of a queue. With recursion of course, so the stack management is all done by Java.
Here is a brief description of the DFS algorithm:
dfs-visit (Graph G, Vertex u) { the vertex u is painted gray for all white successors v of u { dfs-visit(G, v) } u is painted black } dfs (Graph G) { all vertices of G are first painted white dfs-visit(G, root of G) }Watch it run with this applet:
Have a look at the corresponding Java code; it happens to be pretty clean ! :-)
DFS seems really cleaner than BFS: it doesn't make use of an external data structure (a queue) because it is recursive, and therefore is shorter too. But those two search algorithms really address different areas: good examples of using the BFS algorithm instead of DFS are the Web robots: a Depth First Search performed on the World Wide Web (a pretty graph, isn't it ?) would very quickly overload a given web server by the ever growing number of requests sent to it; a Breadth First Search is preferred, dispatching the requests on many servers at a time.
You can download the whole package under the usual academic license and copyright (please do distribute unmodified, I do not make any warranty of any kind, it is all copyright © Renaud Waldura 1995, etc, etc).
Here is a little documentation about the applet attributes:
VERTICES="
n"
DIRECTED="TRUE"|"FALSE"
GRAPH="
url"
1
' character on row i
and column j indicates an edge between vertex
i and vertex j. ALGORITHM="DFS"|"BFS"
SPEED="
s"
VERTICES
, DIRECTED
,
and GRAPH
are mandatory, since they describe the graph
itself; an exception is thrown whenever any of these is not specified
(the applet displays the exception message). The ALGORITHM
attribute defaults to DFS
and SPEED
to 5.
For the time being, the correct width and height for the applet (showing the whole graph with a nice border around it) have to be found by testing various values.
Of course, I welcome any question, remark, criticism, ...
Well, maybe next week -- stay tuned with the URL-Minder for example !
Well, maybe next week -- stay tuned !
I received many questions from people looking for a good library implementing those graph algorithms and others. Although not a specialist in graph theory, I may provide some pointers to documents I found on the Web. Have a look at: