209
Data Structures and Algorithms – COMS21103 Representing and Exploring Graphs Depth First Search and Breadth First Search Benjamin Sach

Depth First Search and Breadth First Search

Embed Size (px)

Citation preview

Page 1: Depth First Search and Breadth First Search

Data Structures and Algorithms – COMS21103

Representing and Exploring Graphs

Depth First Search and Breadth First Search

Benjamin Sach

Page 2: Depth First Search and Breadth First Search

Graph notation recap

G is a Graph,

Vertex

Edge

V is the set of vertices

E is the set of edges

|V | is the number of vertices

|E| is the number of edges

This lecture focuses on exploring undirected and unweighted graphs

1

3

5

2

4

6

8

7

9

v ∈ V is a vertex

(u, v) ∈ E is an edge from u to v

though everything discussed today will work for directed, unweighted graphs too

Page 3: Depth First Search and Breadth First Search

Graph notation recap

an undirected and unweighted graph an directed and unweighted graph

a directed and weighted graphan undirected and weighted graph

374

9

374

2

83

Page 4: Depth First Search and Breadth First Search

Graph notation recap

an undirected and unweighted graph an directed and unweighted graph

a directed and weighted graphan undirected and weighted graph

374

9

374

2

83

Today we’refocused on

these graphs

Page 5: Depth First Search and Breadth First Search

Graph notation recap

an undirected and unweighted graph an directed and unweighted graph

a directed and weighted graphan undirected and weighted graph

374

9

374

2

83

Today we’refocused on

these graphs

But it all works forthese graphs too

Page 6: Depth First Search and Breadth First Search

Graph representations

7

1

3

5

2

4

6

9

0 1 1 0 0 0 0 0 0

1 0 1 1 1 0 0 0 0

1 1 0 1 1 0 0 0 0

0 1 1 0 1 1 0 0 0

0 1 1 1 0 1 0 0 0

0 0 0 1 1 0 0 0 0

0 0 0 0 0 0 0 1 1

0 0 0 0 0 0 1 0 1

0 0 0 0 0 0 1 1 0

1

2

3

4

5

6

7

8

9

1 2 3 4 5 6 7 8 9

FRO

M

TO8

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

Adjacency Matrix

|V |

|V |

We will in general assume that the vertices are numbered 1, 2, 3 . . . |V |

Page 7: Depth First Search and Breadth First Search

Graph representations

7

1

3

5

2

4

6

9

0 1 1 0 0 0 0 0 0

1 0 1 1 1 0 0 0 0

1 1 0 1 1 0 0 0 0

0 1 1 0 1 1 0 0 0

0 1 1 1 0 1 0 0 0

0 0 0 1 1 0 0 0 0

0 0 0 0 0 0 0 1 1

0 0 0 0 0 0 1 0 1

0 0 0 0 0 0 1 1 0

1

2

3

4

5

6

7

8

9

1 2 3 4 5 6 7 8 9

FRO

M

TO8

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

Adjacency Matrix

|V |

|V |

We will in general assume that the vertices are numbered 1, 2, 3 . . . |V |

Page 8: Depth First Search and Breadth First Search

Graph representations

7

1

3

5

2

4

6

9

0 1 1 0 0 0 0 0 0

1 0 1 1 1 0 0 0 0

1 1 0 1 1 0 0 0 0

0 1 1 0 1 1 0 0 0

0 1 1 1 0 1 0 0 0

0 0 0 1 1 0 0 0 0

0 0 0 0 0 0 0 1 1

0 0 0 0 0 0 1 0 1

0 0 0 0 0 0 1 1 0

1

2

3

4

5

6

7

8

9

1 2 3 4 5 6 7 8 9

FRO

M

TO8

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

Adjacency Matrix

|V |

|V |

We will in general assume that the vertices are numbered 1, 2, 3 . . . |V |

Page 9: Depth First Search and Breadth First Search

Graph representations

7

1

3

5

2

4

6

9

0 1 1 0 0 0 0 0 0

1 0 1 1 1 0 0 0 0

1 1 0 1 1 0 0 0 0

0 1 1 0 1 1 0 0 0

0 1 1 1 0 1 0 0 0

0 0 0 1 1 0 0 0 0

0 0 0 0 0 0 0 1 1

0 0 0 0 0 0 1 0 1

0 0 0 0 0 0 1 1 0

1

2

3

4

5

6

7

8

9

1 2 3 4 5 6 7 8 9

FRO

M

TO8

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

Adjacency Matrix

|V |

|V |

We will in general assume that the vertices are numbered 1, 2, 3 . . . |V |

Page 10: Depth First Search and Breadth First Search

Graph representations

7

1

3

5

2

4

6

9

0 1 1 0 0 0 0 0 0

1 0 1 1 1 0 0 0 0

1 1 0 1 1 0 0 0 0

0 1 1 0 1 1 0 0 0

0 1 1 1 0 1 0 0 0

0 0 0 1 1 0 0 0 0

0 0 0 0 0 0 0 1 1

0 0 0 0 0 0 1 0 1

0 0 0 0 0 0 1 1 0

1

2

3

4

5

6

7

8

9

1 2 3 4 5 6 7 8 9

FRO

M

TO8

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

Adjacency Matrix

|V |

|V |

We will in general assume that the vertices are numbered 1, 2, 3 . . . |V |

Page 11: Depth First Search and Breadth First Search

Graph representations

7

1

3

5

2

4

6

9

1

3

6

7

8

9

1 3 54

1

2

2 54

3 62

3 62

54

8 9

7 9

7 8

38

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

Adjacency List(using linked lists)

We will in general assume that the vertices are numbered 1, 2, 3 . . . |V |

|V | 5 4

4 5

2

Page 12: Depth First Search and Breadth First Search

Graph representations

7

1

3

5

2

4

6

9

1

3

6

7

8

9

1 3 54

1

2

2 54

3 62

3 62

54

8 9

7 9

7 8

38

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

Adjacency List(using linked lists)

We will in general assume that the vertices are numbered 1, 2, 3 . . . |V |

|V | 5 4

4 5

2

Page 13: Depth First Search and Breadth First Search

Graph representations

7

1

3

5

2

4

6

9

1

3

6

7

8

9

1 3 54

1

2

2 54

3 62

3 62

54

8 9

7 9

7 8

38

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

Adjacency List(using linked lists)

We will in general assume that the vertices are numbered 1, 2, 3 . . . |V |

|V | 5 4

4 5

2

Page 14: Depth First Search and Breadth First Search

Graph representations

7

1

3

5

2

4

6

9

1

3

6

7

8

9

1 3 54

1

2

2 54

3 62

3 62

54

8 9

7 9

7 8

38

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

Adjacency List(using linked lists)

We will in general assume that the vertices are numbered 1, 2, 3 . . . |V |

|V | 5 4

4 5

2

Page 15: Depth First Search and Breadth First Search

Graph representations

7

1

3

5

2

4

6

9

1

3

6

7

8

9

1 3 54

1

2

2 54

3 62

3 62

54

8 9

7 9

7 8

38

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

Adjacency List(using linked lists)

We will in general assume that the vertices are numbered 1, 2, 3 . . . |V |

|V | 5 4

4 5

2

Page 16: Depth First Search and Breadth First Search

Graph representations

7

1

3

5

2

4

6

9

1

3

6

7

8

9

1 3 54

1

2

2 54

3 62

3 62

54

8 9

7 9

7 8

38

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

Adjacency List(using linked lists)

both representations are symmetric because the graphs are undirected

We will in general assume that the vertices are numbered 1, 2, 3 . . . |V |

|V | 5 4

4 5

2

Page 17: Depth First Search and Breadth First Search

Basic operations

Adjacency Matrix

Adjacency Matrix

Adjacency List(using linked lists)

Adjacency List(using linked lists)

Is there an edge from vertex u to v?

List all the edges leaving u ∈ V

Space

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

0 1 1 0 0

1 0 1 1 1

1 1 0 1 1

0 1 1 0 1

0 1 1 1 0

1

2

3

4

5

1 2 3 4 5 12345

1

12

2

3

2

3

3

4

2

Page 18: Depth First Search and Breadth First Search

Basic operations

Adjacency Matrix

Adjacency Matrix

Adjacency List(using linked lists)

Adjacency List(using linked lists)

Is there an edge from vertex u to v?

List all the edges leaving u ∈ V

Space Θ(|V |2) Θ(|V |+ |E|)

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

0 1 1 0 0

1 0 1 1 1

1 1 0 1 1

0 1 1 0 1

0 1 1 1 0

1

2

3

4

5

1 2 3 4 5 12345

1

12

2

3

2

3

3

4

2

Page 19: Depth First Search and Breadth First Search

Basic operations

