Ada Chapt9 Greedy Technique
Ada Chapt9 Greedy Technique
ALGORITHMS
UNIT-IV
CHAPTER 9:
GREEDY TECHNIQUE
1
OUTLINE
Greedy Technique
Prim’s Algorithm
Kruskal’s Algorithm
Dijkstra’s Algorithm
2
Minimum Spanning Tree
Definition: A spanning tree of a connected graph is its connected
acyclic subgraph (i.e., a tree) that contains all the vertices of the
graph. A minimum spanning tree of a weighted connected graph is
its spanning tree of smallest weight, where the weight of a tree is
defined as the sum of the weights on all its edges. The minimum
spanning tree problem is the problem of finding a minimum spanning tree
for a given weighted connected graph.
Figure: Graph and its spanning trees: T1 is the minimum spanning tree.3
Try to use exhaustive search ?
4
GREEDY APPROACH
The greedy approach suggests constructing a solution
through a sequence of steps, each expanding a partially
constructed solution obtained so far, until a complete
solution to the problem is reached. On each step—and
this is the central point of this technique—the choice
made must be
The algorithm stops after all the graph’s vertices have been included
in the tree being constructed.
6
PRIM’S ALGORITHM
7
PRIM’S ALGORITHM
The nature of Prim’s algorithm makes it necessary to provide each
vertex not in the current tree with the information about the shortest
edge connecting the vertex to a tree vertex.
(1) The name of the nearest tree vertex and the length (the weight) of
the corresponding edge.
(2) Vertices that are not adjacent to any of the tree vertices can be
given the ∞ label indicating their “infinite” distance to the tree
vertices and a null label for the name of the nearest tree vertex.
With such labels, finding the next vertex to be added to the current
tree T={VT, ET} becomes a simple task of finding a vertex with the
smallest distance label in the set V - VT
8
PRIM’S ALGORITHM
After we have identified a vertex u* to be added to the tree,
we need to perform two operations:
9
PRIM‘S ALGORITHM
5
A B
4 6 2
2 D 3
C
3 1 2
E F
4
10
PRIM‘S ALGORITHM
5
A B
4 6 2
2 D 3
C
3 1 2
E F
4
11
PRIM‘S ALGORITHM
5
A B
4 6 2
2 D 3
C
3 1 2
E F
4
12
PRIM‘S ALGORITHM
A B
2
2 D 3
C
3 1 2
E F
4
13
PRIM‘S ALGORITHM
A B
2
2 D 3
C
3 1 2
E F
14
PRIM‘S ALGORITHM
A B
2
2 D 3
C
3 1 2
E F
15
PRIM‘S ALGORITHM
A B
2
2 D
C
3 1 2
E F
16
PRIM‘S ALGORITHM
A B
2
2 D
C
3 1 2
E F
17
PRIM‘S ALGORITHM
minimum- spanning tree
A B
2
2 D
C
3 1 2
E F
18
Figure: Application of Prim’s Algorithm. The parenthesized labels of a vertex in the
middle column indicate the nearest tree vertex and edge weight; selected vertices
and edges are shown in bold.
b 1 c
3 4 4 6
a 5 5
f d
6 2 8
e
Tree vertices Remaining vertices Illustration
3 b 1 c
a(-, -) b(a, 3) c(-, ∞) d(-, ∞) e(a, 6)
6
4 4
f(a, 5) a f d
5 5
2
6 8
e
3
b 1
c
6
b(a, 3) c(b, 1) d(-, ∞) e(a, 6) f(b, 4) 4 4
a f 5 d
5
2
6 8 19
e
Figure: Application of Prim’s Algorithm. The parenthesized labels of a vertex in the
middle column indicate the nearest tree vertex and edge weight; selected vertices
and edges are shown in bold.
Tree vertices Remaining vertices Illustration
3
b 1 c
c(b, 1) d(c, 6) e(a, 6) f(b, 4) 6
4 4
a 5 f 5 d
6 2 8
e
3 b 1
c
6
f(b, 4) d(f, 5) e(f, 2) 4 4
a f 5 d
5
2 8
6
e
3
b 1
c
6
e(f, 2) d(f, 5) 4 4
a f 5 d
5
2 8 20
6
d(f, 5) e
PRIM’S ALGORITHM ANALYSIS
21
PRIM’S ALGORITHM
A min-heap is a complete binary tree in which every element is less
than or equal to its children.
22
KRUSKAL’S ALGORITHM
This is another greedy algorithm for the minimum
spanning tree problem that also always yields an
optimal solution.
23
KRUSKAL’S ALGORITHM
The algorithm constructs a minimum spanning tree as an
expanding sequence of subgraphs, which are always
acyclic but are not necessarily connected on the
intermediate stages of the algorithm.
24
KRUSKAL’S ALGORITHM
25
KRUSKAL’S ALGORITHM ANALYSIS
On each of its iterations, Kruskal’s algorithm has to check whether the
addition of the next edge to the edges already selected would create a
cycle.
A new cycle is created if and only if the new edge connects two
vertices already connected by a path, i.e., if and only if the two
vertices belong to the same connected component as shown in below
figure.
Figure: New edge connecting two vertices may (a) or may not (b) create a cycle
2 D 3
C
3 1 2
E F
4
27
KRUSKAL‘S ALGORITHM
5
A B
4 6 2
2 D 3
C
3 1 2
E F
4
28
KRUSKAL‘S ALGORITHM
5
A B
4 6 2
2 D 3
C
3 1 2
E F
4
29
KRUSKAL‘S ALGORITHM
5
A B
4 6 2
2
C D 3
3 1 2
E F
4
30
KRUSKAL‘S ALGORITHM
5
A B
4 6 2
2 D 3
C
3 1 2
E F
4
31
KRUSKAL‘S ALGORITHM
5
A B
4 6 2
2 D 3 cycle!!
C
3 1 2
E F
4
32
KRUSKAL‘S ALGORITHM
5
A B
4 6 2
2 D 3
C
3 1 2
E F
4
33
KRUSKAL‘S ALGORITHM
5
A B
4 6 2
2 D 3
C
3 1 2
E F
4
34
KRUSKAL‘S ALGORITHM
minimum- spanning tree
A B
2
2 D
C
3 1 2
E F
35
Figure: Application of Kruskal’s Algorithm. Selected edges are shown in bold.
b 1 c
3 4 4 6
a 5 5
f d
6 2 8
e
Tree vertices Sorted list of edges Illustration
3 b 1 c
bc ef ab bf cf af df ae cd de
6
4 4
1 2 3 4 4 5 5 6 6 8 a f d
5 5
2
6 8
e
b 1
c
3 6
bc bc ef ab bf cf af df ae cd de 4 4
1 1 2 3 4 4 5 5 6 6 8 a f 5 d
5
2
6 8 36
e
Figure: Application of Kruskal’s Algorithm. Selected edges are shown in bold.
3
b 1 c
6
ef bc ef ab bf cf af df ae cd de 4 4
2 1 2 3 4 4 5 5 6 6 8 a 5 f 5 d
2
6 8
e
ab bc ef ab bf cf af df ae cd de 3 b 1
c
6
3 1 2 3 4 4 5 5 6 6 8 4 4
a f 5 d
5
2 8
6
e
bf bc ef ab bf cf af df ae cd 3
b 1
c
6
de 4 4
4 1 2 3 4 4 5 5 6 6 8 a f 5 d
5
df 2
6 8 37
5 e
KRUSKAL’S ALGORITHM ANALYSIS
With an efficient Union-find algorithm (check whether two
vertices belong to the same tree) , the running time of
Kruskal’s algorithm will be dominated by the time needed
for sorting the edge weights of a given graph.
38
DIJKSTRA’S ALGORITHM
single-source shortest-paths problem: for a given vertex
called the source in a weighted connected graph, find
shortest paths to all its other vertices.
40
DIJKSTRA’S ALGORITHM
Idea of Dijkstra’s algorithm
43
DIJKSTRA’S ALGORITHM
To facilitate the algorithm’s operations, we label each
vertex with two labels:
4
3 b c
a(-, 0) b(a, 3) c(-, ∞) d(a, 7) e(-,
6
2 5
∞) a d e
7 4
4
3
b c
b(a, 3) c(b, 3+4) d(b, 3+2) e(-, ∞) 6
2 5
a d e
7 4
46
Figure: Application of Dijkstra’s Algorithm. The next closest vertex
is shown in bold.
48
DIJKSTRA’S ALGORITHM ANALYSIS
The time efficiency of Dijkstra’s algorithm depends on the
data structures used for implementing the priority queue
and for representing an input graph itself.
49
End of Chapter 9
50