6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas...

182
6.1 & 6.2: Graph Searching Frank Stajano Thomas Sauerwald Lent 2015 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 - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas...

Page 1: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

6.1 & 6.2: Graph SearchingFrank Stajano Thomas Sauerwald

Lent 2015

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 - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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

6.1 & 6.2: Graph Searching T.S. 2

Page 3: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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

6.1 & 6.2: Graph Searching T.S. 2

Page 4: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 3

Page 5: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 3

Page 6: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 3

Page 7: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Outline

Breadth-First Search

Depth-First Search

Topological Sort

Minimum Spanning Tree Problem

6.1 & 6.2: Graph Searching T.S. 4

Page 8: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 5

Page 9: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 5

Page 10: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 5

Page 11: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 6

Page 12: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 6

Page 13: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 6

Page 14: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 6

Page 15: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 6

Page 16: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 6

Page 17: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 6

Page 18: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 6

Page 19: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 20: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 21: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 22: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 23: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 24: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 25: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 26: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 27: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 28: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 29: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 30: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 31: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 32: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 33: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 34: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 35: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 36: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 37: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 38: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 39: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 40: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 41: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 42: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 43: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 44: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 45: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 46: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 47: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 48: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 49: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 50: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 51: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 52: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 53: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 54: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 55: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 56: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 57: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 58: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 59: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 60: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 61: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 62: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 63: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 64: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 65: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 66: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 67: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 68: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 69: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 70: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 71: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 72: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 73: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 74: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 7

Page 75: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Outline

Breadth-First Search

Depth-First Search

Topological Sort

Minimum Spanning Tree Problem

6.1 & 6.2: Graph Searching T.S. 8

Page 76: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 9

Page 77: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 9

Page 78: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 9

Page 79: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 10

Page 80: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 10

Page 81: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 10

Page 82: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 10

Page 83: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 10

Page 84: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 10

Page 85: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 86: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 87: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 88: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 89: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 90: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 91: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 92: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 93: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 11

Page 94: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 11

Page 95: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 11

Page 96: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 11

Page 97: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 98: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 99: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 100: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 101: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 102: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 103: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 104: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 105: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 106: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 107: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 108: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 109: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 110: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 111: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 112: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 113: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 114: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 115: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 116: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 117: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 118: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 119: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 120: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 121: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 122: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 123: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 124: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 125: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Complete 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. 11

Page 126: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 12

Page 127: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Outline

Breadth-First Search

Depth-First Search

Topological Sort

Minimum Spanning Tree Problem

6.1 & 6.2: Graph Searching T.S. 13

Page 128: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 14

Page 129: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 14

Page 130: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 14

Page 131: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 14

Page 132: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 14

Page 133: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 15

Page 134: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 15

Page 135: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 15

Page 136: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 16

Page 137: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 16

Page 138: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 16

Page 139: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 16

Page 140: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 16

Page 141: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 16

Page 142: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 16

Page 143: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 16

Page 144: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 16

Page 145: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 16

Page 146: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 16

Page 147: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 16

Page 148: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 16

Page 149: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 16

Page 150: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 16

Page 151: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 16

Page 152: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 16

Page 153: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 16

Page 154: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 16

Page 155: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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

6.1 & 6.2: Graph Searching T.S. 17

Page 156: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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

6.1 & 6.2: Graph Searching T.S. 17

Page 157: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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

6.1 & 6.2: Graph Searching T.S. 17

Page 158: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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

6.1 & 6.2: Graph Searching T.S. 17

Page 159: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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

6.1 & 6.2: Graph Searching T.S. 17

Page 160: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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

6.1 & 6.2: Graph Searching T.S. 17

Page 161: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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

6.1 & 6.2: Graph Searching T.S. 17

Page 162: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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

6.1 & 6.2: Graph Searching T.S. 17

Page 163: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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

6.1 & 6.2: Graph Searching T.S. 17

Page 164: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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

6.1 & 6.2: Graph Searching T.S. 17

Page 165: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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

6.1 & 6.2: Graph Searching T.S. 17

Page 166: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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

6.1 & 6.2: Graph Searching T.S. 17

Page 167: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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

6.1 & 6.2: Graph Searching T.S. 17

Page 168: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 18

Page 169: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

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. 18

Page 170: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Outline

Breadth-First Search

Depth-First Search

Topological Sort

Minimum Spanning Tree Problem

6.1 & 6.2: Graph Searching T.S. 19

Page 171: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Minimum Spanning Tree Problem

Given: undirected, connectedgraph G = (V ,E ,w) withnon-negative edge weights

Goal: Find a subgraph ⊆ E ofminimum total weight that linksall vertices

Minimum Spanning Tree Problem

Must be necessarily a tree!

64

11

3

9 8

4

6

53

9

2 7

8

Street Networks, Wiring Electronic Components, Laying Pipes

Weights may represent distances, costs, travel times, capacities,resistance etc.

