RU beehive logo ITEC dept promo banner
ITEC 122
2008fall
ibarland

homeinfolecturesexamsarchive

lect14a
graphs, cont.

 [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).

homeinfolecturesexamsarchive


©2008, Ian Barland, Radford University
Last modified 2008.Dec.12 (Fri)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme