Graph Searching Algorithms

75
S Graph Searching Algorithms

description

Graph Searching Algorithms. Tree. Breadth-First Search (BFS). Breadth-First Search (BFS). Not discovered. white. u. ∞. 0. x. Discovered, adjacent white nodes. gray. v. y. ∞. ∞. Discovered, no adjacent white nodes. black. w. ∞. ∞. z. Breadth-First Search (BFS). u. ∞. 0. - PowerPoint PPT Presentation

Transcript of Graph Searching Algorithms

Page 1: Graph Searching Algorithms

S

Graph Searching Algorithms

Page 2: Graph Searching Algorithms

Tree

Page 3: Graph Searching Algorithms

Breadth-First Search (BFS)

Page 4: Graph Searching Algorithms

Breadth-First Search (BFS)

0u ∞ x

∞v ∞ y

∞ z∞w

white

gray

black

Not discoveredDiscovered,adjacent white nodesDiscovered,no adjacent white nodes

Page 5: Graph Searching Algorithms

Breadth-First Search (BFS)

0u ∞ x

∞v ∞ y

∞ z∞w

BFS(G, u): 1. Initialize the graph color[u] gray π[u] Nil d[u] 0 for each other vertex color[u] white

Page 6: Graph Searching Algorithms

Breadth-First Search (BFS)

0u ∞ x

∞v ∞ y

∞ z∞w

Q uBFS(G, u): 2. Initialize the queue Q Ø Enqueue(Q, u)

Page 7: Graph Searching Algorithms

Breadth-First Search (BFS)

QBFS(G, u): 3. While Q ≠ Ø 1) t Dequeue(Q)

0u ∞ x

∞v ∞ y

∞ z∞w

t = u

Page 8: Graph Searching Algorithms

Breadth-First Search (BFS)

0u 1 x

1v ∞ y

∞ z∞w

Q vBFS(G, u): 3. While Q ≠ Ø 2) for each r adj to t if color[r] = white color[r] gray π[r] t d[r] d[t] + 1 Enqueue(Q, r)

t = ur = x, vx

Page 9: Graph Searching Algorithms

Breadth-First Search (BFS)

0u 1 x

1v ∞ y

∞ z∞w

Q vBFS(G, u): 3. While Q ≠ Ø 3) color[t] black

t = ur = x, vx

Page 10: Graph Searching Algorithms

Breadth-First Search (BFS)

0u 1 x

1v ∞ y

∞ z∞w

Q xBFS(G, u): 3. While Q ≠ Ø 1) t Dequeue(Q) 2) for each r adj to t … 3) color[t] black

t = v

Page 11: Graph Searching Algorithms

Breadth-First Search (BFS)

0u 1 x

1v 2 y

∞ z∞w

Q xBFS(G, u): 3. While Q ≠ Ø 1) t Dequeue(Q) 2) for each r adj to t … 3) color[t] black

t = vr = yy

Page 12: Graph Searching Algorithms

Breadth-First Search (BFS)

0u 1 x

1v 2 y

∞ z∞w

Q xBFS(G, u): 3. While Q ≠ Ø 1) t Dequeue(Q) 2) for each r adj to t … 3) color[t] black

t = vr = yy

Page 13: Graph Searching Algorithms

Breadth-First Search (BFS)

0u 1 x

1v 2 y

∞ z∞w

Q yBFS(G, u): 3. While Q ≠ Ø 1) t Dequeue(Q) 2) for each r adj to t … 3) color[t] black

t = xr =

Page 14: Graph Searching Algorithms

Breadth-First Search (BFS)

0u 1 x

1v 2 y

∞ z3w

Q wBFS(G, u): 3. While Q ≠ Ø 1) t Dequeue(Q) 2) for each r adj to t … 3) color[t] black

t = yr = w

Page 15: Graph Searching Algorithms

Breadth-First Search (BFS)

0u 1 x

1v 2 y

4 z3w

Q zBFS(G, u): 3. While Q ≠ Ø 1) t Dequeue(Q) 2) for each r adj to t … 3) color[t] black

t = wr = z

