SlideShare a Scribd company logo
Data Structures and Algorithms – COMS21103
Representing and Exploring Graphs
Depth First Search and Breadth First Search
Benjamin Sach
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
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
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’re
focused on
these graphs
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’re
focused on
these graphs
But it all works for
these graphs too
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
FROM
TO
8
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 |
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
FROM
TO
8
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 |
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
FROM
TO
8
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 |
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
FROM
TO
8
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 |
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
FROM
TO
8
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 |
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
3
8
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
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
3
8
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
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
3
8
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
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
3
8
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
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
3
8
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
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
3
8
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
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
1
2
3
4
5
1
1
2
2
3
2
3
3
4
2
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
1
2
3
4
5
1
1
2
2
3
2
3
3
4
2
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
1
2
3
4
5
1
1
2
2
3
2
3
3
4
2
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
1
2
3
4
5
1
1
2
2
3
2
3
3
4
2
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
1
2
3
4
5
1
1
2
2
3
2
3
3
4
2
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
1
2
3
4
5
1
1
2
2
3
2
3
3
4
2
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
1
2
3
4
5
1
1
2
2
3
2
3
3
4
2
some vertex u
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
1
2
3
4
5
1
1
2
2
3
2
3
3
4
2
some vertex u
deg(u) = 8
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
1
2
3
4
5
1
1
2
2
3
2
3
3
4
2
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
1
2
3
4
5
1
1
2
2
3
2
3
3
4
2
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
1
2
3
4
5
1
1
2
2
3
2
3
3
4
2
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
1
2
3
4
5
1
1
2
2
3
2
3
3
4
2
Adjacency List
(using hash tables)
1
2
3
4
5
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)
1
2
3
4
5
1
1
2
2
3
2
3
3
4
2
Adjacency List
(using hash tables)
1
2
3
4
5
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)
1
2
3
4
5
1
1
2
2
3
2
3
3
4
2
Adjacency List
(using hash tables)
1
2
3
4
5
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 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
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)
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
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 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)
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.
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...
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.
Intersection Graphs
Intersection Graphs
the intersections of these circles
correspond to the graph below
Intersection Graphs
each circle is a vertex
the intersections of these circles
correspond to the graph below
Intersection Graphs
the intersections of these circles
correspond to the graph below
Intersection Graphs
each intersection
is an edge
the intersections of these circles
correspond to the graph below
Intersection Graphs
each intersection
is an edge
the intersections of these circles
correspond to the graph below
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Exploring a graph
you are here
BAG
Exploring a graph
you are here
BAG Goal: visit every vertex in a
connected graph efficiently
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
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
Exploring a graph
you are here
BAG Goal: visit every vertex in a
connected graph efficiently
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
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
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
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
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
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
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
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
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!
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
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
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
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)
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
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
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
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
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
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
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
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. . .
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. . . ?
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)
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)
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?
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?
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?
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)
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!
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’ 2|E|
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’ 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
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’ 2|E|
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’ 2|E|
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)
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?
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. How fast is it? (in the worst case)
Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. How fast is it? (in the worst case)
Assumption
(we’ll come back to this)
bag operations
take O(1) time
Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. 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
Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. 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
Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. 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
Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. 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
Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. 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
Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. 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
Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. 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
Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. 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
Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. 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
Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. 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
Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. 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
Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. 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
Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. 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
Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. 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
Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. 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
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
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)
Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
distance = 1
Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
distance = 3
Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
distance = 6
Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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?
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?
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
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
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)
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|)
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
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?
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)
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)
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)
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)
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
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
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
s
v
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
s
v
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
s
v
x
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
s
v
x
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
s
v
x
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
s
v
x
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
s
v
x
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

More Related Content

What's hot

Bfs & dfs application
Bfs & dfs applicationBfs & dfs application
Bfs & dfs application
Umme habiba
 
BFS
BFSBFS
Graph Algorithms: Breadth-First Search (BFS)
Graph Algorithms: Breadth-First Search (BFS)Graph Algorithms: Breadth-First Search (BFS)
Graph Algorithms: Breadth-First Search (BFS)
Md. Shafiuzzaman Hira
 
