Graph Searching Algorithms

Post on 22-Feb-2016

57 views 2 download

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

S

Graph Searching Algorithms

Tree

Breadth-First Search (BFS)

Breadth-First Search (BFS)

0u ∞ x

∞v ∞ y

∞ z∞w

white

gray

black

Not discoveredDiscovered,adjacent white nodesDiscovered,no adjacent white nodes

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

Breadth-First Search (BFS)

0u ∞ x

∞v ∞ y

∞ z∞w

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

Breadth-First Search (BFS)

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

0u ∞ x

∞v ∞ y

∞ z∞w

t = u

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

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

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

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

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

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 =

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

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

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 =

Breadth-First Search (BFS)

0u 1 x

1v 2 y

4 z3w

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

Breadth-First Search (BFS)

0u 1 x

1v 2 y

4 z3w

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

Breadth-First Search (BFS)

0u 1 x

1v 2 y

4 z3w

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

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

Depth-First Search (DFS)

Depth-First Search (DFS)

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

v w

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

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

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

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

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

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

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

Depth-First Search (DFS)

u x

v y

zw

white

gray

black

Not discoveredDiscovered,adjacent white nodesDiscovered,no adjacent white nodes

Depth-First Search (DFS)

u x

v y

zw

d /

d / f

Not discoveredDiscovered,adjacent white nodesDiscovered,no adjacent white nodes

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

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

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

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

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

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

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

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

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

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

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

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

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

Depth-First Search (DFS)

DFS(G): - construct a forest

1/8u 4/5 x

2/7v 3/6 y

10/11 z9/12w

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

Topological Sorting

m n o

q

t ur s

m sq uon tr

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)

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

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?

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]

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

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]

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.

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.

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

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

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.

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.

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.

Strongly Connected Component

Strongly Connected Component

Strongly Connected Component

Strongly Connected Component

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

decreasing f(u)

1 23 4 5

89 6 7

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

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

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

Topological Sorting

n o

r s

son r

Strongly Connected Component

Strongly Connected Component

Last f[u]

Last f[u]

>

Strongly Connected Component

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

decreasing f(u)

1 23 4 5

89 6 7

Strongly Connected Component

4 31 2

Strongly Connected Component

4 31 2

Adjacency Matrix of Graphs

1 2

3 4

5

Directed graph Adjacency-Matrix1 2 3 4

5

5

34

21

Adjacency Matrix of Graphs

A = AT =