0% found this document useful (0 votes)
25 views111 pages

Unit 6 GRAPHS

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views111 pages

Unit 6 GRAPHS

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 111

GRAPHS

Data Structures

1
WHAT IS A GRAPH?

 A data structure that consists of a set of nodes


(vertices) and a set of edges that relate the nodes to
each other
 The set of edges describes relationships among the
vertices
 An edge is a pair of vertices. As in mathematics, an
edge (x, y) is said to point or go from x to y.
 If two of the vertices are connected with edge, then
we say these two vertices are adjacent
 A graph may not have an edge from vertex to
vertex i.e.< v , v >
 A graph may not have multiple occurrence of same 2

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

 If graph has exact n(n-1)/2 edges then its complete


3
graph
Example
B
E

A D

 Directed Graph with 5 vertices and 7 edges


4
FORMAL DEFINITION OF GRAPHS
 A graph G is defined as follows:
G=(V,E)
V(G): a finite, nonempty set of vertices
E(G): a set of edges (pairs of vertices)

5
DIRECTED VS. UNDIRECTED GRAPHS

 When the edges in a graph have no direction, the


graph is called undirected

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

E(Graph2) = {(1,3) (3,1) (5,9) (9,11) (5,7)


TREES VS GRAPHS

 Trees are special cases of graphs!!

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

the out-degree of a vertex v is the number of edges


that have v as the tail

9
GRAPH TERMINOLOGY

 Adjacent nodes: two nodes are adjacent if they are


connected by an edge

 Path: a sequence of vertices that connect two nodes


in a graph

 Complete graph: a graph in which every vertex is


directly connected to every other vertex

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

connected not connected


 subgraph: subset of vertices and edges forming a graph
 .

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

 Problem: find a path between two nodes of the


graph (e.g., Austin and Washington)
 Methods: Depth-First-Search (DFS) or Breadth-
First-Search (BFS)

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

Task: Conduct a depth-first search of the


graph starting with node D
25
Walk-Through
Visited Array
F C A
A B
B C
D
H D √
E
G E F
G
H D
The order nodes are visited:
Visit D
D 26
Walk-Through
Visited Array
F C A
A B
B C
D
H D √
E
G E F
G
H D
The order nodes are visited:
Consider nodes adjacent to D, decide to
D
visit C first 27

Prefer Alphabetical order


Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
E
G E F
G C
H D
The order nodes are visited:
Visit C
D, C 28
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
E
G E F
G C
The order nodes are visited: H D

D, C
No nodes adjacent to C; cannot
continue  backtrack, i.e., pop stack 29

and restore previous state


Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
E
G E F
G
H D
The order nodes are visited:
Back to D – C has been visited,
D, C decide to visit E next30
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
E √
G E F
G E
H D
The order nodes are visited:
D, C, E
Back to D – C has been visited, 31

decide to visit E next


Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
E √
G E F
G E
H D
The order nodes are visited:
Only G is adjacent to E
D, C, E 32
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
E √
G E G
F
G √ E
H D
The order nodes are visited:
Visit G
D, C, E, G 33
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
E √
G E G
F
G √ E
H D
The order nodes are visited:
D, C, E, G Nodes D and H are adjacent to
G. D has already been 34

visited. Decide to visit H.


Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
H
E √
G E G
F
G √ E
H √ D
The order nodes are visited:
Visit H
D, C, E, G, H 35
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
H
E √
G E G
F
G √ E
H √ D
The order nodes are visited:
Nodes A and B are adjacent to F.
D, C, E, G, H Decide to visit A next.36
Walk-Through
Visited Array
F C A √
A B
B C √ A
D
H D √
H
E √
G E G
F
G √ E
H √ D
The order nodes are visited:
Visit A
D, C, E, G, H, A 37
Walk-Through
Visited Array
F C A √
A B
B C √ A
D
H D √
H
E √
G E G
F
G √ E
H √ D
The order nodes are visited:
Only Node B is adjacent to A.
D, C, E, G, H, A Decide to visit B next.38
Walk-Through
Visited Array
F C A √
A B √ B
B C √ A
D
H D √
H
E √
G E G
F
G √ E
H √ D
The order nodes are visited:
Visit B
D, C, E, G, H, A, B 39
Walk-Through
Visited Array
F C A √
A B √
B C √ A
D
H D √
H
E √
G E G
F
G √ E
H √ D
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B B. Backtrack (pop the stack).
40
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
H
E √
G E G
F
G √ E
H √ D
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B A. Backtrack (pop the stack).
41
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E G
F
G √ E
H √ D
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B H. Backtrack (pop the42
stack).
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E F
G √ E
H √ D
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B G. Backtrack (pop the43
stack).
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E F
G √
H √ D
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B E. Backtrack (pop the stack).
44
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E F
G √
H √ D
The order nodes are visited:
F is unvisited and is adjacent to
D, C, E, G, H, A, B D. Decide to visit F next.
45
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E F √
G √ F
H √ D
The order nodes are visited:
Visit F
D, C, E, G, H, A, B, F 46
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E F √
G √
H √ D
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B, F F. Backtrack. 47
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E F √
G √
H √
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B, F D. Backtrack. 48
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E F √
G √
H √
The order nodes are visited:
Stack is empty. Depth-first
D, C, E, G, H, A, B, F traversal is done. 49
BREADTH-FIRST-SEARCHING (BFS)
 What is the idea behind BFS?
 Look at all possible paths at the same depth before you
go at a deeper level
 Back up as far as possible when you reach a "dead end"
(i.e., next vertex has been "marked" or there is no next
vertex)

50
 Can be used to attempt to visit all nodes of a
graph in a systematic manner
 Works with directed and undirected graphs

 Works with weighted and unweighted 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

How is this accomplished? Simply replace the stack


with a queue! Rules: (1) Maintain an enqueued 53
array. (2) Visit node when dequeued.
Walk-Through Enqueued Array

F C A
A B QD
B C
D
H D √
E
G E F
G
Nodes visited: H

Enqueue D. Notice, D not yet visited.


54
Walk-Through Enqueued Array

F C A
A B QCEF
B C √
D
H D √
E √
G E F √
G
Nodes visited: D H

Dequeue D. Visit D. Enqueue unenqueued nodes


55
adjacent to D.
Walk-Through Enqueued Array

F C A
A B QEF
B C √
D
H D √
E √
G E F √
G
Nodes visited: D, C H

Dequeue C. Visit C. Enqueue unenqueued nodes


56
adjacent to C.
Walk-Through Enqueued Array

F C A
A B QFG
B C √
D
H D √
E √
G E F √
G
Nodes visited: D, C, E H

Dequeue E. Visit E. Enqueue unenqueued nodes


57
adjacent to E.
Walk-Through Enqueued Array

F C A
A B QG
B C √
D
H D √
E √
G E F √
G √
Nodes visited: D, C, E, F H

Dequeue F. Visit F. Enqueue unenqueued nodes


58
adjacent to F.
Walk-Through Enqueued Array

F C A
A B QH
B C √
D
H D √
E √
G E F √
G √
Nodes visited: D, C, E, F, G H √

Dequeue G. Visit G. Enqueue unenqueued nodes


59
adjacent to G.
Walk-Through Enqueued Array

F C A √
A B √ QA B
B C √
D
H D √
E √
G E F √
G √
Nodes visited: D, C, E, F, G, H H √

Dequeue H. Visit H. Enqueue unenqueued nodes


60
adjacent to H.
Walk-Through Enqueued Array

F C A √
A B √ QB
B C √
D
H D √
E √
G E F √
G √
Nodes visited: D, C, E, F, G, H, A H √

Dequeue A. Visit A. Enqueue unenqueued nodes


61
adjacent to A.
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
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

 There are multiple paths from a source vertex to a


destination vertex
 Shortest path: the path whose total weight (i.e., sum
of edge weights) is minimum
 Examples:
 Austin->Houston->Atlanta->Washington: 1560 miles
 Austin->Dallas->Denver->Atlanta->Washington: 2980
miles

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]