Breadth First Search (BFS)
Breadth First Search (BFS)Breadth First Search (BFS)
Breadth First Search (BFS)
Dhrumil Panchal
 
Breadth first search and depth first search
Breadth first search and  depth first searchBreadth first search and  depth first search
Breadth first search and depth first search
Hossain Md Shakhawat
 
Depth firstsearchalgorithm
Depth firstsearchalgorithmDepth firstsearchalgorithm
Depth firstsearchalgorithm
hansa khan
 
130210107039 2130702
130210107039 2130702130210107039 2130702
130210107039 2130702
Ketaki_Pattani
 
BFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal AlgorithmBFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal Algorithm
MSA Technosoft
 
Bfs dfs
Bfs dfsBfs dfs
Bfs dfs
Praveen Yadav
 
Skiena algorithm 2007 lecture11 breadth deapth first search
Skiena algorithm 2007 lecture11 breadth deapth first searchSkiena algorithm 2007 lecture11 breadth deapth first search
Skiena algorithm 2007 lecture11 breadth deapth first search
zukun
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
Masud Parvaze
 
Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]
Muhammad Hammad Waseem
 
Depth First Search ( DFS )
Depth First Search ( DFS )Depth First Search ( DFS )
Depth First Search ( DFS )
Sazzad Hossain
 
2.5 graph dfs
2.5 graph dfs2.5 graph dfs
2.5 graph dfs
Krish_ver2
 
Problem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsProblem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - Graphs
Yi-Lung Tsai
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
Shuvongkor Barman
 
Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]
DEEPIKA T
 
Graphs
GraphsGraphs
18 Basic Graph Algorithms
18 Basic Graph Algorithms18 Basic Graph Algorithms
18 Basic Graph Algorithms
Andres Mendez-Vazquez
 
Graphss
GraphssGraphss
Graphss
fika sweety
 

What's hot (20)

Bfs & dfs application
Bfs & dfs applicationBfs & dfs application
Bfs & dfs application
 
BFS
BFSBFS
BFS
 
Graph Algorithms: Breadth-First Search (BFS)
Graph Algorithms: Breadth-First Search (BFS)Graph Algorithms: Breadth-First Search (BFS)
Graph Algorithms: Breadth-First Search (BFS)
 
Breadth First Search (BFS)
Breadth First Search (BFS)Breadth First Search (BFS)
Breadth First Search (BFS)
 
Breadth first search and depth first search
Breadth first search and  depth first searchBreadth first search and  depth first search
Breadth first search and depth first search
 
Depth firstsearchalgorithm
Depth firstsearchalgorithmDepth firstsearchalgorithm
Depth firstsearchalgorithm
 
130210107039 2130702
130210107039 2130702130210107039 2130702
130210107039 2130702
 
BFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal AlgorithmBFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal Algorithm
 
Bfs dfs
Bfs dfsBfs dfs
Bfs dfs
 
Skiena algorithm 2007 lecture11 breadth deapth first search
Skiena algorithm 2007 lecture11 breadth deapth first searchSkiena algorithm 2007 lecture11 breadth deapth first search
Skiena algorithm 2007 lecture11 breadth deapth first search
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
 
Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]
 
Depth First Search ( DFS )
Depth First Search ( DFS )Depth First Search ( DFS )
Depth First Search ( DFS )
 
2.5 graph dfs
2.5 graph dfs2.5 graph dfs
2.5 graph dfs
 
Problem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsProblem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - Graphs
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
 
Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]
 
Graphs
GraphsGraphs
Graphs
 
18 Basic Graph Algorithms
18 Basic Graph Algorithms18 Basic Graph Algorithms
18 Basic Graph Algorithms
 
Graphss
GraphssGraphss
Graphss
 

Similar to Depth First Search and Breadth First Search

Graph
GraphGraph
Graph representation
Graph representationGraph representation
Graph representation
Amit Kumar Rathi
 
Graphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ programGraphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ program
Muhammad Danish Badar
 
Graph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptxGraph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptx
bashirabdullah789
 
Graph theory introduction
Graph theory introductionGraph theory introduction
Graph theory introduction
Sagar Khairnar
 
