Professional Documents
Culture Documents
Introduc)on to Graphs
Topics to be Covered
3
Introduc2on
In 1736, the following problem was posed. In the town of Königsberg (now called
Kaliningrad), the river Pregel (Pregolya) flows around the island Kneiphof and then
divides into two branches (see Figure 20-1).
c C
d
g
A e
Königsberg D
a f
b
B
Star%ng at one land area, is dit possible to walk across all of the
g
c
e
bridges exactly once and return
A
to theD star%ng land area? 4
a b
f
Introduc2on
The river has four land areas (A, B, C, and D), as shown in the figure. These land areas
are connected using seven bridges, as shown in Figure 20-1. The bridges are labeled
a, b, c, d, e, f, and g. The Königsberg bridge problem is as follows: Starting at one land
area, is it possible to walk across all of the bridges exactly once and return to the start-
ing land area? In 1736, Euler represented the Königsberg bridge problem as a graph,
• In 1736, Euler represented the Königsberg bridge
as shown in Figure 20-2, and answered the question in the negative. This marked
(as recorded) the birth of graph theory.
problem as a graph …
C
g
c d
e
A D
a b
f
B
…FIGURE
and 20-2 answered
Graph representationthe ques=on
of Königsberg in the nega=ve
bridge problem
EXAMPLE 20-2
EXAMPLE 20-1
• A graph can be shown pictorially Figure 20-4 shows some examples of directed graphs.
Figure 20-3 shows some examples of undirected graphs.
1 2 1
1 2 3 0 1
2 3
4 5
3 4
7 4 5 5
4 6
3 6
G1 6
7
8
G2
• A graph 𝐻 = 𝑉! , 𝐸! is called
V (G )a subgraph
5 {1 Dallas
, 2, 3, 4, 5} of 𝐺 ifE (G ) 5 {(1, 2), (1, 4), (2,
1
Delhi
1
𝑉! ⊆ 𝑉, 𝐸! ⊆ 𝐸
FIGURE 20-3 Various undirected graphs
(3, 6), (4, 8), (5,
7
Let G be an undirected graph. Let u and v be two vertic
Graph Defini2ons and Nota2ons
10
Graph Defini2ons and Nota2ons
3 1 4 5
11
Graph Defini2ons and Nota2ons
13
Graph Representa2on: Adjacency Matrices
14
Graph Representa2on: Examples 1 2 3 4 5
590 Chapter 22 Elementary
1 Graph2Algorithms
5 1 0 1 0 0 1
1 2 2 1 5 3 4 2 1 0 1 1 1
3 2 4 3 01 12 03 14 05
3
14 22 55 3 41 00 11 10 00 11
15 24 25 14 51 32 4 52 11 10 01 11 01
16
Graph Representa2on: Examples 1 2 3 4 5
590 Chapter 22 Elementary
1 Graph2Algorithms
5 1 0 1 0 0 1
1 2 2 1 5 3 4 2 1 0 1 1 1
3 2 4 3 01 12 03 14 05
3
14 22 55 3 41 00 11 10 00 11
15 24 25 14 51 32 4 52 11 10 01 11 01
18
Depth First Traversal
1 4 7
2 5 8 10
3 6 9
19
Depth First Traversal: A Non-recursive Algorithm
dft(graph, v, visited) {
visited[v] = true;
cout << v;
for (p = graph[v]; p; p = p->next;)
if (!visited[p->vertex])
dft(graph, p->vertex, visited);
}
visited[1 .. n] = false;
for (v = 1; v ≤ n; v++)
if (!visited[v])
dft(graph, v, visited); 23
Breadth First Traversal
1 4 7
2 5 8 10
3 6 9
24
Breadth First Traversal: Algorithm
if 𝒗 is an unvisited vertex
2 5 8 10
Add 𝒗 to the queue
Mark 𝒗 as visited 3 6 9
27
Spanning Trees: Example Ap
b
a
h
i
e
Root
d
GURE 20-19 Connected graphs that each have four vertices and three edges
28
Spanning Trees: Some Observa2ons
29
Spanning Trees: Algorithm
30
The DFS Spanning Tree
dfsTree(𝒗)
Mark 𝒗 as visited
for each vertex 𝒖 to which 𝒗 is adjacent
if 𝒖 is an unvisited vertex
Mark the edge between 𝒗 and 𝒖
dfsTree(𝒖)
31
The BFS Spanning Tree
bfsTree(𝒗)
Add 𝒗 to an empty queue
Mark 𝒗 as visited
while the queue is not empty
Extract 𝒖 from the queue
for each vertex 𝒘 to which 𝒖 is adjacent
if 𝒘 is an unvisited vertex
Mark edge between 𝒖 and 𝒘
Add 𝒘 to the queue
Mark 𝒘 as visited
32
Minimum Spanning Trees
33
Example
1 1
𝑎 𝑏 𝑎 𝑏
2 2
5
𝑐 𝑑 𝑐 𝑑
3 3
𝐺𝑟𝑎𝑝ℎ 𝑤 𝑇& = 6
1 1
𝑎 𝑏 𝑎 𝑏
2
5 5
𝑐 𝑑 𝑐 𝑑
3
𝑤 𝑇' = 9 𝑤 𝑇2 = 8
34
Minimum Spanning Trees: Prim’s Algorithm
35
An Outline of Prim’s Algorithm
Prim(𝐺 = 𝑉, 𝑇 )
𝑉3 = 𝑣&
𝐸3 = ∅
for 𝑖 = 1 to 𝑉 − 1
Find a minimum-weight edge 𝑒 ∗ = 𝑢∗ , 𝑣 ∗ among all
edges 𝑢, 𝑣 such that 𝑢 ∈ 𝑉3 , 𝑣 ∈ 𝑉\𝑉3
𝑉3 = 𝑉3 ∪ 𝑣 ∗
𝐸3 = 𝐸3 ∪ 𝑒 ∗
return 𝐺3 = 𝑉3 , 𝐸3
36
An Implementa2on of Prim’s Algorithm
38
𝑏 8 𝑐 7 𝑑
⊗, ∞ ⊗, ∞ ⊗, ∞
4 9
2
𝑎 𝑖 4 𝑒
11 14
⊗, ∞ ⊗, ∞ ⊗, ∞
7 6
8 10
ℎ 𝑔 𝑓
⊗, ∞ 1 ⊗, ∞ 2 ⊗, ∞
39
Prim(G, root) {
for (each vertex v Î V) {
v.dist = ¥;
v.parent = NIL;
}
root.dist = 0; VT = ∅;
createQueue(pQ, V);
while (!isEmpty(pQ)) {
v* = extractQueue(pQ);
add v* to VT;
for (each v Î pQ that is adjacent to v*)
if (G[v*][v] < v.dist) {
v.dist = G[v*][v];
v.parent = v*;
updateQueue(pQ, v);
}
} 40
}
Single-Source Shortest-Paths Problem
41
Dijkstra’s Algorithm: The General Idea
45
𝑏 8 𝑐 7 𝑑
⊗, ∞ ⊗, ∞ ⊗, ∞
4 9
2
𝑎 𝑖 4 𝑒
11 14
⊗, ∞ ⊗, ∞ ⊗, ∞
7 6
8 10
ℎ 𝑔 𝑓
⊗, ∞ 1 ⊗, ∞ 2 ⊗, ∞
46
Dijkstra(G(V, E), source) {
for (each vertex v Î V) {
v.dist = ¥;
v.parent = NIL;
}
source.dist = 0; VT = Æ;
createQueue(pQ, V);
while (!isEmpty(pQ)) {
v* = extractQueue(pQ);
add v* to VT;
for (each v Î pQ that is adjacent to v*)
if (v*.dist + G[v*][v] < v.dist) {
v.parent = v*;
v.dist = v*.dist + G[v*][v];
updateQueue(pQ, v);
}
} 47
}