Adjacency Matrix

Adjacency Matrix

Adjacency List(using linked lists)

Adjacency List(using linked lists)

Is there an edge from vertex u to v?

List all the edges leaving u ∈ V

Space Θ(|V |2) Θ(|V |+ |E|)

O(1) time

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

0 1 1 0 0

1 0 1 1 1

1 1 0 1 1

0 1 1 0 1

0 1 1 1 0

1

2

3

4

5

1 2 3 4 5 12345

1

12

2

3

2

3

3

4

2

Page 20: Depth First Search and Breadth First Search

Basic operations

Adjacency Matrix

Adjacency Matrix

Adjacency List(using linked lists)

Adjacency List(using linked lists)

Is there an edge from vertex u to v?

List all the edges leaving u ∈ V

Space Θ(|V |2) Θ(|V |+ |E|)

O(1) time O(V ) time

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

0 1 1 0 0

1 0 1 1 1

1 1 0 1 1

0 1 1 0 1

0 1 1 1 0

1

2

3

4

5

1 2 3 4 5 12345

1

12

2

3

2

3

3

4

2

Page 21: Depth First Search and Breadth First Search

Basic operations

Adjacency Matrix

Adjacency Matrix

Adjacency List(using linked lists)

Adjacency List(using linked lists)

Is there an edge from vertex u to v?

List all the edges leaving u ∈ V

Space Θ(|V |2) Θ(|V |+ |E|)

O(1) time O(deg(u)) time

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

0 1 1 0 0

1 0 1 1 1

1 1 0 1 1

0 1 1 0 1

0 1 1 1 0

1

2

3

4

5

1 2 3 4 5 12345

1

12

2

3

2

3

3

4

2

Page 22: Depth First Search and Breadth First Search

Basic operations

Adjacency Matrix

Adjacency Matrix

Adjacency List(using linked lists)

Adjacency List(using linked lists)

Is there an edge from vertex u to v?

List all the edges leaving u ∈ V

Space Θ(|V |2) Θ(|V |+ |E|)

O(1) time O(deg(u)) time

deg(u) (the degree of u),

is the number of edges leaving u

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

0 1 1 0 0

1 0 1 1 1

1 1 0 1 1

0 1 1 0 1

0 1 1 1 0

1

2

3

4

5

1 2 3 4 5 12345

1

12

2

3

2

3

3

4

2

Page 23: Depth First Search and Breadth First Search

Basic operations

Adjacency Matrix

Adjacency Matrix

Adjacency List(using linked lists)

Adjacency List(using linked lists)

Is there an edge from vertex u to v?

List all the edges leaving u ∈ V

Space Θ(|V |2) Θ(|V |+ |E|)

O(1) time O(deg(u)) time

deg(u) (the degree of u),

is the number of edges leaving u

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

0 1 1 0 0

1 0 1 1 1

1 1 0 1 1

0 1 1 0 1

0 1 1 1 0

1

2

3

4

5

1 2 3 4 5 12345

1

12

2

3

2

3

3

4

2

some vertex u

Page 24: Depth First Search and Breadth First Search

Basic operations

Adjacency Matrix

Adjacency Matrix

Adjacency List(using linked lists)

Adjacency List(using linked lists)

Is there an edge from vertex u to v?

List all the edges leaving u ∈ V

Space Θ(|V |2) Θ(|V |+ |E|)

O(1) time O(deg(u)) time

deg(u) (the degree of u),

is the number of edges leaving u

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

0 1 1 0 0

1 0 1 1 1

1 1 0 1 1

0 1 1 0 1

0 1 1 1 0

1

2

3

4

5

1 2 3 4 5 12345

1

12

2

3

2

3

3

4

2

some vertex u

deg(u) = 8

Page 25: Depth First Search and Breadth First Search

Basic operations

Adjacency Matrix

Adjacency Matrix

Adjacency List(using linked lists)

Adjacency List(using linked lists)

Is there an edge from vertex u to v?

List all the edges leaving u ∈ V

Space Θ(|V |2) Θ(|V |+ |E|)

O(1) time O(deg(u)) time

deg(u) (the degree of u),

is the number of edges leaving u

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

0 1 1 0 0

1 0 1 1 1

1 1 0 1 1

0 1 1 0 1

0 1 1 1 0

1

2

3

4

5

1 2 3 4 5 12345

1

12

2

3

2

3

3

4

2

Page 26: Depth First Search and Breadth First Search

Basic operations

Adjacency Matrix

Adjacency Matrix

Adjacency List(using linked lists)

Adjacency List(using linked lists)

Is there an edge from vertex u to v?

List all the edges leaving u ∈ V

Space Θ(|V |2) Θ(|V |+ |E|)

O(1) time

O(V ) time

O(deg(u)) time

deg(u) (the degree of u),

is the number of edges leaving u

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

0 1 1 0 0

1 0 1 1 1

1 1 0 1 1

0 1 1 0 1

0 1 1 1 0

1

2

3

4

5

1 2 3 4 5 12345

1

12

2

3

2

3

3

4

2

Page 27: Depth First Search and Breadth First Search

Basic operations

Adjacency Matrix

Adjacency Matrix

Adjacency List(using linked lists)

Adjacency List(using linked lists)

Is there an edge from vertex u to v?

List all the edges leaving u ∈ V

Space Θ(|V |2) Θ(|V |+ |E|)

O(1) time

O(V ) time O(deg(u)) time

O(deg(u)) time

deg(u) (the degree of u),

is the number of edges leaving u

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

0 1 1 0 0

1 0 1 1 1

1 1 0 1 1

0 1 1 0 1

0 1 1 1 0

1

2

3

4

5

1 2 3 4 5 12345

1

12

2

3

2

3

3

4

2

Page 28: Depth First Search and Breadth First Search

Basic operations

Adjacency Matrix

Adjacency Matrix

Adjacency List(using linked lists)

Adjacency List(using linked lists)

Is there an edge from vertex u to v?

List all the edges leaving u ∈ V

Space

Adjacency List(using hash tables)

Θ(|V |2) Θ(|V |+ |E|)

O(1) time

O(V ) time O(deg(u)) time

O(deg(u)) time

deg(u) (the degree of u),

is the number of edges leaving u

Θ(|V |+ |E|)

O(deg(u)) time

O(1) time

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

0 1 1 0 0

1 0 1 1 1

1 1 0 1 1

0 1 1 0 1

0 1 1 1 0

1

2

3

4

5

1 2 3 4 5 12345

1

12

2

3

2

3

3

4

2

Adjacency List(using hash tables)

12345

Page 29: Depth First Search and Breadth First Search

Basic operations

all three representations work for directed and/or weighted graphs too

Adjacency Matrix

Adjacency Matrix

Adjacency List(using linked lists)

Adjacency List(using linked lists)

Is there an edge from vertex u to v?

List all the edges leaving u ∈ V

Space

Adjacency List(using hash tables)

Θ(|V |2) Θ(|V |+ |E|)

O(1) time

O(V ) time O(deg(u)) time

O(deg(u)) time

deg(u) (the degree of u),

is the number of edges leaving u

Θ(|V |+ |E|)

O(deg(u)) time

O(1) time

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

0 1 1 0 0

1 0 1 1 1

1 1 0 1 1

0 1 1 0 1

0 1 1 1 0

1

2

3

4

5

1 2 3 4 5

(with the same complexities)

12345

1

12

2

3

2

3

3

4

2

Adjacency List(using hash tables)

12345

Page 30: Depth First Search and Breadth First Search

Basic operations

all three representations work for directed and/or weighted graphs too

Adjacency Matrix

Adjacency Matrix

Adjacency List(using linked lists)

Adjacency List(using linked lists)

Is there an edge from vertex u to v?

List all the edges leaving u ∈ V

Space

Adjacency List(using hash tables)

Θ(|V |2) Θ(|V |+ |E|)

O(1) time

O(V ) time O(deg(u)) time

O(deg(u)) time

deg(u) (the degree of u),

is the number of edges leaving u

Θ(|V |+ |E|)

O(deg(u)) time

O(1) time

why don’t we always use these?

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

0 1 1 0 0

1 0 1 1 1

1 1 0 1 1

0 1 1 0 1

0 1 1 1 0

1

2

3

4

5

1 2 3 4 5

(with the same complexities)

12345

1

12

2

3

2

3

3

4

2

Adjacency List(using hash tables)

12345

Page 31: Depth First Search and Breadth First Search

Basic operations

Adjacency Matrix Adjacency List(using linked lists)

Is there an edge from vertex u to v?

List all the edges leaving u ∈ V

Space

Adjacency List(using hash tables)

Θ(|V |2) Θ(|V |+ |E|)

O(1) time

O(V ) time O(deg(u)) time

