Lecture6 of The Mafis Hgadd. Uyddddexcfdds
Lecture6 of The Mafis Hgadd. Uyddddexcfdds
Graphs
• Extremely useful tool in modeling problems
• Consist of:
• Vertices
• Edges
Vertices can be
D considered “sites”
E or locations.
C Edges represent
A connections.
F
B
Vertex
Edge
Applications
Air flight system
{a,b} {a,c}
{b,d} {c,d}
{b,e} {c,f}
{e,f}
Terminology
*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}
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
Adjacency Matrix
• Each array entry is indexed by the vertex id (as with adjacency matrix)
0 0 1 2 3 4 5 6 7 8 9
0 0 0 0 0 0 0 0 0 1 0
8
1 0 0 1 1 0 0 0 1 0 1
2 0 1 0 0 1 0 0 0 1 0
2 9
3 0 1 0 0 1 1 0 0 0 0
1
4 0 0 1 1 0 0 0 0 0 0
3 7 5 0 0 0 1 0 0 1 0 0 0
6 6 0 0 0 0 0 1 0 1 0 0
4
5 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
Examples
0 8
0
1 2 3 7 9
8
2 1 4 8
3
2 1 4 5
9 4
1 2 3
5
3 6
3 7 6
5 7
6 7
4 1 6
5 8
0 2 9
9
1 8
Storage of adjacency list
• The array takes up Θ(n) space
deg(v)
vertex v
• However, one cannot tell in O(1) time whether two vertices are connected.
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
Path between vertices
Note: a path is allowed to go through the same vertex or the same edge any
number of times!
Any cycles?
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}
Graph Traversal
• Application example
• Given a graph representation and a vertex s in the
graph
• Find all paths from s to other vertices
1 F
0
2 F
8 3 F
4 F
source 2 9 5 F
1 6 F
7 F
3 7 8 F
6 9 F
4
5
Initialize visited
table (all False)
Q= { }
Initialize Q to be empty
Example
Adjacency List Visited Table (T/F)
0 F
1 F
0
2 T
8 3 F
4 F
source 2 9 5 F
1 6 F
7 F
3 7 8 F
6 9 F
4
5
Flag that 2 has
been visited.
Q= { 2 }
1 T
0
2 T
Neighbors
8 3 F
4 T
source 2 9 5 F
1 6 F
7 F
3 7 8 T
6 9 F
4
5
Mark neighbors
as visited.
Q = {2} → { 8, 1, 4 }
Dequeue 2.
Place all unvisited neighbors of 2 on the queue
Example
Adjacency List Visited Table (T/F)
0 T
1 T
0
2 T
8 3 F
4 T
source 2 9 5 F
1 6 F
7 F
3 7 8 T
Neighbors
6 9 T
4
5
Mark new visited
Neighbors.
Q = { 8, 1, 4 } → { 1, 4, 0, 9 }
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!
Example
Adjacency List Visited Table (T/F)
0 T
1 T
0 Neighbors
2 T
8 3 T
4 T
source 2 9 5 F
1 6 F
7 T
3 7 8 T
6 9 T
4
5
Mark new visited
Neighbors.
Q = { 1, 4, 0, 9 } → { 4, 0, 9, 3, 7 }
Dequeue 1.
-- Place all unvisited neighbors of 1 on the queue.
-- Only nodes 3 and 7 haven’t been visited yet.
Example
Adjacency List Visited Table (T/F)
0 T
1 T
0
2 T
8 3 T
4 T
Neighbors
source 2 9 5 F
1 6 F
7 T
3 7 8 T
6 9 T
4
5
Q = { 4, 0, 9, 3, 7 } → { 0, 9, 3, 7 }
Dequeue 4.
-- 4 has no unvisited neighbors!
Example
Adjacency List Visited Table (T/F)
0 T
Neighbors
1 T
0
2 T
8 3 T
4 T
source 2 9 5 F
1 6 F
7 T
3 7 8 T
6 9 T
4
5
Q = { 0, 9, 3, 7 } → { 9, 3, 7 }
Dequeue 0.
-- 0 has no unvisited neighbors!
Example
Adjacency List Visited Table (T/F)
0 T
1 T
0
2 T
8 3 T
4 T
source 2 9 5 F
1 6 F
7 T
3 7 8 T
6 Neighbors 9 T
4
5
Q = { 9, 3, 7 } → { 3, 7 }
Dequeue 9.
-- 9 has no unvisited neighbors!
Example
Adjacency List Visited Table (T/F)
0 T
1 T
0
2 T
8 3 T
Neighbors
4 T
source 2 9 5 T
1 6 F
7 T
3 7 8 T
6 9 T
4
5
Mark new visited
Vertex 5.
Q = { 3, 7 } → { 7, 5 }
Dequeue 3.
-- place neighbor 5 on the queue.
Example
Adjacency List Visited Table (T/F)
0 T
1 T
0
2 T
8 3 T
4 T
source 2 9 5 T
1 6 T
Neighbors 7 T
3 7 8 T
6 9 T
4
5
Mark new visited
Vertex 6.
Q = { 7, 5 } → { 5, 6 }
Dequeue 7.
-- place neighbor 6 on the queue.
Example
Adjacency List Visited Table (T/F)
0 T
1 T
0
2 T
8 3 T
4 T
source 2 9 Neighbors 5 T
1 6 T
7 T
3 7 8 T
6 9 T
4
5
Q = { 5, 6} → { 6 }
Dequeue 5.
-- no unvisited neighbors of 5.
Example
Adjacency List Visited Table (T/F)
0 T
1 T
0
2 T
8 3 T
4 T
source 2 9 5 T
1 Neighbors 6 T
7 T
3 7
8 T
6
4 9 T
5
Q= {6}→{ }
Dequeue 6.
-- no unvisited neighbors of 6.
Example
Adjacency List Visited Table (T/F)
0 T
1 T
0
2 T
8 3 T
4 T
source 2 9 5 T
1 6 T
7 T
3 7 8 T
6 9 T
4
5
O(n + m)
Σvertex v deg(v) = 2m
O(n2)
1 F -
0
2 F -
8 3 F -
4 F -
source 2 9 5 F -
1 6 F -
7 F -
3 7 8 F -
6 9 F -
4
5 Pred
Initialize visited
table (all False)
Q= { } Initialize Pred to -1
Initialize Q to be empty
Example
Adjacency List Visited Table (T/F)
0 F -
1 F -
0
2 T -
8 3 F -
4 F -
source 2 9 5 F -
1 6 F -
7 F -
3 7 8 F -
6 9 F -
4
5 Pred
Flag that 2 has
been visited.
Q= { 2 }
1 T 2
0
2 T -
Neighbors
8 3 F -
4 T 2
source 2 9 5 F -
-
1 6 F
7 F -
3 7 8 T 2
6 9 F -
4
5 Pred
Mark neighbors
as visited.
1 T 2
0
2 T -
8 3 F -
4 T 2
source 2 9 5 F -
1 6 F -
7 F -
3 7 Neighbors 8 T 2
6 9 T 8
4
5 Pred
Mark new visited
Neighbors.
Q = { 8, 1, 4 } → { 1, 4, 0, 9 } Record in Pred
that we came
Dequeue 8.
from 8.
-- Place all unvisited neighbors of 8 on the queue.
-- Notice that 2 is not placed on the queue again, it has been visited!
Example
Adjacency List Visited Table (T/F)
0 T 8
1 T 2
0 Neighbors
2 T -
8 3 T 1
4 T 2
source 2 9 5 F -
-
1 6 F
7 T 1
3 7 8 T 2
6 9 T 8
4
5 Pred
Mark new visited
Neighbors.
Q = { 1, 4, 0, 9 } → { 4, 0, 9, 3, 7 } Record in Pred
that we came
Dequeue 1.
from 1.
-- Place all unvisited neighbors of 1 on the queue.
-- Only nodes 3 and 7 haven’t been visited yet.
Example
Adjacency List Visited Table (T/F)
0 T 8
1 T 2
0
2 T -
8 3 T 1
4 T 2
Neighbors
source 2 9 5 F -
1 6 F -
7 T 1
3 7 8 T 2
6 9 T 8
4
5 Pred
Q = { 4, 0, 9, 3, 7 } → { 0, 9, 3, 7 }
Dequeue 4.
-- 4 has no unvisited neighbors!
Example
Adjacency List Visited Table (T/F)
0 T 8
Neighbors
1 T 2
0
2 T -
8 3 T 1
4 T 2
source 2 9 5 F -
1 6 F -
7 T 1
3 7 8 T 2
6 9 T 8
4
5 Pred
Q = { 0, 9, 3, 7 } → { 9, 3, 7 }
Dequeue 0.
-- 0 has no unvisited neighbors!
Example
Adjacency List Visited Table (T/F)
0 T 8
1 T 2
0
2 T -
8 3 T 1
4 T 2
source 2 9 5 F -
1 6 F -
7 T 1
3 7 8 T 2
6 Neighbors 9 T 8
4
5 Pred
Q = { 9, 3, 7 } → { 3, 7 }
Dequeue 9.
-- 9 has no unvisited neighbors!
Example
Adjacency List Visited Table (T/F)
0 T
8
1 T
0 2
2 T
-
8 3 T
Neighbors 1
4 T
2
source 2 9 5 T
3
1 6 F
-
7 T
1
3 7 8 T
2
6 9 T
4 8
5
Pred
Mark new visited
Vertex 5.
Q = { 3, 7 } → { 7, 5 }
Record in Pred
Dequeue 3. that we came
-- place neighbor 5 on the queue. from 3.
Example
Adjacency List Visited Table (T/F)
0 T 8
1 T 2
0
2 T -
8 3 T 1
4 T 2
source 2 9 5 T 3
1 6 T 7
Neighbors 7 T 1
3 7 8 T 2
6 9 T 8
4
5 Pred
Mark new visited
Vertex 6.
Q = { 7, 5 } → { 5, 6 }
Record in Pred
Dequeue 7. that we came
-- place neighbor 6 on the queue. from 7.
Example
Adjacency List Visited Table (T/F)
0 T 8
1 T 2
0
2 T -
8 3 T 1
4 T 2
source 2 9 Neighbors 5 T 3
1 6 T 7
7 T 1
3 7 8 T 2
6 9 T 8
4
5 Pred
Q = { 5, 6} → { 6 }
Dequeue 5.
-- no unvisited neighbors of 5.
Example
Adjacency List Visited Table (T/F)
0 T 8
1 T 2
0
2 T -
8 3 T 1
4 T 2
source 2 9 5 T 3
1 Neighbors
6 T 7
7 T 1
3 7 8 T 2
6 9 T 8
4
5 Pred
Q= {6}→{ }
Dequeue 6.
-- no unvisited neighbors of 6.
Example
Adjacency List Visited Table (T/F)
0 T 8
1 T 2
0
2 T -
8 3 T 1
4 T 2
source 2 9 5 T 3
1 6 T 7
7 T 1
3 7 8 T 2
6 9 T 8
4
5 Pred
1 2
2 -
3 1
4 2
5 3
6 7
7 1
8 2
9 8
• 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.
d(v) = ;
d(s) = 0;
d(w)=d(v)+1;
Application of BFS