Graphs
GraphsGraphs
Graphs
Dwight Sabio
 
CS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on GraphsCS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on Graphs
ssuser034ce1
 
CS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on GraphsCS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on Graphs
ssuser034ce1
 
Graph theory
Graph theoryGraph theory
Graph theory
Jeane Paguio
 
Graphs Algorithms
Graphs AlgorithmsGraphs Algorithms
Graphs Algorithms
Kasun Ranga Wijeweera
 
Introduction to Graph Theory
Introduction to Graph TheoryIntroduction to Graph Theory
Introduction to Graph Theory
Premsankar Chakkingal
 
Graphs
GraphsGraphs
Graphs
amudha arul
 
graph_theory_1-11.pdf___________________
graph_theory_1-11.pdf___________________graph_theory_1-11.pdf___________________
graph_theory_1-11.pdf___________________
ssuser1989da
 
topologicalsort-using c++ as development language.pptx
topologicalsort-using c++ as development language.pptxtopologicalsort-using c++ as development language.pptx
topologicalsort-using c++ as development language.pptx
janafridi251
 
Graph algorithms
Graph algorithmsGraph algorithms
Graph algorithms
University of Haripur
 
Topological sort
Topological sortTopological sort
Topological sort
jabishah
 
Graphs.pptx
Graphs.pptxGraphs.pptx
Graphs.pptx
satvikkushwaha1
 
Unit 2: All
Unit 2: AllUnit 2: All
Unit 2: All
Hector Zenil
 
05 graph
05 graph05 graph
05 graph
Pankaj Prateek
 
graph.ppt
graph.pptgraph.ppt

Similar to Depth First Search and Breadth First Search (20)

Graph
GraphGraph
Graph
 
Graph representation
Graph representationGraph representation
Graph representation
 
Graphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ programGraphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ program
 
Graph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptxGraph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptx
 
Graph theory introduction
Graph theory introductionGraph theory introduction
Graph theory introduction
 
Graphs
GraphsGraphs
Graphs
 
CS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on GraphsCS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on Graphs
 
CS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on GraphsCS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on Graphs
 
Graph theory
Graph theoryGraph theory
Graph theory
 
Graphs Algorithms
Graphs AlgorithmsGraphs Algorithms
Graphs Algorithms
 
Introduction to Graph Theory
Introduction to Graph TheoryIntroduction to Graph Theory
Introduction to Graph Theory
 
Graphs
GraphsGraphs
Graphs
 
graph_theory_1-11.pdf___________________
graph_theory_1-11.pdf___________________graph_theory_1-11.pdf___________________
graph_theory_1-11.pdf___________________
 
topologicalsort-using c++ as development language.pptx
topologicalsort-using c++ as development language.pptxtopologicalsort-using c++ as development language.pptx
topologicalsort-using c++ as development language.pptx
 
Graph algorithms
Graph algorithmsGraph algorithms
Graph algorithms
 
Topological sort
Topological sortTopological sort
Topological sort
 
Graphs.pptx
Graphs.pptxGraphs.pptx
Graphs.pptx
 
Unit 2: All
Unit 2: AllUnit 2: All
Unit 2: All
 
05 graph
05 graph05 graph
05 graph
 
graph.ppt
graph.pptgraph.ppt
graph.ppt
 

More from Benjamin Sach

Approximation Algorithms Part Four: APTAS
Approximation Algorithms Part Four: APTASApproximation Algorithms Part Four: APTAS
Approximation Algorithms Part Four: APTAS
Benjamin Sach
 
Approximation Algorithms Part Three: (F)PTAS
Approximation Algorithms Part Three: (F)PTASApproximation Algorithms Part Three: (F)PTAS
Approximation Algorithms Part Three: (F)PTAS
Benjamin Sach
 
Approximation Algorithms Part Two: More Constant factor approximations
Approximation Algorithms Part Two: More Constant factor approximationsApproximation Algorithms Part Two: More Constant factor approximations
Approximation Algorithms Part Two: More Constant factor approximations
Benjamin Sach
 
