6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald...

194
6.1 & 6.2: Graph Searching Frank Stajano Thomas Sauerwald Lent 2016 s v y x s v w x y z r u 1/ s 2/ v 3/ y 4/ x

Transcript of 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald...

Page 1: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

6.1 & 6.2: Graph SearchingFrank Stajano Thomas Sauerwald

Lent 2016

Complete Execution of DFS

s v y

r u

x

w z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 12

Page 2: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Outline

Introduction to Graphs and Graph Searching

Breadth-First Search

Depth-First Search

Topological Sort

6.1 & 6.2: Graph Searching T.S. 2

Page 3: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Origin of Graph Theory

Leonhard Euler (1707-1783)

Seven Bridges at Königsberg 1737

Is there a tour which crosseseach bridge exactly once?

Is there a tour which visits everyisland exactly once?

1B course: Complexity Theory

A

B

C

D

A

B

C

D

Source: Wikipedia

Source: Wikipedia

6.1 & 6.2: Graph Searching T.S. 3

Page 4: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Origin of Graph Theory

Leonhard Euler (1707-1783)Seven Bridges at Königsberg 1737

Is there a tour which crosseseach bridge exactly once?

Is there a tour which visits everyisland exactly once?

1B course: Complexity Theory

A

B

C

D

A

B

C

D

Source: Wikipedia Source: Wikipedia

6.1 & 6.2: Graph Searching T.S. 3

Page 5: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Origin of Graph Theory

Leonhard Euler (1707-1783)Seven Bridges at Königsberg 1737

Is there a tour which crosseseach bridge exactly once?

Is there a tour which visits everyisland exactly once?

1B course: Complexity Theory

A

B

C

D

A

B

C

D

Source: Wikipedia Source: Wikipedia

6.1 & 6.2: Graph Searching T.S. 3

Page 6: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Origin of Graph Theory

Leonhard Euler (1707-1783)Seven Bridges at Königsberg 1737

Is there a tour which crosseseach bridge exactly once?

Is there a tour which visits everyisland exactly once?

1B course: Complexity Theory

A

B

C

D

A

B

C

D

Source: Wikipedia Source: Wikipedia

6.1 & 6.2: Graph Searching T.S. 3

Page 7: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Origin of Graph Theory

Leonhard Euler (1707-1783)Seven Bridges at Königsberg 1737

Is there a tour which crosseseach bridge exactly once?

Is there a tour which visits everyisland exactly once?

1B course: Complexity Theory

A

B

C

D

A

B

C

D

Source: Wikipedia Source: Wikipedia

6.1 & 6.2: Graph Searching T.S. 3

Page 8: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Origin of Graph Theory

Leonhard Euler (1707-1783)Seven Bridges at Königsberg 1737

Is there a tour which crosseseach bridge exactly once?

Is there a tour which visits everyisland exactly once?

1B course: Complexity Theory

A

B

C

D

A

B

C

D

Source: Wikipedia Source: Wikipedia

6.1 & 6.2: Graph Searching T.S. 3

Page 9: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Origin of Graph Theory

Leonhard Euler (1707-1783)Seven Bridges at Königsberg 1737

Is there a tour which crosseseach bridge exactly once?

Is there a tour which visits everyisland exactly once?

1B course: Complexity Theory

A

B

C

D

A

B

C

D

Source: Wikipedia Source: Wikipedia

6.1 & 6.2: Graph Searching T.S. 3

Page 10: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

What is a Graph?

A graph G = (V ,E) consists of:

V : the set of vertices

E : the set of edges (arcs)

Directed Graph

A graph G = (V ,E) consists of:

V : the set of vertices

E : the set of (undirected) edges

Undirected Graph

A sequence of edges between twovertices forms a path

If each pair of vertices has a pathlinking them, then G is connected

Paths and Connectivity

1 2

3 4

V = {1, 2, 3, 4}E = {(1, 2), (1, 3), (2, 3), (3, 1), (3, 4)}

Path p = (1, 2, 3, 4)Path p = (1, 2, 3, 1), which is a cycle

1 2

3 4

V = {1, 2, 3, 4}E = {{1, 2}, {1, 3}, {2, 3}, {3, 4}}

G is not connected

G is connected

Later: edge-weighted graphs G = (V ,E ,w)

6.1 & 6.2: Graph Searching T.S. 4

Page 11: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

What is a Graph?

A graph G = (V ,E) consists of:

V : the set of vertices

E : the set of edges (arcs)

Directed Graph

A graph G = (V ,E) consists of:

V : the set of vertices

E : the set of (undirected) edges

Undirected Graph

A sequence of edges between twovertices forms a path

If each pair of vertices has a pathlinking them, then G is connected

Paths and Connectivity

1 2

3 4

V = {1, 2, 3, 4}E = {(1, 2), (1, 3), (2, 3), (3, 1), (3, 4)}

Path p = (1, 2, 3, 4)Path p = (1, 2, 3, 1), which is a cycle

1 2

3 4

V = {1, 2, 3, 4}E = {{1, 2}, {1, 3}, {2, 3}, {3, 4}}

G is not connected

G is connected

Later: edge-weighted graphs G = (V ,E ,w)

6.1 & 6.2: Graph Searching T.S. 4

Page 12: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

What is a Graph?

A graph G = (V ,E) consists of:

V : the set of vertices

E : the set of edges (arcs)

Directed Graph

A graph G = (V ,E) consists of:

V : the set of vertices

E : the set of (undirected) edges

Undirected Graph

A sequence of edges between twovertices forms a path

If each pair of vertices has a pathlinking them, then G is connected

Paths and Connectivity

1 2

3 4

V = {1, 2, 3, 4}E = {(1, 2), (1, 3), (2, 3), (3, 1), (3, 4)}

Path p = (1, 2, 3, 4)Path p = (1, 2, 3, 1), which is a cycle

1 2

3 4

V = {1, 2, 3, 4}E = {{1, 2}, {1, 3}, {2, 3}, {3, 4}}

G is not connected

G is connected

Later: edge-weighted graphs G = (V ,E ,w)

6.1 & 6.2: Graph Searching T.S. 4

Page 13: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

What is a Graph?

A graph G = (V ,E) consists of:

V : the set of vertices

E : the set of edges (arcs)

Directed Graph

A graph G = (V ,E) consists of:

V : the set of vertices

E : the set of (undirected) edges

Undirected Graph

A sequence of edges between twovertices forms a path

If each pair of vertices has a pathlinking them, then G is connected

Paths and Connectivity

1 2

3 4

V = {1, 2, 3, 4}E = {(1, 2), (1, 3), (2, 3), (3, 1), (3, 4)}

Path p = (1, 2, 3, 4)Path p = (1, 2, 3, 1), which is a cycle

1 2

3 4

V = {1, 2, 3, 4}E = {{1, 2}, {1, 3}, {2, 3}, {3, 4}}

G is not connected

G is connected

Later: edge-weighted graphs G = (V ,E ,w)

6.1 & 6.2: Graph Searching T.S. 4

Page 14: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

What is a Graph?

A graph G = (V ,E) consists of:

V : the set of vertices

E : the set of edges (arcs)

Directed Graph

A graph G = (V ,E) consists of:

V : the set of vertices

E : the set of (undirected) edges

Undirected Graph

A sequence of edges between twovertices forms a path

If each pair of vertices has a pathlinking them, then G is connected

Paths and Connectivity

1 2

3 4

V = {1, 2, 3, 4}E = {(1, 2), (1, 3), (2, 3), (3, 1), (3, 4)}

Path p = (1, 2, 3, 4)Path p = (1, 2, 3, 1), which is a cycle

1 2

3 4

V = {1, 2, 3, 4}E = {{1, 2}, {1, 3}, {2, 3}, {3, 4}}

G is not connected

G is connected

Later: edge-weighted graphs G = (V ,E ,w)

6.1 & 6.2: Graph Searching T.S. 4

Page 15: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

What is a Graph?

A graph G = (V ,E) consists of:

V : the set of vertices

E : the set of edges (arcs)

Directed Graph

A graph G = (V ,E) consists of:

V : the set of vertices

E : the set of (undirected) edges

Undirected Graph

A sequence of edges between twovertices forms a path

If each pair of vertices has a pathlinking them, then G is connected

Paths and Connectivity

1 2

3 4

V = {1, 2, 3, 4}E = {(1, 2), (1, 3), (2, 3), (3, 1), (3, 4)}

Path p = (1, 2, 3, 4)Path p = (1, 2, 3, 1), which is a cycle

1 2

3 4

V = {1, 2, 3, 4}E = {{1, 2}, {1, 3}, {2, 3}, {3, 4}}

G is not connected

G is connected

Later: edge-weighted graphs G = (V ,E ,w)

6.1 & 6.2: Graph Searching T.S. 4

Page 16: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

What is a Graph?