Page 16: Graph Searching Algorithms

Breadth-First Search (BFS)

0u 1 x

1v 2 y

4 z3w

QBFS(G, u): 3. While Q ≠ Ø 1) t Dequeue(Q) 2) for each r adj to t … 3) color[t] black

t = zr =

Page 17: Graph Searching Algorithms

Breadth-First Search (BFS)

0u 1 x

1v 2 y

4 z3w

BFS(G, u): - the shortest-path distance from u

Page 18: Graph Searching Algorithms

Breadth-First Search (BFS)

0u 1 x

1v 2 y

4 z3w

BFS(G, u): - the shortest-path distance from u - construct a tree

Page 19: Graph Searching Algorithms

Breadth-First Search (BFS)

0u 1 x

1v 2 y

4 z3w

BFS(G, u): - Initialization: |V| - Enqueuing/dequeuing: |V| - Scanning adj vertices: |E|

Page 20: Graph Searching Algorithms

Breadth-First Search (BFS)

0u 1 x

1v 2 y

4 z3w

BFS(G, u): - Initialization: O(|V|) - Enqueuing/dequeuing: O(|V|) - Scanning adjacent vertices: O(|E|) => total running time: O(|V| + |E|)

Page 21: Graph Searching Algorithms

Depth-First Search (DFS)

Page 22: Graph Searching Algorithms

Depth-First Search (DFS)

ud[u]: when u is discoveredf[u]: when searching adj of u is finished

v w

Page 23: Graph Searching Algorithms

Depth-First Search (DFS)

ud[u]: when u is discoveredf[u]: when searching adj of u is finished

timestamp: t

d[u] = t

v w

Page 24: Graph Searching Algorithms

Depth-First Search (DFS)

ud[u]: when u is discoveredf[u]: when searching adj of u is finished

timestamp: t+1

d[u] = t

v wd[v] = t+1

Page 25: Graph Searching Algorithms

Depth-First Search (DFS)

ud[u]: when u is discoveredf[u]: when searching adj of u is finished

timestamp: t+2

d[u] = t

v wd[v] = t+1f[v] = t+2

Page 26: Graph Searching Algorithms

Depth-First Search (DFS)

ud[u]: when u is discoveredf[u]: when searching adj of u is finished

timestamp: t+3

d[u] = t

v wd[v] = t+1f[v] = t+2

d[w] = t+3

Page 27: Graph Searching Algorithms

Depth-First Search (DFS)

ud[u]: when u is discoveredf[u]: when searching adj of u is finished

timestamp: t+4

d[u] = t

v wd[v] = t+1f[v] = t+2

d[w] = t+3f[v] = t+4

Page 28: Graph Searching Algorithms

Depth-First Search (DFS)

ud[u]: when u is discoveredf[u]: when searching adj of u is finished

timestamp: t+5

d[u] = tf[u] = t+5

v wd[v] = t+1f[v] = t+2

d[w] = t+3f[w] = t+4

Page 29: Graph Searching Algorithms

Depth-First Search (DFS)

ud[u]: when u is discoveredf[u]: when searching adj of u is finished

d[u] = tf[u] = t+5

v wd[v] = t+1f[v] = t+2

d[w] = t+3f[w] = t+4

1. d[u] < f[u]2. [ d[u], f[u] ] entirely

contains [ d[v], f[v] ]3. [ d[v], f[v] ] and [ d[w],

f[w] ] are entirely disjoint

Page 30: Graph Searching Algorithms

Depth-First Search (DFS)

u x

v y

zw

white

gray

black

Not discoveredDiscovered,adjacent white nodesDiscovered,no adjacent white nodes

Page 31: Graph Searching Algorithms

Depth-First Search (DFS)

u x

v y

zw

d /

d / f

Not discoveredDiscovered,adjacent white nodesDiscovered,no adjacent white nodes

Page 32: Graph Searching Algorithms

Depth-First Search (DFS)

u x

v y

zw

DFS(G): 1. Initialization for each u V[G], color[u] white π[u] Nil time 0

Page 33: Graph Searching Algorithms

Depth-First Search (DFS)

