Professional Documents
Culture Documents
DSA Chapter 7 - Graphs
DSA Chapter 7 - Graphs
1
WHAT IS A GRAPH?
A graph G = (V,E) is composed of:
V: set of vertices
E: set of edges connecting the vertices in V
An edge e = (u,v) is a pair of vertices
V= {a,b,c,d,e}
Example:
E= {(a,b),(a,c),
a b (a,d),
(b,e),(c,d),(c,e),
(d,e)}
c
d e
2
APPLICATIONS
CS16
electronic circuits
JFK
LAX
LAX STL
HNL
DFW
FTL 3
TERMINOLOGY: ADJACENT AND INCIDENT
4
TERMINOLOGY: DEGREE OF A VERTEX
The degree of a vertex is the number of edges incident to that vertex
For directed graph,
the in-degree of a vertex v is the number of edges
that have v as the head
the out-degree of a vertex v is the number of edges
that have v as the tail
if di is the degree of a vertex i in a graph G with n vertices and e
edges, the number of edges is
5
EXAMPLES
0
3 2
0 1 2
3 3
3 1 2 3 3 4 5 6
3G1 1 1 G2 1 1
3
0 in:1, out: 1
directed graph
in-degree
out-degree 1 in: 1, out: 2
2 in: 1, out: 0
G3
6
TERMINOLOGY: PATH
3 3
a b a b
c c
d e d e
abedc bedc
7
MORE TERMINOLOGY
a b
simple path: no repeated vertices
bec
c
d e
a b
cycle: simple path, except that the last vertex is the same as the first
acda
vertex c
d e
8
EVEN MORE TERMINOLOGY
• connected graph: any two vertices are connected by some path
9
SUB-GRAPHS EXAMPLES
0
0 0 1 2 0
1 2
1 2 3 1 2
3
G1 (i) (ii) iii) 3 (iv)
(a) Some of the subgraph of G1
0
0 0 0 0
1 1 1 1
2 2 2
(i) (ii) (iii) (iv)
(b) Some of the subgraph of G2
G2 10
MORE…
tree - connected graph without cycles
forest - collection of trees
tree
tree
forest
tree
tree
11
CONNECTIVITY
Let n = #vertices, and m = #edges
A complete graph: one in which all pairs of vertices are
adjacent
How many total edges in a complete graph?
Each of the n vertices is incident to n-1 edges, however, we would have
counted each edge twice! Therefore, intuitively, m = n(n -1)/2.
Therefore, if a graph is not complete, m < n(n -1)/2
n 5
m (5
12
MORE CONNECTIVITY
If m < n - 1, G is not
connected
n5
n = #vertices
m4
m = #edges
For a tree m = n - 1
n5
m3
13
ORIENTED (DIRECTED) GRAPH
14
DIRECTED VS. UNDIRECTED GRAPH
An undirected graph is one in which the pair of vertices in a
edge is unordered, (v0, v1) = (v1,v0)
A directed graph is one in which each edge is a directed pair
of vertices, <v0, v1> != <v1,v0>
tail head
15
IMPLEMENT A GRAPH
The two most commonly used ways of representing a graph are as follows:
Adjacency Matrix
Adjacency List
16
ADJACENCY MATRIX
v1 0 1
0 0
v2 0 0
1 0
v3 0 0
0 0
v4 1 0
1 0
17
ADJACENCY LIST
18
TRAVERSING A GRAPH
You can traverse a graph with the help of the following two methods:
19
DEPTH FIRST SEARCH (DFS)
Algorithm: DFS(v)
1. Push the starting vertex, v into the stack.
2. Repeat until the stack becomes empty:
20
DFS(CONTD.)
Push the starting vertex, v1 into the stack
v1
21
DFS(CONTD.)
Pop a vertex, v1 from the stack
Visit v1
Push all unvisited vertices adjacent to v1 into the stack
v1
v1
22
DFS(CONTD.)
Pop a vertex, v1 from the stack
Visit v1
Push all unvisited vertices adjacent to v1 into the stack
v2
v4
v1 23
DFS (CONTD.)
Pop a vertex, v2 from the stack
Visit v2
Push all unvisited vertices adjacent to v2 into the stack
v2
v4
v1 v2 24
DFS (Contd.)
Pop a vertex, v2 from the stack
Visit v2
Push all unvisited vertices adjacent to v2 into the stack
v6
v3
v4
Visited:
v1 v2
25
DFS (Contd.)
Pop a vertex, v6 from the stack
Visit v6
Push all unvisited vertices adjacent to v6 into the stack
v6
v3
v4
v3
v4
Visited:
v1 v2 v6 v3
27
DFS (Contd.)
Pop a vertex, v3 from the stack
Visit v3
Push all unvisited vertices adjacent to v3 into the stack
v5
v4
Visited:
v1 v2 v6 v3
28
DFS (Contd.)
Pop a vertex, v5 from the stack
Visit v5
Push all unvisited vertices adjacent to v5 into the stack
v5
v4
v4
Visited:
v1 v2 v6 v3 v5 v4
31
DFS (Contd.)
Although the preceding algorithm provides a simple and convenient method
to traverse a graph, the algorithm will not work correctly if the graph is not
connected.
In such a case, you will not be able to traverse all the vertices from one
single starting vertex.
32
DFS (Contd.)
To solve this problem, you need to execute 1. Repeat step 2 for each vertex, v
the preceding algorithm repeatedly for all in the graph
unvisited vertices in the graph. 2. If v is not visited:
a. Call DFS(v)
33
BFS
Algorithm: BFS(v)
1. Visit the starting vertex, v and insert it into a
queue.
2. Repeat step 3 until the queue becomes empty.
3. Delete the front vertex from the queue, visit all its
unvisited adjacent vertices, and insert them into the
queue.
34
BFS (Contd.)
Visit v1
Insert v1 into the queue
v1
v1
35
BFS (Contd.)
Remove a vertex v1 from the queue
Visit all unvisited vertices adjacent to v1 and insert them in the queue
v1
Visited:
v1
36
BFS (Contd.)
Remove a vertex v1 from the queue
Visit all unvisited vertices adjacent to v1 and insert them in the queue
v2 v4
v1 v2 v4
37
BFS (Contd.)
Remove a vertex v2 from the queue
Visit all unvisited vertices adjacent to v2 and insert them in the queue
v2 v4
v1 v2 v4
38
BFS (Contd.)
Remove a vertex v2 from the queue
Visit all unvisited vertices adjacent to v2 and insert them in the queue
v4 v3 v6
v1 v2 v4 v3 v6
39
BFS (Contd.)
Remove a vertex v4 from the queue
Visit all unvisited vertices adjacent to v4 and insert them in the queue
v4 v3 v6 v5
v1 v2 v4 v3 v6 v5
40
BFS (Contd.)
Remove a vertex v3 from the queue
Visit all unvisited vertices adjacent to v3 and insert them in the queue
v3 v6 v5
v1 v2 v4 v3 v6 v5
41
BFS (Contd.)
Remove a vertex v6 from the queue
Visit all unvisited vertices adjacent to v6 and insert them in the queue
v6 v5
v1 v2 v4 v3 v6 v5
42
BFS (Contd.)
Remove a vertex v6 from the queue
Visit all unvisited vertices adjacent to v6 and insert them in the queue
v5
v1 v2 v4 v3 v6 v5
43
BFS (Contd.)
Remove a vertex v5 from the queue
Visit all unvisited vertices adjacent to v5 and insert them in the queue
v5
v1 v2 v4 v3 v6 v5
44
BFS (Contd.)
Remove a vertex v5 from the queue
Visit all unvisited vertices adjacent to v5 and insert them in
the queue
v1 v2 v4 v3 v6 v5
45
BFS (Contd.)
The queue is now empty
Therefore, traversal is complete
v1 v2 v4 v3 v6 v5
46
BFS (Contd.)
Although the preceding algorithm provides a simple and
convenient method to traverse a graph, the algorithm will not
work correctly if the graph is not connected.
In such a case, you will not be able to traverse all the vertices
from one single starting vertex.
47
BFS (Contd.)
To solve this problem, you need to execute the 1. Repeat step 2 for each
vertex, v in the graph
preceding algorithm repeatedly for all unvisited
2. If v is not visited:
vertices in the graph. a. Call BFS(v)
48
Activity: Implementing a Graph by Using Adjacency Matrix Representation
Problem Statement:
Represent Ethiopian regional capital cities including Addis Ababa and
the distances between them in the form of a graph. Write a program to
represent the graph in the form of an adjacency matrix.
49
Applications of Graphs
Many problems can be easily solved by reducing them in the form of a
graph
Graph theory has been instrumental in analysing and solving problems in
areas as diverse as computer network design, urban planning, finding
shortest paths and molecular biology.
50
Solving the Shortest Path Problem
The shortest path problem can be solved by applying the Dijkstra’s
algorithm on a graph
The Dijkstra’s algorithm is based on the greedy approach
The steps in the Dijkstra’s algorithm are as follows:
1. Choose vertex v corresponding to the smallest distance
recorded in the DISTANCE array such that v is not already in
FINAL.
2. Add v to FINAL.
3. Repeat for each vertex w in the graph that is not in FINAL:
a. If the path from v1 to w via v is shorter than the previously
recorded distance from v1 to w (If ((DISTANCE[v] + weight of
edge(v,w)) < DISTANCE[w])):
i. Set DISTANCE[w]=DISTANCE[v] + weight of edge(v,w).
4. If FINAL does not contain all the vertices, go to step 1.
51
Solving the Shortest Path Problem (Contd.)
5 Suppose you need to find the
shortest distance of all the vertices
from vertex v1.
3 4 6
Add v1 to the FINAL array.
2
3
6 3
v1 v2 v3 v4 v5 v6
DISTANCE 0 5 ∞ 3 ∞ ∞
FINAL v1
52
Solving the Shortest Path Problem (Contd.)
5 In the DISTANCE array, vertex
v4 has the shortest distance from
vertex v1.
3 4 6 Therefore, v4 is added to the
FINAL array.
2
3
6 3
v1 v2 v3 v4 v5 v6
DISTANCE 0 5 ∞ 3 ∞ ∞
FINAL v1 v4
53
Solving the Shortest Path Problem (Contd.)
5
v1 → v2 = 5
v1 → v4 → v2 = 3 + ∞ = ∞
3 4 6
∞>5
Therefore, no change is made.
2
3
6 3
v1 v2 v3 v4 v5 v6
DISTANCE 0 5 ∞ 3 ∞ ∞
FINAL v1 v4
54
Solving the Shortest Path Problem (Contd.)
5
v1 → v3 = ∞
v1 → v4 → v3 = 3 + 2 = 5
3 4 6
5<∞
Therefore, the entry
2
3 corresponding to v3 in the
DISTANCE array is changed to
6 3
5.
v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5
∞ 3 ∞ ∞
FINAL v1 v4
55
Solving the Shortest Path Problem (Contd.)
5
v1 → v5 = ∞
v1 → v4 → v5 = 3 + 6 = 9
3 4 6
9<∞
Therefore, the entry
2
3 corresponding to v5 in the
DISTANCE array is changed to
6 3
9.
v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 ∞
9 ∞
FINAL v1 v4
56
Solving the Shortest Path Problem (Contd.)
5
v1 → v6 = ∞
v1 → v4 → v6 = 3 + ∞ = ∞
3 4 6
v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 9 ∞
FINAL v1 v4
57
Solving the Shortest Path Problem (Contd.)
5
From the DISTANCE array, select
the vertex with the shortest
distance from v1, such that the
selected vertex is not in the
3 4 6
FINAL array.
2
v2 and v3 have the shortest and the
3
same distance from v1.
6 3 Let us select v2 and add it to the
FINAL array.
v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 9 ∞
FINAL v1 v4 v2
58
Solving the Shortest Path Problem (Contd.)
5
v1 → v3 = 5
v1 → v2 → v3 = 5 + 4 = 9
3 4 6
9>5
Therefore, no change is made.
2
3
6 3
v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 9 ∞
FINAL v1 v4 v2
59
Solving the Shortest Path Problem (Contd.)
5
v1 → v5 = 9
v1 → v2 → v5 = 5 + ∞ = ∞
3 4 6
∞>9
Therefore, no change is made.
2
3
6 3
v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 9 ∞
FINAL v1 v4 v2
60
Solving the Shortest Path Problem (Contd.)
5
v1 → v6 = ∞
v1 → v2 → v6 = 5 + 6 = 11
3 4 6
11 < ∞
Therefore, the entry
2
3 corresponding to v6 in the
DISTANCE array is changed to
6 3
11.
Pass 2 complete
v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 9 ∞
11
FINAL v1 v4 v2
61
Solving the Shortest Path Problem (Contd.)
5
From the DISTANCE array, select
the vertex with the shortest
distance from v1, such that the
selected vertex is not in the
3 4 6
FINAL array.
v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 9 11
FINAL v1 v4 v2 v3
62
Solving the Shortest Path Problem (Contd.)
5
v1 → v5 = 9
v1 → v3 → v5 = 5 + 3 = 8
3 4 6
8<9
Therefore, the entry corresponding
2
3 to v5 in the DISTANCE array is
changed to 8.
6 3
v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 98 11
FINAL v1 v4 v2 v3
63
Solving the Shortest Path Problem (Contd.)
5
v1 → v6 = 11
v1 → v3 → v6 = 5 + 3 = 8
3 4 6
8 < 11
Therefore, the entry
2
3 corresponding to v6 in the
DISTANCE array is changed to 8.
6 3
Pass 3 complete
v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 8 11
8
FINAL v1 v4 v2 v3
64
Solving the Shortest Path Problem (Contd.)
5
From the DISTANCE array, select
the vertex with the shortest
distance from v1, such that the
selected vertex is not in the
3 4 6
FINAL array.
v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 8 8
FINAL v1 v4 v2 v3 v5
65
Solving the Shortest Path Problem (Contd.)
5
v1 → v6 = 8
v1 → v5 → v6 = 8 + ∞ = ∞
3 4 6
∞>8
Therefore, no change is made.
2
3
6 3
Pass 4 complete
v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 8 8
FINAL v1 v4 v2 v3 v5
66
Solving the Shortest Path Problem (Contd.)
5
Now add the only remaining
vertex, v6 to the FINAL array.
FINAL v1 v4 v2 v3 v5 v6
67
PRIM’S ALGORITHM
69
EXAMPLE2
4 c 4 c 4 c
a a a
1 1 1
6 6 6
2 2 2
d b d d
b 3 3 b 3
c 4 c
4
a a
6 1
6 1
2 2
d b d
b 3 3
70
PRIM’S ALGORITHM - PERFORMANCE
Efficiency
O(n2) for weight matrix representation of graph and array
implementation of priority queue
O(m log n) for adjacency lists representation of graph with n vertices
and m edges and min-heap implementation of the priority queue
71