Top Banner
Graph Traversal Discrete Mathematics and Its Applications Baojian Hua [email protected]
82
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

Graph Traversal

Discrete Mathematics andIts Applications

Baojian [email protected]

Page 2: 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

Page 3: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

“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

Page 4: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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

Page 5: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

Sample Graph BFS

a

d

b

fe

cbfs (g, “a”, strOutput);

Page 6: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

Sample Graph BFS

a0

d

b

fe

cbfs (g, “a”, strOutput);

print a;

Page 7: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

Sample Graph BFS

a0

d

b1

fe

cbfs (g, “a”, strOutput);

print a;

// a choice

print b;

Page 8: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

Sample Graph BFS

a0

d1

b1

fe

cbfs (g, “a”, strOutput);

print a;

// a choice

print b;

print d;

Page 9: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

Sample Graph BFS

a0

d1

b1

fe2

cbfs (g, “a”, strOutput);

print a;

// a choice

print b;

print d;

print e;

Page 10: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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;

Page 11: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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;

Page 12: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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); }}}}

Page 13: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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);}

Page 14: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

Sample Graph BFS

a0

d

b

fe

cbfs (g, “a”, strOutput);

Queue: a

// color convention: not visited, inQueue, deQueued

Page 15: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

Sample Graph BFS

a0

d1

b1

fe

cbfs (g, “a”, strOutput);

print a;

Queue: b, d

Queue: a

// color convention: not visited, inQueue, deQueued

Page 16: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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

Page 17: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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

Page 18: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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

Page 19: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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

Page 20: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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

Page 21: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

Sample Graph DFS

a

d

b

fe

cAssociate a “discover time” and a “finish time” with each vertex v

with:

discover (start) = 0

Page 22: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

Sample Graph DFS

a

d

b

fe

cdfs (g, “a”, strOutput);

Page 23: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

Sample Graph DFS

a

(0, )

d

b

fe

cdfs (g, “a”, strOutput);

print a;

Page 24: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

Sample Graph DFS

a

(0, )

d

b(1, )

fe

cdfs (g, “a”, strOutput);

print a;

// a choice

print b;

Page 25: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

Sample Graph DFS

a

(0, )

d

b(1, )

fe

(2, )

cdfs (g, “a”, strOutput);

print a;

// a choice

print b;

print e;

Page 26: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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;

Page 27: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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;

Page 28: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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;

Page 29: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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;

Page 30: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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;

Page 31: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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

Page 32: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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

Page 33: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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

Page 34: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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

Page 35: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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++);}

Page 36: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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);}

Page 37: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

Sample Graph DFS

a

(0, )

d

b

fe

cdfs (g, “a”, strOutput);

print a;

dfs(a)

// color convention: not visited, discover, finish

Page 38: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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

Page 39: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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

Page 40: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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

Page 41: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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

Page 42: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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

Page 43: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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

Page 44: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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

Page 45: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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

Page 46: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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

Page 47: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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

Page 48: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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

Page 49: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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

Page 50: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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

Page 51: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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

Page 52: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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

Page 53: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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

Page 54: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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

Page 55: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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

Page 56: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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

Page 57: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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

Page 58: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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

Page 59: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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++);}

Page 60: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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);}

Page 61: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

BFS and DFS Application #1:Topological Sorting

Page 62: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

An Example: Hasse Diagram

1

2 5

20

4

12

From Rosen’s book

Page 63: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

An Example: Hasse Diagram

1

2 5

20

4

12

Sorted Sequence: 1

Page 64: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

An Example: Hasse Diagram

1

2 5

20

4

12

Sorted Sequence: 1, 2

Page 65: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

An Example: Hasse Diagram

1

2 5

20

4

12

Sorted Sequence: 1, 2, 4

Page 66: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

An Example: Hasse Diagram

1

2 5

20

4

12

Sorted Sequence: 1, 2, 4, 12

Page 67: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

An Example: Hasse Diagram

1

2 5

20

4

12

Sorted Sequence: 1, 2, 4, 12, 5

Page 68: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

An Example: Hasse Diagram

1

2 5

20

4

12

Sorted Sequence: 1, 2, 4, 12, 5, 20

Page 69: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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);

Page 70: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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

Page 71: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

An Example: Hasse Diagram

1

2 5

20

4

12

Source queue: 1

// color convention: not visited, enQueue, deQueue

Page 72: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

An Example: Hasse Diagram

1

2 5

20

4

12

Sorted Sequence: 1

Source queue: 2, 5

Page 73: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

An Example: Hasse Diagram

1

2 5

20

4

12

Sorted Sequence: 1, 2

Source queue: 5, 4

Page 74: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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!!

Page 75: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

An Example: Hasse Diagram

1

2 5

20

4

12

Sorted Sequence: 1, 2, 5, 4

Source queue: 12, 20 // a chance

Page 76: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

An Example: Hasse Diagram

1

2 5

20

4

12

Sorted Sequence: 1, 2, 5, 4, 12

Source queue: 20

Page 77: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

An Example: Hasse Diagram

1

2 5

20

4

12

Sorted Sequence: 1, 2, 5, 4, 12, 20

Source queue: empty!

Page 78: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

DFS-based Algorithm

1

2 5

20

4

12

DFS from 4:

Page 79: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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

Page 80: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

Topological Sorting Algorithmdfs (vertex start, tyVisit visit, linkedList lis

t){ visit (start);

for (each edge e=(start, v)) // as before … linkedListInsertHead (list, start) }

Page 81: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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;}

Page 82: Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.

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?