Approximation Algorithms Part One: Constant factor approximations
Approximation Algorithms Part One: Constant factor approximationsApproximation Algorithms Part One: Constant factor approximations
Approximation Algorithms Part One: Constant factor approximations
Benjamin Sach
 
van Emde Boas trees
van Emde Boas treesvan Emde Boas trees
van Emde Boas trees
Benjamin Sach
 
Orthogonal Range Searching
Orthogonal Range SearchingOrthogonal Range Searching
Orthogonal Range Searching
Benjamin Sach
 
Pattern Matching Part Two: k-mismatches
Pattern Matching Part Two: k-mismatchesPattern Matching Part Two: k-mismatches
Pattern Matching Part Two: k-mismatches
Benjamin Sach
 
Pattern Matching Part Three: Hamming Distance
Pattern Matching Part Three: Hamming DistancePattern Matching Part Three: Hamming Distance
Pattern Matching Part Three: Hamming Distance
Benjamin Sach
 
Lowest Common Ancestor
Lowest Common AncestorLowest Common Ancestor
Lowest Common Ancestor
Benjamin Sach
 
Range Minimum Queries
Range Minimum QueriesRange Minimum Queries
Range Minimum Queries
Benjamin Sach
 
Pattern Matching Part Two: Suffix Arrays
Pattern Matching Part Two: Suffix ArraysPattern Matching Part Two: Suffix Arrays
Pattern Matching Part Two: Suffix Arrays
Benjamin Sach
 
Pattern Matching Part One: Suffix Trees
Pattern Matching Part One: Suffix TreesPattern Matching Part One: Suffix Trees
Pattern Matching Part One: Suffix Trees
Benjamin Sach
 
Hashing Part Two: Cuckoo Hashing
Hashing Part Two: Cuckoo HashingHashing Part Two: Cuckoo Hashing
Hashing Part Two: Cuckoo Hashing
Benjamin Sach
 
Hashing Part Two: Static Perfect Hashing
Hashing Part Two: Static Perfect HashingHashing Part Two: Static Perfect Hashing
Hashing Part Two: Static Perfect Hashing
Benjamin Sach
 
Hashing Part One
Hashing Part OneHashing Part One
Hashing Part One
Benjamin Sach
 
Probability Recap
Probability RecapProbability Recap
Probability Recap
Benjamin Sach
 
Bloom Filters
Bloom FiltersBloom Filters
Bloom Filters
Benjamin Sach
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
Benjamin Sach
 
Minimum Spanning Trees (via Disjoint Sets)
Minimum Spanning Trees (via Disjoint Sets)Minimum Spanning Trees (via Disjoint Sets)
Minimum Spanning Trees (via Disjoint Sets)
Benjamin Sach
 
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's AlgorithmShortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Benjamin Sach
 

More from Benjamin Sach (20)

Approximation Algorithms Part Four: APTAS
Approximation Algorithms Part Four: APTASApproximation Algorithms Part Four: APTAS
Approximation Algorithms Part Four: APTAS
 
Approximation Algorithms Part Three: (F)PTAS
Approximation Algorithms Part Three: (F)PTASApproximation Algorithms Part Three: (F)PTAS
Approximation Algorithms Part Three: (F)PTAS
 
Approximation Algorithms Part Two: More Constant factor approximations
Approximation Algorithms Part Two: More Constant factor approximationsApproximation Algorithms Part Two: More Constant factor approximations
Approximation Algorithms Part Two: More Constant factor approximations
 
Approximation Algorithms Part One: Constant factor approximations
Approximation Algorithms Part One: Constant factor approximationsApproximation Algorithms Part One: Constant factor approximations
Approximation Algorithms Part One: Constant factor approximations
 
van Emde Boas trees
van Emde Boas treesvan Emde Boas trees
van Emde Boas trees
 
Orthogonal Range Searching
Orthogonal Range SearchingOrthogonal Range Searching
Orthogonal Range Searching
 
Pattern Matching Part Two: k-mismatches
Pattern Matching Part Two: k-mismatchesPattern Matching Part Two: k-mismatches
Pattern Matching Part Two: k-mismatches
 
