Graphs
Graphs
Introduction
• A graph is an abstract data structure that is used to implement the graph
concept from mathematics. A graph is basically, a collection of vertices (also
called nodes) and edges that connect these vertices. A graph is often viewed as
a generalization of the tree structure, where instead of a having a purely
parent-to-child relationship between tree nodes, any kind of complex
relationships between the nodes can be represented.
Why graphs are useful?
• Graphs are widely used to model any situation where entities or things are
related to each other in pairs; for example, the following information can be
represented by graphs:
• Family trees in which the member nodes have an edge from parent to each of
their children.
• Transportation networks in which nodes are airports, intersections, ports, etc.
The edges can be airline flights, one-way roads, shipping routes, etc.
Definition
A B C
D E
• V(G) = { A, B, C, D , E}
• E(G) = { (A, B), (B, C), (A, D), (B, D), (D, E), (C, E) }.
• 5 vertices or nodes and 6 edges in the graph.
Graph Terminology
● More variations:
■ A weighted graph associates weights with either
the edges or the vertices
○ E.g., a road map: edges might be weighted with distance
■ A multigraph allows multiple edges between the
same vertices
○ E.g., the call graph in a program (a function can get
called from multiple points in another function)
Graph Terminology
A B e4
2
e2 7 1
e3 D E F
C e6 D B
e5 e7 3
Directed Graph
● Assume V = {1, 2, …, n}
● An adjacency matrix represents the graph as a
n x n matrix A:
■ A[i, j] = 1 if edge (i, j) ∈ E (or weight of edge)
= 0 if edge (i, j) ∉ E
Graphs: Adjacency Matrix
● Example:
A 1 2 3 4
1
a 1
2 d
4 2
3
b c
??
3 4
Graphs: Adjacency Matrix
● Example:
A 1 2 3 4
1
a 1 0 1 1 0
2 d
4 2 0 0 1 0
b c 3 0 0 0 0
3 4 0 0 1 0
Graphs: Adjacency Matrix
r s t u
∞ ∞ ∞ ∞
∞ ∞ ∞ ∞
v w x y
Breadth-First Search: Example
r s t u
∞ 0 ∞ ∞
∞ ∞ ∞ ∞
v w x y
Q: s
Breadth-First Search: Example
r s t u
1 0 ∞ ∞
∞ 1 ∞ ∞
v w x y
Q: w r
Breadth-First Search: Example
r s t u
1 0 2 ∞
∞ 1 2 ∞
v w x y
Q: r t x
Breadth-First Search: Example
r s t u
1 0 2 ∞
2 1 2 ∞
v w x y
Q: t x v
Breadth-First Search: Example
r s t u
1 0 2 3
2 1 2 ∞
v w x y
Q: x v u
Breadth-First Search: Example
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: v u y
Breadth-First Search: Example
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: u y
Breadth-First Search: Example
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: y
Breadth-First Search: Example
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: Ø
Breadth-First Search: Properties
| |
| | |
DFS Example
source
vertex d
f
1 | | |
2 | |
| | |
DFS Example
source
vertex d
f
1 | | |
2 | |
3 | | |
DFS Example
source
vertex d
f
1 | | |
2 | |
3 |
| |
4
DFS Example
source
vertex d
f
1 | | |
2 | |
3 |
5 | |
4
DFS Example
source
vertex d
f
1 | | |
2 | |
3 | 5 |
|
4 6
DFS Example
source
vertex d
f
1 | 8 | |
2 |
|
7
3 | 5 |
|
4 6
DFS Example
source
vertex d
f
1 | 8 | |
2 |
|
7
3 | 5 |
|
4 6
DFS Example
source
vertex d
f
1 | 8 | |
2 |
9 |
7
3 | 5 |
|
4 6
DFS Example
source
vertex d
f
1 | 8 | |
2 | 9
7 |10
3 | 5 |
|
4 6
DFS Example
source
vertex d
f
8
1 | |
|11
2 | 9
7 |10
3 | 5 |
|
4 6
DFS Example
source
vertex d
f
1 8
|
|12 |11
2 | 9
7 |10
3 | 5 |
|
4 6
DFS Example
source
vertex d
f
1 8
13|
|12 |11
2 | 9
7 |10
3 | 5 |
|
4 6
DFS Example
source
vertex d
f
1 8
13|
|12 |11
2 | 9
7 |10
3 | 5 |
14|
4 6
DFS Example
source
vertex d
f
1 8
13|
|12 |11
2 | 9
7 |10
3 | 5 | 14|
4 6 15
DFS Example
source
vertex d
f
1 8 13|
|12 |11 16
2 | 9
7 |10
3 | 5 | 14|
4 6 15
Minimum Spanning Trees
Spanning Tree
Central office
Wiring: Naïve Approach
Central office
Expensive!
Wiring: Better Approach
Central office
6 4
5 9
14 2
10
15
3 8
Minimum Spanning Tree
14 2
10
15
3 8
Minimum Spanning Tree
A
6 4
5 9
H B C
14 2
10
15
G E D
3 8
F
Minimum Spanning Tree
● Answer:
A
6 4
5 9
H B C
14 2
10
15
G E D
3 8
F
Generic MST
A=ф
While A does not form a spanning tree
do find an edge (u, v) that is safe for A
A = A U {(u,v)}
Return A
Kruskal’s Algorithm
Kruskal()
{
T = ∅;
sort E by increasing edge weight w
for each (u,v) ∈ E (in sorted order)
if adding (u,v) does not form a cycle)
T = T U {{u,v}};
}
Kruskal’s Algorithm
2 19
9
14 17
8 25
5
21 13 1
Kruskal’s Algorithm
2 19
9
14 17
8 25
5
21 13 1
Kruskal’s Algorithm
2 19
9
14 17
8 25
5
21 13 1?
Kruskal’s Algorithm
2 19
9
14 17
8 25
5
21 13 1
Kruskal’s Algorithm
2? 19
9
14 17
8 25
5
21 13 1
Kruskal’s Algorithm
2 19
9
14 17
8 25
5
21 13 1
Kruskal’s Algorithm
2 19
9
14 17
8 25
5?
21 13 1
Kruskal’s Algorithm
2 19
9
14 17
8 25
5
21 13 1
Kruskal’s Algorithm
2 19
9
14 17
8? 25
5
21 13 1
Kruskal’s Algorithm
2 19
9
14 17
8 25
5
21 13 1
Kruskal’s Algorithm
2 19
9?
14 17
8 25
5
21 13 1
Kruskal’s Algorithm
2 19
9
14 17
8 25
5
21 13 1
Kruskal’s Algorithm
2 19
9
14 17
8 25
5
21 13? 1
Kruskal’s Algorithm
2 19
9
14 17
8 25
5
21 13 1
Kruskal’s Algorithm
2 19
9
14? 17
8 25
5
21 13 1
Kruskal’s Algorithm
2 19
9
14 17
8 25
5
21 13 1
Kruskal’s Algorithm
2 19
9
14 17?
8 25
5
21 13 1
Kruskal’s Algorithm
2 19?
9
14 17
8 25
5
21 13 1
Kruskal’s Algorithm
2 19
9
14 17
8 25
5
21? 13 1
Kruskal’s Algorithm
2 19
9
14 17
8 25?
5
21 13 1
Kruskal’s Algorithm
2 19
9
14 17
8 25
5
21 13 1
Kruskal’s Algorithm
2 19
9
14 17
8 25
5
21 13 1
Prim’s Algorithm
MST-Prim(G, w, r)
Q = V[G];
for each u ∈ Q
key[u] = ∞;
key[r] = 0;
p[r] = NULL;
while (Q not empty)
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6 4
Q = V[G]; 5 9
for each u ∈ Q
key[u] = ∞; 14
10 2
key[r] = 0;
15
p[r] = NULL;
while (Q not empty) 3 8
u = ExtractMin(Q);
for each v ∈ Adj[u] Run on example graph
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6 ∞ 4
Q = V[G]; 5 9
∞ ∞ ∞
for each u ∈ Q
key[u] = ∞; 14 2
10
key[r] = 0; 15
∞ ∞ ∞
p[r] = NULL;
while (Q not empty) 3 ∞ 8
u = ExtractMin(Q); Run on example graph
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6 ∞ 4
Q = V[G]; 5 9
∞ ∞ ∞
for each u ∈ Q
key[u] = ∞; 14 2
10
key[r] = 0; 15
r 0 ∞ ∞
p[r] = NULL;
while (Q not empty) 3 ∞ 8
u = ExtractMin(Q); Pick a start vertex r
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6 ∞ 4
Q = V[G]; 5 9
∞ ∞ ∞
for each u ∈ Q
key[u] = ∞; 14 2
10
key[r] = 0; 15
u 0 ∞ ∞
p[r] = NULL;
while (Q not empty) 3 ∞ 8
u = ExtractMin(Q); Red vertices have been removed from Q
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6 ∞ 4
Q = V[G]; 5 9
∞ ∞ ∞
for each u ∈ Q
key[u] = ∞; 14 2
10
key[r] = 0; 15
u 0 ∞ ∞
p[r] = NULL;
while (Q not empty) 3 3 8
u = ExtractMin(Q); Red arrows indicate parent pointers
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6 ∞ 4
Q = V[G]; 1 5 9
∞ ∞
for each u ∈ Q 4
key[u] = ∞; 14 2
10
key[r] = 0; 15
u 0 ∞ ∞
p[r] = NULL;
while (Q not empty) 3 3 8
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6 ∞ 4
Q = V[G]; 1 5 9
∞ ∞
for each u ∈ Q 4
key[u] = ∞; 14 2
10
key[r] = 0; 15
0 ∞ ∞
p[r] = NULL;
while (Q not empty) 3 3 8
u
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6 ∞ 4
Q = V[G]; 1 5 9
∞ ∞
for each u ∈ Q 4
key[u] = ∞; 14 2
10
key[r] = 0; 15
0 8 ∞
p[r] = NULL;
while (Q not empty) 3 3 8
u
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6 ∞ 4
Q = V[G]; 1 5 9
∞ ∞
for each u ∈ Q 0
key[u] = ∞; 14 2
10
key[r] = 0; 15
0 8 ∞
p[r] = NULL;
while (Q not empty) 3 3 8
u
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6 ∞ 4
Q = V[G]; 1 5 9
∞ ∞
for each u ∈ Q 0
key[u] = ∞; 14 2
10
key[r] = 0; 15
0 8 ∞
p[r] = NULL;
while (Q not empty) 3 3 8
u
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6 ∞ 4
Q = V[G]; 1 5 9
2 ∞
for each u ∈ Q 0
key[u] = ∞; 14 2
10
key[r] = 0; 15
0 8 ∞
p[r] = NULL;
while (Q not empty) 3 3 8
u
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6 ∞ 4
Q = V[G]; 1 5 9
2 ∞
for each u ∈ Q 0
key[u] = ∞; 14 2
10
key[r] = 0; 15 1
0 8
p[r] = NULL; 5
while (Q not empty) 3 3 8
u
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) ∞ u
6 4
Q = V[G]; 1 5 9
2 ∞
for each u ∈ Q 0
key[u] = ∞; 14 2
10
key[r] = 0; 15 1
0 8
p[r] = NULL; 5
while (Q not empty) 3 3 8
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) ∞ u
6 4
Q = V[G]; 1 5 9
2 9
for each u ∈ Q 0
key[u] = ∞; 14 2
10
key[r] = 0; 15 1
0 8
p[r] = NULL; 5
while (Q not empty) 3 3 8
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 4 u
6 4
Q = V[G]; 1 5 9
2 9
for each u ∈ Q 0
key[u] = ∞; 14 2
10
key[r] = 0; 15 1
0 8
p[r] = NULL; 5
while (Q not empty) 3 3 8
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 4 u
6 4
Q = V[G]; 5 9
5 2 9
for each u ∈ Q
key[u] = ∞; 14 2
10
key[r] = 0; 15 1
0 8
p[r] = NULL; 5
while (Q not empty) 3 3 8
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
u
MST-Prim(G, w, r) 6 4 4
Q = V[G]; 5 9
5 2 9
for each u ∈ Q
key[u] = ∞; 14 2
10
key[r] = 0; 15 1
0 8
p[r] = NULL; 5
while (Q not empty) 3 3 8
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) u 4
6 4
Q = V[G]; 5 9
5 2 9
for each u ∈ Q
key[u] = ∞; 14 2
10
key[r] = 0; 15 1
0 8
p[r] = NULL; 5
while (Q not empty) 3 3 8
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
u
MST-Prim(G, w, r) 6 4 4
Q = V[G]; 5 9
5 2 9
for each u ∈ Q
key[u] = ∞; 14 2
10
key[r] = 0; 15 1
0 8
p[r] = NULL; 5
while (Q not empty) 3 3 8
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6 4 4
Q = V[G]; 5 9
5 2 9
for each u ∈ Q
key[u] = ∞; 14 2 u
10
key[r] = 0; 15 1
0 8
p[r] = NULL; 5
while (Q not empty) 3 3 8
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Simplified Algorithm
Dijkstra’s Algorithm
Shortest Paths Problems
1
v t
5 4 9
8 13
u 3 4 y
x
2 10
1 1 2
w z
6
Relax Relax
2 2
5 7 5 6
Dijkstra’s Algorithm
Dijkstra(G)
for each v ∈ V
d[v] = ∞;
d[s] = 0; S = ∅; Q = V;
while (Q ≠ ∅)
u = ExtractMin(Q);
S = S U {u};
for each v ∈ Adj[u]
if (d[v] > d[u]+w(u,v))
Relaxation
d[v] = d[u]+w(u,v); Step
Example
u v
1
∞ ∞
10
9
2 3
s 0 4 6
5 7
∞ ∞
2
x y
Example
u v
1 1
∞
0
10
9
2 3
s 0 4 6
5 7
5 ∞
2
x y
Example
u v
1 1
8
4
10
9
2 3
s 0 4 6
5 7
5 7
2
x y
Example
u v
1 1
8
3
10
9
2 3
s 0 4 6
5 7
5 7
2
x y
Example
u v
1
8 9
10
9
2 3
s 0 4 6
5 7
5 7
2
x y
Example
u v
1
8 9
10
9
2 3
s 0 4 6
5 7
5 7
2
x y
Example
B
10 2
source A 4 3 D
5 1
C
Ex: run the algorithm
All Pairs Shortest Path
Floyd-Warshall Algorithm
Intermediate Vertices
Without loss of generality, we will assume that
V={1,2,…,n}, i.e., that the vertices of the graph
are numbered from 1 to n.
129
Intermediate Vertices
Consider a shortest path p from i to j such that
the intermediate vertices are from the set
{1,…,k}.
● If the vertex k is not an intermediate vertex on p,
then dij(k) = dij(k-1)
● If the vertex k is an intermediate vertex on p,
then dij(k) = dik(k-1) + dkj(k-1)
Interestingly, in either case, the subpaths contain merely nodes
from {1,…,k-1}.
Shortest Path
Therefore, we can conclude that
132
Floyd-Warshall Algorithm
134
Example
5 1 2 3
D(0)
1 3
1 0 8 5
8
3 2 2 3 0 ∞
2 3 ∞ 2 0
Final Result
(k) (k-1)
● pij = pij if dij(k-1) ≤ dik(k-1) + dkj(k-1)
pkj(k-1) if dij(k-1) > dik(k-1) + dkj(k-1)
Example
5 1 2 3
D(1)
1 3
1 0 8 5
8
3 2 2 3 0 8
2 3 ∞ 2 0
1 2 3 1 2 3
P(0) P(1)
1 N 1 1 1 N 1 1
2 2 N N 2 2 N 1
3 N 3 N 3 N 3 N
Example
5 1 2 3
D(2)
1 3
1 0 8 5
8
3 2 2 3 0 8
2 3 5 2 0
1 2 3
P(2)
1 N 1 1
2 2 N 1
3 2 3 N
Example
5 1 2 3
D(3)
1 3
1 0 7 5
8
3 2 2 3 0 8
2 3 5 2 0
1 2 3
P(3)
Use this final predecessor 1 N 3 1
matrix to obtain the
optimal result 2 2 N 1
3 2 3 N
Printing the Shortest Path between vertices i & j
print( P, i, j)
{
if i = j then print i
else if pij = NIL then print “No Path”
else {
print(P, i, pij)
print j
}
}
Example
Floyd-Warshall Algorithm &
Transitive Closure of a Graph
Transitive closure of a directed graph
● The transitive closure of G is defined as the
graph G* = (V, E*), where E* = { (i, j) : there
is a path from vertex i to j in G}
Transitive closure of a directed graph