A graph G = (V ,E) consists of:

V : the set of vertices

E : the set of edges (arcs)

Directed Graph

A graph G = (V ,E) consists of:

V : the set of vertices

E : the set of (undirected) edges

Undirected Graph

A sequence of edges between twovertices forms a path

If each pair of vertices has a pathlinking them, then G is connected

Paths and Connectivity

1 2

3 4

V = {1, 2, 3, 4}E = {(1, 2), (1, 3), (2, 3), (3, 1), (3, 4)}

Path p = (1, 2, 3, 4)

Path p = (1, 2, 3, 1), which is a cycle

1 2

3 4

V = {1, 2, 3, 4}E = {{1, 2}, {1, 3}, {2, 3}, {3, 4}}

G is not connected

G is connected

Later: edge-weighted graphs G = (V ,E ,w)

6.1 & 6.2: Graph Searching T.S. 4

Page 17: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

What is a Graph?

A graph G = (V ,E) consists of:

V : the set of vertices

E : the set of edges (arcs)

Directed Graph

A graph G = (V ,E) consists of:

V : the set of vertices

E : the set of (undirected) edges

Undirected Graph

A sequence of edges between twovertices forms a path

If each pair of vertices has a pathlinking them, then G is connected

Paths and Connectivity

1 2

3 4

V = {1, 2, 3, 4}E = {(1, 2), (1, 3), (2, 3), (3, 1), (3, 4)}

Path p = (1, 2, 3, 4)

Path p = (1, 2, 3, 1), which is a cycle

1 2

3 4

V = {1, 2, 3, 4}E = {{1, 2}, {1, 3}, {2, 3}, {3, 4}}

G is not connected

G is connected

Later: edge-weighted graphs G = (V ,E ,w)

6.1 & 6.2: Graph Searching T.S. 4

Page 18: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

What is a Graph?

A graph G = (V ,E) consists of:

V : the set of vertices

E : the set of edges (arcs)

Directed Graph

A graph G = (V ,E) consists of:

V : the set of vertices

E : the set of (undirected) edges

Undirected Graph

A sequence of edges between twovertices forms a path

If each pair of vertices has a pathlinking them, then G is connected

Paths and Connectivity

1 2

3 4

V = {1, 2, 3, 4}E = {(1, 2), (1, 3), (2, 3), (3, 1), (3, 4)}

Path p = (1, 2, 3, 4)Path p = (1, 2, 3, 1), which is a cycle

1 2

3 4

V = {1, 2, 3, 4}E = {{1, 2}, {1, 3}, {2, 3}, {3, 4}}

G is not connected

G is connected

Later: edge-weighted graphs G = (V ,E ,w)

6.1 & 6.2: Graph Searching T.S. 4

Page 19: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

What is a Graph?

A graph G = (V ,E) consists of:

V : the set of vertices

E : the set of edges (arcs)

Directed Graph

A graph G = (V ,E) consists of:

V : the set of vertices

E : the set of (undirected) edges

Undirected Graph

A sequence of edges between twovertices forms a path

If each pair of vertices has a pathlinking them, then G is connected

Paths and Connectivity

1 2

3 4

V = {1, 2, 3, 4}E = {(1, 2), (1, 3), (2, 3), (3, 1), (3, 4)}

Path p = (1, 2, 3, 4)Path p = (1, 2, 3, 1), which is a cycle

1 2

3 4

V = {1, 2, 3, 4}E = {{1, 2}, {1, 3}, {2, 3}, {3, 4}}

G is not connected

G is connected

Later: edge-weighted graphs G = (V ,E ,w)

6.1 & 6.2: Graph Searching T.S. 4

Page 20: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

What is a Graph?

A graph G = (V ,E) consists of:

V : the set of vertices

E : the set of edges (arcs)

Directed Graph

A graph G = (V ,E) consists of:

V : the set of vertices

E : the set of (undirected) edges

Undirected Graph

A sequence of edges between twovertices forms a path

If each pair of vertices has a pathlinking them, then G is connected

Paths and Connectivity

1 2

3 4

V = {1, 2, 3, 4}E = {(1, 2), (1, 3), (2, 3), (3, 1), (3, 4)}

Path p = (1, 2, 3, 4)Path p = (1, 2, 3, 1), which is a cycle

1 2

3 4

V = {1, 2, 3, 4}E = {{1, 2}, {1, 3}, {2, 3}, {3, 4}}

G is not connected

G is connected

Later: edge-weighted graphs G = (V ,E ,w)

6.1 & 6.2: Graph Searching T.S. 4

Page 21: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Representations of Directed and Undirected Graphs590 Chapter 22 Elementary Graph Algorithms

1 2

3

45

12345

2 51224 1 2

5 34

45 31 0 0 10 1 1 11 0 1 01 1 0 11 0 1 0

01001

1 2 3 4 512345

(a) (b) (c)

Figure 22.1 Two representations of an undirected graph. (a)An undirected graph G with 5 verticesand 7 edges. (b) An adjacency-list representation of G. (c) The adjacency-matrix representationof G.

1 2

54

12345

2 456246

5

1 0 1 00 0 0 10 0 0 11 0 0 00 0 1 0

00000

1 2 3 4 512345

(a) (b) (c)

3

6 6

6

6 0 0 0 0 0 100100

Figure 22.2 Two representations of a directed graph. (a) A directed graph G with 6 vertices and 8edges. (b) An adjacency-list representation of G. (c) The adjacency-matrix representation of G.

shortest-paths algorithms presented in Chapter 25 assume that their input graphsare represented by adjacency matrices.

The adjacency-list representation of a graph G D .V; E/ consists of an ar-ray Adj of jV j lists, one for each vertex in V . For each u 2 V , the adjacency listAdjŒu! contains all the vertices " such that there is an edge .u; "/ 2 E. That is,AdjŒu! consists of all the vertices adjacent to u in G. (Alternatively, it may containpointers to these vertices.) Since the adjacency lists represent the edges of a graph,in pseudocode we treat the array Adj as an attribute of the graph, just as we treatthe edge set E. In pseudocode, therefore, we will see notation such as G:AdjŒu!.Figure 22.1(b) is an adjacency-list representation of the undirected graph in Fig-ure 22.1(a). Similarly, Figure 22.2(b) is an adjacency-list representation of thedirected graph in Figure 22.2(a).

If G is a directed graph, the sum of the lengths of all the adjacency lists is jEj,since an edge of the form .u; "/ is represented by having " appear in AdjŒu!. If G is

Most times we will use the adjacency-list representation!

6.1 & 6.2: Graph Searching T.S. 5

Page 22: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Representations of Directed and Undirected Graphs590 Chapter 22 Elementary Graph Algorithms

1 2

3

45

12345

2 51224 1 2

5 34

45 31 0 0 10 1 1 11 0 1 01 1 0 11 0 1 0

01001

1 2 3 4 512345

(a) (b) (c)

Figure 22.1 Two representations of an undirected graph. (a)An undirected graph G with 5 verticesand 7 edges. (b) An adjacency-list representation of G. (c) The adjacency-matrix representationof G.

1 2

54

12345

2 456246

5

1 0 1 00 0 0 10 0 0 11 0 0 00 0 1 0

00000

1 2 3 4 512345

(a) (b) (c)

3

6 6

6

6 0 0 0 0 0 100100

Figure 22.2 Two representations of a directed graph. (a) A directed graph G with 6 vertices and 8edges. (b) An adjacency-list representation of G. (c) The adjacency-matrix representation of G.

shortest-paths algorithms presented in Chapter 25 assume that their input graphsare represented by adjacency matrices.

The adjacency-list representation of a graph G D .V; E/ consists of an ar-ray Adj of jV j lists, one for each vertex in V . For each u 2 V , the adjacency listAdjŒu! contains all the vertices " such that there is an edge .u; "/ 2 E. That is,AdjŒu! consists of all the vertices adjacent to u in G. (Alternatively, it may containpointers to these vertices.) Since the adjacency lists represent the edges of a graph,in pseudocode we treat the array Adj as an attribute of the graph, just as we treatthe edge set E. In pseudocode, therefore, we will see notation such as G:AdjŒu!.Figure 22.1(b) is an adjacency-list representation of the undirected graph in Fig-ure 22.1(a). Similarly, Figure 22.2(b) is an adjacency-list representation of thedirected graph in Figure 22.2(a).

If G is a directed graph, the sum of the lengths of all the adjacency lists is jEj,since an edge of the form .u; "/ is represented by having " appear in AdjŒu!. If G is

Most times we will use the adjacency-list representation!

6.1 & 6.2: Graph Searching T.S. 5

Page 23: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Overview

Amortized Analysis

Fibonacci Heaps

Disjoint Sets

Graphs, DFS/BFS, Topological Sort

Minimum Spanning Trees

Single-Source/All-Pairs Shortest Paths