DFS(G): 1. Initialization 2. For each u V[G] if color[u] = white DFS-Visit(u)DFS-Visit(u): 1. Initial Setting color[u] gray d[u] time time + 1

1/u x

v y

zw

Page 34: Graph Searching Algorithms

Depth-First Search (DFS)

DFS(G): 1. Initialization 2. For each u V[G] if color[u] = white DFS-Visit(u)DFS-Visit(u): 1. Initial Setting 2. for each adj v of white π[v] u DFS-Visit[v]

1/u x

2/v y

zw

Page 35: Graph Searching Algorithms

Depth-First Search (DFS)

DFS(G): 1. Initialization 2. For each u V[G] if color[u] = white DFS-Visit(u)DFS-Visit(u): 1. Initial Setting 2. for each adj v of white π[v] u DFS-Visit[v]

1/u x

2/v 3/ y

zw

Page 36: Graph Searching Algorithms

Depth-First Search (DFS)

DFS(G): 1. Initialization 2. For each u V[G] if color[u] = white DFS-Visit(u)DFS-Visit(u): 1. Initial Setting 2. for each adj v of white π[v] u DFS-Visit[v]

1/u 4/ x

2/v 3/ y

zw

Page 37: Graph Searching Algorithms

Depth-First Search (DFS)

DFS(G): 1. Initialization 2. For each u V[G] if color[u] = white DFS-Visit(u)DFS-Visit(u): 1. Initial Setting 2. Handling adj vertices 3. color[u] black f[u] time time + 1

1/u 4/5 x

2/v 3/ y

zw

Page 38: Graph Searching Algorithms

Depth-First Search (DFS)

DFS(G): 1. Initialization 2. For each u V[G] if color[u] = white DFS-Visit(u)DFS-Visit(u): 1. Initial Setting 2. Handling adj vertices 3. color[u] black f[u] time time + 1

1/u 4/5 x

2/v 3/6 y

zw

Page 39: Graph Searching Algorithms

Depth-First Search (DFS)

DFS(G): 1. Initialization 2. For each u V[G] if color[u] = white DFS-Visit(u)DFS-Visit(u): 1. Initial Setting 2. Handling adj vertices 3. color[u] black f[u] time time + 1

1/u 4/5 x

2/7v 3/6 y

zw

Page 40: Graph Searching Algorithms

Depth-First Search (DFS)

DFS(G): 1. Initialization 2. For each u V[G] if color[u] = white DFS-Visit(u)DFS-Visit(u): 1. Initial Setting 2. Handling adj vertices 3. color[u] black f[u] time time + 1

1/8u 4/5 x

2/7v 3/6 y

zw

Page 41: Graph Searching Algorithms

Depth-First Search (DFS)

DFS(G): 1. Initialization 2. For each u V[G] if color[u] = white DFS-Visit(u)DFS-Visit(u): 1. Initial Setting 2. Handling adj vertices 3. color[u] black f[u] time time + 1

1/8u 4/5 x

2/7v 3/6 y

z9/w

Page 42: Graph Searching Algorithms

Depth-First Search (DFS)

DFS(G): 1. Initialization 2. For each u V[G] if color[u] = white DFS-Visit(u)DFS-Visit(u): 1. Initial Setting 2. Handling adj vertices 3. color[u] black f[u] time time + 1

1/8u 4/5 x

2/7v 3/6 y

10/ z9/w

Page 43: Graph Searching Algorithms

Depth-First Search (DFS)

DFS(G): 1. Initialization 2. For each u V[G] if color[u] = white DFS-Visit(u)DFS-Visit(u): 1. Initial Setting 2. Handling adj vertices 3. color[u] black f[u] time time + 1

1/8u 4/5 x

2/7v 3/6 y

10/11 z9/w

Page 44: Graph Searching Algorithms

Depth-First Search (DFS)

DFS(G): 1. Initialization 2. For each u V[G] if color[u] = white DFS-Visit(u)DFS-Visit(u): 1. Initial Setting 2. Handling adj vertices 3. color[u] black f[u] time time + 1