Pattern Matching Part Three: Hamming Distance
Pattern Matching Part Three: Hamming DistancePattern Matching Part Three: Hamming Distance
Pattern Matching Part Three: Hamming Distance
 
Lowest Common Ancestor
Lowest Common AncestorLowest Common Ancestor
Lowest Common Ancestor
 
Range Minimum Queries
Range Minimum QueriesRange Minimum Queries
Range Minimum Queries
 
Pattern Matching Part Two: Suffix Arrays
Pattern Matching Part Two: Suffix ArraysPattern Matching Part Two: Suffix Arrays
Pattern Matching Part Two: Suffix Arrays
 
Pattern Matching Part One: Suffix Trees
Pattern Matching Part One: Suffix TreesPattern Matching Part One: Suffix Trees
Pattern Matching Part One: Suffix Trees
 
Hashing Part Two: Cuckoo Hashing
Hashing Part Two: Cuckoo HashingHashing Part Two: Cuckoo Hashing
Hashing Part Two: Cuckoo Hashing
 
Hashing Part Two: Static Perfect Hashing
Hashing Part Two: Static Perfect HashingHashing Part Two: Static Perfect Hashing
Hashing Part Two: Static Perfect Hashing
 
Hashing Part One
Hashing Part OneHashing Part One
Hashing Part One
 
Probability Recap
Probability RecapProbability Recap
Probability Recap
 
Bloom Filters
Bloom FiltersBloom Filters
Bloom Filters
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
Minimum Spanning Trees (via Disjoint Sets)
Minimum Spanning Trees (via Disjoint Sets)Minimum Spanning Trees (via Disjoint Sets)
Minimum Spanning Trees (via Disjoint Sets)
 
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's AlgorithmShortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
 

Recently uploaded

220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx
Kalna College
 
CIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdfCIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdf
blueshagoo1
 
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
EduSkills OECD
 
Simple-Present-Tense xxxxxxxxxxxxxxxxxxx
Simple-Present-Tense xxxxxxxxxxxxxxxxxxxSimple-Present-Tense xxxxxxxxxxxxxxxxxxx
Simple-Present-Tense xxxxxxxxxxxxxxxxxxx
RandolphRadicy
 
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptxRESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
zuzanka
 
SWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptxSWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptx
zuzanka
 
Electric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger HuntElectric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger Hunt
RamseyBerglund
 
How to Manage Reception Report in Odoo 17
How to Manage Reception Report in Odoo 17How to Manage Reception Report in Odoo 17
How to Manage Reception Report in Odoo 17
Celine George
 
220711130083 SUBHASHREE RAKSHIT Internet resources for social science
220711130083 SUBHASHREE RAKSHIT  Internet resources for social science220711130083 SUBHASHREE RAKSHIT  Internet resources for social science
220711130083 SUBHASHREE RAKSHIT Internet resources for social science
Kalna College
 
Juneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School DistrictJuneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School District
David Douglas School District
 
How to Download & Install Module From the Odoo App Store in Odoo 17
How to Download & Install Module From the Odoo App Store in Odoo 17How to Download & Install Module From the Odoo App Store in Odoo 17
How to Download & Install Module From the Odoo App Store in Odoo 17
Celine George
 
CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...
Nguyen Thanh Tu Collection
 
Standardized tool for Intelligence test.
Standardized tool for Intelligence test.Standardized tool for Intelligence test.
Standardized tool for Intelligence test.
deepaannamalai16
 
NIPER 2024 MEMORY BASED QUESTIONS.ANSWERS TO NIPER 2024 QUESTIONS.NIPER JEE 2...
NIPER 2024 MEMORY BASED QUESTIONS.ANSWERS TO NIPER 2024 QUESTIONS.NIPER JEE 2...NIPER 2024 MEMORY BASED QUESTIONS.ANSWERS TO NIPER 2024 QUESTIONS.NIPER JEE 2...
NIPER 2024 MEMORY BASED QUESTIONS.ANSWERS TO NIPER 2024 QUESTIONS.NIPER JEE 2...
Payaamvohra1
 
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptxBIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
RidwanHassanYusuf
 
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptxCapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapitolTechU
 
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
TechSoup
 
Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)
nitinpv4ai
 