Maximum Flow, Bipartite Matchings

Geometric Algorithms

Priority Queues

Sorting

Dynamic Programming

Greedy

6.1 & 6.2: Graph Searching T.S. 0

Page 24: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Overview

Amortized Analysis

Fibonacci Heaps

Disjoint Sets

Graphs, DFS/BFS, Topological Sort

Minimum Spanning Trees

Single-Source/All-Pairs Shortest Paths

Maximum Flow, Bipartite Matchings

Geometric Algorithms

Priority Queues

Sorting

Dynamic Programming

Greedy

6.1 & 6.2: Graph Searching T.S. 0

Page 25: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Overview

Amortized Analysis

Fibonacci Heaps

Disjoint Sets

Graphs, DFS/BFS, Topological Sort

Minimum Spanning Trees

Single-Source/All-Pairs Shortest Paths

Maximum Flow, Bipartite Matchings

Geometric Algorithms

Priority Queues

Sorting

Dynamic Programming

Greedy

6.1 & 6.2: Graph Searching T.S. 0

Page 26: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Overview

Amortized Analysis

Fibonacci Heaps

Disjoint Sets

Graphs, DFS/BFS, Topological Sort

Minimum Spanning Trees

Single-Source/All-Pairs Shortest Paths

Maximum Flow, Bipartite Matchings

Geometric Algorithms

Priority Queues

Sorting

Dynamic Programming

Greedy

6.1 & 6.2: Graph Searching T.S. 0

Page 27: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Overview

Amortized Analysis

Fibonacci Heaps

Disjoint Sets

Graphs, DFS/BFS, Topological Sort

Minimum Spanning Trees

Single-Source/All-Pairs Shortest Paths

Maximum Flow, Bipartite Matchings

Geometric Algorithms

Priority Queues

Sorting

Dynamic Programming

Greedy

6.1 & 6.2: Graph Searching T.S. 0

Page 28: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Graph Searching

1

2

3

4

5

6

7

8

9

10

Graph searching means traversing a graph via the edges in order tovisit all vertices

useful for identifying connected components, computing thediameter etc.

Two strategies: Breadth-First-Search and Depth-First-Search

Overview

Measure time complexity in terms of the size of V and E(often write just V instead of |V |, and E instead of |E |)

6.1 & 6.2: Graph Searching T.S. 6

Page 29: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Graph Searching

1

2

3

4

5

6

7

8

9

10

Graph searching means traversing a graph via the edges in order tovisit all vertices

useful for identifying connected components, computing thediameter etc.

Two strategies: Breadth-First-Search and Depth-First-Search

Overview

Measure time complexity in terms of the size of V and E(often write just V instead of |V |, and E instead of |E |)

6.1 & 6.2: Graph Searching T.S. 6

Page 30: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Graph Searching

1

2

3

4

5

6

7

8

9

10

Graph searching means traversing a graph via the edges in order tovisit all vertices

useful for identifying connected components, computing thediameter etc.

Two strategies: Breadth-First-Search and Depth-First-Search

Overview

Measure time complexity in terms of the size of V and E(often write just V instead of |V |, and E instead of |E |)

6.1 & 6.2: Graph Searching T.S. 6

Page 31: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Outline

Introduction to Graphs and Graph Searching

Breadth-First Search

Depth-First Search

Topological Sort

6.1 & 6.2: Graph Searching T.S. 7

Page 32: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Breadth-First Search: Basic Ideas

s

Given an undirected/directed graph G = (V ,E) and source vertex s

BFS sends out a wave from s compute distances/shortest paths

Vertex Colours:

White = Unvisited

Grey = Visited, but not all neighbors (=adjacent vertices)

Black = Visited and all neighbors

Basic Idea

6.1 & 6.2: Graph Searching T.S. 8

Page 33: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Breadth-First Search: Basic Ideas

s

Given an undirected/directed graph G = (V ,E) and source vertex s

BFS sends out a wave from s compute distances/shortest paths

Vertex Colours:

White = Unvisited

Grey = Visited, but not all neighbors (=adjacent vertices)

Black = Visited and all neighbors

Basic Idea

6.1 & 6.2: Graph Searching T.S. 8

Page 34: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Breadth-First Search: Basic Ideas

s

Given an undirected/directed graph G = (V ,E) and source vertex s

BFS sends out a wave from s compute distances/shortest paths

Vertex Colours:

White = Unvisited

Grey = Visited, but not all neighbors (=adjacent vertices)

Black = Visited and all neighbors

Basic Idea

6.1 & 6.2: Graph Searching T.S. 8

Page 35: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Breadth-First-Search: Pseudocode

0: def bfs(G,s)1: Run BFS on the given graph G2: starting from source s3:4: assert(s in G.vertices())5:6: # Initialize graph and queue7: for v in G.vertices():8: v.predecessor = None9: v.d = Infinity # .d = distance from s10: v.colour = "white"11: Q = Queue()12:13: # Visit source vertex14: s.d = 015: s.colour = "grey"16: Q.insert(s)17:18: # Visit the adjacents of each vertex in Q19: while not Q.isEmpty():20: u = Q.extract()21: assert (u.colour == "grey")22: for v in u.adjacent()23: if v.colour = "white"24: v.colour = "grey"25: v.d = u.d+126: v.predecessor = u27: Q.insert(v)28: u.colour = "black"

From any vertex, visit all adjacentvertices before going any deeper

Vertex Colours:

White = Unvisited

Grey = Visited, but not all neighbors

Black = Visited and all neighbors

Runtime

Assuming that all executions of the FOR-loopfor u takes O(|u.adj|) (adjacency list model!)

∑u∈V |u.adj| = 2|E |

6.1 & 6.2: Graph Searching T.S. 9

Page 36: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Breadth-First-Search: Pseudocode

0: def bfs(G,s)1: Run BFS on the given graph G2: starting from source s3:4: assert(s in G.vertices())5:6: # Initialize graph and queue7: for v in G.vertices():8: v.predecessor = None9: v.d = Infinity # .d = distance from s10: v.colour = "white"11: Q = Queue()12:13: # Visit source vertex14: s.d = 015: s.colour = "grey"16: Q.insert(s)17:18: # Visit the adjacents of each vertex in Q19: while not Q.isEmpty():20: u = Q.extract()21: assert (u.colour == "grey")22: for v in u.adjacent()23: if v.colour = "white"24: v.colour = "grey"25: v.d = u.d+126: v.predecessor = u27: Q.insert(v)28: u.colour = "black"

From any vertex, visit all adjacentvertices before going any deeper

Vertex Colours:

White = Unvisited

Grey = Visited, but not all neighbors

Black = Visited and all neighbors

Runtime

Assuming that all executions of the FOR-loopfor u takes O(|u.adj|) (adjacency list model!)

∑u∈V |u.adj| = 2|E |

6.1 & 6.2: Graph Searching T.S. 9

Page 37: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Breadth-First-Search: Pseudocode

0: def bfs(G,s)1: Run BFS on the given graph G2: starting from source s3:4: assert(s in G.vertices())5:6: # Initialize graph and queue7: for v in G.vertices():8: v.predecessor = None9: v.d = Infinity # .d = distance from s10: v.colour = "white"11: Q = Queue()12:13: # Visit source vertex14: s.d = 015: s.colour = "grey"16: Q.insert(s)17:18: # Visit the adjacents of each vertex in Q19: while not Q.isEmpty():20: u = Q.extract()21: assert (u.colour == "grey")22: for v in u.adjacent()23: if v.colour = "white"24: v.colour = "grey"25: v.d = u.d+126: v.predecessor = u27: Q.insert(v)28: u.colour = "black"

From any vertex, visit all adjacentvertices before going any deeper

Vertex Colours:

White = Unvisited

Grey = Visited, but not all neighbors

Black = Visited and all neighbors

Runtime

Assuming that all executions of the FOR-loopfor u takes O(|u.adj|) (adjacency list model!)

∑u∈V |u.adj| = 2|E |

6.1 & 6.2: Graph Searching T.S. 9

Page 38: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Breadth-First-Search: Pseudocode

0: def bfs(G,s)1: Run BFS on the given graph G2: starting from source s3:4: assert(s in G.vertices())5:6: # Initialize graph and queue7: for v in G.vertices():8: v.predecessor = None9: v.d = Infinity # .d = distance from s10: v.colour = "white"11: Q = Queue()12:13: # Visit source vertex14: s.d = 015: s.colour = "grey"16: Q.insert(s)17:18: # Visit the adjacents of each vertex in Q19: while not Q.isEmpty():20: u = Q.extract()21: assert (u.colour == "grey")22: for v in u.adjacent()23: if v.colour = "white"24: v.colour = "grey"25: v.d = u.d+126: v.predecessor = u27: Q.insert(v)28: u.colour = "black"