Initialize C={2,3,…..n}{ S=N\C}

For i= 2 to n do D[i]=L[1,i]

Loop n-2 times

v= {some element of C minimizing D[v] }

C=C\{v} {S=S union {v}}

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.

 The idea is to find another graph with the same number


of vertices, but with minimum number of edges.

 Let G = (V,E) be connected undirected graph. A


minimum spanning tree for G is a graph, T = (V’, E’)
with the following properties:
 V’ = V
 T is connected
 T is acyclic.

70
Example of Minimum Spanning Tree

• The following figure shows a graph G1 together with its three


possible minimum spanning trees.

•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

• A spanning tree can be constructed using any of the traversal


algorithms discussed, and taking note of the edges that are
actually followed during the traversals.

• The spanning tree obtained using breadth-first traversal is called


breadth-first spanning tree, while that obtained with depth-first
traversal is called depth-first spanning tree.

• For example, for the graph in figure 1, figure (c) is a depth-first


spanning tree, while figure (d) is a breadth-first spanning tree.

• In the rest of the lecture, we discuss two algorithms for


constructing minimum-cost spanning tree.
73
ALGORITHM CHARACTERISTICS

 Both Prim’s and Kruskal’s Algorithms work with


undirected graphs
 Both work with weighted and unweighted graphs but
