Algorithm Analysis and Design: Divide & Conquer and Greedy Strategy
Algorithm Analysis and Design: Divide & Conquer and Greedy Strategy
Design
MODULE 3
D I V I D E & C O N Q U E R A N D G R E E D Y S T R AT E G Y
100 20 15 30 5 75 40
100 20 15 30 5 75 40
100 20 15 30 5 75
20 100 15 30 5 75
15 20 30 100 5 40 75
5 15 20 30 40 75 100
NEENU R, AP, CSE 9
Merge Sort
Algorithm MergeSort(low, high) //a[low:high] is a global array to be sorted
1. if (low<high) then //Small(P) is true if there is only one element to sort. In this
case the list is already sorted.
2. {
3. mid = [(low+high)/2]; //Divide P into subproblems
5. MergeSort(mid+1,high);
6. Merge(low,mid,high); //combine the solutions .
7. }
T(n)=O(n log n)
Time – O(
Time
8 multiplications + 4 additions
8T(n/2) + 4
O()
(x1,x2,x3)
1 (1/2, 1/3, 1/4) 16.5 24.25
2 (1, 2/15, 0) 20 28.2
3 (0, 2/3, 1) 20 31
4 (0,1,1/2) 20 31.5
|E|C(|V|-1) - no of cycles
For a complete graph, no of spanning trees =
4 3
6 4 3
6
5
1 2 1 2
2 2
4 3 Cost= 13
4 Cost= 9 3 4 3
6
07/26/2022 NEENU R, AP, CSE 32
Applications
Design of networks including computer networks, telecommunications networks,
transportation networks, water supply networks, and electrical grids.
Taxonomy
Cluster analysis
Circuit designs
Image sgmentation.
.
.
.
Cost =5
Cost =10
Cost =16
Cost =23
Cost =30
Cost =39
7. A= A
8. UNION(u,v).
9. return A
Edge Weight
MST-KRUSKAL(G,w) 0-3 5
A={(0,3), (2,4),(3,5), (0,1),(1,4)}
1. A = {0,3,5,1 ,2, 4} {6} 2-4 5
Edge Weight
MST-KRUSKAL(G,w) 0-3 5
A={(0,3), (2,4),(3,5), (0,1),(1,4)}
1. A = {0,3,5,1 ,2, 4} {6} 2-4 5
Edge Weight
MST-KRUSKAL(G,w) 0-3 5
A={(0,3), (2,4),(3,5), (0,1),(1,4)}
1. A = {0,3,5,1 ,2, 4} {6} 2-4 5
Edge Weight
MST-KRUSKAL(G,w) 0-3 5
A={(0,3), (2,4),(3,5), (0,1),(1,4)}
1. A = {0,3,5,1 ,2, 4, 6} 2-4 5
Edge Weight
MST-KRUSKAL(G,w) 0-3 5
A={(0,3), (2,4),(3,5), (0,1),(1,4)}
1. A = {0,3,5,1 ,2, 4, 6} 2-4 5
Algorithms:
◦ Dijkstra’s algorithm
4 5
3 5
3
4 5
3 5
3
4 ∞
2 ∞𝟗
7
2 4
1
2
S 1
1
2
6 ∞
4 5
3 5
3
4 3 ∞
2 𝟗
7
2 4
1
2
S 1
1
2
6 ∞
4 5
3 5
3
3 ∞𝟔
2 8
7
2 4
1
2
S 1
1
2
6 ∞𝟏𝟏
4 5
3 5
3
3 𝟔
2 8
7
2 4
1
2
S 1
1
2
6 𝟏𝟏𝟗
4 5
3 5
3
3 𝟔
2 8
7
2 4
1
2
S 1
1
2
6 𝟗
4 5
3 5
3
3 𝟔
2. v.d = u. d + w(u,v).
3.
1. INITIALIZE-SINGLE-
SOURCE( G, s) 2 NIL
2. S =
3 NIL
3. Q = G.V
4 NIL
4. while Q
5. u= EXTRACT-MIN(Q) 5 NIL
6. s= s
6 NIL
7. for each vertex
8. RELAX(u, v, w). 7 NIL
8 NIL
0 0 NIL
DIJKSTRA(G, w, s ) 1 0
1. INITIALIZE-SINGLE-
SOURCE( G, s) 2 NIL
2. S =
3 NIL
3. Q = G.V
4. while Q 0 4 NIL
5. u= EXTRACT-MIN(Q)
5 NIL
6. s= s
7. for each vertex 6 NIL
8. RELAX(u, v, w).
7 0
8 NIL
8. RELAX(u, v, w).
7 0
8 NIL
1. INITIALIZE-SINGLE-
SOURCE( G, s) 4 2 1
2. S = 3 NIL
3. Q = G.V
4 NIL
4. while Q 0
5. u= EXTRACT-MIN(Q) 5 NIL
6. s= s
6 7
7. for each vertex
8. RELAX(u, v, w). 7 0
8
8 7
1. INITIALIZE-SINGLE-
SOURCE( G, s) 4 2 1
2. S = 3 NIL
3. Q = G.V
4 NIL
4. while Q 0
5. u= EXTRACT-MIN(Q) 5 6
6. s= s
7. for each vertex
6 7
8. RELAX(u, v, w).
8 9 7 0
8 7
1. INITIALIZE-SINGLE-
SOURCE( G, s) 4 2 1
2. S = 3 25 5
3. Q = G.V
4 5
4. while Q 0
5. u= EXTRACT-MIN(Q)
5 6
6. s= s
7. for each vertex 6 7
8. RELAX(u, v, w).
8 9 11 7 0
8 7
1. INITIALIZE-SINGLE-
SOURCE( G, s) 4 2 1
12
2. S = 3 19 2
3. Q = G.V
4 5
4. while Q 0
5. u= EXTRACT-MIN(Q) 5 6
6. s= s
6 7
7. for each vertex
8. RELAX(u, v, w). 7 0
8 9 11
8 2
1. INITIALIZE-SINGLE-
SOURCE( G, s) 4 2 1
12
2. S = 3 25 5
3. Q = G.V
4 5
4. while Q 0 14
5. u= EXTRACT-MIN(Q) 5 6
6. s= s
6 7
7. for each vertex
8. RELAX(u, v, w). 7 0
8 9 11
8 2
1. INITIALIZE-SINGLE-
SOURCE( G, s) 4 12 2 1
19
2. S =
3 19 2
3. Q = G.V
4. while Q 0 14 4 5
5. u= EXTRACT-MIN(Q)
6. s= s 5 6
8 2
07/26/2022 NEENU R, AP, CSE 75
Dijkstra’s algorithm –
Exercise
V v.d v.
0 0 NIL
DIJKSTRA(G, w, s ) 1 0
1. INITIALIZE-SINGLE-
SOURCE( G, s) 4 12 2 1
19
2. S =
3 19 2
3. Q = G.V
4. while Q 0 14
21 4 5
5. u= EXTRACT-MIN(Q)
5 6
6. s= s
7. for each vertex 6 7
8. RELAX(u, v, w).
8 9 11 7 0
8 2
4 12 19 2 1
3 19 2
21
0 4 5
14
5 6
6 7
8 9 11 7 0
8 2
07/26/2022 NEENU R, AP, CSE 77
Dijkstra’s algorithm - Analysis
DIJKSTRA(G, w, s )
1. INITIALIZE-SINGLE-SOURCE( G, s)
2. S =
3. Q = G.V
4. while Q ) V times
5. u= EXTRACT-MIN(Q)
O(V) in each iteration if O(
queue is implemented
6. s= s using array
7. for each vertex E times in the course of entire algorithm
8. RELAX(u, v, w).
If queue is implemented using Fibonacci heap, O(