O(deg(u)) time

Θ(|V |+ |E|)

O(deg(u)) time

O(1) time

why don’t we always use these?

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

Page 32: Depth First Search and Breadth First Search

Basic operations

Adjacency Matrix Adjacency List(using linked lists)

Is there an edge from vertex u to v?

List all the edges leaving u ∈ V

Space

Adjacency List(using hash tables)

Θ(|V |2) Θ(|V |+ |E|)

O(1) time

O(V ) time O(deg(u)) time

O(deg(u)) time

Θ(|V |+ |E|)

O(deg(u)) time

O(1) time

why don’t we always use these?

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

Basic hash tables give expected time complexities (constant time ‘on average’)

- no worst case guarantees against collisions

Page 33: Depth First Search and Breadth First Search

Basic operations

Adjacency Matrix Adjacency List(using linked lists)

Is there an edge from vertex u to v?

List all the edges leaving u ∈ V

Space

Adjacency List(using hash tables)

Θ(|V |2) Θ(|V |+ |E|)

O(1) time

O(V ) time O(deg(u)) time

O(deg(u)) time

Θ(|V |+ |E|)

O(deg(u)) time

O(1) time

why don’t we always use these?

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

Basic hash tables give expected time complexities (constant time ‘on average’)

- no worst case guarantees against collisions

If you’re interested in hashing with provable guarantees,come to COMS31900 (Advanced Algorithms)

Page 34: Depth First Search and Breadth First Search

Basic operations

Adjacency Matrix Adjacency List(using linked lists)

Is there an edge from vertex u to v?

List all the edges leaving u ∈ V

Space

Adjacency List(using hash tables)

Θ(|V |2) Θ(|V |+ |E|)

O(1) time

O(V ) time O(deg(u)) time

O(deg(u)) time

Θ(|V |+ |E|)

O(deg(u)) time

O(1) time

why don’t we always use these?

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

Basic hash tables give expected time complexities (constant time ‘on average’)

- no worst case guarantees against collisions

If you’re interested in hashing with provable guarantees,come to COMS31900 (Advanced Algorithms)

Fortunately, few algorithms need to ask “is there an edge?”

Normally, “tell me all the edges” is fine

Page 35: Depth First Search and Breadth First Search

Basic operations

Adjacency Matrix Adjacency List(using linked lists)

Is there an edge from vertex u to v?

List all the edges leaving u ∈ V

Space

Adjacency List(using hash tables)

Θ(|V |2) Θ(|V |+ |E|)

O(1) time

O(V ) time O(deg(u)) time

O(deg(u)) time

Θ(|V |+ |E|)

O(deg(u)) time

O(1) time

why don’t we always use these?

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

Page 36: Depth First Search and Breadth First Search

Basic operations

Adjacency Matrix Adjacency List(using linked lists)

Is there an edge from vertex u to v?

List all the edges leaving u ∈ V

Space

Adjacency List(using hash tables)

Θ(|V |2) Θ(|V |+ |E|)

O(1) time

O(V ) time O(deg(u)) time

O(deg(u)) time

Θ(|V |+ |E|)

O(deg(u)) time

O(1) time

why don’t we always use these?

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

For this course we’ll stick to Adjacency Matrices and Lists (using linked lists)(for today just Adjacency Lists)

Page 37: Depth First Search and Breadth First Search

Basic operations

Adjacency Matrix Adjacency List(using linked lists)

Is there an edge from vertex u to v?

List all the edges leaving u ∈ V

Space

Adjacency List(using hash tables)

Θ(|V |2) Θ(|V |+ |E|)

O(1) time

O(V ) time O(deg(u)) time

O(deg(u)) time

Θ(|V |+ |E|)

O(deg(u)) time

O(1) time

why don’t we always use these?

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

For this course we’ll stick to Adjacency Matrices and Lists (using linked lists)(for today just Adjacency Lists)

One practical reason to stick to standard representations is that

you may not have control over how your graph is stored.

Page 38: Depth First Search and Breadth First Search

Basic operations

Adjacency Matrix Adjacency List(using linked lists)

Is there an edge from vertex u to v?

List all the edges leaving u ∈ V

Space

Adjacency List(using hash tables)

Θ(|V |2) Θ(|V |+ |E|)

O(1) time

O(V ) time O(deg(u)) time

O(deg(u)) time

Θ(|V |+ |E|)

O(deg(u)) time

O(1) time

why don’t we always use these?

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

For this course we’ll stick to Adjacency Matrices and Lists (using linked lists)(for today just Adjacency Lists)

One practical reason to stick to standard representations is that

you may not have control over how your graph is stored.

it may not even be stored as a graph...

Page 39: Depth First Search and Breadth First Search

Basic operations

Adjacency Matrix Adjacency List(using linked lists)

Is there an edge from vertex u to v?

List all the edges leaving u ∈ V

Space

Adjacency List(using hash tables)

Θ(|V |2) Θ(|V |+ |E|)

O(1) time

O(V ) time O(deg(u)) time

O(deg(u)) time

Θ(|V |+ |E|)

O(deg(u)) time

O(1) time

why don’t we always use these?

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

For this course we’ll stick to Adjacency Matrices and Lists (using linked lists)(for today just Adjacency Lists)

One practical reason to stick to standard representations is that

you may not have control over how your graph is stored.

it may not even be stored as a graph...

Fortunately in many cases, you can pretend your graph is stored as one of the above.

Page 40: Depth First Search and Breadth First Search

Intersection Graphs

Page 41: Depth First Search and Breadth First Search

Intersection Graphs

the intersections of these circles

correspond to the graph below

Page 42: Depth First Search and Breadth First Search

Intersection Graphs

each circle is a vertex the intersections of these circles

correspond to the graph below

Page 43: Depth First Search and Breadth First Search

Intersection Graphs

the intersections of these circles

correspond to the graph below

Page 44: Depth First Search and Breadth First Search

Intersection Graphs

each intersection

is an edge

the intersections of these circles

correspond to the graph below

Page 45: Depth First Search and Breadth First Search

Intersection Graphs

each intersection

is an edge

the intersections of these circles

correspond to the graph below

Page 46: Depth First Search and Breadth First Search

Intersection Graphs

each intersection

is an edge

we can pretend we have

an Adjacency Matrix

without building one

even if we only store the circles,

the intersections of these circles

correspond to the graph below

Page 47: Depth First Search and Breadth First Search

Other intersection graphs. . .

(these are 1D intervals on a line spread out for visual clarity)

both of these ‘collections’ also

correspond to the example graph

Page 48: Depth First Search and Breadth First Search

Other intersection graphs. . .

= an edge

= a vertex

(these are 1D intervals on a line spread out for visual clarity)

both of these ‘collections’ also

correspond to the example graph

Page 49: Depth First Search and Breadth First Search

Other intersection graphs. . .

= an edge

= a vertex

there is an edge if two intervals intersect

each interval is a vertex

(these are 1D intervals on a line spread out for visual clarity)

both of these ‘collections’ also

correspond to the example graph

Page 50: Depth First Search and Breadth First Search

Other intersection graphs. . .

= an edge

= a vertex

there is an edge if two intervals intersect

each interval is a vertex

(these are 1D intervals on a line spread out for visual clarity)

we can pretend we have

an Adjacency Matrix

without building one

Again,

both of these ‘collections’ also

correspond to the example graph

Page 51: Depth First Search and Breadth First Search

Configuration Graphs

This is the configuration graph for the

(4 disk) towers of Hanoi

a node is a state that the

game can be in

there is an edge if you can get from

one state to another in a single move

Page 52: Depth First Search and Breadth First Search

Configuration Graphs

This is the configuration graph for the

(4 disk) towers of Hanoi

a node is a state that the

game can be in

there is an edge if you can get from

one state to another in a single move

Page 53: Depth First Search and Breadth First Search

Configuration Graphs

This is the configuration graph for the

(4 disk) towers of Hanoi

a node is a state that the

game can be in

there is an edge if you can get from

one state to another in a single move

Page 54: Depth First Search and Breadth First Search

Configuration Graphs

This is the configuration graph for the

(4 disk) towers of Hanoi

a node is a state that the

game can be in

there is an edge if you can get from

one state to another in a single move

Page 55: Depth First Search and Breadth First Search

Configuration Graphs

This is the configuration graph for the

(4 disk) towers of Hanoi

a node is a state that the

game can be in

there is an edge if you can get from

one state to another in a single move

Page 56: Depth First Search and Breadth First Search

Configuration Graphs

This is the configuration graph for the

(4 disk) towers of Hanoi

a node is a state that the

game can be in

there is an edge if you can get from

one state to another in a single move

Page 57: Depth First Search and Breadth First Search

Configuration Graphs

This is the configuration graph for the

