+ All Categories
Home > Documents > Applications of DFShaimk/algorithms/topological-sort... · Topological sort •Order the vertices...

Applications of DFShaimk/algorithms/topological-sort... · Topological sort •Order the vertices...

Date post: 21-Jul-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
24
Applications of DFS
Transcript
Page 1: Applications of DFShaimk/algorithms/topological-sort... · Topological sort •Order the vertices by reverse finishing times 9/14 10/13 11/12 1/8 2/7 3/6 4/5 9/14 10/13 11/12 3/6

Applications of DFS

Page 2: Applications of DFShaimk/algorithms/topological-sort... · Topological sort •Order the vertices by reverse finishing times 9/14 10/13 11/12 1/8 2/7 3/6 4/5 9/14 10/13 11/12 3/6

DFS(G)1. For each vertex 𝑢 ∈ 𝐺. 𝑉2. u.color = WHITE3. u.𝜋 = NIL4. time = 05. For each vertex 𝑢 ∈ 𝐺. 𝑉6. if u.color == WHITE7. DFS-Visit(G,u)

DFS-Visit(G,u)1. time = time + 12. u.d = time3. u.color = GRAY4. For each vertex 𝑣 ∈ 𝐺. 𝐴𝑑𝑗 𝑢5. if v.color == WHITE6. v.𝜋 = u7. DFS-Visit(G,v)8. u.color = BLACK9. time = time + 110. u.f = time

Each vertex has two timestamps:𝒖. 𝒅 : First discovered and GRAYed𝒖. 𝒇 : Finished and Blackened∀𝒖: 𝟏 ≤ 𝒖. 𝒅 < 𝒖. 𝒇 ≤ 𝟐𝒏

Page 3: Applications of DFShaimk/algorithms/topological-sort... · Topological sort •Order the vertices by reverse finishing times 9/14 10/13 11/12 1/8 2/7 3/6 4/5 9/14 10/13 11/12 3/6

u

v

y

x

1 2 3 4 5 6 7 8 9 10 11 12

w

z

Page 4: Applications of DFShaimk/algorithms/topological-sort... · Topological sort •Order the vertices by reverse finishing times 9/14 10/13 11/12 1/8 2/7 3/6 4/5 9/14 10/13 11/12 3/6

DFS(G)1. For each vertex 𝑢 ∈ 𝐺. 𝑉2. u.color = WHITE3. u.𝜋 = NIL4. time = 05. For each vertex 𝑢 ∈ 𝐺. 𝑉6. if u.color == WHITE7. DFS-Visit(G,u)

DFS-Visit(G,u)1. time = time + 12. u.d = time3. u.color = GRAY4. For each vertex 𝑣 ∈ 𝐺. 𝐴𝑑𝑗 𝑢5. if v.color == WHITE6. v.𝜋 = u7. DFS-Visit(G,v)8. u.color = BLACK9. time = time + 110. u.f = time

Thm 2: 𝑣. 𝑑, 𝑣. 𝑓 ⊂ 𝑢. 𝑑, 𝑢. 𝑓 iff v is a descendant of u in the DFS-forest, 𝑣. 𝑑, 𝑣. 𝑓 ∩ 𝑢. 𝑑, 𝑢. 𝑓 = ∅ iff u and v

are unrelated in the DFS-forest

Page 5: Applications of DFShaimk/algorithms/topological-sort... · Topological sort •Order the vertices by reverse finishing times 9/14 10/13 11/12 1/8 2/7 3/6 4/5 9/14 10/13 11/12 3/6

DFS(G)1. For each vertex 𝑢 ∈ 𝐺. 𝑉2. u.color = WHITE3. u.𝜋 = NIL4. time = 05. For each vertex 𝑢 ∈ 𝐺. 𝑉6. if u.color == WHITE7. DFS-Visit(G,u)

DFS-Visit(G,u)1. time = time + 12. u.d = time3. u.color = GRAY4. For each vertex 𝑣 ∈ 𝐺. 𝐴𝑑𝑗 𝑢5. if v.color == WHITE6. v.𝜋 = u7. DFS-Visit(G,v)8. u.color = BLACK9. time = time + 110. u.f = time

Thm 3: 𝑣 is a descendant of 𝑢 in the DFS-forest iff when we invoke DFS-Visit(u) there is a white path from 𝑢to 𝑣

Page 6: Applications of DFShaimk/algorithms/topological-sort... · Topological sort •Order the vertices by reverse finishing times 9/14 10/13 11/12 1/8 2/7 3/6 4/5 9/14 10/13 11/12 3/6

u

v

y

x

1 2 3 4 5 6 7 8 9 10 11 12

w

z

The green forward edge should point to x

Page 7: Applications of DFShaimk/algorithms/topological-sort... · Topological sort •Order the vertices by reverse finishing times 9/14 10/13 11/12 1/8 2/7 3/6 4/5 9/14 10/13 11/12 3/6

Edge classification

• Tree edges: (u,v), DFS-Visit(v) was called from DFS-visit(u)

• Back edges: (u,v) such that v is an ancestor of u in the DFS-forest

• Forward edges: nontree edges (u,v) such that v is a descendant of u

• Cross edges: (u,v) such that 𝑣. 𝑓 < 𝑢. 𝑑

uv

Observe, among non-tree edges:1. (Only) backward edges go to a vertex with a later finish time2. (Only) forward edges go to a vertex with later discovery time

Page 8: Applications of DFShaimk/algorithms/topological-sort... · Topological sort •Order the vertices by reverse finishing times 9/14 10/13 11/12 1/8 2/7 3/6 4/5 9/14 10/13 11/12 3/6