From any vertex, visit all adjacentvertices before going any deeper

Vertex Colours:

White = Unvisited

Grey = Visited, but not all neighbors

Black = Visited and all neighbors

Runtime ???

Assuming that all executions of the FOR-loopfor u takes O(|u.adj|) (adjacency list model!)

∑u∈V |u.adj| = 2|E |

6.1 & 6.2: Graph Searching T.S. 9

Page 39: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Breadth-First-Search: Pseudocode

0: def bfs(G,s)1: Run BFS on the given graph G2: starting from source s3:4: assert(s in G.vertices())5:6: # Initialize graph and queue7: for v in G.vertices():8: v.predecessor = None9: v.d = Infinity # .d = distance from s10: v.colour = "white"11: Q = Queue()12:13: # Visit source vertex14: s.d = 015: s.colour = "grey"16: Q.insert(s)17:18: # Visit the adjacents of each vertex in Q19: while not Q.isEmpty():20: u = Q.extract()21: assert (u.colour == "grey")22: for v in u.adjacent()23: if v.colour = "white"24: v.colour = "grey"25: v.d = u.d+126: v.predecessor = u27: Q.insert(v)28: u.colour = "black"

From any vertex, visit all adjacentvertices before going any deeper

Vertex Colours:

White = Unvisited

Grey = Visited, but not all neighbors

Black = Visited and all neighbors

Runtime ???

Assuming that all executions of the FOR-loopfor u takes O(|u.adj|) (adjacency list model!)

∑u∈V |u.adj| = 2|E |

6.1 & 6.2: Graph Searching T.S. 9

Page 40: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Breadth-First-Search: Pseudocode

0: def bfs(G,s)1: Run BFS on the given graph G2: starting from source s3:4: assert(s in G.vertices())5:6: # Initialize graph and queue7: for v in G.vertices():8: v.predecessor = None9: v.d = Infinity # .d = distance from s10: v.colour = "white"11: Q = Queue()12:13: # Visit source vertex14: s.d = 015: s.colour = "grey"16: Q.insert(s)17:18: # Visit the adjacents of each vertex in Q19: while not Q.isEmpty():20: u = Q.extract()21: assert (u.colour == "grey")22: for v in u.adjacent()23: if v.colour = "white"24: v.colour = "grey"25: v.d = u.d+126: v.predecessor = u27: Q.insert(v)28: u.colour = "black"

From any vertex, visit all adjacentvertices before going any deeper

Vertex Colours:

White = Unvisited

Grey = Visited, but not all neighbors

Black = Visited and all neighbors

Runtime ???

Assuming that all executions of the FOR-loopfor u takes O(|u.adj|) (adjacency list model!)

∑u∈V |u.adj| = 2|E |

6.1 & 6.2: Graph Searching T.S. 9

Page 41: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Breadth-First-Search: Pseudocode

0: def bfs(G,s)1: Run BFS on the given graph G2: starting from source s3:4: assert(s in G.vertices())5:6: # Initialize graph and queue7: for v in G.vertices():8: v.predecessor = None9: v.d = Infinity # .d = distance from s10: v.colour = "white"11: Q = Queue()12:13: # Visit source vertex14: s.d = 015: s.colour = "grey"16: Q.insert(s)17:18: # Visit the adjacents of each vertex in Q19: while not Q.isEmpty():20: u = Q.extract()21: assert (u.colour == "grey")22: for v in u.adjacent()23: if v.colour = "white"24: v.colour = "grey"25: v.d = u.d+126: v.predecessor = u27: Q.insert(v)28: u.colour = "black"

From any vertex, visit all adjacentvertices before going any deeper

Vertex Colours:

White = Unvisited

Grey = Visited, but not all neighbors

Black = Visited and all neighbors

Runtime ???

Assuming that all executions of the FOR-loopfor u takes O(|u.adj|) (adjacency list model!)∑

u∈V |u.adj| = 2|E |

6.1 & 6.2: Graph Searching T.S. 9

Page 42: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Breadth-First-Search: Pseudocode

0: def bfs(G,s)1: Run BFS on the given graph G2: starting from source s3:4: assert(s in G.vertices())5:6: # Initialize graph and queue7: for v in G.vertices():8: v.predecessor = None9: v.d = Infinity # .d = distance from s10: v.colour = "white"11: Q = Queue()12:13: # Visit source vertex14: s.d = 015: s.colour = "grey"16: Q.insert(s)17:18: # Visit the adjacents of each vertex in Q19: while not Q.isEmpty():20: u = Q.extract()21: assert (u.colour == "grey")22: for v in u.adjacent()23: if v.colour = "white"24: v.colour = "grey"25: v.d = u.d+126: v.predecessor = u27: Q.insert(v)28: u.colour = "black"

From any vertex, visit all adjacentvertices before going any deeper

Vertex Colours:

White = Unvisited

Grey = Visited, but not all neighbors

Black = Visited and all neighbors

Runtime O(V + E)

Assuming that all executions of the FOR-loopfor u takes O(|u.adj|) (adjacency list model!)∑

u∈V |u.adj| = 2|E |

6.1 & 6.2: Graph Searching T.S. 9

Page 43: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s∞

t∞

u

v

w

x

y

s�As r�Ar w�Zw v�Av t x�Ct u�Ax y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w1

w

2

t

2

x1

w2

v2

v2

v

2

t

3

u

2

t

2

x2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 44: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s∞

t∞

u

v

w

x

y

s

�As r�Ar w�Zw v�Av t x�Ct u�Ax y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w1

w

2

t

2

x1

w2

v2

v2

v

2

t

3

u

2

t

2

x2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 45: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r�Ar w�Zw v�Av t x�Ct u�Ax y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w1

w

2

t

2

x1

w2

v2

v2

v

2

t

3

u

2

t

2

x2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 46: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r�Ar w�Zw v�Av t x�Ct u�Ax y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w1

w

2

t

2

x1

w2

v2

v2

v

2

t

3

u

2

t

2

x2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 47: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As r

�Ar w�Zw v�Av t x�Ct u�Ax y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w1

w

2

t

2

x1

w2

v2

v2

v

2

t

3

u

2

t

2

x2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 48: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As r

�Ar w�Zw v�Av t x�Ct u�Ax y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w1

w

2

t

2

x1

w2

v2

v2

v

2

t

3

u

2

t

2

x2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 49: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As r

�Ar

w

�Zw v�Av t x�Ct u�Ax y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x1

w2

v2

v2

v

2

t

3

u

2

t

2

x2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 50: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As r

�Ar

w

�Zw v�Av t x�Ct u�Ax y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x1

w2

v2

v2

v

2

t

3

u

2

t

2

x2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 51: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar w

�Zw v�Av t x�Ct u�Ax y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x1

w2

v2

v2

v

2

t

3

u

2

t

2

x2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 52: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar w

�Zw v�Av t x�Ct u�Ax y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x1

w2

v2

v2

v

2

t

3

u

2

t

2

x2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 53: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar w

�Zw v�Av t x�Ct u�Ax y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x1

w2

v2

v2

v

2

t

3

u

2

t

2

x2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 54: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar w

�Zw v�Av t x�Ct u�Ax y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x1

w2

v2

v2

v

2

t

3

u

2

t

2

x2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 55: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar w

�Zw

v

�Av t x�Ct u�Ax y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x1

w

2

v

2

v2

v

2

t

3

u

2

t

2

x2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 56: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar w

�Zw

v

�Av t x�Ct u�Ax y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x1

w

2

v

2

v2

v

2

t

3

u

2

t

2

x2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 57: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw v

�Av t x�Ct u�Ax y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w1

w

2

t

2

x1

w

2

v

2

v2

v

2

t

3

u

2

t

2

x2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 58: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw v

�Av t x�Ct u�Ax y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w1

w

2

t

2

x1

w

2

v

2

v2

v

2

t

3

u

2

t

2

x2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 59: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw v

�Av t x�Ct u�Ax y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w1

w

2

t

2

x1

w

2

v

2

v2

v

2

t

3

u

2

t

2

x2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 60: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw v

�Av t x�Ct u�Ax y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w1

w

2

t

2

x1

w

2

v

2

v2

v

2

t

3

u

2

t

2

x2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 61: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw v

�Av

t

x�Ct u�Ax y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w1

w

2

t

2

x1

w

2

v

2

v2

v

2

t

3

u

2

t

2

x2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 62: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw v

�Av

t

x�Ct u�Ax y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w1

w

2

t

2

x1

w

2

v

2

v2

v

2

t

3

u

2

t

2

x2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 63: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw v

�Av

t x

�Ct u�Ax y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x

1

w

2

v

2

v2

v

2

t

3

u

2

t

2

x2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 64: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw v

�Av

t x

�Ct u�Ax y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x1

w2

v

2

v2

v

2

t

3

u

2

t

2