(4 disk) towers of Hanoi

a node is a state that the

game can be in

there is an edge if you can get from

one state to another in a single move

Page 58: Depth First Search and Breadth First Search

Configuration Graphs

This is the configuration graph for the

(4 disk) towers of Hanoi

a node is a state that the

game can be in

there is an edge if you can get from

one state to another in a single move

Page 59: Depth First Search and Breadth First Search

Configuration Graphs

This is the configuration graph for the

(4 disk) towers of Hanoi

a node is a state that the

game can be in

there is an edge if you can get from

one state to another in a single move

Page 60: Depth First Search and Breadth First Search

Configuration Graphs

This is the configuration graph for the

(4 disk) towers of Hanoi

a node is a state that the

game can be in

there is an edge if you can get from

one state to another in a single move

storing this uses much less space than

storing this this graph

Page 61: Depth First Search and Breadth First Search

Configuration Graphs

This is the configuration graph for the

(4 disk) towers of Hanoi

a node is a state that the

game can be in

there is an edge if you can get from

one state to another in a single move

storing this uses much less space than

storing this this graph

we can pretend we have

an Adjacency List

without building one

Page 62: Depth First Search and Breadth First Search

Exploring a graph

you are here

BAG

Page 63: Depth First Search and Breadth First Search

Exploring a graph

you are here

BAG Goal: visit every vertex in a

connected graph efficiently

Page 64: Depth First Search and Breadth First Search

Exploring a graph

you are here

BAG Goal: visit every vertex in a

connected graph efficiently

The bag (secretly a data structure)

We can put a vertex in the bag

We can take a vertex from the bag

We don’t care how it works (for now)

(the bag doesn’t forget or change things)

but we don’t know which one we’ll get

Page 65: Depth First Search and Breadth First Search

Exploring a graph

BAG Goal: visit every vertex in a

connected graph efficiently

We going to use an adjacency list so the

graph is really stored like this:

The bag (secretly a data structure)

We can put a vertex in the bag

We can take a vertex from the bag

We don’t care how it works (for now)

(the bag doesn’t forget or change things)

but we don’t know which one we’ll get

Page 66: Depth First Search and Breadth First Search

Exploring a graph

you are here

BAG Goal: visit every vertex in a

connected graph efficiently

Page 67: Depth First Search and Breadth First Search

Exploring a graph

vertex s

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

Goal: visit every vertex in a

connected graph efficiently

Page 68: Depth First Search and Breadth First Search

Exploring a graph

vertex s

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

Goal: visit every vertex in a

connected graph efficiently

Page 69: Depth First Search and Breadth First Search

Exploring a graph

vertex s

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

Goal: visit every vertex in a

connected graph efficiently

Page 70: Depth First Search and Breadth First Search

Exploring a graph

vertex s

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

Goal: visit every vertex in a

connected graph efficiently

Page 71: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

Goal: visit every vertex in a

connected graph efficiently

Page 72: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

Goal: visit every vertex in a

connected graph efficiently

Page 73: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

Goal: visit every vertex in a

connected graph efficiently

Page 74: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

Goal: visit every vertex in a

connected graph efficiently

Page 75: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

Goal: visit every vertex in a

connected graph efficiently

back in!

in twice!

Page 76: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

Goal: visit every vertex in a

connected graph efficiently

Page 77: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

Goal: visit every vertex in a

connected graph efficiently

Page 78: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

Goal: visit every vertex in a

connected graph efficiently

Page 79: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

Goal: visit every vertex in a

connected graph efficiently

already marked

(do nothing)

Page 80: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

Goal: visit every vertex in a

connected graph efficiently

Page 81: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

Goal: visit every vertex in a

connected graph efficiently

Page 82: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

Goal: visit every vertex in a

connected graph efficiently

Page 83: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

Goal: visit every vertex in a

connected graph efficiently

Page 84: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

Goal: visit every vertex in a

connected graph efficiently

Page 85: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

Goal: visit every vertex in a

connected graph efficiently

Page 86: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

Goal: visit every vertex in a

connected graph efficiently

Page 87: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

Goal: visit every vertex in a

connected graph efficiently

Some time later. . .

Page 88: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

Goal: visit every vertex in a

connected graph efficiently

Some time later. . . ?

Page 89: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

Goal: visit every vertex in a

connected graph efficiently

Some time later. . . ?

Questions:

1. Does TRAVERSE always stop?

2. Does it always visit every vertex?

3. How fast is it? (in the worst case)

Page 90: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

Questions:

1. Does TRAVERSE always stop?

2. Does it always visit every vertex?

3. How fast is it? (in the worst case)

Page 91: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

1. Does TRAVERSE always stop?

Page 92: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

1. Does TRAVERSE always stop?

Page 93: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

back in!

in twice!

1. Does TRAVERSE always stop?

Page 94: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

back in!

in twice!

1. Does TRAVERSE always stop?

We may put a vertex in the bag many times but each edge is only

processed twice (once in each direction)

Page 95: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

back in!

in twice!

1. Does TRAVERSE always stop?

We may put a vertex in the bag many times but each edge is only

processed twice (once in each direction)

never seen

again!

Page 96: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

back in!

in twice!

1. Does TRAVERSE always stop?

We may put a vertex in the bag many times but each edge is only

processed twice (once in each direction)

never seen

again!

Important later: number of ‘puts’ 6 2|E|

Page 97: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

back in!

in twice!

1. Does TRAVERSE always stop?

We may put a vertex in the bag many times but each edge is only

processed twice (once in each direction)

never seen

again!

Important later: number of ‘puts’ 6 2|E|

TRAVERSE(s)

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

Page 98: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

back in!

in twice!

1. Does TRAVERSE always stop?

We may put a vertex in the bag many times but each edge is only

processed twice (once in each direction)

never seen

again!

Important later: number of ‘puts’ 6 2|E|

Page 99: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

back in!

in twice!

1. Does TRAVERSE always stop? Yes

We may put a vertex in the bag many times but each edge is only

processed twice (once in each direction)

never seen

again!

Important later: number of ‘puts’ 6 2|E|

Page 100: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

Questions:

1. Does TRAVERSE always stop?

2. Does it always visit every vertex?

Yes

3. How fast is it? (in the worst case)

Page 101: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

2. Does it always visit every vertex?

Page 102: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

2. Does it always visit every vertex?

When a vertex on this path is marked, the next vertex is put in the bag

Eventually, that vertex is removed from the bag and is marked.

Consider any path from s to some v:

Proof (by induction)

(unless it was already marked)

Page 103: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

2. Does it always visit every vertex?

When a vertex on this path is marked, the next vertex is put in the bag

Eventually, that vertex is removed from the bag and is marked.

Consider any path from s to some v:

s

v

Proof (by induction)

(unless it was already marked)

Page 104: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

2. Does it always visit every vertex?

When a vertex on this path is marked, the next vertex is put in the bag

Eventually, that vertex is removed from the bag and is marked.

Consider any path from s to some v:

s

v

Proof (by induction)

(unless it was already marked)

Page 105: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

2. Does it always visit every vertex?

When a vertex on this path is marked, the next vertex is put in the bag

Eventually, that vertex is removed from the bag and is marked.

Consider any path from s to some v:

s

v

Proof (by induction)

(unless it was already marked)

Page 106: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

2. Does it always visit every vertex?

When a vertex on this path is marked, the next vertex is put in the bag

Eventually, that vertex is removed from the bag and is marked.

Consider any path from s to some v:

s

v

Proof (by induction)

(unless it was already marked)

Page 107: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

2. Does it always visit every vertex?

When a vertex on this path is marked, the next vertex is put in the bag

Eventually, that vertex is removed from the bag and is marked.

Consider any path from s to some v:

s

v

Proof (by induction)

(unless it was already marked)

Page 108: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

2. Does it always visit every vertex?

When a vertex on this path is marked, the next vertex is put in the bag

Eventually, that vertex is removed from the bag and is marked.

Consider any path from s to some v:

s

v

Proof (by induction)

(unless it was already marked)

Page 109: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

2. Does it always visit every vertex?

When a vertex on this path is marked, the next vertex is put in the bag

Eventually, that vertex is removed from the bag and is marked.

Consider any path from s to some v:

s

v

Proof (by induction)

(unless it was already marked)

Page 110: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

2. Does it always visit every vertex?

When a vertex on this path is marked, the next vertex is put in the bag

Eventually, that vertex is removed from the bag and is marked.

Consider any path from s to some v:

s

v

Proof (by induction)

(unless it was already marked)

Page 111: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

2. Does it always visit every vertex?

When a vertex on this path is marked, the next vertex is put in the bag

Eventually, that vertex is removed from the bag and is marked.

Consider any path from s to some v:

s

v

