Unit 6 GRAPHS
Unit 6 GRAPHS
Data Structures
1
WHAT IS A GRAPH?
edge
Example
B
Vertices E
A D
Edges
C
Undirected Graph with 5 vertices and 7 edges
Maxm number of edges in undirected graph n(n-1)/2
A D
5
DIRECTED VS. UNDIRECTED GRAPHS
6
DIRECTED VS. UNDIRECTED GRAPHS
(CONT.)
When the edges in a graph have a direction, the
graph is called directed (or digraph)
if the graph is
directed, the
order of the
vertices in each
edge is important
8
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
9
GRAPH TERMINOLOGY
10
MORE TERMINOLOGY
simple path: no repeated vertices
a b
bec
c
d e
cycle: simple path, except that the last vertex is the same
as the first vertex
a b
acd
a ac d a
c
11
d e
EVEN MORE TERMINOLOGY
•connected graph: any two vertices are connected by some path
12
GRAPH TERMINOLOGY (CONT.)
What is the number of edges in a
complete directed graph with N
vertices?
N * (N-1)
13
GRAPH TERMINOLOGY (CONT.)
What is the number of edges in a complete
undirected graph with N vertices?
N * (N-1) / 2
14
GRAPH TERMINOLOGY (CONT.)
Weighted graph: a graph in which each edge
carries a value
15
GRAPH TRAVERSAL
Use the same depth-first and breadth-first traversal
algorithms seen for the binary trees.
Differences between graph and tree traversals:
1) Tree traversal always visit all the nodes in the tree
2 ) Graph traversal visits all the nodes in the graph only
when it is connected. Otherwise it visits only a subset of
the nodes. This subset is call the connected component of
the graph.
Recursive and iterative implementations of the algorithms.
Iterative: Use a stack for the depth-first search (dfs)
Use a queue for the breadth-first search (bfs) 16
GRAPH IMPLEMENTATION
Array-based implementation
A 1D array is used to represent the vertices
A 2D array (adjacency matrix) is used to represent the
edges
17
ARRAY-BASED IMPLEMENTATION
18
GRAPH IMPLEMENTATION (CONT.)
Linked-list implementation
A 1D array is used to represent the vertices
A list is used for each vertex v which
contains the vertices which are adjacent from
v (adjacency list)
19
LINKED-LIST IMPLEMENTATION
20
ADJACENCY MATRIX VS.
ADJACENCY LIST REPRESENTATION
Adjacency matrix
Good for dense graphs
Memory requirements( Consider all edges)
Connectivity between two vertices can be tested quickly
Adjacency list
Good for sparse graphs
Memory requirements(Consider only connected edges)
Vertices adjacent to another vertex can be found quickly
21
GRAPH SEARCHING
22
DEPTH-FIRST-SEARCH (DFS)
What is the idea behind DFS?
Travel as far as you can down a path
Back up as little as possible when you reach a "dead
end" (i.e., next vertex has been "marked" or there is
no next vertex)
DFS can be implemented efficiently using a
stack
23
DEPTH-FIRST-SEARCH (DFS) (CONT.)
Set found to false
stack.Push(startVertex)
DO
stack.Pop(vertex)
IF vertex == endVertex
Set found to true
ELSE
Push all adjacent vertices onto stack
WHILE !stack.IsEmpty() AND !found
IF(!found)
Write "Path does not exist"
24
WALK-THROUGH
Visited Array
F C A
A B
B C
D
H D
E
G E F
G
H
D, C
No nodes adjacent to C; cannot
continue backtrack, i.e., pop stack 29
50
Can be used to attempt to visit all nodes of a
graph in a systematic manner
Works with directed and undirected graphs
Steps
Breadth-first search starts with given node
Then visits nodes adjacent in some specified order
(e.g., alphabetical)
51
BREADTH-FIRST-SEARCHING (BFS)
(CONT.)
BFScan be implemented efficiently using a
queue IF(!found)
Write "Path does not exist"
Set found to false
queue.Enqueue(startVertex)
DO
queue.Dequeue(vertex)
IF vertex == endVertex
Set found to true
ELSE
Enqueue all adjacent vertices onto queue
WHILE !queue.IsEmpty() AND !found
52
Walk-Through Enqueued Array
F C A
A B Q
B C
D
H D
E
G E F
G
H
F C A
A B QD
B C
D
H D √
E
G E F
G
Nodes visited: H
F C A
A B QCEF
B C √
D
H D √
E √
G E F √
G
Nodes visited: D H
F C A
A B QEF
B C √
D
H D √
E √
G E F √
G
Nodes visited: D, C H
F C A
A B QFG
B C √
D
H D √
E √
G E F √
G
Nodes visited: D, C, E H
F C A
A B QG
B C √
D
H D √
E √
G E F √
G √
Nodes visited: D, C, E, F H
F C A
A B QH
B C √
D
H D √
E √
G E F √
G √
Nodes visited: D, C, E, F, G H √
F C A √
A B √ QA B
B C √
D
H D √
E √
G E F √
G √
Nodes visited: D, C, E, F, G, H H √
F C A √
A B √ QB
B C √
D
H D √
E √
G E F √
G √
Nodes visited: D, C, E, F, G, H, A H √
F C A √
A B √ Q empty
B C √
D
H D √
E √
G E F √
G √
Nodes visited: D, C, E, F, G, H, H √
A, B
Dequeue B. Visit B. Enqueue unenqueued nodes
62
adjacent to B.
Walk-Through Enqueued Array
F C A √
A B √ Q empty
B C √
D
H D √
E √
G E F √
G √
Nodes visited: D, C, E, F, G, H, H √
A, B
Q empty. Algorithm done.
63
SINGLE-SOURCE SHORTEST-PATH
PROBLEM
64
SINGLE-SOURCE SHORTEST-PATH
PROBLEM (CONT.)
Common algorithms: Dijkstra's algorithm,
Bellman-Ford algorithm
BFS can be used to solve the shortest graph
problem when the graph is weightless or all the
weights are the same
(mark vertices before Enqueue)
65
Dijkstra(L[1…n,1…n]):array[2….n]
Array D[2….n]
For i= 2 to n do D[i]=L[1,i]
For w belongs to C do
D[w]=min(D[w],D[v]+L[v,w])
Return D 66
void dij(int n,int s,int cost[10][10],int dist[])
{ int i,u,count,v,flag[10],min;
for(i=1;i<=n;i++)
{ flag[i]=0; dist[i]=cost[s][i];
} count=2;
while(count<=n)
{ min=99;
for(v=1;v<=n;v++)
if(dist[v]<min && !flag[v])
{ min=dist[v]; u=v; }
flag[u]=1;
count++;
for(v=1;v<=n;v++)
if((dist[u]+cost[u][v]<dist[v]) && !flag[v])
dist[v]=dist[u]+cost[u][v];
} 67
}
MINIMUM SPANNING TREES
Prim’s Algorithm
• Similar to Dijkstra’s Algorithm
Kruskal’s Algorithm
• Focuses on edges, rather than nodes
68
DEFINITION
A Minimum Spanning Tree (MST) is a subgraph
of an undirected graph such that the subgraph
spans (includes) all nodes, is connected, is
acyclic, and has minimum total edge weight
69
Minimum Spanning Tree is concerned with connected
undirected graphs.
70
Example of Minimum Spanning Tree
•a •b •d
•c •e •f
•a •b •d •a •b •d •a •b •d
•c •e •f •c •e •f •c •e •f
• The following figure shows a graph G1 together with its three possible
71
minimum spanning trees.
WHAT IS A MINIMUM-COST SPANNING
TREE
• Minimum-Cost spanning tree is concerned with edge-weighted
connected undirected graphs.
• For an edge-weighted , connected, undirected graph, G, the total
cost of G is the sum of the weights on all its edges.
• A minimum-cost spanning tree for G is a minimum spanning tree
of G that has the least total cost.
72
CONSTRUCTING MINIMUM SPANNING TREE
74
PRIM’S ALGORITHM
75
PRIM’S ALGORITHM
Prim’s algorithm finds a minimum cost spanning tree by selecting
edges from the graph one-by-one as follows:
It starts with a tree, T, consisting of the starting vertex, x.
Then, it adds the shortest edge emanating from x that connects T
to the rest of the graph.
It then moves to the added vertex and repeat the process.
78
2 Update distances of
adjacent, unselected nodes
3
10
F C K dv pv
A 7
4
3 A
8 B
18
4
9
B D C 3 D
10
H 25 D T 0
2
3 E 25 D
G 7
E F 18 D
G 2 D
H
79
2 Select node with
minimum distance
3
10
F C
A 7 3 K dv pv
8 4
18 A
4
9
B D B
10
H 25
C 3 D
2
3 D T 0
G 7
E E 25 D
F 18 D
G T 2 D
H
80
2 Update distances of
adjacent, unselected nodes
3
10
F C K dv pv
A 7
4
3 A
8 B
18
4
9
B D C 3 D
10
H 25 D T 0
2
3 E 7 G
G 7
E F 18 D
G T 2 D
H 3 G
81
2 Select node with
minimum distance
3
10
F C K dv pv
A 7
4
3 A
8 B
18
4
9
B D C T 3 D
10
H 25 D T 0
2
3 E 7 G
G 7
E F 18 D
G T 2 D
H 3 G
82
2 Update distances of
adjacent, unselected nodes
3
10
F C K dv pv
A 7
4
3 A
8 B 4 C
18
4
9
B D C T 3 D
10
H 25 D T 0
2
3 E 7 G
G 7
E F 3 C
G T 2 D
H 3 G
83
2 Select node with
minimum distance
3
10
F C K dv pv
A 7
4
3 A
8 B 4 C
18
4
9
B D C T 3 D
10
H 25 D T 0
2
3 E 7 G
G 7
E F T 3 C
G T 2 D
H 3 G
84
2 Update distances of
adjacent, unselected nodes
3
10
F C K dv pv
A 7
4
3 A 10 F
8 B 4 C
18
4
9
B D C T 3 D
10
H 25 D T 0
2
3 E 2 F
G 7
E F T 3 C
G T 2 D
H 3 G
85
2 Select node with
minimum distance
3
10
F C K dv pv
A 7
4
3 A 10 F
8 B 4 C
18
4
9
B D C T 3 D
10
H 25 D T 0
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H 3 G
86
2 Update distances of
adjacent, unselected nodes
3
10
F C K dv pv
A 7
4
3 A 10 F
8 B 4 C
18
4
9
B D C T 3 D
10
H 25 D T 0
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H 3 G
Table entries unchanged
87
2 Select node with
minimum distance
3
10
F C K dv pv
A 7
4
3 A 10 F
8 B 4 C
18
4
9
B D C T 3 D
10
H 25 D T 0
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H T 3 G
88
2 Update distances of
adjacent, unselected nodes
3
10
F C K dv pv
A 7
4
3 A 4 H
8 B 4 C
18
4
9
B D C T 3 D
10
H 25 D T 0
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H T 3 G
89
2 Select node with
minimum distance
3
10
F C K dv pv
A 7
4
3 A T 4 H
8 B 4 C
18
4
9
B D C T 3 D
10
H 25 D T 0
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H T 3 G
90
2 Update distances of
adjacent, unselected nodes
3
10
F C K dv pv
A 7
4
3 A T 4 H
8 B 4 C
18
4
9
B D C T 3 D
10
H 25 D T 0
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H T 3 G
Table entries unchanged
91
2 Select node with
minimum distance
3
10
F C K dv pv
A 7
4
3 A T 4 H
8 B T 4 C
18
4
9
B D C T 3 D
10
H 25 D T 0
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H T 3 G
92
2 Cost of Minimum
Spanning Tree = dv = 21
3
F C K dv pv
A 4
3 A T 4 H
B T 4 C
4
B D C T 3 D
H D T 0
2
3 E T 2 F
G E F T 3 C
G T 2 D
H T 3 G
Done
93
Kruskal’s Algorithm
94
KRUSKAL'S ALGORITHM.
Kruskal’ÿ algorithm also finds the minimum cost spanning tree of
a graph by adding edges one-by-one.
95
Walk-Through
Consider an undirected, weight graph
3
10
F C
A 4
4
3
8
6
5
4
B D
4
H 1
2
3
G 3
E
96
Sort the edges by increasing edge weight
3
10
F C edge dv edge dv
A 4
4
3 (D,E) 1 (B,E) 4
8
6
5 (D,G) 2 (B,F) 4
4
B D
4
H 1
(E,G) 3 (B,H) 4
2
3 (C,D) 3 (A,H) 5
G 3
E
(G,H) 3 (D,F) 6
(C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
97
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
8 4
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
98
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
99
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
100
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
101
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
102
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
103
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
104
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
105
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3
8 4 (D,E) 1 (B,E) 4
6
5
4
B D (D,G) 2 (B,F) 4
4
H 1 (E,G) 3 (B,H) 4
2
3 (C,D) 3 (A,H) 5
G 3
E
(G,H) 3 (D,F) 6
(C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
106
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
107
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
108
Select first |V|–1 edges which do not
generate a cycle
edge dv edge dv
3
F C (D,E) 1 (B,E) 4
A 4
3
(D,G) 2 (B,F) 4
5
B D (E,G) 3 (B,H) 4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G E
(C,F)
(B,C)
3
4
(A,B)
(A,F)
8
10
}
not
Done considered
111