x2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 65: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av t x

�Ct u�Ax y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x1

w

2

v

2

v

2

v

2

t

3

u

2

t

2

x2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 66: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av t x

�Ct u�Ax y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x1

w

2

v

2

v

2

v

2

t

3

u

2

t

2

x2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 67: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av t x

�Ct u�Ax y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x1

w

2

v2

v

2

v

2

t

3

u

2

t

2

x2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 68: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av

t

x�Ct

u�Ax y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x1

w

2

v2

v

2

v

2

t

3

u

2

t

2

x2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 69: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av

t

x�Ct

u�Ax y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x1

w

2

v2

v

2

v

2

t

3

u

2

t

2

x2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 70: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av

t

x�Ct u

�Ax y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x1

w

2

v2

v

2

v

2

t

3

u

2

t

2

x2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 71: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av

t

x�Ct u

�Ax y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x1

w

2

v2

v

2

v

2

t

3

u

2

t

2

x2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 72: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av

t

x�Ct u

�Ax y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x1

w

2

v2

v

2

v

2

t

3

u

2

t

2

x2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 73: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av

t

x�Ct u

�Ax y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x1

w

2

v2

v

2

v

2

t

3

u

2

t

2

x2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 74: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av

t

x�Ct u

�Ax y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x1

w

2

v2

v

2

v

2

t

3

u

2

t

2

x2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 75: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av

t

x�Ct u

�Ax y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x1

w

2

v2

v

2

v

2

t

3

u

2

t

2

x2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 76: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av

t x

�Ct u�Ax

y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x

1

w

2

v2

v

2

v

2

t

3

u

2

t

2

x

2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 77: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av

t x

�Ct u�Ax

y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x

1

w

2

v2

v

2

v

2

t

3

u

2

t

2

x

2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 78: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av

t x

�Ct u�Ax

y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x

1

w

2

v2

v

2

v

2

t

3

u

2

t

2

x

2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 79: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av

t x

�Ct u�Ax

y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x

1

w

2

v2

v

2

v

2

t

3

u

2

t

2

x

2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 80: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av

t x

�Ct u�Ax

y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x

1

w

2

v2

v

2

v

2

t

3

u

2

t

2

x

2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 81: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av

t x

�Ct u�Ax

y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x

1

w

2

v2

v

2

v

2

t

3

u

2

t

2

x

2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 82: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av

t x

�Ct u�Ax

y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x

1

w

2

v2

v

2

v

2

t

3

u

2

t

2

x

2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 83: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av

t x

�Ct u�Ax

y�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x

1

w

2

v2

v

2

v

2

t

3

u

2

t

2

x

2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 84: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av

t x

�Ct u�Ax y

�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x

1

w

2

v2

v

2

v

2

t

3

u

2

t

2

x

2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 85: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av

t x

�Ct u�Ax y

�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x

1

w

2

v2

v

2

v

2

t

3

u

2

t

2

x

2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 86: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av

t x

�Ct

u

�Ax y�Au

�Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x

1

w

2

v2

v

2

v

2

t

3

u

2

t

2

x

2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 87: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av

t x

�Ct

u

�Ax y�Au

�Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x

1

w

2

v2

v

2

v

2

t

3

u

2

t

2

x

2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 88: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av

t x

�Ct

u

�Ax y�Au

�Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x

1

w

2

v2

v

2

v

2

t

3

u

2

t

2

x

2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 89: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av

t x

�Ct

u

�Ax y�Au

�Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x

1

w

2

v2

v

2

v

2

t

3

u

2

t

2

x

2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 90: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av

t x

�Ct

u

�Ax y�Au

�Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x

1

w

2

v2

v

2

v

2

t

3

u

2

t

2

x

2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 91: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av

t x

�Ct

u

�Ax y�Au

�Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x

1

w

2

v2

v

2

v

2

t

3

u

2

t

2

x

2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 92: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av

t x

�Ct

u

�Ax y�Au

�Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x

1

w

2

v2

v

2

v

2

t

3

u

2

t

2

x

2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 93: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av

t x

�Ct

u

�Ax y�Au

�Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x

1

w

2

v2

v

2

v

2

t

3

u

2

t

2

x

2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 94: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av

t x

�Ct

u

�Ax y�Au

�Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x

1

w

2

v2

v

2

v

2

t

3

u

2

t

2

x

2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 95: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av

t x

�Ct

u

�Ax

y

�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x

1

w

2

v2

v

2

v

2

t

3

u

2

t

2

x

2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 96: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av

t x

�Ct

u

�Ax

y

�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x

1

w

2

v2

v

2

v

2

t

3

u

2

t

2

x

2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 97: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av

t x

�Ct

u

�Ax

y

�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x

1

w

2

v2

v

2

v

2

t

3

u

2

t

2

x

2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 98: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of BFS (Figure 22.3)

Queue:

r

0

s

t∞

u

v

w

x

y

s

�As

r

�Ar

w

�Zw

v

�Av

t x

�Ct

u

�Ax

y

�Au �Ay

0

s

0

s

1

r

1

w

1

r

1

r

1

w

1

w

2

t

2

x

1

w

2

v2

v

2

v

2

t

3

u

2

t

2

x

2

x

3

y

3

u

3

u

3

y

3

y

6.1 & 6.2: Graph Searching T.S. 10

Page 99: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Outline

Introduction to Graphs and Graph Searching

Breadth-First Search

Depth-First Search

Topological Sort

6.1 & 6.2: Graph Searching T.S. 11

Page 100: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Depth-First Search: Basic Ideas

Given an undirected/directed graph G = (V ,E) and source vertex s

As soon as we discover a vertex, explore from it Solving Mazes

Two time stamps for every vertex: Discovery Time, Finishing Time

Basic Idea

6.1 & 6.2: Graph Searching T.S. 12

Page 101: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Depth-First Search: Basic Ideas

Given an undirected/directed graph G = (V ,E) and source vertex s

As soon as we discover a vertex, explore from it Solving Mazes

Two time stamps for every vertex: Discovery Time, Finishing Time

Basic Idea

6.1 & 6.2: Graph Searching T.S. 12

Page 102: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Depth-First Search: Basic Ideas

Given an undirected/directed graph G = (V ,E) and source vertex s

As soon as we discover a vertex, explore from it Solving Mazes

Two time stamps for every vertex: Discovery Time, Finishing Time

Basic Idea

6.1 & 6.2: Graph Searching T.S. 12

Page 103: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Depth-First-Search: Pseudocode

0: def dfs(G,s):1: Run DFS on the given graph G2: starting from the given source s3:4: assert(s in G.vertices())5:6: # Initialize graph7: for v in G.vertices():8: v.predecessor = None9: v.colour = "white"

10: dfsRecurse(G,s)

0: def dfsRecurse(G,s):1: s.colour = "grey"2: s.d = time() # .d = discovery time3: for v in s.adjacent()4: if v.colour = "white"5: v.predecessor = s6: dfsRecurse(G,v)7: s.colour = "black"8: s.f = time() # .f = finish time

We always go deeper before visitingother neighbors

Discovery and Finish times, .d and .f

Vertex Colours:

White = Unvisited

Grey = Visited, but not all neighbors

Black = Visited and all neighbors

Runtime O(V + E)

6.1 & 6.2: Graph Searching T.S. 13

Page 104: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Depth-First-Search: Pseudocode

0: def dfs(G,s):1: Run DFS on the given graph G2: starting from the given source s3:4: assert(s in G.vertices())5:6: # Initialize graph7: for v in G.vertices():8: v.predecessor = None9: v.colour = "white"

10: dfsRecurse(G,s)

0: def dfsRecurse(G,s):1: s.colour = "grey"2: s.d = time() # .d = discovery time3: for v in s.adjacent()4: if v.colour = "white"5: v.predecessor = s6: dfsRecurse(G,v)7: s.colour = "black"8: s.f = time() # .f = finish time

We always go deeper before visitingother neighbors

Discovery and Finish times, .d and .f

Vertex Colours:

White = Unvisited

Grey = Visited, but not all neighbors

Black = Visited and all neighbors

Runtime O(V + E)

6.1 & 6.2: Graph Searching T.S. 13

Page 105: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Depth-First-Search: Pseudocode

0: def dfs(G,s):1: Run DFS on the given graph G2: starting from the given source s3:4: assert(s in G.vertices())5:6: # Initialize graph7: for v in G.vertices():8: v.predecessor = None9: v.colour = "white"

10: dfsRecurse(G,s)

0: def dfsRecurse(G,s):1: s.colour = "grey"2: s.d = time() # .d = discovery time3: for v in s.adjacent()4: if v.colour = "white"5: v.predecessor = s6: dfsRecurse(G,v)7: s.colour = "black"8: s.f = time() # .f = finish time

We always go deeper before visitingother neighbors

Discovery and Finish times, .d and .f

