depth first search and breadth first search
TRANSCRIPT
![Page 1: Depth First Search and Breadth First Search](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/1.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/2.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/3.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/4.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/5.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/6.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/7.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/8.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/9.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/10.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/11.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/12.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/13.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/14.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/15.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/16.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/17.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/18.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/19.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/20.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/21.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/22.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/23.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/24.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/25.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/26.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/27.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/28.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/29.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/30.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/31.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/32.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/33.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/34.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/35.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/36.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/37.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/38.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/39.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/40.jpg)
Intersection Graphs
![Page 41: Depth First Search and Breadth First Search](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/41.jpg)
Intersection Graphs
the intersections of these circles
correspond to the graph below
![Page 42: Depth First Search and Breadth First Search](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/42.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/43.jpg)
Intersection Graphs
the intersections of these circles
correspond to the graph below
![Page 44: Depth First Search and Breadth First Search](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/44.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/45.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/46.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/47.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/48.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/49.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/50.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/51.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/52.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/53.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/54.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/55.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/56.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/57.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/58.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/59.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/60.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/61.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/62.jpg)
Exploring a graph
you are here
BAG
![Page 63: Depth First Search and Breadth First Search](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/63.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/64.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/65.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/66.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/67.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/68.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/69.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/70.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/71.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/72.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/73.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/74.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/75.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/76.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/77.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/78.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/79.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/80.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/81.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/82.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/83.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/84.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/85.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/86.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/87.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/88.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/89.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/90.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/91.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/92.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/93.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/94.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/95.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/96.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/97.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/98.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/99.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/100.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/101.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/102.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/103.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/104.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/105.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/106.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/107.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/108.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/109.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/110.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/111.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/112.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/113.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/114.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/115.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/116.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/117.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/118.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/119.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/120.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/121.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/122.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/123.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/124.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/125.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/126.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/127.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/128.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/129.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/130.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/131.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/132.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/133.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/134.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/135.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/136.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/137.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/138.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/139.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/140.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/141.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/142.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/143.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/144.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/145.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/146.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/147.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/148.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/149.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/150.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/151.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/152.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/153.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/154.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/155.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/156.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/157.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/158.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/159.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/160.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/161.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/162.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/163.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/164.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/165.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/166.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/167.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/168.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/169.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/170.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/171.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/172.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/173.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/174.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/175.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/176.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/177.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/178.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/179.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/180.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/181.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/182.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/183.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/184.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/185.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/186.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/187.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/188.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/189.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/190.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/191.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/192.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/193.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/194.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/195.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/196.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/197.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/198.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/199.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/200.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/201.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/202.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/203.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/204.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/205.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/206.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/207.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/208.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/58eee3181a28abc72e8b458f/html5/thumbnails/209.jpg)
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