1/8u 4/5 x

2/7v 3/6 y

10/11 z9/12w

Page 45: Graph Searching Algorithms

Depth-First Search (DFS)

DFS(G): - construct a forest

1/8u 4/5 x

2/7v 3/6 y

10/11 z9/12w

Page 46: Graph Searching Algorithms

Depth-First Search (DFS)

1/8u 4/5 x

2/7v 3/6 y

10/11 z9/12w

DFS(G): - Initialization: O(|V|) - Traversing vertices: O(|V|) - Scanning adjacent vertices: O(|E|) => total running time: O(|V| + |E|)

Page 47: Graph Searching Algorithms

Topological Sorting

m n o

q

t ur s

m sq uon tr

Page 48: Graph Searching Algorithms

Topological Sorting

Brute-Force way 1. Find a vertex without edges. 2. Put it onto the front of the list, and remove it from G. 3. Remove all edges to the removed edge. 4. Repeat 1~3.

n o

qt u

r s

m

O(|V|2 + |V||E|)Or

O(|V|2)

Page 49: Graph Searching Algorithms

Topological Sorting

Using DFS 1. Call DFS(G) 2. When a vertex is finished, put it onto the front of the list.

n o

qt u

r s

m

O(|V| + |E|)

Page 50: Graph Searching Algorithms

Topological Sorting

Using DFS 1. Call DFS(G) 2. When a vertex is finished, put it onto the front of the list.

u

v

1) v is white: d[u] < d[v] < f[u]2) v is black: f[v] < d[u]3) v is gray: d[v] < d[u] < f[v]

At d[u]:

v enters the list before u?

Page 51: Graph Searching Algorithms

Topological Sorting

Using DFS 1. Call DFS(G) 2. When a vertex is finished, put it onto the front of the list.

u

v

1) v is white: d[u] < d[v] At d[u]:

v enters the list before u?

t

t is gray: d[t] < d[u] < f[t]t is black: f[t] < d[u]

Page 52: Graph Searching Algorithms

Topological Sorting

1. If v is a descendant, d[u] < d[v] < f[v] <

f[u]2. If v is an anscestor, d[v] < d[u] < f[u] <

f[v] 3. Otherwise, d[v] < f[v] < d[u] <

f[u] or d[u] < f[u] < d[v] <

f[v]

u

v w

Page 53: Graph Searching Algorithms

Depth-First Search (DFS)

u

v w

1. d[u] < f[u]2. [ d[u], f[u] ] entirely

contains [ d[v], f[v] ]3. [ d[v], f[v] ] and [ d[w],

f[w] ] are entirely disjointIn a depth-first forest,

v is a descendant of u if and only if d[u] < d[v] < f[v] <

f[u]

Page 54: Graph Searching Algorithms

Topological Sorting

1. If v is a descendant, d[u] < d[v] < f[v] < f[u]2. If v is an anscestor, d[v] < d[u] < f[u] < f[v] 3. Otherwise, d[v] < f[v] < d[u] < f[u] or d[u] < f[u] <

d[v] < f[v]u

v

At d[u]:1) v is white: d[u] < d[v]

t is gray: d[t] < d[u] < f[t]t is black: f[t] < d[u]

t

Contradiction:

G is not acyclic.

Page 55: Graph Searching Algorithms

Topological Sorting

1. If v is a descendant, d[u] < d[v] < f[v] < f[u]2. If v is an anscestor, d[v] < d[u] < f[u] < f[v] 3. Otherwise, d[v] < f[v] < d[u] < f[u] or d[u] < f[u] <

d[v] < f[v]u

v

At d[u]:1) v is white: d[u] < d[v]

t is gray: d[t] < d[u] < f[t]t is black: f[t] < d[u]

t

Contradiction:v should be

black.

Page 56: Graph Searching Algorithms

Topological Sorting

1. If v is a descendant, d[u] < d[v] < f[v] < f[u]2. If v is an anscestor, d[v] < d[u] < f[u] < f[v] 3. Otherwise, d[v] < f[v] < d[u] < f[u] or d[u] < f[u] <

d[v] < f[v]u