Proof (by induction)

(unless it was already marked)

Page 112: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

2. Does it always visit every vertex?

When a vertex on this path is marked, the next vertex is put in the bag

Eventually, that vertex is removed from the bag and is marked.

Consider any path from s to some v:

s

v

Proof (by induction)

there could be a ‘shortcut’

(unless it was already marked)

Page 113: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

2. Does it always visit every vertex?

When a vertex on this path is marked, the next vertex is put in the bag

Eventually, that vertex is removed from the bag and is marked.

Consider any path from s to some v:

s

v

Proof (by induction)

there could be a ‘shortcut’

(unless it was already marked)

Page 114: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

2. Does it always visit every vertex?

When a vertex on this path is marked, the next vertex is put in the bag

Eventually, that vertex is removed from the bag and is marked.

Consider any path from s to some v:

s

v

Proof (by induction)

there could be a ‘shortcut’

(unless it was already marked)

Page 115: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

2. Does it always visit every vertex?

When a vertex on this path is marked, the next vertex is put in the bag

Eventually, that vertex is removed from the bag and is marked.

Consider any path from s to some v:

s

v

Proof (by induction)

there could be a ‘shortcut’

(unless it was already marked)

Page 116: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

2. Does it always visit every vertex?

When a vertex on this path is marked, the next vertex is put in the bag

Eventually, that vertex is removed from the bag and is marked.

Consider any path from s to some v:

s

v

Proof (by induction)

there could be a ‘shortcut’

(unless it was already marked)

Page 117: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

2. Does it always visit every vertex?

When a vertex on this path is marked, the next vertex is put in the bag

Eventually, that vertex is removed from the bag and is marked.

Consider any path from s to some v:

s

v

Proof (by induction)

there could be a ‘shortcut’

(unless it was already marked)

Page 118: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

2. Does it always visit every vertex?

When a vertex on this path is marked, the next vertex is put in the bag

Eventually, that vertex is removed from the bag and is marked.

Consider any path from s to some v:

s

v

Proof (by induction)

(unless it was already marked)

Page 119: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

2. Does it always visit every vertex?

When a vertex on this path is marked, the next vertex is put in the bag

Eventually, that vertex is removed from the bag and is marked.

Consider any path from s to some v:

s

v

Proof (by induction)

(unless it was already marked)

Page 120: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

2. Does it always visit every vertex?

When a vertex on this path is marked, the next vertex is put in the bag

Eventually, that vertex is removed from the bag and is marked.

Consider any path from s to some v:

s

v

Proof (by induction)

(unless it was already marked)

Page 121: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

2. Does it always visit every vertex?

When a vertex on this path is marked, the next vertex is put in the bag

Eventually, that vertex is removed from the bag and is marked.

Consider any path from s to some v:

s

v

Proof (by induction)

(unless it was already marked)

Page 122: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

2. Does it always visit every vertex?

When a vertex on this path is marked, the next vertex is put in the bag

Eventually, that vertex is removed from the bag and is marked.

Consider any path from s to some v:

s

v

Proof (by induction)

(unless it was already marked)

Page 123: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

2. Does it always visit every vertex?

When a vertex on this path is marked, the next vertex is put in the bag

Eventually, that vertex is removed from the bag and is marked.

Consider any path from s to some v:

s

v

Proof (by induction)

(unless it was already marked)

Page 124: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

2. Does it always visit every vertex?

When a vertex on this path is marked, the next vertex is put in the bag

Eventually, that vertex is removed from the bag and is marked.

Consider any path from s to some v:

s

v

Proof (by induction)

(unless it was already marked)

Page 125: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

2. Does it always visit every vertex?

When a vertex on this path is marked, the next vertex is put in the bag

Eventually, that vertex is removed from the bag and is marked.

Consider any path from s to some v:

s

v

Proof (by induction)

Yes

(unless it was already marked)

Page 126: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

2. Does it always visit every vertex?

When a vertex on this path is marked, the next vertex is put in the bag

Eventually, that vertex is removed from the bag and is marked.

Consider any path from s to some v:

s

v

Proof (by induction)

Yes

The correctness doesn’t depend

on how the bag works!

(unless it was already marked)

Page 127: Depth First Search and Breadth First Search

Exploring a graph

BAG

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

2. Does it always visit every vertex?

When a vertex on this path is marked, the next vertex is put in the bag

Eventually, that vertex is removed from the bag and is marked.

Consider any path from s to some v:

s

v

Proof (by induction)

Yes

The correctness doesn’t depend

on how the bag works!

(but this doesn’t tell you anything about

which order vertices are marked in)

(unless it was already marked)

Page 128: Depth First Search and Breadth First Search

Exploring a graph

Questions:

1. Does TRAVERSE always stop?

2. Does it always visit every vertex?

Yes

Yes3. How fast is it? (in the worst case)

Page 129: Depth First Search and Breadth First Search

Exploring a graph

Questions:

1. Does TRAVERSE always stop?

2. Does it always visit every vertex?

Yes

Yes3. How fast is it? (in the worst case)

Assumption

(we’ll come back to this)

bag operations

take O(1) time

Page 130: Depth First Search and Breadth First Search

Exploring a graph

Questions:

1. Does TRAVERSE always stop?

2. Does it always visit every vertex?

Yes

Yes3. How fast is it? (in the worst case)

Assumption

(we’ll come back to this)

bag operations

take O(1) time

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

Page 131: Depth First Search and Breadth First Search

Exploring a graph

Questions:

1. Does TRAVERSE always stop?

2. Does it always visit every vertex?

Yes

Yes3. How fast is it? (in the worst case)

Assumption

(we’ll come back to this)

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

bag operations

take O(1) time

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

Page 132: Depth First Search and Breadth First Search

Exploring a graph

Questions:

1. Does TRAVERSE always stop?

2. Does it always visit every vertex?

Yes

Yes3. How fast is it? (in the worst case)

Time complexity:

Assumption

(we’ll come back to this)

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

bag operations

take O(1) time

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

Page 133: Depth First Search and Breadth First Search

Exploring a graph

Questions:

1. Does TRAVERSE always stop?

2. Does it always visit every vertex?

Yes

Yes3. How fast is it? (in the worst case)

Time complexity:

Assumption

(we’ll come back to this)

O(1) time every time we take from the bag

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

bag operations

take O(1) time

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

Page 134: Depth First Search and Breadth First Search

Exploring a graph

Questions:

1. Does TRAVERSE always stop?

2. Does it always visit every vertex?

Yes

Yes3. How fast is it? (in the worst case)

Time complexity:

Assumption

(we’ll come back to this)

(store an array where mark[u] = 1 iff u is marked)

O(1) time every time we take from the bag

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

bag operations

take O(1) time

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

Page 135: Depth First Search and Breadth First Search

Exploring a graph

Questions:

1. Does TRAVERSE always stop?

2. Does it always visit every vertex?

Yes

Yes3. How fast is it? (in the worst case)

Time complexity:

Assumption

(we’ll come back to this)

(store an array where mark[u] = 1 iff u is marked)

O(1) time every time we take from the bag

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

bag operations

take O(1) time

O(1) time every time we put into the bag

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

Page 136: Depth First Search and Breadth First Search

Exploring a graph

Questions:

1. Does TRAVERSE always stop?

2. Does it always visit every vertex?

Yes

Yes3. How fast is it? (in the worst case)

Time complexity:

Assumption

(we’ll come back to this)

(store an array where mark[u] = 1 iff u is marked)

O(1) time every time we take from the bag

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

bag operations

take O(1) time

O(1) time every time we put into the bag

What is the total number of bag operations?

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

Page 137: Depth First Search and Breadth First Search

Exploring a graph

Questions:

1. Does TRAVERSE always stop?

2. Does it always visit every vertex?

Yes

Yes3. How fast is it? (in the worst case)

Time complexity:

Assumption

(we’ll come back to this)

(store an array where mark[u] = 1 iff u is marked)

O(1) time every time we take from the bag

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

bag operations

take O(1) time

O(1) time every time we put into the bag

What is the total number of bag operations?

we do at most 2|E| put operations

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

Page 138: Depth First Search and Breadth First Search

Exploring a graph

Questions:

1. Does TRAVERSE always stop?

2. Does it always visit every vertex?

Yes

Yes3. How fast is it? (in the worst case)

Time complexity:

Assumption

(we’ll come back to this)

(store an array where mark[u] = 1 iff u is marked)

O(1) time every time we take from the bag

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

bag operations

take O(1) time

O(1) time every time we put into the bag

What is the total number of bag operations?

we do at most 2|E| put operations

so we do at most 2|E| take operations

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

Page 139: Depth First Search and Breadth First Search

Exploring a graph

Questions:

1. Does TRAVERSE always stop?