Vertex Colours:

White = Unvisited

Grey = Visited, but not all neighbors

Black = Visited and all neighbors

Runtime O(V + E)

6.1 & 6.2: Graph Searching T.S. 13

Page 106: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Depth-First-Search: Pseudocode

0: def dfs(G,s):1: Run DFS on the given graph G2: starting from the given source s3:4: assert(s in G.vertices())5:6: # Initialize graph7: for v in G.vertices():8: v.predecessor = None9: v.colour = "white"

10: dfsRecurse(G,s)

0: def dfsRecurse(G,s):1: s.colour = "grey"2: s.d = time() # .d = discovery time3: for v in s.adjacent()4: if v.colour = "white"5: v.predecessor = s6: dfsRecurse(G,v)7: s.colour = "black"8: s.f = time() # .f = finish time

We always go deeper before visitingother neighbors

Discovery and Finish times, .d and .f

Vertex Colours:

White = Unvisited

Grey = Visited, but not all neighbors

Black = Visited and all neighbors

Runtime O(V + E)

6.1 & 6.2: Graph Searching T.S. 13

Page 107: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Depth-First-Search: Pseudocode

0: def dfs(G,s):1: Run DFS on the given graph G2: starting from the given source s3:4: assert(s in G.vertices())5:6: # Initialize graph7: for v in G.vertices():8: v.predecessor = None9: v.colour = "white"

10: dfsRecurse(G,s)

0: def dfsRecurse(G,s):1: s.colour = "grey"2: s.d = time() # .d = discovery time3: for v in s.adjacent()4: if v.colour = "white"5: v.predecessor = s6: dfsRecurse(G,v)7: s.colour = "black"8: s.f = time() # .f = finish time

We always go deeper before visitingother neighbors

Discovery and Finish times, .d and .f

Vertex Colours:

White = Unvisited

Grey = Visited, but not all neighbors

Black = Visited and all neighbors

Runtime O(V + E)

6.1 & 6.2: Graph Searching T.S. 13

Page 108: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Depth-First-Search: Pseudocode

0: def dfs(G,s):1: Run DFS on the given graph G2: starting from the given source s3:4: assert(s in G.vertices())5:6: # Initialize graph7: for v in G.vertices():8: v.predecessor = None9: v.colour = "white"

10: dfsRecurse(G,s)

0: def dfsRecurse(G,s):1: s.colour = "grey"2: s.d = time() # .d = discovery time3: for v in s.adjacent()4: if v.colour = "white"5: v.predecessor = s6: dfsRecurse(G,v)7: s.colour = "black"8: s.f = time() # .f = finish time

We always go deeper before visitingother neighbors

Discovery and Finish times, .d and .f

Vertex Colours:

White = Unvisited

Grey = Visited, but not all neighbors

Black = Visited and all neighbors

Runtime O(V + E)

6.1 & 6.2: Graph Searching T.S. 13

Page 109: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v y r uxw z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 110: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s

v y r uxw z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 111: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s

v y r uxw z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 112: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s

v y r uxw z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 113: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v

y r uxw z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 114: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v

y r uxw z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 115: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v y

r uxw z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 116: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v y

r uxw z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 117: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v y

r u

x

w z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 118: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v y

r u

x

w z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 119: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v y

r u

x

w z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 120: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v y

r u

x

w z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 121: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v y

r uxw z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 122: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v y

r uxw z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 123: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v y r

uxw z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 124: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v y r

uxw z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 125: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v y r u

xw z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 126: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v y r u

xw z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 127: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v y r

uxw z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 128: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v y r

uxw z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 129: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v y

r uxw z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 130: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v y

r uxw z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 131: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v

y r uxw z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 132: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v

y r uxw z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 133: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s

v y r uxw z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 134: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s

v y r uxw z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 135: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s

v y r uxw z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 136: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s

v y r uxw z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 137: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v y r uxw z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 138: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v y r ux

w

z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 139: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v y r ux

w

z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 140: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v y r ux

w

z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 141: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v y r ux

w

z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 142: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v y r ux

w

z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 143: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v y r ux

w z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 144: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v y r ux

w z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 145: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v y r ux

w z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 146: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v y r ux

w z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 147: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v y r ux

w

z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 148: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v y r ux

w

z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 149: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of DFS

s v y r uxw z

s v w

x y z

ru

1/

s

2/

v

3/

y

4/

x

4/5

x

6/

r

7/

u

7/8

u

6/9

r

3/10

y

2/11

v

1/12

s

13/

w

14/

z

14/15

z

13/16

w

6.1 & 6.2: Graph Searching T.S. 14

Page 150: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Paranthesis Theorem (Theorem 22.7)

4/5

x

6/9

r

7/8

u

3/10

y

2/11

v

14/15

z

13/16

w

1/12

s

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

s

v

y

x r

u

w

z

(s (v (y (x x) (r (u u) r) y) v) s) (w (z z) w)

6.1 & 6.2: Graph Searching T.S. 15

Page 151: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Outline

Introduction to Graphs and Graph Searching

Breadth-First Search

Depth-First Search

Topological Sort

6.1 & 6.2: Graph Searching T.S. 16

Page 152: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Topological Sort

undershorts

pants

belt

socks

shoes

watch

shirt

tie

jacket

socks undershorts pants shoes watch shirt belt tie jacket

Given: a directed acyclic graph (DAG)

Goal: Output a linear ordering of all vertices

Problem

6.1 & 6.2: Graph Searching T.S. 17

Page 153: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Topological Sort

undershorts

pants

belt

socks

shoes

watch

shirt

tie

jacket

socks undershorts pants shoes watch shirt belt tie jacket

Given: a directed acyclic graph (DAG)

Goal: Output a linear ordering of all vertices

Problem

6.1 & 6.2: Graph Searching T.S. 17

Page 154: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Topological Sort

undershorts

pants

belt

socks

shoes

watch

shirt

tie

jacket

socks undershorts pants shoes watch shirt belt tie jacket

Given: a directed acyclic graph (DAG)

Goal: Output a linear ordering of all vertices

Problem

6.1 & 6.2: Graph Searching T.S. 17

Page 155: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Topological Sort

undershorts

pants

belt

socks

shoes

watch

shirt

tie

jacket

socks undershorts pants shoes watch shirt belt tie jacket

Given: a directed acyclic graph (DAG)

Goal: Output a linear ordering of all vertices

Problem

6.1 & 6.2: Graph Searching T.S. 17

Page 156: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Topological Sort

undershorts

pants

belt

socks

shoes

watch

shirt

tie

jacket

socks undershorts pants shoes watch shirt belt tie jacket

Given: a directed acyclic graph (DAG)

Goal: Output a linear ordering of all vertices

Problem

6.1 & 6.2: Graph Searching T.S. 17

Page 157: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Solving Topological Sort

undershorts

pants

belt

socks

shoes

watch

shirt

tie

jacket

Perform DFS’s so that all vertices are visited

Output vertices in decreasing order of their finishing time

Knuth’s Algorithm (1968)

Runtime O(V + E)Don’t need to sort the ver-tices – use DFS directly!

6.1 & 6.2: Graph Searching T.S. 18

Page 158: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Solving Topological Sort

undershorts

pants

belt

socks

shoes

watch

shirt

tie

jacket

Perform DFS’s so that all vertices are visited

Output vertices in decreasing order of their finishing time

Knuth’s Algorithm (1968)

Runtime O(V + E)

Don’t need to sort the ver-tices – use DFS directly!

6.1 & 6.2: Graph Searching T.S. 18

Page 159: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Solving Topological Sort

undershorts

pants

belt

socks

shoes

watch

shirt

tie

jacket

Perform DFS’s so that all vertices are visited

Output vertices in decreasing order of their finishing time

Knuth’s Algorithm (1968)

Runtime O(V + E)Don’t need to sort the ver-tices – use DFS directly!

6.1 & 6.2: Graph Searching T.S. 18

Page 160: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of Knuth’s Algorithm

4/5

x

6/9

r

7/8

u

3/10

y

2/11

v

14/15

z

13/16

w

1/12

s

13/16

w

14/15

z

1/12

s

2/11

v

3/10

y

6/9

r

7/8

u

4/5

x

6.1 & 6.2: Graph Searching T.S. 19

Page 161: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of Knuth’s Algorithm

4/5

x

6/9

r

7/8

u

3/10

y

2/11

v

14/15

z

13/16

w

1/12

s

13/16

w

14/15

z

1/12

s

2/11

v

3/10

y

6/9

r

7/8

u

4/5

x

6.1 & 6.2: Graph Searching T.S. 19

Page 162: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of Knuth’s Algorithm

4/5

x

6/9

r

7/8

u

3/10

y

2/11

v

14/15

z

13/16

w

1/12

s

13/16

w

14/15

z

1/12

s

2/11

v

3/10

y

6/9

