[We'll talk about FSMs on Wednesday;
you can work on the first and last problems before then.]
We saw def'n of graphs, and things that a graph can represent.
Def'n: Tree: An undirected (connected) graph with no cycles.
Some standard problems:
- searching (DFS, BFS)
- 4-coloring, k-coloring (register allocation)
- weighted graphs:
shortest path (Dijkstra is modified BFS)
minimum spanning tree
// Depth-first search:
Set seen = new Set();
Stack pending = new Stack();
pending.push(V.get(0));
while (!pending.isEmpty()) {
Vertex curr = pending.pop();
if (!seen.contains(curr)) { // This test not needed for trees!
seen.add(curr);
System.out.println( "Processing "+ curr.toString() + " (in pre-order)" );
for ( Vertex nhbr : curr.getNeighbors() ) {
pending.add(nhbr);
}
}
The code for breadth-first-search is
just the same,
except you use a queue instead of a stack
(and of course you enqueue and dequeue, rather than push and pop).