2. Does it always visit every vertex?

Yes

Yes3. How fast is it? (in the worst case)

Time complexity:

Assumption

(we’ll come back to this)

(store an array where mark[u] = 1 iff u is marked)

O(1) time every time we take from the bag

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

bag operations

take O(1) time

O(1) time every time we put into the bag

What is the total number of bag operations?

we do at most 2|E| put operations

so we do at most 2|E| take operations

The overall time complexity is O(|E|)

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

Page 140: Depth First Search and Breadth First Search

Exploring a graph

Questions:

1. Does TRAVERSE always stop?

2. Does it always visit every vertex?

Yes

Yes3. How fast is it? (in the worst case)

Time complexity:

Assumption

(we’ll come back to this)

(store an array where mark[u] = 1 iff u is marked)

O(1) time every time we take from the bag

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

bag operations

take O(1) time

O(1) time every time we put into the bag

What is the total number of bag operations?

we do at most 2|E| put operations

so we do at most 2|E| take operations

The overall time complexity is O(|E|)

O(|E|)

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

Page 141: Depth First Search and Breadth First Search

Exploring a graph

Questions:

1. Does TRAVERSE always stop?

2. Does it always visit every vertex?

Yes

Yes3. How fast is it? (in the worst case)

Assumption

(we’ll come back to this)

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

bag operations

take O(1) time

O(|E|)

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

Page 142: Depth First Search and Breadth First Search

Exploring a graph

Questions:

1. Does TRAVERSE always stop?

2. Does it always visit every vertex?

Yes

Yes3. How fast is it? (in the worst case)

Assumption

(we’ll come back to this)

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

bag operations

take O(1) time

O(|E|)

What if we had used an

adjacency matrix?

0 1 1 0 0

1 0 1 1 1

1 1 0 1 1

0 1 1 0 1

0 1 1 1 0

1

2

3

4

5

1 2 3 4 5

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

Page 143: Depth First Search and Breadth First Search

Exploring a graph

Questions:

1. Does TRAVERSE always stop?

2. Does it always visit every vertex?

Yes

Yes3. How fast is it? (in the worst case)

Assumption

(we’ll come back to this)

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

bag operations

take O(1) time

O(|E|)

What if we had used an

adjacency matrix?

finding all edges leaving u would have taken O(|V |) time

0 1 1 0 0

1 0 1 1 1

1 1 0 1 1

0 1 1 0 1

0 1 1 1 0

1

2

3

4

5

1 2 3 4 5

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

Page 144: Depth First Search and Breadth First Search

Exploring a graph

Questions:

1. Does TRAVERSE always stop?

2. Does it always visit every vertex?

Yes

Yes3. How fast is it? (in the worst case)

Assumption

(we’ll come back to this)

put s into the bag

while the bag is not empty

take u from the bag

if u unmarked

mark u

for every edge (u, v)

put v into the bag

TRAVERSE(s)

bag operations

take O(1) time

O(|E|)

What if we had used an

adjacency matrix?

finding all edges leaving u would have taken O(|V |) time

0 1 1 0 0

1 0 1 1 1

1 1 0 1 1

0 1 1 0 1

0 1 1 1 0

1

2

3

4

5

1 2 3 4 5

overall, the time complexity would have been O(|V |2) instead of O(|E|).

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges

Page 145: Depth First Search and Breadth First Search

How should we implement the bag?

BAG

Queue Stack

Operations take O(1) time

Depth First SearchBreadth First Search

but in different orders with different types of bag

TRAVERSE visits every vertex in a connected graph in O(|E|) time

with a Queue the algorithm is called with a Stack the algorithm is called

Applications Applications

Shortest paths in unweighted graphs

Max-Flow

Testing whether a graph is bipartite

Finding (strongly) connected components

Topicologically sorting a Directed Acyclic Graph

Testing for planarity

Page 146: Depth First Search and Breadth First Search

How should we implement the bag?

BAG

Queue Stack

Operations take O(1) time

Depth First SearchBreadth First Search

but in different orders with different types of bag

TRAVERSE visits every vertex in a connected graph in O(|E|) time

with a Queue the algorithm is called with a Stack the algorithm is called

Applications Applications

Shortest paths in unweighted graphs

Max-Flow

Testing whether a graph is bipartite

Finding (strongly) connected components

Topicologically sorting a Directed Acyclic Graph

Testing for planarity

(and it works for directed graphs too)

Page 147: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Page 148: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

distance = 1

Page 149: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

distance = 3

Page 150: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

distance = 6

Page 151: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Page 152: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 153: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 154: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 155: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 156: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 157: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 158: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 159: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 160: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 161: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 162: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

these are all in front of these

in the queue

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 163: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

these are all in front of these

in the queue

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 164: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 165: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 166: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 167: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 168: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 169: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 170: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 171: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 172: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

This is why it’s called

Breadth-First Search

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 173: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

This is why it’s called

Breadth-First Search

Lemma In BFS, the vertices are marked in

distance order (smallest first)

Proof by induction (on the distance from s)

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 174: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

This is why it’s called

Breadth-First Search

Base Case: BFS marks all vertices at distance 0 from s

before marking any vertex at distance > 0 .

Lemma In BFS, the vertices are marked in

distance order (smallest first)

Proof by induction (on the distance from s)

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 175: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

This is why it’s called

Breadth-First Search

Base Case: BFS marks all vertices at distance 0 from s

before marking any vertex at distance > 0 .

Lemma In BFS, the vertices are marked in

distance order (smallest first)

Proof by induction (on the distance from s)

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 176: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

This is why it’s called

Breadth-First Search

Base Case: BFS marks all vertices at distance 0 from s

before marking any vertex at distance > 0 .

Inductive Hypothesis: Assume that BFS marks all vertices at distance (i− 1)

before marking any vertex at distance > (i− 1).

Lemma In BFS, the vertices are marked in

distance order (smallest first)

Proof by induction (on the distance from s)

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 177: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

This is why it’s called

Breadth-First Search

Base Case: BFS marks all vertices at distance 0 from s

before marking any vertex at distance > 0 .

Inductive Hypothesis: Assume that BFS marks all vertices at distance (i− 1)

before marking any vertex at distance > (i− 1).

When BFS marks a vertex at distance (i− 1),it puts all its neighbours in the queue.

Lemma In BFS, the vertices are marked in

distance order (smallest first)

Proof by induction (on the distance from s)

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 178: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

This is why it’s called

Breadth-First Search

Base Case: BFS marks all vertices at distance 0 from s

before marking any vertex at distance > 0 .

Inductive Hypothesis: Assume that BFS marks all vertices at distance (i− 1)

before marking any vertex at distance > (i− 1).

When BFS marks a vertex at distance (i− 1),it puts all its neighbours in the queue.

Lemma In BFS, the vertices are marked in

distance order (smallest first)

Proof by induction (on the distance from s)

v is u’s neighbour iff

(u, v) ∈ E

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 179: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

This is why it’s called

Breadth-First Search

Base Case: BFS marks all vertices at distance 0 from s

before marking any vertex at distance > 0 .

Inductive Hypothesis: Assume that BFS marks all vertices at distance (i− 1)

before marking any vertex at distance > (i− 1).

When BFS marks a vertex at distance (i− 1),

Immediately after marking all vertices at distance (i− 1):

it puts all its neighbours in the queue.

Lemma In BFS, the vertices are marked in

distance order (smallest first)

Proof by induction (on the distance from s)

v is u’s neighbour iff

(u, v) ∈ E

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 180: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

This is why it’s called

Breadth-First Search

Base Case: BFS marks all vertices at distance 0 from s

before marking any vertex at distance > 0 .

Inductive Hypothesis: Assume that BFS marks all vertices at distance (i− 1)

before marking any vertex at distance > (i− 1).

When BFS marks a vertex at distance (i− 1),

Immediately after marking all vertices at distance (i− 1):

it puts all its neighbours in the queue.

every vertex at distance i is in the queue.

no vertex at distance > i has been put in the queue.

Lemma In BFS, the vertices are marked in

distance order (smallest first)

Proof by induction (on the distance from s)

v is u’s neighbour iff

(u, v) ∈ E

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 181: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

This is why it’s called

Breadth-First Search

Base Case: BFS marks all vertices at distance 0 from s

before marking any vertex at distance > 0 .

Inductive Hypothesis: Assume that BFS marks all vertices at distance (i− 1)

before marking any vertex at distance > (i− 1).

When BFS marks a vertex at distance (i− 1),

Immediately after marking all vertices at distance (i− 1):

it puts all its neighbours in the queue.

every vertex at distance i is in the queue.

no vertex at distance > i has been put in the queue.

because we haven’t marked any of its neighbours