r

7/8

u

4/5

x

6.1 & 6.2: Graph Searching T.S. 19

Page 163: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of Knuth’s Algorithm

4/5

x

6/9

r

7/8

u

3/10

y

2/11

v

14/15

z

13/16

w

1/12

s

13/16

w

14/15

z

1/12

s

2/11

v

3/10

y

6/9

r

7/8

u

4/5

x

6.1 & 6.2: Graph Searching T.S. 19

Page 164: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of Knuth’s Algorithm

4/5

x

6/9

r

7/8

u

3/10

y

2/11

v

14/15

z

13/16

w

1/12

s

13/16

w

14/15

z

1/12

s

2/11

v

3/10

y

6/9

r

7/8

u

4/5

x

6.1 & 6.2: Graph Searching T.S. 19

Page 165: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of Knuth’s Algorithm

4/5

x

6/9

r

7/8

u

3/10

y

2/11

v

14/15

z

13/16

w

1/12

s

13/16

w

14/15

z

1/12

s

2/11

v

3/10

y

6/9

r

7/8

u

4/5

x

6.1 & 6.2: Graph Searching T.S. 19

Page 166: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of Knuth’s Algorithm

4/5

x

6/9

r

7/8

u

3/10

y

2/11

v

14/15

z

13/16

w

1/12

s

13/16

w

14/15

z

1/12

s

2/11

v

3/10

y

6/9

r

7/8

u

4/5

x

6.1 & 6.2: Graph Searching T.S. 19

Page 167: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of Knuth’s Algorithm

4/5

x

6/9

r

7/8

u

3/10

y

2/11

v

14/15

z

13/16

w

1/12

s

13/16

w

14/15

z

1/12

s

2/11

v

3/10

y

6/9

r

7/8

u

4/5

x

6.1 & 6.2: Graph Searching T.S. 19

Page 168: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of Knuth’s Algorithm

4/5

x

6/9

r

7/8

u

3/10

y

2/11

v

14/15

z

13/16

w

1/12

s

13/16

w

14/15

z

1/12

s

2/11

v

3/10

y

6/9

r

7/8

u

4/5

x

6.1 & 6.2: Graph Searching T.S. 19

Page 169: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of Knuth’s Algorithm

4/5

x

6/9

r

7/8

u

3/10

y

2/11

v

14/15

z

13/16

w

1/12

s

13/16

w

14/15

z

1/12

s

2/11

v

3/10

y

6/9

r

7/8

u

4/5

x

6.1 & 6.2: Graph Searching T.S. 19

Page 170: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of Knuth’s Algorithm

4/5

x

6/9

r

7/8

u

3/10

y

2/11

v

14/15

z

13/16

w

1/12

s

13/16

w

14/15

z

1/12

s

2/11

v

3/10

y

6/9

r

7/8

u

4/5

x

6.1 & 6.2: Graph Searching T.S. 19

Page 171: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of Knuth’s Algorithm

4/5

x

6/9

r

7/8

u

3/10

y

2/11

v

14/15

z

13/16

w

1/12

s

13/16

w

14/15

z

1/12

s

2/11

v

3/10

y

6/9

r

7/8

u

4/5

x

6.1 & 6.2: Graph Searching T.S. 19

Page 172: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of Knuth’s Algorithm

4/5

x

6/9

r

7/8

u

3/10

y

2/11

v

14/15

z

13/16

w

1/12

s

13/16

w

14/15

z

1/12

s

2/11

v

3/10

y

6/9

r

7/8

u

4/5

x

6.1 & 6.2: Graph Searching T.S. 19

Page 173: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of Knuth’s Algorithm

4/5

x

6/9

r

7/8

u

3/10

y

2/11

v

14/15

z

13/16

w

1/12

s

13/16

w

14/15

z

1/12

s

2/11

v

3/10

y

6/9

r

7/8

u

4/5

x

6.1 & 6.2: Graph Searching T.S. 19

Page 174: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of Knuth’s Algorithm

4/5

x

6/9

r

7/8

u

3/10

y

2/11

v

14/15

z

13/16

w

1/12

s

13/16

w

14/15

z

1/12

s

2/11

v

3/10

y

6/9

r

7/8

u

4/5

x

6.1 & 6.2: Graph Searching T.S. 19

Page 175: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of Knuth’s Algorithm

4/5

x

6/9

r

7/8

u

3/10

y

2/11

v

14/15

z

13/16

w

1/12

s

13/16

w

14/15

z

1/12

s

2/11

v

3/10

y

6/9

r

7/8

u

4/5

x

6.1 & 6.2: Graph Searching T.S. 19

Page 176: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of Knuth’s Algorithm

4/5

x

6/9

r

7/8

u

3/10

y

2/11

v

14/15

z

13/16

w

1/12

s

13/16

w

14/15

z

1/12

s

2/11

v

3/10

y

6/9

r

7/8

u

4/5

x

6.1 & 6.2: Graph Searching T.S. 19

Page 177: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of Knuth’s Algorithm

4/5

x

6/9

r

7/8

u

3/10

y

2/11

v

14/15

z

13/16

w

1/12

s

13/16

w

14/15

z

1/12

s

2/11

v

3/10

y

6/9

r

7/8

u

4/5

x

6.1 & 6.2: Graph Searching T.S. 19

Page 178: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Execution of Knuth’s Algorithm

4/5

x

6/9

r

7/8

u

3/10

y

2/11

v

14/15

z

13/16

w

1/12

s

13/16

w

14/15

z

1/12

s

2/11

v

3/10

y

6/9

r

7/8

u

4/5

x

6.1 & 6.2: Graph Searching T.S. 19

Page 179: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Correctness of Topological Sort using DFS

13/16

w

14/15

z

1/12

s

2/11

v

3/10

y

6/9

r

7/8

u

4/5

x

If the input graph is a DAG, then the algorithm computes a linear order.Theorem 22.12

Proof:

Consider any edge (u, v) ∈ E(G) being explored,

⇒ u is grey and we have to show that v .f < u.f

1. If v is grey,

then there is a cycle(can’t happen, because G is acyclic!).

2. If v is black,

then v .f < u.f .

3. If v is white,

we call DFS(v) and v .f < u.f .

⇒ In all cases v .f < u.f , so v appears after u.

1/

s

7/

u v

?/

v

4/

v

3/4

vv

9/?

v

9/?

v

6.1 & 6.2: Graph Searching T.S. 20

Page 180: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Correctness of Topological Sort using DFS

13/16

w

14/15

z

1/12

s

2/11

v

3/10

y

6/9

r

7/8

u

4/5

x

If the input graph is a DAG, then the algorithm computes a linear order.Theorem 22.12

Proof:

Consider any edge (u, v) ∈ E(G) being explored,

⇒ u is grey and we have to show that v .f < u.f

1. If v is grey,

then there is a cycle(can’t happen, because G is acyclic!).

2. If v is black,

then v .f < u.f .

3. If v is white,

we call DFS(v) and v .f < u.f .

⇒ In all cases v .f < u.f , so v appears after u.

1/

s

7/

u v

?/

v

4/

v

3/4

vv

9/?

v

9/?

v

6.1 & 6.2: Graph Searching T.S. 20

Page 181: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Correctness of Topological Sort using DFS

13/16

w

14/15

z

1/12

s

2/11

v

3/10

y

6/9

r

7/8

u

4/5

x

If the input graph is a DAG, then the algorithm computes a linear order.Theorem 22.12

Proof:

Consider any edge (u, v) ∈ E(G) being explored,

⇒ u is grey and we have to show that v .f < u.f

1. If v is grey,

then there is a cycle(can’t happen, because G is acyclic!).

2. If v is black,

then v .f < u.f .

3. If v is white,

we call DFS(v) and v .f < u.f .

⇒ In all cases v .f < u.f , so v appears after u.

1/

s

7/

u v

?/

v

4/

v

3/4

vv

9/?

v

9/?

v

6.1 & 6.2: Graph Searching T.S. 20

Page 182: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Correctness of Topological Sort using DFS

13/16

w

14/15

z

1/12

s

2/11

v

3/10

y

6/9

r

7/8

u

4/5

x

If the input graph is a DAG, then the algorithm computes a linear order.Theorem 22.12

Proof:

Consider any edge (u, v) ∈ E(G) being explored,⇒ u is grey and we have to show that v .f < u.f

1. If v is grey,

then there is a cycle(can’t happen, because G is acyclic!).

2. If v is black,

then v .f < u.f .

3. If v is white,

we call DFS(v) and v .f < u.f .

⇒ In all cases v .f < u.f , so v appears after u.

1/

s

7/

u v

?/

v

4/

v

3/4

vv

9/?

v

9/?

v

6.1 & 6.2: Graph Searching T.S. 20

Page 183: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Correctness of Topological Sort using DFS

13/16

w

14/15

