Map, Set & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua [email protected].
Graph Traversal Discrete Mathematics and Its Applications Baojian Hua [email protected].
-
date post
21-Dec-2015 -
Category
Documents
-
view
229 -
download
0
Transcript of Graph Traversal Discrete Mathematics and Its Applications Baojian Hua [email protected].
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
a
d
b
fe
c
For BFS, associate each vertex with a “distance” property.
distance(v): the number of edges from the vertex “start” to vertex “v”,withdistance(start)=0
Sample Graph BFS
a0
d1
b1
fe2
cbfs (g, “a”, strOutput);
print a;
// a choice
print b;
print d;
print e;
Sample Graph BFS
a0
d1
b1
fe2
c0
bfs (g, “a”, strOutput);
print a;
// a choice
print b;
print d;
print e;
// a choice
print c;
Sample Graph BFS
a0
d1
b1
f1
e2
c0
bfs (g, “a”, strOutput);
print a;
// a choice
print b;
print d;
print e;
// a choice
print c;
print f;
BFS Algorithmbfs (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 Algorithmvoid 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
a0
d
b
fe
cbfs (g, “a”, strOutput);
Queue: a
// color convention: not visited, inQueue, deQueued
Sample Graph BFS
a0
d1
b1
fe
cbfs (g, “a”, strOutput);
print a;
Queue: b, d
Queue: a
// color convention: not visited, inQueue, deQueued
Sample Graph BFS
a0
d1
b1
fe2
cbfs (g, “a”, strOutput);
print a;
// a choice
print b;
Queue: b, d
Queue: a
Queue: d, e
// color convention: not visited, inQueue, deQueued
Sample Graph BFS
a0
d1
b1
fe2
cbfs (g, “a”, strOutput);
print a;
// a choice
print b;
print d;
Queue: e
Queue: b, d
Queue: a
Queue: d, e
// color convention: not visited, inQueue, deQueued
Sample Graph BFS
a0
d1
b1
fe2
c0
bfs (g, “a”, strOutput);
print a;
// a choice
print b;
print d;
print e;
Queue:
Queue: e
Queue: b, d
Queue: a
Queue: d, e
Queue: c
// color convention: not visited, inQueue, deQueued
Sample Graph BFS
a0
d1
b1
f1
e2
c0
bfs (g, “a”, strOutput);
print a;
// a choice
print b;
print d;
print e;
// a choice
print c;Queue:
Queue: e
Queue: b, d
Queue: a
Queue: d, e
Queue: c
Queue: f
// color convention: not visited, inQueue, deQueued
Sample Graph BFS
a0
d1
b1
f1
e2
c0
bfs (g, “a”, strOutput);
print a;
// a choice
print b;
print d;
print e;
// a choice
print c;
print f; Queue:
Queue: e
Queue: b, d
Queue: a
Queue: d, e
Queue: c
Queue: f
Queue:
// color convention: not visited, inQueue, deQueued
Sample Graph DFS
a
d
b
fe
cAssociate a “discover time” and a “finish time” with each vertex v
with:
discover (start) = 0
Sample Graph DFS
a
(0, )
d
b(1, )
fe
(2, )
cdfs (g, “a”, strOutput);
print a;
// a choice
print b;
print e;
Sample Graph DFS
a
(0, )
d(3, )
b(1, )
fe
(2, )
cdfs (g, “a”, strOutput);
print a;
// a choice
print b;
print e;
print d;
Sample Graph DFS
a
(0, )
d(3, 4)
b(1, )
fe
(2, )
cdfs (g, “a”, strOutput);
print a;
// a choice
print b;
print e;
print d;
Sample Graph DFS
a
(0, )
d(3, 4)
b(1, )
fe
(2, 5)
cdfs (g, “a”, strOutput);
print a;
// a choice
print b;
print e;
print d;
Sample Graph DFS
a
(0, )
d(3, 4)
b(1, 6)
fe
(2, 5)
cdfs (g, “a”, strOutput);
print a;
// a choice
print b;
print e;
print d;
Sample Graph DFS
a
(0, 7)
d(3, 4)
b(1, 6)
fe
(2, 5)
cdfs (g, “a”, strOutput);
print a;
// a choice
print b;
print e;
print d;
Sample Graph DFS
a
(0, 7)
d(3, 4)
b(1, 6)
fe
(2, 5)
c(8, )
dfs (g, “a”, strOutput);
print a;
// a choice
print b;
print e;
print d;
// a choice
print c
Sample Graph DFS
a
(0, 7)
d(3, 4)
b(1, 6)
f(9, )
e(2, 5)
c(8, )
dfs (g, “a”, strOutput);
print a;
// a choice
print b;
print e;
print d;
// a choice
print c
print f
Sample Graph DFS
a
(0, 7)
d(3, 4)
b(1, 6)
f(9, 10)
e(2, 5)
c(8, )
dfs (g, “a”, strOutput);
print a;
// a choice
print b;
print e;
print d;
// a choice
print c
Sample Graph DFS
a
(0, 7)
d(3, 4)
b(1, 6)
f(9, 10)
e(2, 5)
c(8, 11)
dfs (g, “a”, strOutput);
print a;
// a choice
print b;
print e;
print d;
// a choice
print c
DFS Algorithmdfs (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 Algorithmvoid 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
a
(0, )
d
b
fe
cdfs (g, “a”, strOutput);
print a;
dfs(a)
// color convention: not visited, discover, finish
Sample Graph DFS
a
(0, )
d
b(1, )
fe
cdfs (g, “a”, strOutput);
print a;
// a choice
print b;
dfs(a) => dfs(b)
// color convention: not visited, discover, finish
Sample Graph DFS
a
(0, )
d
b(1, )
fe
(2, )
cdfs (g, “a”, strOutput);
print a;
// a choice
print b;
print e;
dfs(a) => dfs(b) => dfs(e)
// color convention: not visited, discover, finish
Sample Graph DFS
a
(0, )
d(3, )
b(1, )
fe
(2, )
cdfs (g, “a”, strOutput);
print a;
// a choice
print b;
print e;
print d;dfs(a) => dfs(b) => dfs(e) => dfs(d)
// color convention: not visited, discover, finish
Sample Graph DFS
a
(0, )
d(3, )
b(1, )
fe
(2, )
cdfs (g, “a”, natOutput);
print a;
// a choice
print b;
print e;
print d;dfs(a) => dfs(b) => dfs(e) => dfs(d) => dfs(b)???
// color convention: not visited, discover, finish
Sample Graph DFS
a
(0, )
d(3, 4)
b(1, )
fe
(2, )
cdfs (g, “a”, strOutput);
print a;
// a choice
print b;
print e;
print d;dfs(a) => dfs(b) => dfs(e)
// color convention: not visited, discover, finish
Sample Graph DFS
a
(0, )
d(3, 4)
b(1, )
fe
(2, 5)
cdfs (g, “a”, strOutput);
print a;
// a choice
print b;
print e;
print d;dfs(a) => dfs(b)
// color convention: not visited, discover, finish
Sample Graph DFS
a
(0, )
d(3, 4)
b(1, 6)
fe
(2, 5)
cdfs (g, “a”, strOutput);
print a;
// a choice
print b;
print e;
print d;dfs(a)
// color convention: not visited, discover, finish
Sample Graph DFS
a
(0, )
d(3, 4)
b(1, 6)
fe
(2, 5)
cdfs (g, “a”, strOutput);
print a;
// a choice
print b;
print e;
print d;dfs(a) =>dfs(d)???
// color convention: not visited, discover, finish
Sample Graph DFS
a
(0, )
d(3, 4)
b(1, 6)
fe
(2, 5)
cdfs (g, “a”, strOutput);
print a;
// a choice
print b;
print e;
print d;dfs(a)
// color convention: not visited, discover, finish
Sample Graph DFS
a
(0, 7)
d(3, 4)
b(1, 6)
fe
(2, 5)
cdfs (g, “a”, strOutput);
print a;
// a choice
print b;
print e;
print d;empty!
// color convention: not visited, discover, finish
Sample Graph DFS
a
(0, 7)
d(3, 4)
b(1, 6)
fe
(2, 5)
c(8, )
dfs (g, “a”, strOutput);
print a;
// a choice
print b;
print e;
print d;
// a choice
print c;
dfs(c)
// color convention: not visited, discover, finish
Sample Graph DFS
a
(0, 7)
d(3, 4)
b(1, 6)
f(9, )
e(2, 5)
c(8, )
dfs (g, “a”, strOutput);
print a;
// a choice
print b;
print e;
print d;
// a choice
print c;
print f;
dfs(c)=>dfs(f)
// color convention: not visited, discover, finish
Sample Graph DFS
a
(0, 7)
d(3, 4)
b(1, 6)
f(9, )
e(2, 5)
c(8, )
dfs (g, “a”, strOutput);
print a;
// a choice
print b;
print e;
print d;
// a choice
print c;
print f;
dfs(c)=>dfs(f)=>dfs(f)???
// color convention: not visited, discover, finish
Sample Graph DFS
a
(0, 7)
d(3, 4)
b(1, 6)
f(9, )
e(2, 5)
c(8, )
dfs (g, “a”, strOutput);
print a;
// a choice
print b;
print e;
print d;
// a choice
print c;
print f;
dfs(c)=>dfs(f)=>dfs(f)???
// color convention: not visited, discover, finish
Sample Graph DFS
a
(0, 7)
d(3, 4)
b(1, 6)
f(9, 10)
e(2, 5)
c(8, )
dfs (g, “a”, strOutput);
print a;
// a choice
print b;
print e;
print d;
// a choice
print c;
print f;
dfs(c)
// color convention: not visited, discover, finish
Sample Graph DFS
a
(0, 7)
d(3, 4)
b(1, 6)
f(9, 10)
e(2, 5)
c(8, )
dfs (g, “a”, strOutput);
print a;
// a choice
print b;
print e;
print d;
// a choice
print c;
print f;
dfs(c)=>dfs(e)???
// color convention: not visited, discover, finish
Sample Graph DFS
a
(0, 7)
d(3, 4)
b(1, 6)
f(9, 10)
e(2, 5)
c(8, 11)
dfs (g, “a”, strOutput);
print a;
// a choice
print b;
print e;
print d;
// a choice
print c;
print f;
dfs(c)
// color convention: not visited, discover, finish
Sample Graph DFS
a
(0, 7)
d(3, 4)
b(1, 6)
f(9, 10)
e(2, 5)
c(8, 11)
dfs (g, “a”, strOutput);
print a;
// a choice
print b;
print e;
print d;
// a choice
print c;
print f;
empty!
// color convention: not visited, discover, finish
Edge Classification
Once we obtain the DFS (or BFS) spanning trees (forests), the graph edges could be classified according to the trees: tree edges: edges in the trees forward edges: ancestors to descants back edges: descants to ancestors cross edges: others
Edge Classification Example
a
d
b
fe
c• tree edges:
a->b, b->e, e->d, c->f
• forward edges:
a->d
• back edges:
d->b, f->f
• cross edges:
c->e
Edge Classification Algorithm
Based on discover and finish time, for each edge e=(u, v): if v not visited, e is tree edge if v not finished, e is back edge if v finished
if discover(u)<discover(v), e is forward edge if discover(u)>discover(v), e is cross edge
Edge Classification Algorithmdfs (vertex start, tyVisit visit, time time) { visit (start); setDiscover (start, time++);
for (each edge e=(start, v)) { if (not visited v) { dfs (v, visit, time); classifyEdge (e, “TreeEdge”); } else { if (not setFinish v) classifyEdge (e, “BackEdge”); else { // leave to you } } setFinish (start, time++);}
DFS Algorithmvoid 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);}
Source-queue Topological Sorting AlgorithmtopoSortBfs (graph g) { for (each vertex v) calculate in-degree for v; // vertices in queue q are candidates for // deletion queue q = newQueue ();
for (each vertex v) if (in-degree of v ==0) enQueue (q, v);
Source-queue Topological Sorting Algorithm (cont’) while (q not empty) { vertex current = deQueue (q); for (each edge e=(current, v)) if (not visited v) { in-degree of v --; if (in-degree of v ==0) enQueue (q, v); } }}// BFS-based algorithm
An Example: Hasse Diagram
1
2 5
20
4
12
Source queue: 1
// color convention: not visited, enQueue, deQueue
An Example: Hasse Diagram
1
2 5
20
4
12
Sorted Sequence: 1, 2, 5
Source queue: 4
// Note that we don’t
// enQueue 20!!
An Example: Hasse Diagram
1
2 5
20
4
12
Sorted Sequence: 1, 2, 5, 4
Source queue: 12, 20 // a chance
DFS-based Algorithm
1(6, 11)
2(7, 8)
5(9, 10)
20(3, 4)
4(0, 5)
12(1, 2)
DFS from 4:
When each vertex v finishes, insert v onto head of a linked list
Topological Sorting Algorithmdfs (vertex start, tyVisit visit, linkedList lis
t){ visit (start);
for (each edge e=(start, v)) // as before … linkedListInsertHead (list, start) }
DFS AlgorithmlinkedList topoSortDfs (graph g, poly start, tyVisit visit){ vertex startV = searchVertex (g, start); linkedList list = newLinkedList (); dfs (startV, visit, list);
for (each vertex u in graph g) if (not visited u) dfs (u, visit, list);
return list;}
Some Extra Programming Assignments Simple path: is there a simple path from vertex u
to v? Or, are vertices u and v connected?
Is an undirected g connected? Or, how many connected components are there in g?
Cycle detection: is there a cycle in a digraph g? Two colorability: is it possible to color all the ver
tices in a digraph g using two colors, such that no adjacent vertices are of the same color?