Elementary Graph Algorithms Comp 122, Fall 2004.

31
Elementary Graph Algorithms Comp 122, Fall 2004

Transcript of Elementary Graph Algorithms Comp 122, Fall 2004.

Page 1: Elementary Graph Algorithms Comp 122, Fall 2004.

Elementary Graph Algorithms

Comp 122, Fall 2004

Page 2: Elementary Graph Algorithms Comp 122, Fall 2004.
Page 3: Elementary Graph Algorithms Comp 122, Fall 2004.
Page 4: Elementary Graph Algorithms Comp 122, Fall 2004.

BFS(G,s)1. for each vertex u in V[G] – {s}2 do color[u] white3 d[u] 4 [u] null5 color[s] gray6 d[s] 07 [s] null8 Q 9 enqueue(Q,s)10 while Q 11 do u dequeue(Q)12 for each v in Adj[u]13 do if color[v] = white14 then color[v]

gray15 d[v] d[u] + 116 [v] u17 enqueue(Q,v)18 color[u] black

BFS(G,s)1. for each vertex u in V[G] – {s}2 do color[u] white3 d[u] 4 [u] null5 color[s] gray6 d[s] 07 [s] null8 Q 9 enqueue(Q,s)10 while Q 11 do u dequeue(Q)12 for each v in Adj[u]13 do if color[v] = white14 then color[v]

gray15 d[v] d[u] + 116 [v] u17 enqueue(Q,v)18 color[u] black

Comp 122, Fall 2004

white: undiscoveredgray: discoveredblack: finished

Q: a queue of discovered verticescolor[v]: color of vd[v]: distance from s to v[u]: predecessor of v

Example: animation.

Page 5: Elementary Graph Algorithms Comp 122, Fall 2004.

Example (BFS)

Comp 122, Fall 2004

0

r s t u

v w x y

Q: s 0

Page 6: Elementary Graph Algorithms Comp 122, Fall 2004.

Example (BFS)

Comp 122, Fall 2004

1 0

1

r s t u

v w x y

Q: w r 1 1

Page 7: Elementary Graph Algorithms Comp 122, Fall 2004.

Example (BFS)

Comp 122, Fall 2004

2

2 t u

x y

Q: r t x 1 2 2

1 0

1

r s

v w

Page 8: Elementary Graph Algorithms Comp 122, Fall 2004.

Example (BFS)

Comp 122, Fall 2004

2

u

v y

Q: t x v 2 2 2

2

2

t

1 0

1

r s

xw

Page 9: Elementary Graph Algorithms Comp 122, Fall 2004.

Example (BFS)

Comp 122, Fall 2004

3

u

y

Q: x v u 2 2 3

2

2

t

1 0

1

r s

xw

2

v

Page 10: Elementary Graph Algorithms Comp 122, Fall 2004.

Example (BFS)

Comp 122, Fall 2004

3

y

Q: v u y 2 3 3

2

2

t

1 0

1

r s

2

3

u

xwv

Page 11: Elementary Graph Algorithms Comp 122, Fall 2004.

Example (BFS)

Comp 122, Fall 2004

Q: u y 3 3

3

y

2

2

t

1 0

1

r s

2

3

u

xwv

Page 12: Elementary Graph Algorithms Comp 122, Fall 2004.

Example (BFS)

Comp 122, Fall 2004

Q: y 3

3

y

2

2

t

1 0

1

r s

2

3

u

xwv

Page 13: Elementary Graph Algorithms Comp 122, Fall 2004.

Example (BFS)

Comp 122, Fall 2004

Q:

3

y

2

2

t

1 0

1

r s

2

3

u

xwv

Page 14: Elementary Graph Algorithms Comp 122, Fall 2004.

Example (BFS)

Comp 122, Fall 2004

1 0

1 2 3

2 3

2

r s t u

v w x y

BFS Tree

Page 15: Elementary Graph Algorithms Comp 122, Fall 2004.

DFSDFS(G)1. for each vertex u V[G]2. do color[u] white3. [u] NULL4. time 05. for each vertex u V[G]6. do if color[u] = white7. then DFS-Visit(u)