z

1/12

s

2/11

v

3/10

y

6/9

r

7/8

u

4/5

x

If the input graph is a DAG, then the algorithm computes a linear order.Theorem 22.12

Proof:

Consider any edge (u, v) ∈ E(G) being explored,⇒ u is grey and we have to show that v .f < u.f

1. If v is grey,

then there is a cycle(can’t happen, because G is acyclic!).

2. If v is black,

then v .f < u.f .

3. If v is white,

we call DFS(v) and v .f < u.f .

⇒ In all cases v .f < u.f , so v appears after u.

1/

s

7/

u

v

?/

v

4/

v

3/4

vv

9/?

v

9/?

v

6.1 & 6.2: Graph Searching T.S. 20

Page 184: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Correctness of Topological Sort using DFS

13/16

w

14/15

z

1/12

s

2/11

v

3/10

y

6/9

r

7/8

u

4/5

x

If the input graph is a DAG, then the algorithm computes a linear order.Theorem 22.12

Proof:

Consider any edge (u, v) ∈ E(G) being explored,⇒ u is grey and we have to show that v .f < u.f

1. If v is grey,

then there is a cycle(can’t happen, because G is acyclic!).

2. If v is black,

then v .f < u.f .

3. If v is white,

we call DFS(v) and v .f < u.f .

⇒ In all cases v .f < u.f , so v appears after u.

1/

s

7/

u

v

?/

v

4/

v

3/4

vv

9/?

v

9/?

v

6.1 & 6.2: Graph Searching T.S. 20

Page 185: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Correctness of Topological Sort using DFS

13/16

w

14/15

z

1/12

s

2/11

v

3/10

y

6/9

r

7/8

u

4/5

x

If the input graph is a DAG, then the algorithm computes a linear order.Theorem 22.12

Proof:

Consider any edge (u, v) ∈ E(G) being explored,⇒ u is grey and we have to show that v .f < u.f

1. If v is grey, then there is a cycle(can’t happen, because G is acyclic!).

2. If v is black,

then v .f < u.f .

3. If v is white,

we call DFS(v) and v .f < u.f .

⇒ In all cases v .f < u.f , so v appears after u.

1/

s

7/

u

v

?/

v

4/

v

3/4

vv

9/?

v

9/?

v

6.1 & 6.2: Graph Searching T.S. 20

Page 186: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Correctness of Topological Sort using DFS

13/16

w

14/15

z

1/12

s

2/11

v

3/10

y

6/9

r

7/8

u

4/5

x

If the input graph is a DAG, then the algorithm computes a linear order.Theorem 22.12

Proof:

Consider any edge (u, v) ∈ E(G) being explored,⇒ u is grey and we have to show that v .f < u.f

1. If v is grey, then there is a cycle(can’t happen, because G is acyclic!).

2. If v is black,

then v .f < u.f .3. If v is white,

we call DFS(v) and v .f < u.f .

⇒ In all cases v .f < u.f , so v appears after u.

1/

s

7/

u

v

?/

v

4/

v

3/4

v

v

9/?

v

9/?

v

6.1 & 6.2: Graph Searching T.S. 20

Page 187: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Correctness of Topological Sort using DFS

13/16

w

14/15

z

1/12

s

2/11

v

3/10

y

6/9

r

7/8

u

4/5

x

If the input graph is a DAG, then the algorithm computes a linear order.Theorem 22.12

Proof:

Consider any edge (u, v) ∈ E(G) being explored,⇒ u is grey and we have to show that v .f < u.f

1. If v is grey, then there is a cycle(can’t happen, because G is acyclic!).

2. If v is black, then v .f < u.f .

3. If v is white,

we call DFS(v) and v .f < u.f .

⇒ In all cases v .f < u.f , so v appears after u.

1/

s

7/

u

v

?/

v

4/

v

3/4

v

v

9/?

v

9/?

v

6.1 & 6.2: Graph Searching T.S. 20

Page 188: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Correctness of Topological Sort using DFS

13/16

w

14/15

z

1/12

s

2/11

v

3/10

y

6/9

r

7/8

u

4/5

x

If the input graph is a DAG, then the algorithm computes a linear order.Theorem 22.12

Proof:

Consider any edge (u, v) ∈ E(G) being explored,⇒ u is grey and we have to show that v .f < u.f

1. If v is grey, then there is a cycle(can’t happen, because G is acyclic!).

2. If v is black, then v .f < u.f .3. If v is white,

we call DFS(v) and v .f < u.f .

⇒ In all cases v .f < u.f , so v appears after u.

1/

s

7/

u

v

?/

v

4/

v

3/4

v

v

9/?

v

9/?

v

6.1 & 6.2: Graph Searching T.S. 20

Page 189: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Correctness of Topological Sort using DFS

13/16

w

14/15

z

1/12

s

2/11

v

3/10

y

6/9

r

7/8

u

4/5

x

If the input graph is a DAG, then the algorithm computes a linear order.Theorem 22.12

Proof:

Consider any edge (u, v) ∈ E(G) being explored,⇒ u is grey and we have to show that v .f < u.f

1. If v is grey, then there is a cycle(can’t happen, because G is acyclic!).

2. If v is black, then v .f < u.f .3. If v is white, we call DFS(v) and v .f < u.f .

⇒ In all cases v .f < u.f , so v appears after u.

1/

s

7/

u

v

?/

v

4/

v

3/4

vv

9/?

v

9/?

v

6.1 & 6.2: Graph Searching T.S. 20

Page 190: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Correctness of Topological Sort using DFS

13/16

w

14/15

z

1/12

s

2/11

v

3/10

y

6/9

r

7/8

u

4/5

x

If the input graph is a DAG, then the algorithm computes a linear order.Theorem 22.12

Proof:

Consider any edge (u, v) ∈ E(G) being explored,⇒ u is grey and we have to show that v .f < u.f

1. If v is grey, then there is a cycle(can’t happen, because G is acyclic!).

2. If v is black, then v .f < u.f .3. If v is white, we call DFS(v) and v .f < u.f .

⇒ In all cases v .f < u.f , so v appears after u.

1/

s

7/

u

v

?/

v

4/

v

3/4

vv

9/?

v

9/?

v

6.1 & 6.2: Graph Searching T.S. 20

Page 191: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Correctness of Topological Sort using DFS

13/16

w

14/15

z

1/12

s

2/11

v

3/10

y

6/9

r

7/8

u

4/5

x

If the input graph is a DAG, then the algorithm computes a linear order.Theorem 22.12

Proof:

Consider any edge (u, v) ∈ E(G) being explored,⇒ u is grey and we have to show that v .f < u.f

1. If v is grey, then there is a cycle(can’t happen, because G is acyclic!).

2. If v is black, then v .f < u.f .3. If v is white, we call DFS(v) and v .f < u.f .

⇒ In all cases v .f < u.f , so v appears after u.

1/

s

7/

u

v

?/

v

4/

v

3/4

vv

9/?

v

9/?

v

6.1 & 6.2: Graph Searching T.S. 20

Page 192: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Correctness of Topological Sort using DFS

13/16

w

14/15

z

1/12

s

2/11

v

3/10

y

6/9

r

7/8

u

4/5

x

If the input graph is a DAG, then the algorithm computes a linear order.Theorem 22.12

Proof:

Consider any edge (u, v) ∈ E(G) being explored,⇒ u is grey and we have to show that v .f < u.f

1. If v is grey, then there is a cycle(can’t happen, because G is acyclic!).

2. If v is black, then v .f < u.f .3. If v is white, we call DFS(v) and v .f < u.f .

⇒ In all cases v .f < u.f , so v appears after u.

1/

s

7/

u

v

?/

v

4/

v

3/4

vv

9/?

v

9/?

v

6.1 & 6.2: Graph Searching T.S. 20

Page 193: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Summary of Graph Searching

vertices are processed by a queue

computes distances and shortest paths similar idea used later in Prim’s and Dijkstra’s algorithm

Runtime O(V + E)

Breadth-First-Search

vertices are processed by recursive calls (≈ stack)

discovery and finishing times

application: Topogical Sorting of DAGs

Runtime O(V + E)

Depth-First-Search

6.1 & 6.2: Graph Searching T.S. 21

Page 194: 6.1 & 6.2: Graph Searching - cl.cam.ac.uk6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2016 Complete Execution of DFS s v y r u x w z s v w x y z u r 1/ 2/ 4/ 3/ 4/5

Summary of Graph Searching

vertices are processed by a queue

computes distances and shortest paths similar idea used later in Prim’s and Dijkstra’s algorithm

Runtime O(V + E)

Breadth-First-Search

vertices are processed by recursive calls (≈ stack)

discovery and finishing times

application: Topogical Sorting of DAGs

Runtime O(V + E)

Depth-First-Search

6.1 & 6.2: Graph Searching T.S. 21