Chapter 7: Graph
Chapter 7: Graph
1
OBJECTIVE
• Introduces:
Graph terminology and concept
Graph storage structures
Traversing operation
Determining shortest path
2
CONTENTS
• Introductions
• Graph storage structures
• Graph Traversing
• Networks
– Shortest Distance
– Spanning Tree
3
Introduction
4
Cities relationship
WWW relationship
6
• In general, the graph has always been used
because it represents cycle and also has more than
one connection.
• Graphs maybe indirected or directed:
Directed Graph (or digraph) –each line called an arc,
has a direction indicating how it may be traversed.
Figure 1(a) shows the example of directed graph.
Undirected graph –there is no direction on the lines
known as edges and it may be traversed in either
direction. Figure 1(b) shows the example of undirected
graph.
7
Figure 1: Graphs
9
• Example: 4 Simple cycle:
{1, 2, 3, 1}
Not cycle 5 {4, 5, 6, 7, 4}
{1, 2, 3, 4, 6, 8} {4, 5, 6, 4}
{4, 6, 7, 4}
4
6
2
7
8
3 2 Complex cycle:
{1, 2, 1}
1 {4, 5, 6, 4, 7, 6, 4}
11
• The degree of a vertex is the number of lines incident
to it.
12
Exercise:
C
E G
A
B F H
13
Graph Storage Structures
14
Array: Adjacency Matrix
• For a graph with n node (1, 2, 3, 4, ... , n), the
adjacency matrix is the (n x n) matrix, in which, if
there is an edge between vertices, the entry in row i and
column j is 1 (true) and is 0 (or false) otherwise.
16
• Limitations in the adjacency matrix:
The size of the graph must be known before the
program starts
Only one edge can be stored between any two
vertices
17
Linked list: Adjacency List
• Use a linked list to store the vertices. The pointer at the left
of the list linked the vertex entries. In this method, the
vertex list is a singly linked list of the vertices in the list.
An adjacency list is shown in the figure below:
• Example:
18
Exercise
• What is the adjacency matrix of the weighted directed graph below.
10
10 F
50
C
B 20
40
20
A 10
80
H
50 90 20 10
D
20
30
E G
19
Traversing Graph
20
• Example: Depth-first traversal and breadth-first
traversal of a tree
2 3 4
5 6 7 8
Depth-first traversal: 1 2 5 6 3 4 7 8
Breadth-first traversal: 1 2 3 4 5 6 7 8
21
• Example: Depth-first traversal and breadth-first
traversal of an undirected graph
22
Algorithm:
1)Push the first vertex, A, into the stack.
2)Loop, pop the stack, and after processing the vertex, push all of
the adjacent vertices into the stack, process it, and then push G
and H into the stack, shown as in the following diagram.
3)When the stack is empty, the traversal is complete.
23
Depth-first traversal: A X H P E Y M J G
Stack contents:
24
• Breadth-first traversal of an undirected graph, we process
all adjacent vertices of a vertex before going to the next
level. Begin by picking a starting vertex; after processing
it, we process all of its adjacent vertices. After we process
all of the first vertex’s adjacent vertices, we pick the first
adjacent vertex and process all of its vertices, then the
second adjacent vertex and process all of its vertices, and
so forth until we are finished.
25
• Algorithm:
1) Enqueue vertex A in the queue.
2) Loop, dequeue the queue and process the vertex from the
front of the queue. After processing the vertex, place all
of its adjacent vertices into the queue. Therefore at Step
2, dequeue vertex X, process it, and then place vertices
G and H in the queue. In Step 3, process vertex G. When
the queue is empty, the traversal is complete.
26
Breadth-first traversal: A X G H P E M Y J
Queue contents:
27
Example: Depth-first traversal and breadth-first traversal of a
directed graph.
28
Depth-first: 0 1 2 4 3 5 6 8 10 7 9
Breadth-first: 0 1 5 2 3 6 4 8 10 7 9
29
• Example: Disjoint Graph – Depth-first
Depth-first traversal: 1 2 6 5 7 8 3 4 9 10 11 12 13 14 30
• Example: Disjoint Graph – Breadth-first
Breadth-first traversal: 1 5 4 3 2 6 7 8 9 10 11 12 13 14
31
Networks
• A network is a graph where lines are weighted also
known as weighted graph.
• The meaning of the weights depends on the application
such as mileage, time, or cost.
7
2 3
3 5
10 8
1 4
2
5 6
6 5
7
33
• Pseudocode:
1) S = {1}
2) Dist[2..n] = Edge[1][2 .. n]
3) for (i = 1; i <= n-1)
3.1 Choose W = S for Dist[W] is the minimum
then S = S+{W}
3.2 For all vertex J =/ S, then
Dist[J] = min(Dist[j], Dist[W]+ Edge[W][J])
/
34
• Example:
1
30 100
50
2 40 6
40 30
70
10
3 5
10 20
4
35
• Output:
Shortest paths between vertex 1 and all other vertices:
Vertex Path Length Predecessor
2 30 1
3 60 4
4 50 1
5 40 1
6 90 3
36
DIJKSTRA'S ALGORITHM - WHY USE
IT?
• As mentioned, Dijkstra’s algorithm
calculates the shortest path to every vertex.
• However, it is about as computationally
expensive to calculate the shortest path
from vertex u to every vertex using Dijkstra’s
as it is to calculate the shortest path to
some particular vertex v.
• Therefore, anytime we want to know the
optimal path to some other vertex from a
determined origin, we can use Dijkstra’s
algorithm.
Applications of Dijkstra's Algorithm
- Traffic Information Systems are most
prominent use
- Mapping (Map Quest, Google Maps)
- Routing Systems
Applications of Dijkstra's Algorithm
One particularly relevant this
week: epidemiology
Prof. Lauren Meyers (Biology
Dept.) uses networks to model the
spread of infectious diseases and
design prevention and response
strategies.
Vertices represent individuals,
and edges their possible contacts.
It is useful to calculate how a
particular individual is connected to
others.
Knowing the shortest path
lengths to other individuals can be
a relevant indicator of the potential
of a particular individual to infect
others.
Exercise
• Use Dijkstra's algorithm to determine the lowest cost path from vertex A
to all of the other vertices in the graph.
• What is the shortest and total path from A to H?
10
10 F
50
C
B 20
40
20
A 10
80
H
50 90 20 10
D
20
30
E G
40
Spanning Tree
41
Problem: Laying Telephone Wire
Central office
42
Wiring: Naïve Approach
Central office
Expensive!
43
Wiring: Better Approach
Central office
44
Example :
• To determine the minimum spanning tree
5
B D
6 3
A 2 2 F
3
3 5
C E
4
45
• We can start with any vertex, let start with A.
• Then, add the vertex that gives the minimum-weighted edge with A, AC.
• The edge AB has a weight of 6, the edge BC has a weight of 2, the edge
CD has a weight of 3, and the edge CE has a weight of 4.
• Therefore the minimum-weighted edge is BC.
• The general process, use the following rule:
From all the vertices in the tree, select the edge with the minimal value to a
vertex not currently in the tree and insert it into the tree.
• Using this rule: add CD (weight 3), DE (weight 2), and DF(weight 3) in
turn. The steps graphically shown in Figure 9.
46
B
A A 2
A
3 3
C C
(a) Insert first vertex (b) Insert edge AC (c) Insert edge BC
B D B D
A 2 A 2 2
3 3
3 3
C C E
5
B D B D
3 6 3
A 2 2 F A 2 2 F
3 3
3 3 5
C E C E
4
(f) Insert edge DF
(e) The final tree in the graph
9 b
a 2 6 F = {a}, {b}, {c}, {d}, {e}
d A=
4 5
5 4
E = {(a,d), (c,d), (d,e), (a,c),
5 e (b,e), (c,e), (b,d), (a,b)}
c
48
Kruskal’s algorithm
For each edge (u,v) E in increasing order
while more than one set remains:
If u and v, belong to different sets U and V
a. add edge (u,v) to the safe edge set
A = A {(u,v)}
b. merge the sets U and V
F = F - U - V + (U V)
Return A
49
Kruskal’s algorithm
9 b
a 2 6
E = {(a,d), (c,d), (d,e), (a,c),
d
5 4
4 5 (b,e), (c,e), (b,d), (a,b)}
5 e
c
Forest A
{a}, {b}, {c}, {d}, {e}
{a,d}, {b}, {c}, {e} {(a,d)}
{a,d,c}, {b}, {e} {(a,d), (c,d)}
{a,d,c,e}, {b} {(a,d), (c,d), (d,e)}
{a,d,c,e,b} {(a,d), (c,d), (d,e), (b,e)}
50
Kruskal’s Algorithm Invariant
51
Exercise
• Develop minimum spanning tree based on the directed graph below.
10
10 F
50
C
B 20
40
20
A 10
80
H
50 90 20 10
D
20
30
E G
52