DFS(G)1. for each vertex u V[G]2. do color[u] white3. [u] NULL4. time 05. for each vertex u V[G]6. do if color[u] = white7. then DFS-Visit(u)

Comp 122, Fall 2004

Uses a global timestamp time.

DFS-Visit(u)

1. color[u] GRAY

2. time time + 1

3. d[u] time

4. for each v Adj[u]

5. do if color[v] = WHITE

6. then [v] u

7. DFS-Visit(v)

8. color[u] BLACK

9. time time + 1

10. f[u] time

DFS-Visit(u)

1. color[u] GRAY

2. time time + 1

3. d[u] time

4. for each v Adj[u]

5. do if color[v] = WHITE

6. then [v] u

7. DFS-Visit(v)

8. color[u] BLACK

9. time time + 1

10. f[u] time

Page 16: Elementary Graph Algorithms Comp 122, Fall 2004.

Example (DFS)

Comp 122, Fall 2004

1/

u v w

x y z

Page 17: Elementary Graph Algorithms Comp 122, Fall 2004.

Example (DFS)

Comp 122, Fall 2004

1/ 2/

u v w

x y z

Page 18: Elementary Graph Algorithms Comp 122, Fall 2004.

Example (DFS)

Comp 122, Fall 2004

1/

3/

2/

u v w

x y z

Page 19: Elementary Graph Algorithms Comp 122, Fall 2004.

Example (DFS)

Comp 122, Fall 2004

1/

4/ 3/

2/

u v w

x y z

Page 20: Elementary Graph Algorithms Comp 122, Fall 2004.

Example (DFS)

Comp 122, Fall 2004

1/

4/ 3/

2/

u v w

x y z

B

Page 21: Elementary Graph Algorithms Comp 122, Fall 2004.

Example (DFS)

Comp 122, Fall 2004

1/

4/5 3/

2/

u v w

x y z

B

Page 22: Elementary Graph Algorithms Comp 122, Fall 2004.

Example (DFS)

Comp 122, Fall 2004

1/

4/5 3/6

2/

u v w

x y z

B

Page 23: Elementary Graph Algorithms Comp 122, Fall 2004.

Example (DFS)

Comp 122, Fall 2004

1/

4/5 3/6

2/7

u v w

x y z

B

Page 24: Elementary Graph Algorithms Comp 122, Fall 2004.

Example (DFS)

Comp 122, Fall 2004

1/

4/5 3/6

2/7

u v w

x y z

BF

Page 25: Elementary Graph Algorithms Comp 122, Fall 2004.

Example (DFS)

Comp 122, Fall 2004

1/8

4/5 3/6

2/7

u v w

x y z

BF

Page 26: Elementary Graph Algorithms Comp 122, Fall 2004.

Example (DFS)

Comp 122, Fall 2004

1/8

4/5 3/6

2/7 9/

u v w

x y z

BF

Page 27: Elementary Graph Algorithms Comp 122, Fall 2004.

Example (DFS)

Comp 122, Fall 2004

1/8

4/5 3/6

2/7 9/

u v w

x y z

BF C

Page 28: Elementary Graph Algorithms Comp 122, Fall 2004.

Example (DFS)

Comp 122, Fall 2004

1/8

4/5 3/6 10/

2/7 9/

u v w

x y z

BF C

Page 29: Elementary Graph Algorithms Comp 122, Fall 2004.

Example (DFS)

Comp 122, Fall 2004

1/8

4/5 3/6 10/

2/7 9/

u v w

x y z

BF C

B

Page 30: Elementary Graph Algorithms Comp 122, Fall 2004.

Example (DFS)

Comp 122, Fall 2004

1/8

4/5 3/6 10/11

2/7 9/

u v w

x y z

BF C

B

Page 31: Elementary Graph Algorithms Comp 122, Fall 2004.

Example (DFS)

Comp 122, Fall 2004

1/8

4/5 3/6 10/11

2/7 9/12

u v w

x y z

BF C

B