910 likes | 1.16k Views
Graph Traversal. Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn. BFS and DFS. BFS: breath first searching start from one vertex, near to far generates BFS forest flat DFS: depth first searching recursion and back-tracking generates DFS forest narrow.
E N D
Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn
BFS and DFS • BFS: breath first searching • start from one vertex, near to far • generates BFS forest • flat • DFS: depth first searching • recursion and back-tracking • generates DFS forest • narrow
“graph” ADT in C: Interface // in file “graph.h” #ifndef GRAPH_H #define GRAPH_H typedef struct graph *graph; typedef void (*tyVisit)(poly); graph newGraph (); void insertVertex (graph g, poly data); void insertEdge (graph g, poly from, poly to); void dfs (graph g, poly start, tyVisit visit); void bfs (graph g, poly start, tyVisit visit); // we’d see more later … #endif
Sample Graph For BFS, associate each vertex with a “distance” property. distance(v): the number of edges from the vertex “start” to vertex “v”, with distance(start)=0 a b c d e f
Sample Graph BFS bfs (g, “a”, strOutput); a b c d e f
Sample Graph BFS bfs (g, “a”, strOutput); print a; a 0 b c d e f
Sample Graph BFS bfs (g, “a”, strOutput); print a; // a choice print b; a 0 b 1 c d e f
Sample Graph BFS bfs (g, “a”, strOutput); print a; // a choice print b; print d; a 0 b 1 c d 1 e f
Sample Graph BFS bfs (g, “a”, strOutput); print a; // a choice print b; print d; print e; a 0 b 1 c d 1 e 2 f
Sample Graph BFS bfs (g, “a”, strOutput); print a; // a choice print b; print d; print e; // a choice print c; a 0 b 1 c 0 d 1 e 2 f
Sample Graph BFS bfs (g, “a”, strOutput); print a; // a choice print b; print d; print e; // a choice print c; print f; a 0 b 1 c 0 d 1 e 2 f 1
BFS Algorithm bfs (vertex start, tyVisit visit){ queue q = newQueue (); setDistance (start, 0); //Invariant: all vertices in q have distance property enQueue (q, start); while (q not empty) { vertex current = deQueue (q); int dist = getDistance (current); visit (current); for (each adjacent vertex u of “current”){ if (not visited u){ setDistance (u, dist+1); enQueue (q, u); } }}}
BFS Algorithm void bfsMain (graph g, poly start, tyVisit visit) { vertex startV = searchVertex (g, start); bfs (startV, visit); for (each vertex u in graph g) if (not visited u) bfs (q, u); }
Sample Graph BFS // color convention: not visited, inQueue, deQueued bfs (g, “a”, strOutput); a 0 b c d e f Queue: a
Sample Graph BFS // color convention: not visited, inQueue, deQueued bfs (g, “a”, strOutput); print a; a 0 b 1 c d 1 e f Queue: a Queue: b, d
Sample Graph BFS // color convention: not visited, inQueue, deQueued bfs (g, “a”, strOutput); print a; // a choice print b; a 0 b 1 c d 1 e 2 f Queue: a Queue: b, d Queue: d, e
Sample Graph BFS // color convention: not visited, inQueue, deQueued bfs (g, “a”, strOutput); print a; // a choice print b; print d; a 0 b 1 c d 1 e 2 f Queue: a Queue: b, d Queue: d, e Queue: e
Sample Graph BFS // color convention: not visited, inQueue, deQueued bfs (g, “a”, strOutput); print a; // a choice print b; print d; print e; a 0 b 1 c 0 d 1 e 2 f Queue: a Queue: c Queue: b, d Queue: d, e Queue: e Queue:
Sample Graph BFS // color convention: not visited, inQueue, deQueued bfs (g, “a”, strOutput); print a; // a choice print b; print d; print e; // a choice print c; a 0 b 1 c 0 d 1 e 2 f 1 Queue: a Queue: c Queue: b, d Queue: f Queue: d, e Queue: e Queue:
Sample Graph BFS // color convention: not visited, inQueue, deQueued bfs (g, “a”, strOutput); print a; // a choice print b; print d; print e; // a choice print c; print f; a 0 b 1 c 0 d 1 e 2 f 1 Queue: a Queue: c Queue: b, d Queue: f Queue: d, e Queue: e Queue: Queue:
Sample Graph DFS Associate a “discover time” and a “finish time” with each vertex v with: discover (start) = 0 a b c d e f
Sample Graph DFS dfs (g, “a”, strOutput); a b c d e f
Sample Graph DFS dfs (g, “a”, strOutput); print a; a (0, ) b c d e f
Sample Graph DFS dfs (g, “a”, strOutput); print a; // a choice print b; a (0, ) b (1, ) c d e f
Sample Graph DFS dfs (g, “a”, strOutput); print a; // a choice print b; print e; a (0, ) b (1, ) c d e (2, ) f
Sample Graph DFS dfs (g, “a”, strOutput); print a; // a choice print b; print e; print d; a (0, ) b (1, ) c d (3, ) e (2, ) f
Sample Graph DFS dfs (g, “a”, strOutput); print a; // a choice print b; print e; print d; a (0, ) b (1, ) c d (3, 4) e (2, ) f
Sample Graph DFS dfs (g, “a”, strOutput); print a; // a choice print b; print e; print d; a (0, ) b (1, ) c d (3, 4) e (2, 5) f
Sample Graph DFS dfs (g, “a”, strOutput); print a; // a choice print b; print e; print d; a (0, ) b (1, 6) c d (3, 4) e (2, 5) f
Sample Graph DFS dfs (g, “a”, strOutput); print a; // a choice print b; print e; print d; a (0, 7) b (1, 6) c d (3, 4) e (2, 5) f
Sample Graph DFS dfs (g, “a”, strOutput); print a; // a choice print b; print e; print d; // a choice print c a (0, 7) b (1, 6) c (8, ) d (3, 4) e (2, 5) f
Sample Graph DFS dfs (g, “a”, strOutput); print a; // a choice print b; print e; print d; // a choice print c print f a (0, 7) b (1, 6) c (8, ) d (3, 4) e (2, 5) f (9, )
Sample Graph DFS dfs (g, “a”, strOutput); print a; // a choice print b; print e; print d; // a choice print c a (0, 7) b (1, 6) c (8, ) d (3, 4) e (2, 5) f (9, 10)
Sample Graph DFS dfs (g, “a”, strOutput); print a; // a choice print b; print e; print d; // a choice print c a (0, 7) b (1, 6) c (8, 11) d (3, 4) e (2, 5) f (9, 10)
DFS Algorithm dfs (vertex start, tyVisit visit, time time) { visit (start); setDiscover (start, time++); for (each adjacent vertex u of “start”) if (not visited u) dfs (u, visit, time); setFinish (start, time++); }
DFS Algorithm void dfsMain (graph g, poly start, tyVisit visit) { vertex startV = searchVertex (g, start); time time = newTime (); dfs (startV, visit, time); for (each vertex u in graph g) if (not visited u) dfs (u, visit, time); }
Sample Graph DFS // color convention: not visited, discover, finish dfs (g, “a”, strOutput); print a; a (0, ) b c d e f dfs(a)
Sample Graph DFS // color convention: not visited, discover, finish dfs (g, “a”, strOutput); print a; // a choice print b; a (0, ) b (1, ) c d e f dfs(a) => dfs(b)
Sample Graph DFS // color convention: not visited, discover, finish dfs (g, “a”, strOutput); print a; // a choice print b; print e; a (0, ) b (1, ) c d e (2, ) f dfs(a) => dfs(b) => dfs(e)
Sample Graph DFS // color convention: not visited, discover, finish dfs (g, “a”, strOutput); print a; // a choice print b; print e; print d; a (0, ) b (1, ) c d (3, ) e (2, ) f dfs(a) => dfs(b) => dfs(e) => dfs(d)
Sample Graph DFS // color convention: not visited, discover, finish dfs (g, “a”, natOutput); print a; // a choice print b; print e; print d; a (0, ) b (1, ) c d (3, ) e (2, ) f dfs(a) => dfs(b) => dfs(e) => dfs(d) => dfs(b)???
Sample Graph DFS // color convention: not visited, discover, finish dfs (g, “a”, strOutput); print a; // a choice print b; print e; print d; a (0, ) b (1, ) c d (3, 4) e (2, ) f dfs(a) => dfs(b) => dfs(e)
Sample Graph DFS // color convention: not visited, discover, finish dfs (g, “a”, strOutput); print a; // a choice print b; print e; print d; a (0, ) b (1, ) c d (3, 4) e (2, 5) f dfs(a) => dfs(b)
Sample Graph DFS // color convention: not visited, discover, finish dfs (g, “a”, strOutput); print a; // a choice print b; print e; print d; a (0, ) b (1, 6) c d (3, 4) e (2, 5) f dfs(a)
Sample Graph DFS // color convention: not visited, discover, finish dfs (g, “a”, strOutput); print a; // a choice print b; print e; print d; a (0, ) b (1, 6) c d (3, 4) e (2, 5) f dfs(a) =>dfs(d)???
Sample Graph DFS // color convention: not visited, discover, finish dfs (g, “a”, strOutput); print a; // a choice print b; print e; print d; a (0, ) b (1, 6) c d (3, 4) e (2, 5) f dfs(a)
Sample Graph DFS // color convention: not visited, discover, finish dfs (g, “a”, strOutput); print a; // a choice print b; print e; print d; a (0, 7) b (1, 6) c d (3, 4) e (2, 5) f empty!
Sample Graph DFS // color convention: not visited, discover, finish dfs (g, “a”, strOutput); print a; // a choice print b; print e; print d; // a choice print c; a (0, 7) b (1, 6) c (8, ) d (3, 4) e (2, 5) f dfs(c)
Sample Graph DFS // color convention: not visited, discover, finish dfs (g, “a”, strOutput); print a; // a choice print b; print e; print d; // a choice print c; a (0, 7) b (1, 6) c (8, ) d (3, 4) e (2, 5) f dfs(c) => dsf(e)???
Sample Graph DFS // color convention: not visited, discover, finish dfs (g, “a”, strOutput); print a; // a choice print b; print e; print d; // a choice print c; a (0, 7) b (1, 6) c (8, ) d (3, 4) e (2, 5) f dfs(c)