Bonku-Babus-Friend by Sathyajith Ray (9)
Bonku-Babus-Friend by Sathyajith Ray  (9)Bonku-Babus-Friend by Sathyajith Ray  (9)
Bonku-Babus-Friend by Sathyajith Ray (9)
nitinpv4ai
 
Observational Learning
Observational Learning Observational Learning
Observational Learning
sanamushtaq922
 

Recently uploaded (20)

220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx
 
CIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdfCIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdf
 
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
 
Simple-Present-Tense xxxxxxxxxxxxxxxxxxx
Simple-Present-Tense xxxxxxxxxxxxxxxxxxxSimple-Present-Tense xxxxxxxxxxxxxxxxxxx
Simple-Present-Tense xxxxxxxxxxxxxxxxxxx
 
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptxRESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
 
SWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptxSWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptx
 
Electric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger HuntElectric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger Hunt
 
How to Manage Reception Report in Odoo 17
How to Manage Reception Report in Odoo 17How to Manage Reception Report in Odoo 17
How to Manage Reception Report in Odoo 17
 
220711130083 SUBHASHREE RAKSHIT Internet resources for social science
220711130083 SUBHASHREE RAKSHIT  Internet resources for social science220711130083 SUBHASHREE RAKSHIT  Internet resources for social science
220711130083 SUBHASHREE RAKSHIT Internet resources for social science
 
Juneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School DistrictJuneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School District
 
How to Download & Install Module From the Odoo App Store in Odoo 17
How to Download & Install Module From the Odoo App Store in Odoo 17How to Download & Install Module From the Odoo App Store in Odoo 17
How to Download & Install Module From the Odoo App Store in Odoo 17
 
CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...
 
Standardized tool for Intelligence test.
Standardized tool for Intelligence test.Standardized tool for Intelligence test.
Standardized tool for Intelligence test.
 
NIPER 2024 MEMORY BASED QUESTIONS.ANSWERS TO NIPER 2024 QUESTIONS.NIPER JEE 2...
NIPER 2024 MEMORY BASED QUESTIONS.ANSWERS TO NIPER 2024 QUESTIONS.NIPER JEE 2...NIPER 2024 MEMORY BASED QUESTIONS.ANSWERS TO NIPER 2024 QUESTIONS.NIPER JEE 2...
NIPER 2024 MEMORY BASED QUESTIONS.ANSWERS TO NIPER 2024 QUESTIONS.NIPER JEE 2...
 
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptxBIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
 
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptxCapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
 
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
 
Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)
 
Bonku-Babus-Friend by Sathyajith Ray (9)
Bonku-Babus-Friend by Sathyajith Ray  (9)Bonku-Babus-Friend by Sathyajith Ray  (9)
Bonku-Babus-Friend by Sathyajith Ray (9)
 
Observational Learning
Observational Learning Observational Learning
Observational Learning
 