Lemma In BFS, the vertices are marked in

distance order (smallest first)

Proof by induction (on the distance from s)

(they are all at distance > (i− 1))

v is u’s neighbour iff

(u, v) ∈ E

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 182: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

This is why it’s called

Breadth-First Search

Base Case: BFS marks all vertices at distance 0 from s

before marking any vertex at distance > 0 .

Inductive Hypothesis: Assume that BFS marks all vertices at distance (i− 1)

before marking any vertex at distance > (i− 1).

When BFS marks a vertex at distance (i− 1),

Immediately after marking all vertices at distance (i− 1):

it puts all its neighbours in the queue.

every vertex at distance i is in the queue.

no vertex at distance > i has been put in the queue.

because we haven’t marked any of its neighbours

Therefore, all vertices at distance i will be marked before any vertex at distance > i

Lemma In BFS, the vertices are marked in

distance order (smallest first)

Proof by induction (on the distance from s)

(they are all at distance > (i− 1))

v is u’s neighbour iff

(u, v) ∈ E

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 183: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 184: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

we can use this to find the shortest path

from s to every other vertex in O(|V |+ |E|) time

With a little bit of book-keeping,

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 185: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

This also works for directed, unweighted graphs

we can use this to find the shortest path

from s to every other vertex in O(|V |+ |E|) time

With a little bit of book-keeping,

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 186: Depth First Search and Breadth First Search

Shortest paths in unweighted graphs

s

Goal: find the distance from the

source s to every other vertex

Breadth-First Search (BFS)

is TRAVERSE using a queue as the bag

Lemma In BFS, the vertices

are marked in distance order (smallest first)

This also works for directed, unweighted graphs

we can use this to find the shortest path

from s to every other vertex in O(|V |+ |E|) time

With a little bit of book-keeping,

It does not work for weighted graphs

TRAVERSE(s) - with a queue

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

Page 187: Depth First Search and Breadth First Search

Shortest Paths using BFS

for all v, set dist(v) =∞ (NEW!)

set dist(s) = 0 (NEW!)

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

if dist(v) =∞ (NEW!)

dist(v) = dist(u) + 1 (NEW!)

BFS(s)

dist(v) gives the distance

between s and v

We’ve added four (NEW!) lines to TRAVERSE

to track the distances from s

Page 188: Depth First Search and Breadth First Search

Shortest Paths using BFS

for all v, set dist(v) =∞ (NEW!)

set dist(s) = 0 (NEW!)

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

if dist(v) =∞ (NEW!)

dist(v) = dist(u) + 1 (NEW!)

BFS(s)

dist(v) gives the distance

between s and v

We’ve added four (NEW!) lines to TRAVERSE

to track the distances from s How does this affect the time complexity?

Page 189: Depth First Search and Breadth First Search

Shortest Paths using BFS

for all v, set dist(v) =∞ (NEW!)

set dist(s) = 0 (NEW!)

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

if dist(v) =∞ (NEW!)

dist(v) = dist(u) + 1 (NEW!)

BFS(s)

dist(v) gives the distance

between s and v

Time complexity:

We’ve added four (NEW!) lines to TRAVERSE

to track the distances from s How does this affect the time complexity?

Page 190: Depth First Search and Breadth First Search

Shortest Paths using BFS

for all v, set dist(v) =∞ (NEW!)

set dist(s) = 0 (NEW!)

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

if dist(v) =∞ (NEW!)

dist(v) = dist(u) + 1 (NEW!)

BFS(s)

dist(v) gives the distance

between s and v

Time complexity:

We’ve added four (NEW!) lines to TRAVERSE

to track the distances from s How does this affect the time complexity?

O(|V |) time to initialise an array dist

Page 191: Depth First Search and Breadth First Search

Shortest Paths using BFS

for all v, set dist(v) =∞ (NEW!)

set dist(s) = 0 (NEW!)

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

if dist(v) =∞ (NEW!)

dist(v) = dist(u) + 1 (NEW!)

BFS(s)

dist(v) gives the distance

between s and v

Time complexity:

We’ve added four (NEW!) lines to TRAVERSE

to track the distances from s How does this affect the time complexity?

O(|V |) time to initialise an array dist

O(1) time to set the distance to s

Page 192: Depth First Search and Breadth First Search

Shortest Paths using BFS

for all v, set dist(v) =∞ (NEW!)

set dist(s) = 0 (NEW!)

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

if dist(v) =∞ (NEW!)

dist(v) = dist(u) + 1 (NEW!)

BFS(s)

dist(v) gives the distance

between s and v

Time complexity:

We’ve added four (NEW!) lines to TRAVERSE

to track the distances from s How does this affect the time complexity?

O(|V |) time to initialise an array dist

O(1) time to set the distance to s

O(1) time whenever we put into the queue

(so this doesn’t change the complexity)

Page 193: Depth First Search and Breadth First Search

Shortest Paths using BFS

for all v, set dist(v) =∞ (NEW!)

set dist(s) = 0 (NEW!)

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

if dist(v) =∞ (NEW!)

dist(v) = dist(u) + 1 (NEW!)

BFS(s)

dist(v) gives the distance

between s and v

Time complexity:

We’ve added four (NEW!) lines to TRAVERSE

to track the distances from s How does this affect the time complexity?

O(|V |) time to initialise an array dist

O(1) time to set the distance to s

O(1) time whenever we put into the queue

(so this doesn’t change the complexity)

so the overall complexity is O(|V |+ |E|)

Page 194: Depth First Search and Breadth First Search

Shortest Paths using BFS

for all v, set dist(v) =∞ (NEW!)

set dist(s) = 0 (NEW!)

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

if dist(v) =∞ (NEW!)

dist(v) = dist(u) + 1 (NEW!)

BFS(s)

dist(v) gives the distance

between s and v

We’ve added four (NEW!) lines to TRAVERSE

to track the distances from s

Page 195: Depth First Search and Breadth First Search

Shortest Paths using BFS

for all v, set dist(v) =∞ (NEW!)

set dist(s) = 0 (NEW!)

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

if dist(v) =∞ (NEW!)

dist(v) = dist(u) + 1 (NEW!)

BFS(s)

dist(v) gives the distance

between s and v

We’ve added four (NEW!) lines to TRAVERSE

to track the distances from s

Why does this work?

Page 196: Depth First Search and Breadth First Search

Shortest Paths using BFS

for all v, set dist(v) =∞ (NEW!)

set dist(s) = 0 (NEW!)

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

if dist(v) =∞ (NEW!)

dist(v) = dist(u) + 1 (NEW!)

BFS(s)

dist(v) gives the distance

between s and v

We’ve added four (NEW!) lines to TRAVERSE

to track the distances from s

Why does this work?

Lemma In BFS, the vertices are marked in

distance order (smallest first)

Page 197: Depth First Search and Breadth First Search

Shortest Paths using BFS

for all v, set dist(v) =∞ (NEW!)

set dist(s) = 0 (NEW!)

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

if dist(v) =∞ (NEW!)

dist(v) = dist(u) + 1 (NEW!)

BFS(s)

dist(v) gives the distance

between s and v

We’ve added four (NEW!) lines to TRAVERSE

to track the distances from s

Why does this work?

Correctness sketch: (using the Lemma)

Lemma In BFS, the vertices are marked in

distance order (smallest first)

Page 198: Depth First Search and Breadth First Search

Shortest Paths using BFS

for all v, set dist(v) =∞ (NEW!)

set dist(s) = 0 (NEW!)

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

if dist(v) =∞ (NEW!)

dist(v) = dist(u) + 1 (NEW!)

BFS(s)

dist(v) gives the distance

between s and v

We’ve added four (NEW!) lines to TRAVERSE

to track the distances from s

Why does this work?

Correctness sketch: (using the Lemma)

For each v, dist(v) is set when it is first ‘discovered’

and inserted into the queue. (and it never changes)

Lemma In BFS, the vertices are marked in

distance order (smallest first)

Page 199: Depth First Search and Breadth First Search

Shortest Paths using BFS

for all v, set dist(v) =∞ (NEW!)

set dist(s) = 0 (NEW!)

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

if dist(v) =∞ (NEW!)

dist(v) = dist(u) + 1 (NEW!)

BFS(s)

dist(v) gives the distance

between s and v

We’ve added four (NEW!) lines to TRAVERSE

to track the distances from s

Why does this work?

Correctness sketch: (using the Lemma)

For each v, dist(v) is set when it is first ‘discovered’

and inserted into the queue. (and it never changes)

Let u be the vertex which first ‘discovered’ v

Lemma In BFS, the vertices are marked in

distance order (smallest first)

Page 200: Depth First Search and Breadth First Search

Shortest Paths using BFS

for all v, set dist(v) =∞ (NEW!)

