csc263 week 8toni/courses/263-2015/lectures/lec08-bfs.pdf · 7d[s]←0 8enqueue(q,s)...
TRANSCRIPT
![Page 1: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/1.jpg)
CSC263 Week 8
![Page 2: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/2.jpg)
Announcements Problem Set 4 is out! Due Tuesday (Nov 17)
![Page 3: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/3.jpg)
Other Announcements ➔ Drop date Nov 8
➔ Final exam schedule is posted CSC263 exam Dec 11, 2-5pm
![Page 4: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/4.jpg)
This week’s outline
➔ Graphs ➔ BFS
![Page 5: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/5.jpg)
A really, really important ADT that is used to model relationships between objects.
Graph
Reference: http://steve-yegge.blogspot.ca/2008/03/get-that-job-at-google.html
![Page 6: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/6.jpg)
Things that can be modelled using graphs
➔ Web ➔ Facebook ➔ Task scheduling ➔ Maps & GPS ➔ Compiler (garbage collection) ➔ OCR (computer vision) ➔ Database ➔ Rubik’s cube ➔ …. (many many other things)
![Page 7: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/7.jpg)
Definition
G = (V, E) Set of vertices e.g., {a, b, c}
Set of edges e.g., { (a, b), (c, a) }
![Page 8: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/8.jpg)
Flavours of graphs
![Page 9: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/9.jpg)
Undirected Directed
each edge is an unordered pair (u, v) = (v, u)
each edge is an ordered pair (u, v) ≠ (v, u)
![Page 10: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/10.jpg)
10 200
-3
Unweighted Weighted
![Page 11: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/11.jpg)
Simple Non-simple
No multiple edge, no self-loop
![Page 12: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/12.jpg)
Acyclic Cyclic
![Page 13: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/13.jpg)
Connected Disconnected
![Page 14: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/14.jpg)
Dense Sparse
![Page 15: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/15.jpg)
Path
Length of path = number of edges
A path of length 3
Read Appendix B.4 for more background on graphs.
![Page 16: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/16.jpg)
Operations on a graph
➔ Add a vertex; remove a vertex
➔ Add an edge; remove an edge
➔ Get neighbours (undirected graph) ◆ Neighbourhood(u): all v ∈ V such that (u, v) ∈ E
➔ Get in-neighbours / out-neighbours (directed graph)
➔ Traversal: visit every vertex in the graph
![Page 17: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/17.jpg)
Many other operations:
➔ Traversal: BFS (breadth first search)
DFS (depth first search)
➔ Given s,t find a (minimum length) path from s to t
➔ Given a connected graph G, output a spanning tree of G
➔ Is G connected?
![Page 18: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/18.jpg)
Data structures for the graph ADT
➔ Adjacency matrix ➔ Adjacency list
![Page 19: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/19.jpg)
Adjacency matrix
A |V|x|V| matrix A
![Page 20: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/20.jpg)
Adjacency matrix
1
3
4 2
1 2 3 4
1 2 3 4
1 1
1
1
0 0
0 0 0
0 0 0
0 0 0 0
![Page 21: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/21.jpg)
1 2 3 4
1 2 3 4
1 1
1
1
0 0
0 0 0
0 0 0
0 0 0 0
How much space does it take?
|V|²
Adjacency matrix
![Page 22: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/22.jpg)
Adjacency matrix (undirected graph)
1
3
4 2
1 2 3 4
1 2 3 4
1 1
1
1
0 1
1 0 0
1 0 0
1 0 0 0
The adjacency matrix of an undirected graph is _________________. symmetric
![Page 23: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/23.jpg)
1 2 3 4
1 2 3 4
1 1
1
1
0 1
1 0 0
1 0 0
1 0 0 0
Adjacency matrix (undirected graph)
How much space does it take?
|V|²
![Page 24: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/24.jpg)
Adjacency list
![Page 25: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/25.jpg)
Adjacency list (directed graph)
Each vertex vi stores a list A[i] of vj that satisfies (vi , vj) ∈ E
1
2
3
4
2 4
1
1
3
4 2
1 4 3
![Page 26: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/26.jpg)
1
2
3
4
2 4
1
1 4 3
Adjacency list (directed graph)
How much space does it take?
|V|+|E|
|V| |E|
![Page 27: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/27.jpg)
1
2
3
4
2 4
1
1 4 3
Adjacency list (directed graph)
How much space does it take?
|V|+|E|
|V| |E|
This assumes we can store the name of a vertex in one cell of the linked list. In terms of bits, the size would be more like |V| + |E| (log |V|)
![Page 28: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/28.jpg)
1
2
3
4
2 4
1
1 4 3
Adjacency list (directed graph)
How much space does it take?
|V|+|E|
|V| |E|
One often ignores these lower order factors of log n. (Recall for hashing, we assume h(x) in constant time.)
![Page 29: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/29.jpg)
Adjacency list (undirected graph)
1
3
4 2
1
2
3
4
2 4 3
1 3
2 1
1
![Page 30: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/30.jpg)
1
2
3
4
2 4 3
1 3
2 1
1
Adjacency list (undirected graph)
How much space does it take?
|V|+2|E|
|V| 2|E|
![Page 31: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/31.jpg)
Matrix versus List
In term of space complexity ➔ adjacency matrix is Θ(|V|²) ➔ adjacency list is Θ(|V|+|E|)
Which one is more space-efficient?
Adjacency list, if |E| ≪ |V|² , i.e., the graph is not very dense.
![Page 32: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/32.jpg)
Matrix versus List
Anything that Matrix does better than List?
Check whether edge (vi , vj) is in E ➔ Matrix: just check if A[i, j] = 1, O(1) ➔ List: go through list A[i] see if j is in there,
O(length of list)
![Page 33: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/33.jpg)
Takeaway
Adjacency matrix or adjacency list? Choose the more appropriate one depending on the problem.
![Page 34: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/34.jpg)
Graph Traversals
They are twins!
BFS and DFS
![Page 35: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/35.jpg)
Graph traversals
Visiting every vertex once, starting from a given vertex. The visits can follow different orders, we will learn about the following two ways ➔ Breadth First Search (BFS) ➔ Depth First Search (DFS)
![Page 36: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/36.jpg)
Intuitions of BFS and DFS
Consider a special graph -- a tree
![Page 37: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/37.jpg)
“The knowledge learning tree”
High School
College
PhD
Science
CS Physics Geology
AI DB String Theory
Black hole
Rocks Sand
Traversing this graph means learning all these subjects.
![Page 38: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/38.jpg)
The Breadth-First ways of learning these subjects ➔ Level by level, finish high school, then all subjects at
College level, then finish all subjects in PhD level.
High School
College
PhD
Science
CS Physics Geology
AI DB String Theory
Black hole
Rocks Sand
![Page 39: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/39.jpg)
The Depth-First way of learning these subjects ➔ Go towards PhD whenever possible; only start
learning physics after finishing everything in CS.
High School
College
PhD
Science
CS Physics Geology
AI DB String Theory
Black hole
Rocks Sand
![Page 40: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/40.jpg)
Now let’s seriously start studying BFS
![Page 41: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/41.jpg)
Review CSC148: BFS in a tree (starting from root) is a ______________________ traversal.
Special case: BFS in a tree
level-by-level
What ADT did we use for implementing the level-by-level traversal?
Queue!
(NOT preorder!)
![Page 42: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/42.jpg)
Special case: BFS in a tree
NOT_YET_BFS(root): Q ← Queue() Enqueue(Q, root) while Q not empty: x ← Dequeue(Q) print x for each child c of x: Enqueue(Q, c)
a
b c
d e f
a
DQ
b Queue: c d e f
DQ DQ DQ DQ DQ EMPTY!
Output: a b c d e f
![Page 43: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/43.jpg)
The real deal: BFS in a Graph
r t s
w x v
u
y
NOT_YET_BFS(root): Q ← Queue() Enqueue(Q, root) while Q not empty: x ← Dequeue(Q) print x for each neighbr c of x: Enqueue(Q, c)
If we just run NOT_YET_BFS(t) on the above graph. What problem would we have?
It would want to visit some vertices more than once (e.g., x)
![Page 44: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/44.jpg)
How avoid visiting a vertex twice
Remember you visited it by labelling it using colours. ➔ White: “unvisited” ➔ Gray: “encountered” ➔ Black: “explored”
➔ Initially all vertices are white ➔ Colour a vertex gray the first time visiting it ➔ Colour a vertex black when all its neighbours
have been encountered ➔ Avoid visiting gray or black vertices ➔ In the end, all vertices are black (sort-of)
![Page 45: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/45.jpg)
Some other values we want to remember during the traversal...
➔ pi[v]: the vertex from which v is encountered ◆ “I was introduced as whose neighbour?”
➔ d[v]: the distance value ◆ the distance from v to the source vertex of the BFS
r t s
w xv
u
y
This d[v] is going to be really useful!
![Page 46: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/46.jpg)
Pseudocode: the real BFS BFS(G=(V, E), s): 1 for all v in V: 2 colour[v] ← white 3 d[v] ← ∞ 4 pi[v] ← NIL 5 Q ← Queue() 6 colour[s] ← gray 7 d[s] ← 0 8 Enqueue(Q, s) 9 while Q not empty: 10 u ← Dequeue(Q) 11 for each neighbour v of u: 12 if colour[v] = white 13 colour[v] ← gray 14 d[v] ← d[u] + 1 15 pi[v] ← u 16 Enqueue(Q, v) 17 colour[u] ← black
The blue lines are the same as NOT_YET_BFS
# Initialize vertices
# distance from s to s is 0
# only visit unvisited vertices
# v is “1-level” farther from s than u # v is introduced as u’s neighbour
# all neighbours of u have been encountered, therefore u is explored
# source s is encountered
![Page 47: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/47.jpg)
Let’s run an example!
r t s
w x v
u
y
BFS(G, s)
![Page 48: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/48.jpg)
After initialization
r t s
w x v
u
y
∞ ∞
∞ ∞ ∞ ∞
∞ ∞
All vertices are white and have d = ∞
![Page 49: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/49.jpg)
Start by “encountering” the source
r t s
w x v
u
y
∞ ∞
∞ ∞ ∞ d=0
∞ ∞
Colour the source gray and set its d = 0, and Enqueue it
Queue: s
![Page 50: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/50.jpg)
Dequeue, explore neighbours
r t s
w x v
u
y
∞ ∞
∞ ∞ ∞ 0
∞ ∞
Queue: s
DQ
r
1
w 1
r w
The red edge indicates the pi[v] that got remembered
![Page 51: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/51.jpg)
Colour black after exploring all neighbours
r t s
w x v
u
y
∞ ∞
∞ ∞ ∞ 0
∞ ∞
Queue: s
DQ
r
1
w 1
r w
![Page 52: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/52.jpg)
Dequeue, explore neighbours (2)
r t s
w x v
u
y
∞ ∞
∞ ∞ ∞ 0
∞ ∞
Queue: s
DQ
r
1
w 1
r w
DQ
v
2
r
v
![Page 53: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/53.jpg)
Dequeue, explore neighbours (3)
r t s
w x v
u
y
∞ ∞
∞ ∞ ∞ 0
∞ ∞
Queue: s
DQ
r
1
w 1
r w
DQ
v
2
r
v
DQ
t
2
x
2
t x
w
![Page 54: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/54.jpg)
after a few more steps...
![Page 55: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/55.jpg)
r t s
w x v
u
y
∞ ∞
∞ ∞ 3 0
∞
Queue: s
DQ
r
1
w 1
r w
DQ
2
r
v
DQ
2
2
t x
w
BFS done!
3
u y
DQ DQ DQ DQ DQ
![Page 56: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/56.jpg)
What do we get after doing all this?
![Page 57: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/57.jpg)
r t s
w x v
u
y
∞ ∞
∞ ∞ 3 0
∞
r
1
w 1 2
r
2
2 w
3
First of all, we get to visit every vertex once.
![Page 58: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/58.jpg)
r t s
w x v
u
y
r
w
r
w
This is called the BFS-tree, it’s a tree that connects all vertices, if the graph is connected.
Did you know? The official name of the red edges are called “tree edges”.
![Page 59: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/59.jpg)
r t s
w x v
u
y
∞ ∞
∞ ∞ 3 0
∞
r
1
w 1 2
r
2
2 w
3
These d[v] values, we said they were going to be really useful.
The value indicates the vertex’s distance from the source vertex.
Actually more than that, it’s the shortest-path distance, we can prove it.
How about finding short path itself? Follow the red edges, pi[v] comes in handy for this.
Short path from u to s: u → pi[u] → pi[pi[u]] → pi[pi[pi[u]]] → … → s
![Page 60: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/60.jpg)
What if G is disconnected?
r t s
w x v
u
y
∞ ∞
∞ ∞ 3 0
∞
r
1
w 1 2
r
2
2 w
3
z ∞
The infinite distance value of z indicates that it is unreachable from the source vertex.
After BFS(s), z is of white colour and d[v] = ∞
![Page 61: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/61.jpg)
Runtime analysis!
The total amount of work (use adjacency list): ➔ Visit each vertex once ◆ Enqueue, Dequeue, change colours, assign d[v], …,
constant work per vertex ◆ in total: O(|V|)
➔ At each vertex, check all its neighbours (all its incident edges) ◆ Each edge is checked twice (by the two end vertices) ◆ in total: O(|E|)
r t s
w xv
u
y
r
w
r
w
Total runtime: O(|V|+|E|)
![Page 62: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/62.jpg)
Summary of BFS
➔ Explores breadth rather than depth
➔ Useful for getting single-source shortest paths on unweighted graphs
➔ Useful for testing reachability
➔ Runtime O(|V|+|E|) with adjacency list (with adjacency matrix it’ll be different)
![Page 63: CSC263 Week 8toni/Courses/263-2015/lectures/lec08-BFS.pdf · 7d[s]←0 8Enqueue(Q,s) 9whileQnotempty: 10u←Dequeue(Q) 11foreachneighbourvofu: 12ifcolour[v]=white 13colour[v]←gray](https://reader035.vdocuments.site/reader035/viewer/2022062507/5fcf6401bdb8e23c33393809/html5/thumbnails/63.jpg)
Next week
DFS BFS