Download presentation
Presentation is loading. Please wait.
1
Discrete Mathematics and
Graph Traversal Discrete Mathematics and Its Applications Baojian Hua
2
BFS and DFS BFS: breath first searching DFS: depth 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
3
“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
4
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
5
Sample Graph BFS bfs (g, “a”, strOutput); a b c d e f
6
Sample Graph BFS bfs (g, “a”, strOutput); print a; a b c d e f
7
Sample Graph BFS bfs (g, “a”, strOutput); print a; // a choice
print b; a b 1 c d e f
8
Sample Graph BFS bfs (g, “a”, strOutput); print a; // a choice
print b; print d; a b 1 c d 1 e f
9
Sample Graph BFS bfs (g, “a”, strOutput); print a; // a choice
print b; print d; print e; a b 1 c d 1 e 2 f
10
Sample Graph BFS bfs (g, “a”, strOutput); print a; // a choice
print b; print d; print e; print c; a b 1 c d 1 e 2 f
11
Sample Graph BFS bfs (g, “a”, strOutput); print a; // a choice
print b; print d; print e; print c; print f; a b 1 c d 1 e 2 f 1
12
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); } }}}
13
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); }
14
Sample Graph BFS // color convention: not visited, inQueue, deQueued
bfs (g, “a”, strOutput); a b c d e f Queue: a
15
Sample Graph BFS // color convention: not visited, inQueue, deQueued
bfs (g, “a”, strOutput); print a; a b 1 c d 1 e f Queue: a Queue: b, d
16
Sample Graph BFS // color convention: not visited, inQueue, deQueued
bfs (g, “a”, strOutput); print a; // a choice print b; a b 1 c d 1 e 2 f Queue: a Queue: b, d Queue: d, e
17
Sample Graph BFS // color convention: not visited, inQueue, deQueued
bfs (g, “a”, strOutput); print a; // a choice print b; print d; a b 1 c d 1 e 2 f Queue: a Queue: b, d Queue: d, e Queue: e
18
Sample Graph BFS // color convention: not visited, inQueue, deQueued
bfs (g, “a”, strOutput); print a; // a choice print b; print d; print e; a b 1 c d 1 e 2 f Queue: a Queue: c Queue: b, d Queue: d, e Queue: e Queue:
19
Sample Graph BFS // color convention: not visited, inQueue, deQueued
bfs (g, “a”, strOutput); print a; // a choice print b; print d; print e; print c; a b 1 c d 1 e 2 f 1 Queue: a Queue: c Queue: b, d Queue: d, e Queue: f Queue: e Queue:
20
Sample Graph BFS // color convention: not visited, inQueue, deQueued
bfs (g, “a”, strOutput); print a; // a choice print b; print d; print e; print c; print f; a b 1 c d 1 e 2 f 1 Queue: a Queue: c Queue: b, d Queue: d, e Queue: f Queue: e Queue: Queue:
21
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
22
Sample Graph DFS dfs (g, “a”, strOutput); a b c d e f
23
Sample Graph DFS dfs (g, “a”, strOutput); print a; a (0, ) b c d e f
24
Sample Graph DFS dfs (g, “a”, strOutput); print a; // a choice
print b; a (0, ) b (1, ) c d e f
25
Sample Graph DFS dfs (g, “a”, strOutput); print a; // a choice
print b; print e; a (0, ) b (1, ) c d e (2, ) f
26
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
27
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
28
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
29
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
30
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
31
Sample Graph DFS dfs (g, “a”, strOutput); print a; // a choice
print b; print e; print d; print c a (0, 7) b (1, 6) c (8, ) d (3, 4) e (2, 5) f
32
Sample Graph DFS dfs (g, “a”, strOutput); print a; // a choice
print b; print e; print d; print c print f a (0, 7) b (1, 6) c (8, ) d (3, 4) e (2, 5) f (9, )
33
Sample Graph DFS dfs (g, “a”, strOutput); print a; // a choice
print b; print e; print d; print c a (0, 7) b (1, 6) c (8, ) d (3, 4) e (2, 5) f (9, 10)
34
Sample Graph DFS dfs (g, “a”, strOutput); print a; // a choice
print b; print e; print d; print c a (0, 7) b (1, 6) c (8, 11) d (3, 4) e (2, 5) f (9, 10)
35
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++); }
36
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); }
37
Sample Graph DFS // color convention: not visited, discover, finish
dfs (g, “a”, strOutput); print a; a (0, ) b c d e f dfs(a)
38
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)
39
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)
40
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)
41
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)???
42
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)
43
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)
44
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)
45
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)???
46
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)
47
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!
48
Sample Graph DFS // color convention: not visited, discover, finish
dfs (g, “a”, strOutput); print a; // a choice print b; print e; print d; print c; a (0, 7) b (1, 6) c (8, ) d (3, 4) e (2, 5) f dfs(c)
49
Sample Graph DFS // color convention: not visited, discover, finish
dfs (g, “a”, strOutput); print a; // a choice print b; print e; print d; print c; print f; a (0, 7) b (1, 6) c (8, ) d (3, 4) e (2, 5) f (9, ) dfs(c)=>dfs(f)
50
Sample Graph DFS // color convention: not visited, discover, finish
dfs (g, “a”, strOutput); print a; // a choice print b; print e; print d; print c; print f; a (0, 7) b (1, 6) c (8, ) d (3, 4) e (2, 5) f (9, ) dfs(c)=>dfs(f)=>dfs(f)???
51
Sample Graph DFS // color convention: not visited, discover, finish
dfs (g, “a”, strOutput); print a; // a choice print b; print e; print d; print c; print f; a (0, 7) b (1, 6) c (8, ) d (3, 4) e (2, 5) f (9, ) dfs(c)=>dfs(f)=>dfs(f)???
52
Sample Graph DFS // color convention: not visited, discover, finish
dfs (g, “a”, strOutput); print a; // a choice print b; print e; print d; print c; print f; a (0, 7) b (1, 6) c (8, ) d (3, 4) e (2, 5) f (9, 10) dfs(c)
53
Sample Graph DFS // color convention: not visited, discover, finish
dfs (g, “a”, strOutput); print a; // a choice print b; print e; print d; print c; print f; a (0, 7) b (1, 6) c (8, ) d (3, 4) e (2, 5) f (9, 10) dfs(c)=>dfs(e)???
54
Sample Graph DFS // color convention: not visited, discover, finish
dfs (g, “a”, strOutput); print a; // a choice print b; print e; print d; print c; print f; a (0, 7) b (1, 6) c (8, 11) d (3, 4) e (2, 5) f (9, 10) dfs(c)
55
Sample Graph DFS // color convention: not visited, discover, finish
dfs (g, “a”, strOutput); print a; // a choice print b; print e; print d; print c; print f; a (0, 7) b (1, 6) c (8, 11) d (3, 4) e (2, 5) f (9, 10) empty!
56
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
57
Edge Classification Example
tree edges: a->b, b->e, e->d, c->f forward edges: a->d back edges: d->b, f->f cross edges: c->e a b c d e f
58
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
59
Edge Classification Algorithm
dfs (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++);
60
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); }
61
BFS and DFS Application #1:
Topological Sorting
62
An Example: Hasse Diagram
12 20 4 2 5 1 From Rosen’s book
63
An Example: Hasse Diagram
12 20 4 2 5 1 Sorted Sequence: 1
64
An Example: Hasse Diagram
12 20 4 2 5 1 Sorted Sequence: 1, 2
65
An Example: Hasse Diagram
12 20 4 2 5 1 Sorted Sequence: 1, 2, 4
66
An Example: Hasse Diagram
12 20 4 2 5 1 Sorted Sequence: 1, 2, 4, 12
67
An Example: Hasse Diagram
12 20 4 2 5 1 Sorted Sequence: 1, 2, 4, 12, 5
68
An Example: Hasse Diagram
12 20 4 2 5 1 Sorted Sequence: 1, 2, 4, 12, 5, 20
69
Source-queue Topological Sorting Algorithm
topoSortBfs (graph g) { for (each vertex v) calculate in-degree for v; // vertices in queue q are candidates for // deletion queue q = newQueue (); if (in-degree of v ==0) enQueue (q, v);
70
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
71
An Example: Hasse Diagram
12 20 4 2 5 1 Source queue: 1 // color convention: not visited, enQueue, deQueue
72
An Example: Hasse Diagram
12 20 4 2 5 1 Source queue: 2, 5 Sorted Sequence: 1
73
An Example: Hasse Diagram
12 20 4 2 5 1 Source queue: 5, 4 Sorted Sequence: 1, 2
74
An Example: Hasse Diagram
12 20 4 // Note that we don’t // enQueue 20!! 2 5 1 Source queue: 4 Sorted Sequence: 1, 2, 5
75
An Example: Hasse Diagram
12 20 4 2 5 1 Source queue: 12, 20 // a chance Sorted Sequence: 1, 2, 5, 4
76
An Example: Hasse Diagram
12 20 4 2 5 1 Source queue: 20 Sorted Sequence: 1, 2, 5, 4, 12
77
An Example: Hasse Diagram
12 20 4 2 5 1 Source queue: empty! Sorted Sequence: 1, 2, 5, 4, 12, 20
78
DFS-based Algorithm 12 20 4 2 5 1 DFS from 4:
79
DFS-based Algorithm 12 (1, 2) 20 (3, 4) 4 (0, 5) 2 (7, 8) 5 (9, 10) 1
(6, 11) DFS from 4: When each vertex v finishes, insert v onto head of a linked list
80
Topological Sorting Algorithm
dfs (vertex start, tyVisit visit, linkedList list) { visit (start); for (each edge e=(start, v)) // as before … linkedListInsertHead (list, start) }
81
DFS Algorithm linkedList 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; }
82
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 vertices in a digraph g using two colors, such that no adjacent vertices are of the same color?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.