Is my directed graph acyclic ?

Thm 6: 𝐺 is acyclic iff DFS on 𝐺 does not produce back edges

This is actually Thm 5 if we want to be consistent with previous videos

Page 9: Applications of DFShaimk/algorithms/topological-sort... · Topological sort •Order the vertices by reverse finishing times 9/14 10/13 11/12 1/8 2/7 3/6 4/5 9/14 10/13 11/12 3/6
Page 10: Applications of DFShaimk/algorithms/topological-sort... · Topological sort •Order the vertices by reverse finishing times 9/14 10/13 11/12 1/8 2/7 3/6 4/5 9/14 10/13 11/12 3/6

Topological sort

• Input: 𝐺 = (𝑉, 𝐸) a directed, acyclic graph (DAG)

Page 11: Applications of DFShaimk/algorithms/topological-sort... · Topological sort •Order the vertices by reverse finishing times 9/14 10/13 11/12 1/8 2/7 3/6 4/5 9/14 10/13 11/12 3/6

Topological sort

• Input: G=(V,E) a directed, acyclic graph

• Output: Ordering of the vertices such that if 𝑢, 𝑣 ∈ 𝐸 then 𝑢precedes 𝑣

1

3

564

2

0 6543210

Page 12: Applications of DFShaimk/algorithms/topological-sort... · Topological sort •Order the vertices by reverse finishing times 9/14 10/13 11/12 1/8 2/7 3/6 4/5 9/14 10/13 11/12 3/6

Recall:

Observe, among non-tree edges:1. (Only) backward edges go to a vertex with a later finish time2. (Only) forward edges go to a vertex with later discovery time

Page 13: Applications of DFShaimk/algorithms/topological-sort... · Topological sort •Order the vertices by reverse finishing times 9/14 10/13 11/12 1/8 2/7 3/6 4/5 9/14 10/13 11/12 3/6

Topological sort

• Lets run DFS

1/

Page 14: Applications of DFShaimk/algorithms/topological-sort... · Topological sort •Order the vertices by reverse finishing times 9/14 10/13 11/12 1/8 2/7 3/6 4/5 9/14 10/13 11/12 3/6

Topological sort

• Lets run DFS

2/

1/

Page 15: Applications of DFShaimk/algorithms/topological-sort... · Topological sort •Order the vertices by reverse finishing times 9/14 10/13 11/12 1/8 2/7 3/6 4/5 9/14 10/13 11/12 3/6

Topological sort

• Lets run DFS

3/

2/

1/

Page 16: Applications of DFShaimk/algorithms/topological-sort... · Topological sort •Order the vertices by reverse finishing times 9/14 10/13 11/12 1/8 2/7 3/6 4/5 9/14 10/13 11/12 3/6

Topological sort

• Lets run DFS

4/3/

2/

1/

Page 17: Applications of DFShaimk/algorithms/topological-sort... · Topological sort •Order the vertices by reverse finishing times 9/14 10/13 11/12 1/8 2/7 3/6 4/5 9/14 10/13 11/12 3/6

Topological sort

• Lets run DFS

4/53/

2/

1/

Page 18: Applications of DFShaimk/algorithms/topological-sort... · Topological sort •Order the vertices by reverse finishing times 9/14 10/13 11/12 1/8 2/7 3/6 4/5 9/14 10/13 11/12 3/6

Topological sort

• Lets run DFS

4/53/6

2/

1/

Page 19: Applications of DFShaimk/algorithms/topological-sort... · Topological sort •Order the vertices by reverse finishing times 9/14 10/13 11/12 1/8 2/7 3/6 4/5 9/14 10/13 11/12 3/6

Topological sort

• Lets run DFS

4/53/6

2/7

1/

Page 20: Applications of DFShaimk/algorithms/topological-sort... · Topological sort •Order the vertices by reverse finishing times 9/14 10/13 11/12 1/8 2/7 3/6 4/5 9/14 10/13 11/12 3/6

Topological sort

• Lets run DFS

4/53/6

2/7

1/

Page 21: Applications of DFShaimk/algorithms/topological-sort... · Topological sort •Order the vertices by reverse finishing times 9/14 10/13 11/12 1/8 2/7 3/6 4/5 9/14 10/13 11/12 3/6

Topological sort

• Lets run DFS

4/53/6

2/7

1/8

Page 22: Applications of DFShaimk/algorithms/topological-sort... · Topological sort •Order the vertices by reverse finishing times 9/14 10/13 11/12 1/8 2/7 3/6 4/5 9/14 10/13 11/12 3/6

Topological sort

• Lets run DFS

9/14

10/13

11/12

4/53/6

2/7

1/8

Page 23: Applications of DFShaimk/algorithms/topological-sort... · Topological sort •Order the vertices by reverse finishing times 9/14 10/13 11/12 1/8 2/7 3/6 4/5 9/14 10/13 11/12 3/6

Topological sort

• Order the vertices by reverse finishing times

9/14

10/13

11/12

4/53/6

2/7

1/8

Page 24: Applications of DFShaimk/algorithms/topological-sort... · Topological sort •Order the vertices by reverse finishing times 9/14 10/13 11/12 1/8 2/7 3/6 4/5 9/14 10/13 11/12 3/6

Topological sort

• Order the vertices by reverse finishing times

9/14 10/13 11/12 4/53/62/71/8

9/14

10/13

11/12

4/53/6

2/7

1/8


Recommended