Graph Bfs 1
Graph Bfs 1
Graphs
☛ Extremely useful tool in modeling problems
☛ Consist of:
■ Vertices
Vertices can be
■ Edges D considered “sites”
E
or locations.
C
A Edges represent
F connections.
B
Vertex
Edge
Graph & BFS / Slide 3
Application 1
Air flight system
Application 2
Wireless communication
Definition
☛ A graph G=(V, E) consists a set of vertices, V, and a set
of edges, E.
☛ Each edge is a pair of (v, w), where v, w belongs to V
☛ If the pair is unordered, the graph is undirected;
otherwise it is directed
{a,b} {a,c}
{b,d} {c,d}
{b,e} {c,f}
{e,f}
An undirected graph
Graph & BFS / Slide 7
Terminology
• If v1 and v2 are connected, they are said to
be adjacent vertices
v1 and v2 are endpoints of the edge {v1, v2}
If •we are talking about directed graphs, where edges have direction. This
{v , v } = {v , v1} graphs are drawn with arrows (called arcs)
means that1{v1,v22} ≠ {v2,v12} . Directed
between edges. A B This means {A,B} only, not {B,A}
Graph & BFS / Slide 8
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
3. Adjacency List
Use a 1D array of linked lists
Adjacency Matrix
Graph & BFS / Slide 9
Adjacency List
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 9 2 0 1 0 0 1 0 0 0 1 0
1 3 0 1 0 0 1 1 0 0 0 0
4 0 0 1 1 0 0 0 0 0 0
3 7
6 5 0 0 0 1 0 0 1 0 0 0
4 6 0 0 0 0 0 1 0 1 0 0
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
Graph & BFS / Slide 12
0 8
0
1 2 3 7 9
8
2 1 4 8
2 3 1 4 5
9
1 4 2 3
5 3 6
3 7
6 5 7
6
4 7 1 6
5
8 0 2 9
9 1 8
Graph & BFS / Slide 13
∑ deg(v)
vertex v
☛ However, one cannot tell in O(1) time whether two vertices are
connected
Graph & BFS / Slide 14
☛ 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
■ It’s a matrix, some algorithms can be solved by matrix computation!
Graph & BFS / Slide 15
Note: a path is allowed to go through the same vertex or the same edge any
number of times!
Types of paths
Any cycles?
1. {a,c,f,e}
3. {a,b,d,c,f,e}
5. {a, c, d, b, d, c, f, e}
6. {a,c,d,b,a}
8. {a,c,f,e,b,d,c,a}
Graph & BFS / Slide 18
Summary
☛ A graph G=(V, E) consists a set of vertices, V, and a
set of edges, E. Each edge is a pair of (v, w), where v,
w belongs to V
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
Example
0
Consider s=vertex 1
8
2
Nodes at distance 1?
1 2 s 9 1 2, 3, 7, 9
1
1
Nodes at distance 2?
3 7 8, 6, 5, 4
1
6 2
4 Nodes at distance 3?
5
2 2 0
Graph & BFS / Slide 21
BFS Algorithm
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
Graph & BFS / Slide 23
1 6 F
7 F
3 7 8 F
6 9 F
4
5
Flag that 2 has
been visited
Q= { 2 }
1 6 F
7 F
3 7 8 T
6 9 F
4
5
Mark neighbors
as visited 1, 4, 8
Q = {2} → { 8, 1, 4 }
Dequeue 2.
Place all unvisited neighbors of 2 on the queue
Graph & BFS / Slide 25
1 6 F
7 F
3 7 8 T
Neighbors
6 9 T
4
5
Mark new visited
Neighbors 0, 9
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!
Graph & BFS / Slide 26
1 6 F
7 T
3 7 8 T
6 9 T
4
5
Mark new visited
Neighbors 3, 7
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.
Graph & BFS / Slide 27
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!
Graph & BFS / Slide 28
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!
Graph & BFS / Slide 29
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!
Graph & BFS / Slide 30
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.
Graph & BFS / Slide 31
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
Graph & BFS / Slide 32
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
Graph & BFS / Slide 33
Q= {6}→{ }
Dequeue 6.
-- no unvisited neighbors of 6
Graph & BFS / Slide 34
1 6 T
7 T
3 7 8 T
6 9 T
4
5
O(n + m)
Running Time
☛ Recall: Given a graph with m edges, what is
the total degree?
Σvertex v deg(v) = 2m
O(n2)