are more interesting when edges are weighted
 Both are greedy algorithms that produce optimal
solutions

74
PRIM’S ALGORITHM

 Similar to Dijkstra’s Algorithm except that


dv( Distance of Vertex ) records edge weights, not path
lengths

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.

•Let T be a tree consisting of only the starting vertex x


•While (T has fewer than n vertices)
•{
• find the smallest edge connecting T to G-T
• add it to T
•}
76
WALK-THROUGH
2
Initialize array
3
10
F C K dv pv
A 7 3 A False  
8 4
18 B False  
4
9
B D C False  
10
H 25 D False  
2
3 E False  
G 7
E F False  
G False  
H False  

•dv( Distance of Vertex )


•Pv( Previous Vertex )
77
2
Start with any node, say D
3
10
F C K dv pv
A 7
4
3 A
8 B
18
4
9
B D C
10
H 25 D T 0 
2
3 E
G 7
E F
G
H

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

• Work with edges, rather than nodes


• Two steps:
– Sort edges by increasing edge weight
– Select the first |V| – 1 edges that do not
generate a cycle

94
KRUSKAL'S ALGORITHM.
 Kruskal’ÿ algorithm also finds the minimum cost spanning tree of
a graph by adding edges one-by-one.

 However, Kruskal’ÿ algorithm forms a forest of trees, which are


joined together incrementally to form the minimum cost spanning
tree.

•sort the edges of G in increasing order of weights


•form a forest by taking each vertex in G to be a tree
•for each edge e in sorted order
• if the endpoints of e are in separate trees
• join the two tree to form a bigger tree
•return the resulting single tree

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

Accepting edge (E,G) would create a cycle

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

Total Cost =  dv = 21 109


• Kruskal’s algorithm • Prim’s algorithm

1. Select the shortest edge in a 1. Select any vertex


network
2. Select the shortest edge
2. Select the next shortest edge connected to that vertex
which does not create a cycle
3. Select the shortest edge
3. Repeat step 2 until all vertices connected to any vertex
have been connected already connected

4. Repeat step 3 until all


vertices have been
connected 110
•Some points to note

•Both algorithms will always give solutions with


the same length.

•They will usually select edges in a different order

•Occasionally they will use different edges – this


may happen when you have to choose between
edges with the same length. In this case there is
more than one minimum connector for the
network.

111

You might also like