This document and the software it presents are outdated, and kept online for reference purposes only. You may nonetheless be interested in some more recent material the author has published about graph algorithms in Java.
You keep reading about the famous graph algorithms, BFS, DFS, Shortest
Path First, etc. -- used from network routing to game 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, one is immediately convinced 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 fun to code in their new Java language. I could have implemented other sorting algorithms such as ShellSort and thus re-use James Gosling's framework, but this wouldn't have been any fun. After sorting algorithms, the next step had to be of course... 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 the creation of 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 vertices found 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.
You may want to have a look at the corresponding Java code.
The general idea is the same, but we now use 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:
You may want to have a look at the corresponding Java code.
DFS seems cleaner than BFS: recursive, it doesn't make use of an external data structure (a queue), and is shorter too. But it is important to realize these two algorithms address very different needs. An example of BFS usage, instead of DFS, is to implement a Web robot; a Depth First Search performed on the World Wide Web (a neat graph, isn't it?) would quickly overload any given web server with an ever growing number of requests. Therefore a Breadth First Search is preferred, since it dispatches requests to many servers at a time.
You can download the whole package under the usual academic license -- no warranties of any kind, please give me credit, I'd like to hear about your modifications, and it is all copyright © Renaud Waldura 1995, 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 of the applet (showing the whole graph with a nice border around it) have to be found by trial and error.
Of course, I welcome any question, remark, criticism, ...