set dist(s) = 0 (NEW!)

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

if dist(v) =∞ (NEW!)

dist(v) = dist(u) + 1 (NEW!)

BFS(s)

dist(v) gives the distance

between s and v

We’ve added four (NEW!) lines to TRAVERSE

to track the distances from s

Why does this work?

Correctness sketch: (using the Lemma)

For each v, dist(v) is set when it is first ‘discovered’

and inserted into the queue. (and it never changes)

Let u be the vertex which first ‘discovered’ v

Lemma In BFS, the vertices are marked in

distance order (smallest first)

BFS sets dist(v) = dist(u) + 1

Page 201: Depth First Search and Breadth First Search

Shortest Paths using BFS

for all v, set dist(v) =∞ (NEW!)

set dist(s) = 0 (NEW!)

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

if dist(v) =∞ (NEW!)

dist(v) = dist(u) + 1 (NEW!)

BFS(s)

dist(v) gives the distance

between s and v

We’ve added four (NEW!) lines to TRAVERSE

to track the distances from s

Why does this work?

Correctness sketch: (using the Lemma)

For each v, dist(v) is set when it is first ‘discovered’

and inserted into the queue. (and it never changes)

Let u be the vertex which first ‘discovered’ v

Lemma In BFS, the vertices are marked in

distance order (smallest first)

Assume for a contradiction that there is a path

from s to v with length < dist(u) + 1

BFS sets dist(v) = dist(u) + 1

Page 202: Depth First Search and Breadth First Search

Shortest Paths using BFS

for all v, set dist(v) =∞ (NEW!)

set dist(s) = 0 (NEW!)

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

if dist(v) =∞ (NEW!)

dist(v) = dist(u) + 1 (NEW!)

BFS(s)

dist(v) gives the distance

between s and v

We’ve added four (NEW!) lines to TRAVERSE

to track the distances from s

Why does this work?

Correctness sketch: (using the Lemma)

For each v, dist(v) is set when it is first ‘discovered’

and inserted into the queue. (and it never changes)

Let u be the vertex which first ‘discovered’ v

Lemma In BFS, the vertices are marked in

distance order (smallest first)

Assume for a contradiction that there is a path

from s to v with length < dist(u) + 1

BFS sets dist(v) = dist(u) + 1

length < dist(u) + 1

sv

Page 203: Depth First Search and Breadth First Search

Shortest Paths using BFS

for all v, set dist(v) =∞ (NEW!)

set dist(s) = 0 (NEW!)

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

if dist(v) =∞ (NEW!)

dist(v) = dist(u) + 1 (NEW!)

BFS(s)

dist(v) gives the distance

between s and v

We’ve added four (NEW!) lines to TRAVERSE

to track the distances from s

Why does this work?

Correctness sketch: (using the Lemma)

For each v, dist(v) is set when it is first ‘discovered’

and inserted into the queue. (and it never changes)

Let u be the vertex which first ‘discovered’ v

Lemma In BFS, the vertices are marked in

distance order (smallest first)

Let x be the previous vertex on this path

Assume for a contradiction that there is a path

from s to v with length < dist(u) + 1

BFS sets dist(v) = dist(u) + 1

length < dist(u) + 1

sv

Page 204: Depth First Search and Breadth First Search

Shortest Paths using BFS

for all v, set dist(v) =∞ (NEW!)

set dist(s) = 0 (NEW!)

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

if dist(v) =∞ (NEW!)

dist(v) = dist(u) + 1 (NEW!)

BFS(s)

dist(v) gives the distance

between s and v

We’ve added four (NEW!) lines to TRAVERSE

to track the distances from s

Why does this work?

Correctness sketch: (using the Lemma)

For each v, dist(v) is set when it is first ‘discovered’

and inserted into the queue. (and it never changes)

Let u be the vertex which first ‘discovered’ v

Lemma In BFS, the vertices are marked in

distance order (smallest first)

Let x be the previous vertex on this path

Assume for a contradiction that there is a path

from s to v with length < dist(u) + 1

BFS sets dist(v) = dist(u) + 1

length < dist(u) + 1

sv

x

Page 205: Depth First Search and Breadth First Search

Shortest Paths using BFS

for all v, set dist(v) =∞ (NEW!)

set dist(s) = 0 (NEW!)

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

if dist(v) =∞ (NEW!)

dist(v) = dist(u) + 1 (NEW!)

BFS(s)

dist(v) gives the distance

between s and v

We’ve added four (NEW!) lines to TRAVERSE

to track the distances from s

Why does this work?

Correctness sketch: (using the Lemma)

For each v, dist(v) is set when it is first ‘discovered’

and inserted into the queue. (and it never changes)

Let u be the vertex which first ‘discovered’ v

Lemma In BFS, the vertices are marked in

distance order (smallest first)

Let x be the previous vertex on this path

Assume for a contradiction that there is a path

from s to v with length < dist(u) + 1

BFS sets dist(v) = dist(u) + 1

length < dist(u) length = 1

sv

x

Page 206: Depth First Search and Breadth First Search

Shortest Paths using BFS

for all v, set dist(v) =∞ (NEW!)

set dist(s) = 0 (NEW!)

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

if dist(v) =∞ (NEW!)

dist(v) = dist(u) + 1 (NEW!)

BFS(s)

dist(v) gives the distance

between s and v

We’ve added four (NEW!) lines to TRAVERSE

to track the distances from s

Why does this work?

Correctness sketch: (using the Lemma)

For each v, dist(v) is set when it is first ‘discovered’

and inserted into the queue. (and it never changes)

Let u be the vertex which first ‘discovered’ v

Lemma In BFS, the vertices are marked in

distance order (smallest first)

Let x be the previous vertex on this path

Assume for a contradiction that there is a path

from s to v with length < dist(u) + 1

BFS sets dist(v) = dist(u) + 1

length < dist(u) length = 1

sv

x

Page 207: Depth First Search and Breadth First Search

Shortest Paths using BFS

for all v, set dist(v) =∞ (NEW!)

set dist(s) = 0 (NEW!)

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

if dist(v) =∞ (NEW!)

dist(v) = dist(u) + 1 (NEW!)

BFS(s)

dist(v) gives the distance

between s and v

We’ve added four (NEW!) lines to TRAVERSE

to track the distances from s

Why does this work?

Correctness sketch: (using the Lemma)

For each v, dist(v) is set when it is first ‘discovered’

and inserted into the queue. (and it never changes)

Let u be the vertex which first ‘discovered’ v

Lemma In BFS, the vertices are marked in

distance order (smallest first)

Let x be the previous vertex on this path

Assume for a contradiction that there is a path

from s to v with length < dist(u) + 1

x has distance < dist(u) so was marked before u

(by the Lemma) and ‘discovered’ v first

BFS sets dist(v) = dist(u) + 1

length < dist(u) length = 1

sv

x

Page 208: Depth First Search and Breadth First Search

Shortest Paths using BFS

for all v, set dist(v) =∞ (NEW!)

set dist(s) = 0 (NEW!)

put s into the queue

while the queue is not empty

take u from the queue

if u unmarked

mark u

for every edge (u, v)

put v into the queue

if dist(v) =∞ (NEW!)

dist(v) = dist(u) + 1 (NEW!)

BFS(s)

dist(v) gives the distance

between s and v

We’ve added four (NEW!) lines to TRAVERSE

to track the distances from s

Why does this work?

Correctness sketch: (using the Lemma)

For each v, dist(v) is set when it is first ‘discovered’

and inserted into the queue. (and it never changes)

Let u be the vertex which first ‘discovered’ v

Lemma In BFS, the vertices are marked in

distance order (smallest first)

Let x be the previous vertex on this path

Assume for a contradiction that there is a path

from s to v with length < dist(u) + 1

x has distance < dist(u) so was marked before u

(by the Lemma) and ‘discovered’ v first

Contradiction!

BFS sets dist(v) = dist(u) + 1

length < dist(u) length = 1

sv

x

Page 209: Depth First Search and Breadth First Search

Conclusion

Depth First Search (DFS)Breadth First Search (BFS)

- the traversal order depends on the type of bag

TRAVERSE visits every vertex in a connected graph in O(|E|) time

with a Queue the algorithm is called with a Stack the algorithm is called

Applications Applications

Shortest paths in unweighted graphs

Max-Flow

Testing whether a graph is bipartite

Finding (strongly) connected components

Topicologically sorting a Directed Acyclic Graph

Testing for planarity

(with an O(1) time bag)

take O(|V |+ |E|) time using BFS

Question

What does TRAVERSE do on an

unconnected graph?(works for directed graphs too)

(and it works for directed graphs too)

V is the set of vertices|V | is the number of vertices

E is the set of edges|E| is the number of edges