Depth First Search and Breadth First Search

  • 1. Data Structures and Algorithms – COMS21103 Representing and Exploring Graphs Depth First Search and Breadth First Search Benjamin Sach
  • 2. 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
  • 3. 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
  • 4. 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’re focused on these graphs
  • 5. 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’re focused on these graphs But it all works for these graphs too
  • 6. 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 FROM TO 8 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 |
  • 7. 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 FROM TO 8 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 |
  • 8. 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 FROM TO 8 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 |
  • 9. 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 FROM TO 8 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 |
  • 10. 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 FROM TO 8 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 |
  • 11. 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 3 8 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
  • 12. 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 3 8 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
  • 13. 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 3 8 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
  • 14. 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 3 8 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
  • 15. 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 3 8 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
  • 16. 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 3 8 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
  • 17. 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 1 2 3 4 5 1 1 2 2 3 2 3 3 4 2
  • 18. 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 1 2 3 4 5 1 1 2 2 3 2 3 3 4 2
  • 19. 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 1 2 3 4 5 1 1 2 2 3 2 3 3 4 2
  • 20. 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 1 2 3 4 5 1 1 2 2 3 2 3 3 4 2
  • 21. 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 1 2 3 4 5 1 1 2 2 3 2 3 3 4 2
  • 22. 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 1 2 3 4 5 1 1 2 2 3 2 3 3 4 2
  • 23. 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 1 2 3 4 5 1 1 2 2 3 2 3 3 4 2 some vertex u
  • 24. 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 1 2 3 4 5 1 1 2 2 3 2 3 3 4 2 some vertex u deg(u) = 8
  • 25. 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 1 2 3 4 5 1 1 2 2 3 2 3 3 4 2
  • 26. 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 1 2 3 4 5 1 1 2 2 3 2 3 3 4 2
  • 27. 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 1 2 3 4 5 1 1 2 2 3 2 3 3 4 2
  • 28. 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 1 2 3 4 5 1 1 2 2 3 2 3 3 4 2 Adjacency List (using hash tables) 1 2 3 4 5
  • 29. 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) 1 2 3 4 5 1 1 2 2 3 2 3 3 4 2 Adjacency List (using hash tables) 1 2 3 4 5
  • 30. 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) 1 2 3 4 5 1 1 2 2 3 2 3 3 4 2 Adjacency List (using hash tables) 1 2 3 4 5
  • 31. 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
  • 32. 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
  • 33. 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)
  • 34. 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
  • 35. 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
  • 36. 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)
  • 37. 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.
  • 38. 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...
  • 39. 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.
  • 41. Intersection Graphs the intersections of these circles correspond to the graph below
  • 42. Intersection Graphs each circle is a vertex the intersections of these circles correspond to the graph below
  • 43. Intersection Graphs the intersections of these circles correspond to the graph below
  • 44. Intersection Graphs each intersection is an edge the intersections of these circles correspond to the graph below
  • 45. Intersection Graphs each intersection is an edge the intersections of these circles correspond to the graph below
  • 46. 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
  • 47. 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
  • 48. 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
  • 49. 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
  • 50. 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
  • 51. 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
  • 52. 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
  • 53. 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
  • 54. 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
  • 55. 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
  • 56. 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
  • 57. 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
  • 58. 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
  • 59. 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
  • 60. 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
  • 61. 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
  • 62. Exploring a graph you are here BAG
  • 63. Exploring a graph you are here BAG Goal: visit every vertex in a connected graph efficiently
  • 64. 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
  • 65. 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
  • 66. Exploring a graph you are here BAG Goal: visit every vertex in a connected graph efficiently
  • 67. 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
  • 68. 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
  • 69. 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
  • 70. 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
  • 71. 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
  • 72. 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
  • 73. 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
  • 74. 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
  • 75. 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!
  • 76. 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
  • 77. 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
  • 78. 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
  • 79. 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)
  • 80. 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
  • 81. 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
  • 82. 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
  • 83. 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
  • 84. 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
  • 85. 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
  • 86. 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
  • 87. 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. . .
  • 88. 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. . . ?
  • 89. 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)
  • 90. 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)
  • 91. 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?
  • 92. 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?
  • 93. 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?
  • 94. 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)
  • 95. 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!
  • 96. 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’ 2|E|
  • 97. 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’ 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
  • 98. 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’ 2|E|
  • 99. 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’ 2|E|
  • 100. 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)
  • 101. 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?
  • 102. 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)
  • 103. 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)
  • 104. 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)
  • 105. 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)
  • 106. 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)
  • 107. 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)
  • 108. 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)
  • 109. 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)
  • 110. 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)
  • 111. 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)
  • 112. 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)
  • 113. 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)
  • 114. 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)
  • 115. 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)
  • 116. 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)
  • 117. 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)
  • 118. 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)
  • 119. 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)
  • 120. 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)
  • 121. 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)
  • 122. 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)
  • 123. 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)
  • 124. 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)
  • 125. 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)
  • 126. 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)
  • 127. 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)
  • 128. Exploring a graph Questions: 1. Does TRAVERSE always stop? 2. Does it always visit every vertex? Yes Yes 3. How fast is it? (in the worst case)
  • 129. Exploring a graph Questions: 1. Does TRAVERSE always stop? 2. Does it always visit every vertex? Yes Yes 3. How fast is it? (in the worst case) Assumption (we’ll come back to this) bag operations take O(1) time
  • 130. Exploring a graph Questions: 1. Does TRAVERSE always stop? 2. Does it always visit every vertex? Yes Yes 3. 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
  • 131. Exploring a graph Questions: 1. Does TRAVERSE always stop? 2. Does it always visit every vertex? Yes Yes 3. 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
  • 132. Exploring a graph Questions: 1. Does TRAVERSE always stop? 2. Does it always visit every vertex? Yes Yes 3. 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
  • 133. Exploring a graph Questions: 1. Does TRAVERSE always stop? 2. Does it always visit every vertex? Yes Yes 3. 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
  • 134. Exploring a graph Questions: 1. Does TRAVERSE always stop? 2. Does it always visit every vertex? Yes Yes 3. 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
  • 135. Exploring a graph Questions: 1. Does TRAVERSE always stop? 2. Does it always visit every vertex? Yes Yes 3. 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
  • 136. Exploring a graph Questions: 1. Does TRAVERSE always stop? 2. Does it always visit every vertex? Yes Yes 3. 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
  • 137. Exploring a graph Questions: 1. Does TRAVERSE always stop? 2. Does it always visit every vertex? Yes Yes 3. 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
  • 138. Exploring a graph Questions: 1. Does TRAVERSE always stop? 2. Does it always visit every vertex? Yes Yes 3. 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
  • 139. Exploring a graph Questions: 1. Does TRAVERSE always stop? 2. Does it always visit every vertex? Yes Yes 3. 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
  • 140. Exploring a graph Questions: 1. Does TRAVERSE always stop? 2. Does it always visit every vertex? Yes Yes 3. 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
  • 141. Exploring a graph Questions: 1. Does TRAVERSE always stop? 2. Does it always visit every vertex? Yes Yes 3. 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
  • 142. Exploring a graph Questions: 1. Does TRAVERSE always stop? 2. Does it always visit every vertex? Yes Yes 3. 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
  • 143. Exploring a graph Questions: 1. Does TRAVERSE always stop? 2. Does it always visit every vertex? Yes Yes 3. 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
  • 144. Exploring a graph Questions: 1. Does TRAVERSE always stop? 2. Does it always visit every vertex? Yes Yes 3. 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
  • 145. 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
  • 146. 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)
  • 147. Shortest paths in unweighted graphs s Goal: find the distance from the source s to every other vertex
  • 148. Shortest paths in unweighted graphs s Goal: find the distance from the source s to every other vertex distance = 1
  • 149. Shortest paths in unweighted graphs s Goal: find the distance from the source s to every other vertex distance = 3
  • 150. Shortest paths in unweighted graphs s Goal: find the distance from the source s to every other vertex distance = 6
  • 151. Shortest paths in unweighted graphs s Goal: find the distance from the source s to every other vertex
  • 152. 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
  • 153. 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
  • 154. 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
  • 155. 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
  • 156. 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
  • 157. 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
  • 158. 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
  • 159. 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
  • 160. 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
  • 161. 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
  • 162. 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
  • 163. 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
  • 164. 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
  • 165. 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
  • 166. 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
  • 167. 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
  • 168. 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
  • 169. 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
  • 170. 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
  • 171. 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
  • 172. 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
  • 173. 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
  • 174. 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
  • 175. 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
  • 176. 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
  • 177. 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
  • 178. 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
  • 179. 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
  • 180. 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
  • 181. 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
  • 182. 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
  • 183. 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
  • 184. 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
  • 185. 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
  • 186. 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
  • 187. 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
  • 188. 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?
  • 189. 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?
  • 190. 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
  • 191. 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
  • 192. 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)
  • 193. 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|)
  • 194. 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
  • 195. 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?
  • 196. 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)
  • 197. 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)
  • 198. 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)
  • 199. 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)
  • 200. 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
  • 201. 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
  • 202. 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 s v
  • 203. 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 s v
  • 204. 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 s v x
  • 205. 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 s v x
  • 206. 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 s v x
  • 207. 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 s v x
  • 208. 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 s v x
  • 209. 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