Applications

6.1 & 6.2: Graph Searching T.S. 20

Page 172: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Minimum Spanning Tree Problem

Given: undirected, connectedgraph G = (V ,E ,w) withnon-negative edge weights

Goal: Find a subgraph ⊆ E ofminimum total weight that linksall vertices

Minimum Spanning Tree Problem

Must be necessarily a tree!

64

11

3

9 8

4

6

53

9

2 7

8

Street Networks, Wiring Electronic Components, Laying Pipes

Weights may represent distances, costs, travel times, capacities,resistance etc.

Applications

6.1 & 6.2: Graph Searching T.S. 20

Page 173: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Minimum Spanning Tree Problem

Given: undirected, connectedgraph G = (V ,E ,w) withnon-negative edge weights

Goal: Find a subgraph ⊆ E ofminimum total weight that linksall vertices

Minimum Spanning Tree Problem

Must be necessarily a tree!

64

11

3

9 8

4

6

53

9

2 7

8

Street Networks, Wiring Electronic Components, Laying Pipes

Weights may represent distances, costs, travel times, capacities,resistance etc.

Applications

6.1 & 6.2: Graph Searching T.S. 20

Page 174: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Minimum Spanning Tree Problem

Given: undirected, connectedgraph G = (V ,E ,w) withnon-negative edge weights

Goal: Find a subgraph ⊆ E ofminimum total weight that linksall vertices

Minimum Spanning Tree Problem

Must be necessarily a tree!

64

11

3

9 8

4

6

53

9

2 7

8

Street Networks, Wiring Electronic Components, Laying Pipes

Weights may represent distances, costs, travel times, capacities,resistance etc.

Applications

6.1 & 6.2: Graph Searching T.S. 20

Page 175: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Generic Algorithm

0: def minimum spanningTree(G)1: A = empty set of edges2: while A does not span all vertices yet:3: add a safe edge to A

An edge of G is safe if by adding the edge to A, the resulting subgraphis still a subset of a minimum spanning tree.

Definition

How to find a safe edge?

6.1 & 6.2: Graph Searching T.S. 21

Page 176: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Generic Algorithm

0: def minimum spanningTree(G)1: A = empty set of edges2: while A does not span all vertices yet:3: add a safe edge to A

An edge of G is safe if by adding the edge to A, the resulting subgraphis still a subset of a minimum spanning tree.

Definition

How to find a safe edge?

6.1 & 6.2: Graph Searching T.S. 21

Page 177: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Generic Algorithm

0: def minimum spanningTree(G)1: A = empty set of edges2: while A does not span all vertices yet:3: add a safe edge to A

An edge of G is safe if by adding the edge to A, the resulting subgraphis still a subset of a minimum spanning tree.

Definition

How to find a safe edge?

6.1 & 6.2: Graph Searching T.S. 21

Page 178: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Finding safe edges

a cut is a partition of V into at leasttwo disjoint sets

a cut respects A ⊆ E if no edge ofA goes across the cut

Definitions

Let A ⊆ E be a subset of a MST of G. Then for any cut that respects A,the lightest edge of G that goes across the cut is safe.

Theorem

6.1 & 6.2: Graph Searching T.S. 22

Page 179: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Finding safe edges

a cut is a partition of V into at leasttwo disjoint sets

a cut respects A ⊆ E if no edge ofA goes across the cut

Definitions

Let A ⊆ E be a subset of a MST of G. Then for any cut that respects A,the lightest edge of G that goes across the cut is safe.

Theorem

6.1 & 6.2: Graph Searching T.S. 22

Page 180: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Finding safe edges

a cut is a partition of V into at leasttwo disjoint sets

a cut respects A ⊆ E if no edge ofA goes across the cut

Definitions

Let A ⊆ E be a subset of a MST of G. Then for any cut that respects A,the lightest edge of G that goes across the cut is safe.

Theorem

6.1 & 6.2: Graph Searching T.S. 22

Page 181: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Finding safe edges

a cut is a partition of V into at leasttwo disjoint sets

a cut respects A ⊆ E if no edge ofA goes across the cut

Definitions

Let A ⊆ E be a subset of a MST of G. Then for any cut that respects A,the lightest edge of G that goes across the cut is safe.

Theorem

6.1 & 6.2: Graph Searching T.S. 22

Page 182: 6.1 & 6.2: Graph Searching - University of Cambridge6.1 & 6.2: Graph Searching Frank StajanoThomas Sauerwald Lent 2015 Complete Execution of DFS s v y r u x w z s v w x y z u r 1

Finding safe edges

a cut is a partition of V into at leasttwo disjoint sets

a cut respects A ⊆ E if no edge ofA goes across the cut

Definitions

Let A ⊆ E be a subset of a MST of G. Then for any cut that respects A,the lightest edge of G that goes across the cut is safe.

Theorem

6.1 & 6.2: Graph Searching T.S. 22