v

At d[u]:1) v is white: d[u] < d[v]

t is white.t

Page 57: Graph Searching Algorithms

Topological Sorting

1. If v is a descendant, d[u] < d[v] < f[v] < f[u]2. If v is an anscestor, d[v] < d[u] < f[u] < f[v] 3. Otherwise, d[v] < f[v] < d[u] < f[u] or d[u] < f[u] <

d[v] < f[v]u

v

At d[u]:1) v is white: d[u] < d[v] < f[u]t is white.

t

Page 58: Graph Searching Algorithms

Topological Sorting

1. If v is a descendant, d[u] < d[v] < f[v] < f[u]2. If v is an anscestor, d[v] < d[u] < f[u] < f[v] 3. Otherwise, d[v] < f[v] < d[u] < f[u] or d[u] < f[u] <

d[v] < f[v]u

v

1) v is white: d[u] < d[v] < f[u]2) v is black: f[v] < d[u]3) v is gray: d[v] < d[u] < f[v]

At d[u]:

v will enter the list

before u.

Page 59: Graph Searching Algorithms

Topological Sorting

1. If v is a descendant, d[u] < d[v] < f[v] < f[u]2. If v is an anscestor, d[v] < d[u] < f[u] < f[v] 3. Otherwise, d[v] < f[v] < d[u] < f[u] or d[u] < f[u] <

d[v] < f[v]u

v

1) v is white: d[u] < d[v] < f[u]2) v is black: f[v] < d[u]3) v is gray: d[v] < d[u] < f[v]

At d[u]:

v is already in the list.

Page 60: Graph Searching Algorithms

Topological Sorting

1. If v is a descendant, d[u] < d[v] < f[v] < f[u]2. If v is an anscestor, d[v] < d[u] < f[u] < f[v] 3. Otherwise, d[v] < f[v] < d[u] < f[u] or d[u] < f[u] <

d[v] < f[v]u

v

1) v is white: d[u] < d[v] < f[u]2) v is black: f[v] < d[u]3) v is gray: d[v] < d[u] < f[v]

At d[u]:

Contradiction:

G is not acyclic.

Page 61: Graph Searching Algorithms

Strongly Connected Component

Page 62: Graph Searching Algorithms

Strongly Connected Component

Page 63: Graph Searching Algorithms

Strongly Connected Component

Page 64: Graph Searching Algorithms

Strongly Connected Component

1. Call DFS(G)2. Arrange the vertices in order of

decreasing f(u)

1 23 4 5

89 6 7

Page 65: Graph Searching Algorithms

Strongly Connected Component

1. Call DFS(G)2. Arrange the vertices in order of

decreasing f(u)3. Compute GT

1 23 4 5

89 6 7

Page 66: Graph Searching Algorithms

Strongly Connected Component

1. Call DFS(G)2. Arrange the vertices in order of

decreasing f(u)3. Compute GT 4. Run DFS(GT)

1 23 4 5

89 6 7

Page 67: Graph Searching Algorithms

Strongly Connected Component

1. Call DFS(G)2. Arrange the vertices in order of

decreasing f(u)3. Compute GT 4. Run DFS(GT)

1 23 4 5

89 6 7

Page 68: Graph Searching Algorithms

Topological Sorting

n o

r s

son r

Page 69: Graph Searching Algorithms

Strongly Connected Component

Page 70: Graph Searching Algorithms

Strongly Connected Component

Last f[u]

Last f[u]

>

Page 71: Graph Searching Algorithms

Strongly Connected Component

1. Call DFS(G)2. Arrange the vertices in order of

decreasing f(u)

1 23 4 5

89 6 7

Page 72: Graph Searching Algorithms

Strongly Connected Component

4 31 2

Page 73: Graph Searching Algorithms

Strongly Connected Component

4 31 2

Page 74: Graph Searching Algorithms

Adjacency Matrix of Graphs

1 2

3 4

5

Directed graph Adjacency-Matrix1 2 3 4

5

5

34

21

Page 75: Graph Searching Algorithms

Adjacency Matrix of Graphs

A = AT =