Ds lect 25 -graphs - i
-
Upload
googler700-blogspot -
Category
Engineering
-
view
438 -
download
4
description
Transcript of Ds lect 25 -graphs - i
![Page 1: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/1.jpg)
Graphs
Lecture 34
Suggested Readings: Weiss Ch. 9
![Page 2: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/2.jpg)
Graphs
Extremely useful tool in modeling problems
Consist of:
Vertices
Edges
D
E
A
C
FB
Vertex
Edge
Vertices can be
considered “sites”
or locations.
Edges represent
connections.
![Page 3: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/3.jpg)
ApplicationsAir flight system
• Each vertex represents a city
• Each edge represents a direct flight between two cities
• A query on direct flights becomes a query on whether an edge exists
• A query on how to get to a location is “does a path exist from A to B?”
• We can even associate costs to edges (weighted graphs), then ask
“what is the cheapest path from A to B”
![Page 4: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/4.jpg)
Definition Undirected graph
An undirected graph is specified by an ordered pair (V,E), where
V is the set of vertices and E is the set of edges
{c,f}
{a,c}{a,b}
{b,d} {c,d}
{e,f}
{b,e}
![Page 5: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/5.jpg)
Terminology
1. If v1 and v2 are connected, they are said to be
adjacent vertices
v1 and v2 are endpoints of the edge {v1, v2}
2. If an edge e is connected to v, then v is said to be
incident on e. Also, the edge e is said to be incident
on v.
3. {v1, v2} = {v2, v1}*
*Later, we will talk about “directed graphs”, where edges have direction. This
means that {v1,v2} ≠ {v2,v1} . Directed graphs are drawn with arrows (called arcs)
between edges. A B This means {A,B} only, not {B,A}
![Page 6: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/6.jpg)
Terminology (continued…)
In a directed graph, the number of edges coming out of a vertex
is called the out-degree of that vertex. Number of edges coming
in is the in-degree.
In an undirected graph, we just talk of degree of a vertex. It is
the number of edges incident on the vertex.
![Page 7: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/7.jpg)
Terminology (continued…)
For a directed graph G = (V, E),
where |E| means the cardinality of the set E, i.e., the number of
edges.
For an undirected graph G = (V, E),
where |E| means the cardinality of the set E, i.e., the number of
edges.
v v
in-degree(v) = out-degree(v) = |E|V V
v
degree(v) = 2|E|V
![Page 8: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/8.jpg)
Graph Representation
Two popular computer representations of a
graph. Both represent the vertex set and the
edge set, but in different ways.
1. Adjacency Matrix
Use a 2D matrix to represent the graph
2. Adjacency List
Use a 1D array of linked lists
![Page 9: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/9.jpg)
Adjacency Matrix
2D array A[0..n-1, 0..n-1], where n is the number of vertices in the graph
Each row and column is indexed by the vertex id.
e,g a=0, b=1, c=2, d=3, e=4
An array entry A [i] [j] is equal to 1 if there is an edge connecting
vertices i and j. Otherwise, A [i] [j] is 0.
The storage requirement is Θ(n2). Not efficient if the graph has few
edges.
We can detect in O(1) time whether two vertices are connected.
![Page 10: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/10.jpg)
Adjacency list
• The adjacency list is an array A[0..n-1] of lists, where n is the number of
vertices in the graph.
• Each array entry is indexed by the vertex id (as with adjacency matrix)
• The list A[i] stores the ids of the vertices adjacent to i.
![Page 11: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/11.jpg)
Examples
2
4
3
5
1
7
6
9
8
0 0 1 2 3 4 5 6 7 8 9
0 0 0 0 0 0 0 0 0 1 0
1 0 0 1 1 0 0 0 1 0 1
2 0 1 0 0 1 0 0 0 1 0
3 0 1 0 0 1 1 0 0 0 0
4 0 0 1 1 0 0 0 0 0 0
5 0 0 0 1 0 0 1 0 0 0
6 0 0 0 0 0 1 0 1 0 0
7 0 1 0 0 0 0 1 0 0 0
8 1 0 1 0 0 0 0 0 0 1
9 0 1 0 0 0 0 0 0 1 0
![Page 12: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/12.jpg)
Examples
2
4
3
5
1
7
6
9
8
0 0
1
2
3
4
5
6
7
8
9
2 3 7 9
8
1 4 8
1 4 5
2 3
3 6
5 7
1 6
0 2 9
1 8
![Page 13: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/13.jpg)
Digraph Representation
*Digraph = Directed Graph
![Page 14: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/14.jpg)
Storage of adjacency list The array takes up Θ(n) space
Define degree of v, deg(v), to be the number of edges incident to v. Then, the
total space to store the graph is proportional to:
An edge e={u,v} of the graph contributes a count of 1 to deg(u) and contributes a
count 1 to deg(v)
Therefore, Σvertex vdeg(v) = 2m, where m is the total number of edges
In all, the adjacency list takes up Θ(n+m) space.
If m = O(n2), both adjacent matrix and adjacent lists use Θ(n2) space.
If m = O(n), adjacent list outperform adjacent matrix
However, one cannot tell in O(1) time whether two vertices are connected.
v
vvertex
)deg(
![Page 15: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/15.jpg)
Adjacency Lists vs. Matrix
Adjacency Lists
More compact than adjacency matrices if graph has few edges
Requires more time to find if an edge exists
Adjacency Matrix
Always require n2 space
This can waste a lot of space if the number of edges are sparse
Can quickly find if an edge exists
![Page 16: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/16.jpg)
Path between vertices
A path is a sequence of vertices (v0, v1, v2,… vk) such that:
For 0 ≤ i < k, {vi, vi+1} is an edge
For 0 ≤ i < k-1, vi ≠ vi+2
That is, the edge {vi, vi+1} ≠ {vi+1, vi+2}
Note: a path is allowed to go through the same vertex or the same edge any number of times!
The length of a path is the number of edges on the path
![Page 17: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/17.jpg)
Types of paths
A path is simple if and only if it does not
contain a vertex more than once.
A path is a cycle if and only if v0= vk
The beginning and end are the same vertex!
A path contains a cycle if some vertex
appears twice or more
![Page 18: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/18.jpg)
ExamplesUndirected Graph
1. {a,c,f,e}
2. {a,b,d,c,f,e}
3. {a, c, d, b, d, c, f, e}
4. {a,c,d,b,a}
5. {a,c,f,e,b,d,c,a}
Are these paths?
Any cycles?
What is the path’s length?
![Page 19: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/19.jpg)
ExamplesDirected Graph
![Page 20: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/20.jpg)
A vertex w is reachable from vertex u if there
is a path from u to w.
A path is simple if all vertices (except possibly
the fist and last) are distinct.
A Eulerian cycle is a cycle that visits every
edge of the graph exactly once.
![Page 21: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/21.jpg)
Acyclic Graph
A graph is said to be acyclic if it contains no
cycles. A graph is connected if every vertex
can reach every other vertex.
Directed Acyclic Graph
A directed graph that is acyclic is called a
directed acyclic graph (DAG).
![Page 22: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/22.jpg)
Questions?
![Page 23: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/23.jpg)
The Graph ADT
![Page 24: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/24.jpg)
![Page 25: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/25.jpg)
Implementation of Graphs
![Page 26: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/26.jpg)
![Page 27: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/27.jpg)
Implementation of Graphs
![Page 28: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/28.jpg)
Lab Task 01
Implement the Graph ADT using the adjacency
matrix
You need to provide the following functions
insertEdge
removeEdge
isConnected(int v1, int v2) Checks if an edge exist between two given vertices v1 and v2
(where v1 and v2 are in the range {0, 1, … MAX-1}
![Page 29: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/29.jpg)
![Page 30: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/30.jpg)
Graph Traversal
Application example
Given a graph representation and a vertex s in the
graph
Find all paths from s to other vertices
Two common graph traversal algorithms Breadth-First Search (BFS)
Find the shortest paths in an unweighted graph
Depth-First Search (DFS)
Topological sort
Find strongly connected components
Let’s first look at BFS . . .
![Page 31: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/31.jpg)
BFS and Shortest Path Problem
Given any source vertex s, BFS visits the other
vertices at increasing distances away from s. In doing
so, BFS discovers paths from s to other vertices
What do we mean by “distance”? The number of
edges on a path from s.
2
4
3
5
1
7
6
9
8
0
Consider s=vertex 1
Nodes at distance 1?
2, 3, 7, 91
1
1
12
22
2
s
Example
Nodes at distance 2?
8, 6, 5, 4
Nodes at distance 3?
0
![Page 32: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/32.jpg)
BSF algorithm
Why use queue? Need FIFO
![Page 33: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/33.jpg)
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
F
F
F
F
F
F
F
F
F
F
Q = { }
Initialize visited
table (all False)
Initialize Q to be empty
![Page 34: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/34.jpg)
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
F
F
T
F
F
F
F
F
F
F
Q = { 2 }
Flag that 2 has
been visited.
Place source 2 on the queue.
![Page 35: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/35.jpg)
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
F
T
T
F
T
F
F
F
T
F
Q = {2} → { 8, 1, 4 }
Mark neighbors
as visited.
Dequeue 2.
Place all unvisited neighbors of 2 on the queue
Neighbors
![Page 36: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/36.jpg)
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
F
T
F
F
F
T
T
Q = { 8, 1, 4 } → { 1, 4, 0, 9 }
Mark new visited
Neighbors.
Dequeue 8.
-- Place all unvisited neighbors of 8 on the queue.
-- Notice that 2 is not placed on the queue again, it has been visited!
Neighbors
![Page 37: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/37.jpg)
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
F
F
T
T
T
Q = { 1, 4, 0, 9 } → { 4, 0, 9, 3, 7 }
Mark new visited
Neighbors.
Dequeue 1.
-- Place all unvisited neighbors of 1 on the queue.
-- Only nodes 3 and 7 haven’t been visited yet.
Neighbors
![Page 38: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/38.jpg)
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
F
F
T
T
T
Q = { 4, 0, 9, 3, 7 } → { 0, 9, 3, 7 }
Dequeue 4.
-- 4 has no unvisited neighbors!
Neighbors
![Page 39: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/39.jpg)
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
F
F
T
T
T
Q = { 0, 9, 3, 7 } → { 9, 3, 7 }
Dequeue 0.
-- 0 has no unvisited neighbors!
Neighbors
![Page 40: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/40.jpg)
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
F
F
T
T
T
Q = { 9, 3, 7 } → { 3, 7 }
Dequeue 9.
-- 9 has no unvisited neighbors!
Neighbors
![Page 41: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/41.jpg)
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
F
T
T
T
Q = { 3, 7 } → { 7, 5 }
Dequeue 3.
-- place neighbor 5 on the queue.
Neighbors
Mark new visited
Vertex 5.
![Page 42: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/42.jpg)
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
Q = { 7, 5 } → { 5, 6 }
Dequeue 7.
-- place neighbor 6 on the queue.
Neighbors
Mark new visited
Vertex 6.
![Page 43: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/43.jpg)
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
Q = { 5, 6} → { 6 }
Dequeue 5.
-- no unvisited neighbors of 5.
Neighbors
![Page 44: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/44.jpg)
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
Q = { 6 } → { }
Dequeue 6.
-- no unvisited neighbors of 6.
Neighbors
![Page 45: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/45.jpg)
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
Q = { } STOP!!! Q is empty!!!
What did we discover?
Look at “visited” tables.
There exists a path from source
vertex 2 to all vertices in the graph.
![Page 46: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/46.jpg)
Time Complexity of BFS(Using adjacency list)
Assume adjacency list
n = number of vertices m = number of edges
Each vertex will enter Q at most once.
Each iteration takes time proportional to
deg(v) + 1 (the number 1 is to include the
case where deg(v) = 0).
O(n + m)
![Page 47: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/47.jpg)
Running time
Given a graph with m edges, what is the total degree?
The total running time of the while loop is:
this is summing over all the iterations in the while
loop!
O( Σvertex v (deg(v) + 1) ) = O(n+m)
Σvertex v deg(v) = 2m
![Page 48: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/48.jpg)
Time Complexity of BFS(Using adjacency matrix)
Assume adjacency list
n = number of vertices m = number of edges
Finding the adjacent vertices of v
requires checking all elements in the
row. This takes linear time O(n).
Summing over all the n iterations, the
total running time is O(n2).
O(n2)
So, with adjacency matrix, BFS is O(n2)
independent of number of edges m.
With adjacent lists, BFS is O(n+m); if
m=O(n2) like a dense graph, O(n+m)=O(n2).
![Page 49: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/49.jpg)
Shortest Path Recording
BFS we saw only tells us whether a path exists from source s, to other vertices v. It doesn’t tell us the path!
We need to modify the algorithm to record the path.
How can we do that? Note: we do not know which vertices lie on this path until we
reach v!
Efficient solution:
Use an additional array pred[0..n-1]
Pred[w] = v means that vertex w was visited from v
![Page 50: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/50.jpg)
BFS + Path Finding
initialize all pred[v] to -1
Record where you
came from
![Page 51: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/51.jpg)
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
F
F
F
F
F
F
F
F
F
F
Q = { }
Initialize visited
table (all False)
Initialize Pred to -1
Initialize Q to be empty
-
-
-
-
-
-
-
-
-
-
Pred
![Page 52: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/52.jpg)
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
F
F
T
F
F
F
F
F
F
F
Q = { 2 }
Flag that 2 has
been visited.
Place source 2 on the queue.
-
-
-
-
-
-
-
-
-
-
Pred
![Page 53: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/53.jpg)
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
F
T
T
F
T
F
F
F
T
F
Q = {2} → { 8, 1, 4 }
Mark neighbors
as visited.
Record in Pred
that we came
from 2.Dequeue 2.
Place all unvisited neighbors of 2 on the queue
Neighbors
-
2
-
-
2
-
-
-
2
-
Pred
![Page 54: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/54.jpg)
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
F
T
F
F
F
T
T
Q = { 8, 1, 4 } → { 1, 4, 0, 9 }
Mark new visited
Neighbors.
Record in Pred
that we came
from 8.Dequeue 8.
-- Place all unvisited neighbors of 8 on the queue.
-- Notice that 2 is not placed on the queue again, it has been visited!
Neighbors
8
2
-
-
2
-
-
-
2
8
Pred
![Page 55: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/55.jpg)
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
F
F
T
T
T
Q = { 1, 4, 0, 9 } → { 4, 0, 9, 3, 7 }
Mark new visited
Neighbors.
Record in Pred
that we came
from 1.Dequeue 1.
-- Place all unvisited neighbors of 1 on the queue.
-- Only nodes 3 and 7 haven’t been visited yet.
Neighbors
8
2
-
1
2
-
-
1
2
8
Pred
![Page 56: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/56.jpg)
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
F
F
T
T
T
Q = { 4, 0, 9, 3, 7 } → { 0, 9, 3, 7 }
Dequeue 4.
-- 4 has no unvisited neighbors!
Neighbors
8
2
-
1
2
-
-
1
2
8
Pred
![Page 57: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/57.jpg)
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
F
F
T
T
T
Q = { 0, 9, 3, 7 } → { 9, 3, 7 }
Dequeue 0.
-- 0 has no unvisited neighbors!
Neighbors8
2
-
1
2
-
-
1
2
8
Pred
![Page 58: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/58.jpg)
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
F
F
T
T
T
Q = { 9, 3, 7 } → { 3, 7 }
Dequeue 9.
-- 9 has no unvisited neighbors!
Neighbors
8
2
-
1
2
-
-
1
2
8
Pred
![Page 59: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/59.jpg)
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
F
T
T
T
Q = { 3, 7 } → { 7, 5 }
Dequeue 3.
-- place neighbor 5 on the queue.
Neighbors
Mark new visited
Vertex 5.
Record in Pred
that we came
from 3.
8
2
-
1
2
3
-
1
2
8
Pred
![Page 60: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/60.jpg)
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
Q = { 7, 5 } → { 5, 6 }
Dequeue 7.
-- place neighbor 6 on the queue.
Neighbors
Mark new visited
Vertex 6.
Record in Pred
that we came
from 7.
8
2
-
1
2
3
7
1
2
8
Pred
![Page 61: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/61.jpg)
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
Q = { 5, 6} → { 6 }
Dequeue 5.
-- no unvisited neighbors of 5.
Neighbors
8
2
-
1
2
3
7
1
2
8
Pred
![Page 62: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/62.jpg)
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
Q = { 6 } → { }
Dequeue 6.
-- no unvisited neighbors of 6.
Neighbors
8
2
-
1
2
3
7
1
2
8
Pred
![Page 63: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/63.jpg)
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
Q = { } STOP!!! Q is empty!!!
Pred now can be traced backward
to report the path!
8
2
-
1
2
3
7
1
2
8
Pred
![Page 64: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/64.jpg)
Path reporting
8
2
-
1
2
3
7
1
2
8
0
1
2
3
4
5
6
7
8
9
nodes visited from
Try some examples, report path from s to v:
Path(0) ->
Path(6) ->
Path(1) ->
The path returned is the shortest from s to v
(minimum number of edges).
![Page 65: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/65.jpg)
BFS tree
The paths found by BFS is often drawn as a rooted tree (called
BFS tree), with the starting vertex as the root of the tree.
BFS tree for vertex s=2.
Question: What would a “level” order traversal tell you?
![Page 66: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/66.jpg)
How do we record the shortest
distances?
d(v) = ;
d(w)=d(v)+1;
d(s) = 0;
![Page 67: Ds lect 25 -graphs - i](https://reader034.fdocuments.net/reader034/viewer/2022042700/559b29281a28ab5f2c8b45c7/html5/thumbnails/67.jpg)
Application of BFS
One application concerns how to find
connected components in a graph
If a graph has more than one connected
components, BFS builds a BFS-forest (not just
BFS-tree)!
Each tree in the forest is a connected component.