Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
44 views

Algorithm Analysis and Design: Divide & Conquer and Greedy Strategy

The document summarizes algorithms for divide and conquer and greedy strategies. It discusses merge sort and matrix multiplication as examples of divide and conquer. It also covers the fractional knapsack problem and provides an algorithm for its greedy solution in O(n log n) time.

Uploaded by

Aswin Sasikumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Algorithm Analysis and Design: Divide & Conquer and Greedy Strategy

The document summarizes algorithms for divide and conquer and greedy strategies. It discusses merge sort and matrix multiplication as examples of divide and conquer. It also covers the fractional knapsack problem and provides an algorithm for its greedy solution in O(n log n) time.

Uploaded by

Aswin Sasikumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 80

Algorithm Analysis and

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

NEENU R, AP, CSE 1


Syllabus
The Control Abstraction of Divide and Conquer- 2-way Merge sort, Strassen’s
Algorithm for Matrix Multiplication-Analysis.

The Control Abstraction of Greedy Strategy- Fractional Knapsack Problem,


Minimum Cost Spanning Tree Computation- Kruskal’s Algorithms - Analysis,
Single Source Shortest Path Algorithm - Dijkstra’s Algorithm-Analysis.

NEENU R, AP, CSE 2


Strategies for Problem Solving
 Divide and Conquer
 Dynamic Programming
 Greedy Method
 Backtracking
 Branch and Bound

NEENU R, AP, CSE 3


Divide and Conquer
Merge Sort
Strassen’s matrix multiplication

NEENU R, AP, CSE 4


Divide and Conquer
 Solves recursively.
 Divide a large problem into sub problems that are smaller instances of the same problem
(DIVIDE)
 Solve the sub problems(CONQUER)
 Combine the results of sub problems to get the solution of the original problem(COMBINE)

NEENU R, AP, CSE 5


Control Abstraction
DAC(P)
{
If(small(P))
{
S(P)
}
else
{
divide P into p1, P2, P3,…Pk
Apply DAC(P1), DAC(P2),…DAC(Pk)
Combine the solutions(DAC(P1), DAC(P2),…DAC(Pk))
}

NEENU R, AP, CSE 6


Divide and Conquer

NEENU R, AP, CSE 7


Problems using Divide and Conquer
 Binary search
 Maximum Subarray problem
 Merge Sort
 Quick Sort
 Strassen’s matrix multiplication

NEENU R, AP, CSE 8


Merge Sort
100 20 15 30 5 75 40

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

4. MergeSort (low,mid); //solve the subproblems.

5. MergeSort(mid+1,high);
6. Merge(low,mid,high); //combine the solutions .
7. }

NEENU R, AP, CSE 10


a[low:high] is a global array containing two sorted subsets in a[low:mid]
and in a[mid+1:high].
The goal is to merge these 2 sets into a single set residing in a[low:high].

Merge Sort b[] is an auxiliary global array.

10. if (h>mid) then


Algorithm Merge(low, mid, high)
11. for k=j to high do
1. h=low; i=low; j=mid+1;
2. while ((h<=mid) and (j<=high)) do
12. b[i]=a[k]; i=i+1;
3. { 13. else
4. if (a[h]<=a[j]) then 14. for k=h to mid do
5. b[i]=a[h]; h = h+1; 15. b[i]=a[k]; i=i+1;
6. else 16. for k=low to high do a[k]= b[k];
7. b[i]= a[j]; j=j+1;
8. i=i+1;
9. }
NEENU R, AP, CSE 11
Merge Sort - Analysis
The computing time for merge sort is described by the recurrence relation.

T(n)= ‘a’ and ‘c’ are constants.

NEENU R, AP, CSE 12


Merge Sort - Analysis
Solve this equation by iterative method.
T(n) =2(2T(n/4) +cn/2) +cn
= 4T(n/4)+2cn
= 4(2T(n/8)+cn/4)+2cn
…..
= 2^k T(1)+kcn.
= an + cn log n. 

T(n)=O(n log n)

NEENU R, AP, CSE 13


Matrix Multiplication
B

NEENU R, AP, CSE 14


Matrix Multiplication

Time – O(

NEENU R, AP, CSE 15


Matrix Multiplication using D&C
 The problem is small if the matrices to be multiplied is of the order 1 X 1. )
 B


4 units of time
 or
 8 multiplications
constant time

NEENU R, AP, CSE 16


Matrix Multiplication using D&C
 The problem is large if the matrices to be multiplied is of order greater than 2 X 2.
Assume matrices are square matrices of order in powers of 2. ie, 4 x 4, 8 X 8,…

 B 𝐴11 𝐴12 𝐵11 𝐵12


𝐴21 𝐴22 𝐵21 𝐵22

NEENU R, AP, CSE 17


Matrix Multiplication using D&C

Time
8 multiplications + 4 additions
8T(n/2) + 4
O()

NEENU R, AP, CSE 18


Strassen’s matrix multiplication
 reduced the number of multiplication from 8 to 7
Defined 7 matrices
P= (
Q= (
R=
S= RECURRENCE
T= ( T(n)=
T(n) =O() = O(
U= (
V= (

NEENU R, AP, CSE 19


Greedy Strategy
 For optimization problems
 Works in stages, considering one input at a time.
 At each stage, a decision is made whether a particular input is an optimal solution.
 If the addition of next input to the partial solution makes the solution infeasible, it is not
added. Otherwise added.

26/07/2022 NEENU R, AJCE 20


Control Abstraction
Select(a) : Selects an input from a and removes it
from a.
Feasible(solution,x): a Boolean function which
checks whether x can be included to the solution
or not.
Union : Combines x with solution and updates it.

26/07/2022 NEENU R, AJCE 21


Knapsack Problem
Given the following-
•A knapsack (kind of shoulder bag) with limited weight capacity.
•Few items each having some weight and value.
 
The problem states-
Which items should be placed into the knapsack such that-
•The value or profit obtained by putting the items into the knapsack is maximum.
•The weight limit of the knapsack does not exceed.

26/07/2022 NEENU R, AJCE 22


Knapsack Problem
Two variants
 Fractional Knapsack ----- items are divisible.
0/1 Knapsack ----- items not divisible

26/07/2022 NEENU R, AJCE 23


Fractional Knapsack Problem
 Given n objects
Object i has weight and profit .
If a fraction , 0<= is placed in the knapsack, profit obtained is and weight is
 Capacity of knapsack m
Problem

26/07/2022 NEENU R, AJCE 24


Fractional Knapsack Problem
Example:
n=3, m=20, (p1,p2,p3)= (25,24,15), (w1,w2,w3)=(18,15,10)
Feasible Solutions

(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

26/07/2022 NEENU R, AJCE 25


Algorithm
Fractional Knapsack (p[1..n], w[1..n], int M) 7. for i = 1 to n - - > O(n)
1. for i =1 to n 8. if weight + w[i] ≤ M then
2. calculate cost[i] <- p[i] / w[i] 9. x[i] = 1
3. Sort p and w in descending order based on - - > O(nlogn) 10. weight = weight + w[i]
cost.
11. else
4. for i = 1 to n
12. x[i] = (M - weight) / w[i]
5. do x[i] = 0
13. weight = M
6. weight = 0
14. break
15. return x
Total time = O( nlogn)
26/07/2022 NEENU R, AJCE 26
Example
M= 60
Item A B C D
Profit 280 100 120 120
Weight 40 10 20 24
W=0
W = 10 Profit = 280+100+120/2+0
Pi/wi 7 10 6 5 W= 50 =440
Xi 0 01 0 0 W=60
1 10/20 =1/2

26/07/2022 NEENU R, AJCE 27


Spanning Trees

07/26/2022 NEENU R, AP, CSE 28


Spanning Tree
  A spanning tree T of a graph G is a subgraph that is a tree which includes all of
the vertices of G.

07/26/2022 NEENU R, AP, CSE 29


Spanning Tree
  G=(V,E)
 S=(V,E’)
 |E’|=|V|-1

07/26/2022 NEENU R, AP, CSE 30


Spanning Trees
 No of spanning Trees possible for a graph G=(V,E)

 |E|C(|V|-1) - no of cycles
 For a complete graph, no of spanning trees =

07/26/2022 NEENU R, AP, CSE 31


Minimum cost spanning tree
5
1 2 5
1 2
2
4 3
3 Cost= 14

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.
.
.
.

07/26/2022 NEENU R, AP, CSE 33


Minimum Spanning Tree algorithms
 Prim’s Algorithm
 Kruskal’s algorithm Greedy method

07/26/2022 NEENU R, AP, CSE 34


Kruskal’s Algorithm
Construct the minimum spanning tree for the given graph

07/26/2022 NEENU R, AP, CSE 35


Kruskal’s Algorithm
Select the minimum weighted edge in the graph.

Cost =5

07/26/2022 NEENU R, AP, CSE 36


Kruskal’s Algorithm
Select the next minimum weighted edge connected to the tree which does not form cycle.

Cost =10

07/26/2022 NEENU R, AP, CSE 37


Kruskal’s Algorithm
Select the next minimum weighted edge connected to the tree which does not form cycle.

Cost =16

07/26/2022 NEENU R, AP, CSE 38


Kruskal’s Algorithm
Select the next minimum weighted edge connected to the tree which does not form cycle.

Cost =23

07/26/2022 NEENU R, AP, CSE 39


Kruskal’s Algorithm
Select the next minimum weighted edge connected to the tree which does not form cycle.

Cost =30

07/26/2022 NEENU R, AP, CSE 40


Kruskal’s Algorithm
Select the next minimum weighted edge connected to the tree which does not form cycle.

Cost =39

07/26/2022 NEENU R, AP, CSE 41


Kruskal’s Algorithm
MST-KRUSKAL(G,w)
MAKE-SET (v) – make a set with v
1. A =
as an element.
2. for each vertex v ϵ G.V
FIND-SET(u) – returns the pointer
3. MAKE-SET (v) to the set in which u belongs.
4. Sort the edges of G.E into nondecreasing order
by weight w UNION(u,v) – merges the two sets
in which u and v belongs.
5. for each edge (u,v) ϵ G.V, taken in
nondecreasing order by weight.
6. if FIND-SET(u) FIND-SET(v)

7. A= A

8. UNION(u,v).
9. return A

07/26/2022 NEENU R, AP, CSE 42


Kruskal’s Algorithm Edge Weight
MST-KRUSKAL(G,w) 0-3 5
A={}
1. A = {0} {1} {2} {3} {4} {5} {6} 2-4 5

2. for each vertex v ϵ G.V 3-5 6


0-1 7
3. MAKE-SET (v)
1-4 7
4. Sort the edges of G.E into
1-2 8
nondecreasing order by weight w
5-4 8
5. for each edge (u,v) ϵ G.V, taken in
nondecreasing order by weight. 3-1 9
4-6 9
6. if FIND-SET(u) FIND-SET(v)
5-6 11
7. A= A
3-4 15
8. UNION(u,v).
9. return A

07/26/2022 NEENU R, AP, CSE 43


Kruskal’s Algorithm Edge Weight
MST-KRUSKAL(G,w) 0-3 5
A={(0,3)}
1. A = {0,3} {1} {2} {4} {5} {6} 2-4 5

2. for each vertex v ϵ G.V 3-5 6


0-1 7
3. MAKE-SET (v)
1-4 7
4. Sort the edges of G.E into
1-2 8
nondecreasing order by weight w
5-4 8
5. for each edge (u,v) ϵ G.V, taken in
nondecreasing order by weight. 3-1 9
4-6 9
6. if FIND-SET(u) FIND-SET(v)
5-6 11
7. A= A
3-4 15
8. UNION(u,v).
9. return A

07/26/2022 NEENU R, AP, CSE 44


Kruskal’s Algorithm Edge Weight
MST-KRUSKAL(G,w) 0-3 5
A={(0,3), (2,4)}
1. A = {0,3} {1} {2,4} {5} {6} 2-4 5

2. for each vertex v ϵ G.V 3-5 6


0-1 7
3. MAKE-SET (v)
1-4 7
4. Sort the edges of G.E into
1-2 8
nondecreasing order by weight w
5-4 8
5. for each edge (u,v) ϵ G.V, taken in
nondecreasing order by weight. 3-1 9
4-6 9
6. if FIND-SET(u) FIND-SET(v)
5-6 11
7. A= A
3-4 15
8. UNION(u,v).
9. return A

07/26/2022 NEENU R, AP, CSE 45


Kruskal’s Algorithm Edge Weight
MST-KRUSKAL(G,w) 0-3 5
A={(0,3), (2,4),(3,5), (0,1)}
1. A = {0,3,5,1 } {2,4} {6} 2-4 5

2. for each vertex v ϵ G.V 3-5 6


0-1 7
3. MAKE-SET (v)
1-4 7
4. Sort the edges of G.E into
1-2 8
nondecreasing order by weight w
5-4 8
5. for each edge (u,v) ϵ G.V, taken in
nondecreasing order by weight. 3-1 9
4-6 9
6. if FIND-SET(u) FIND-SET(v)
5-6 11
7. A= A
3-4 15
8. UNION(u,v).
9. return A

07/26/2022 NEENU R, AP, CSE 46


Kruskal’s Algorithm 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

2. for each vertex v ϵ G.V 3-5 6


0-1 7
3. MAKE-SET (v)
1-4 7
4. Sort the edges of G.E into
1-2 8
nondecreasing order by weight w
5-4 8
5. for each edge (u,v) ϵ G.V, taken in
nondecreasing order by weight. 3-1 9
4-6 9
6. if FIND-SET(u) FIND-SET(v)
5-6 11
7. A= A
3-4 15
8. UNION(u,v).
9. return A

07/26/2022 NEENU R, AP, CSE 47


Cannot be taken since 1
and 2 belongs to same

Kruskal’s Algorithm set

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

2. for each vertex v ϵ G.V 3-5 6


0-1 7
3. MAKE-SET (v)
1-4 7
4. Sort the edges of G.E into
1-2 8
nondecreasing order by weight w
5-4 8
5. for each edge (u,v) ϵ G.V, taken in
nondecreasing order by weight. 3-1 9
4-6 9
6. if FIND-SET(u) FIND-SET(v)
5-6 11
7. A= A
3-4 15
8. UNION(u,v).
9. return A

07/26/2022 NEENU R, AP, CSE 48


Cannot be taken since 5
and 4 belongs to same

Kruskal’s Algorithm set

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

2. for each vertex v ϵ G.V 3-5 6


0-1 7
3. MAKE-SET (v)
1-4 7
4. Sort the edges of G.E into
1-2 8
nondecreasing order by weight w
5-4 8
5. for each edge (u,v) ϵ G.V, taken in
nondecreasing order by weight. 3-1 9
4-6 9
6. if FIND-SET(u) FIND-SET(v)
5-6 11
7. A= A
3-4 15
8. UNION(u,v).
9. return A

07/26/2022 NEENU R, AP, CSE 49


Cannot be taken since 3
and 1 belongs to same

Kruskal’s Algorithm set

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

2. for each vertex v ϵ G.V 3-5 6


0-1 7
3. MAKE-SET (v)
1-4 7
4. Sort the edges of G.E into
1-2 8
nondecreasing order by weight w
5-4 8
5. for each edge (u,v) ϵ G.V, taken in
nondecreasing order by weight. 3-1 9
4-6 9
6. if FIND-SET(u) FIND-SET(v)
5-6 11
7. A= A
3-4 15
8. UNION(u,v).
9. return A

07/26/2022 NEENU R, AP, CSE 50


Kruskal’s Algorithm Edge Weight
MST-KRUSKAL(G,w) A={(0,3), (2,4),(3,5), (0,1),(1,4), (4,6)} 0-3 5
1. A = 2-4 5
{0,3,5,1 ,2, 4, 6}
2. for each vertex v ϵ G.V 3-5 6
0-1 7
3. MAKE-SET (v)
1-4 7
4. Sort the edges of G.E into
1-2 8
nondecreasing order by weight w
5-4 8
5. for each edge (u,v) ϵ G.V, taken in
nondecreasing order by weight. 3-1 9
4-6 9
6. if FIND-SET(u) FIND-SET(v)
5-6 11
7. A= A
3-4 15
8. UNION(u,v).
9. return A

07/26/2022 NEENU R, AP, CSE 51


Cannot be taken since 5
and 6 belongs to same

Kruskal’s Algorithm set

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

2. for each vertex v ϵ G.V 3-5 6


0-1 7
3. MAKE-SET (v)
1-4 7
4. Sort the edges of G.E into
1-2 8
nondecreasing order by weight w
5-4 8
5. for each edge (u,v) ϵ G.V, taken in
nondecreasing order by weight. 3-1 9
4-6 9
6. if FIND-SET(u) FIND-SET(v)
5-6 11
7. A= A
3-4 15
8. UNION(u,v).
9. return A

07/26/2022 NEENU R, AP, CSE 52


Cannot be taken since 3
and 4 belongs to same

Kruskal’s Algorithm set

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

2. for each vertex v ϵ G.V 3-5 6


0-1 7
3. MAKE-SET (v)
1-4 7
4. Sort the edges of G.E into
1-2 8
nondecreasing order by weight w
5-4 8
5. for each edge (u,v) ϵ G.V, taken in
nondecreasing order by weight. 3-1 9
4-6 9
6. if FIND-SET(u) FIND-SET(v)
5-6 11
7. A= A
3-4 15
8. UNION(u,v).
9. return A

07/26/2022 NEENU R, AP, CSE 53


Exercise Pblm

07/26/2022 NEENU R, AP, CSE 54


Single Source Shortest path
Algorithm

07/26/2022 NEENU R, AP, CSE 55


Single-source shortest-paths problem
Given a graph G = (V, E) , find a shortest path from a given source vertex s ϵ
V to each vertex v ϵ V.

Algorithms:
◦ Dijkstra’s algorithm

◦ Bellman – Ford algorithm

07/26/2022 NEENU R, AP, CSE 56


Dijkstra’s algorithm
 Solves the single-source shortest-paths problem on a weighted, directed graph G = (V,E) for
the case in which all edge weights are nonnegative.

 Running time < Bellman- Ford Algorithm

07/26/2022 NEENU R, AP, CSE 57


Dijkstra’s algorithm
7
2 4
1
2
S 1
2
1 6

4 5
3 5
3

07/26/2022 NEENU R, AP, CSE 58


Dijkstra’s algorithm
2 ∞
7
2 4
1
2
S 1
1 2
6 ∞

4 5
3 5
3
4 ∞

07/26/2022 NEENU R, AP, CSE 59


Dijkstra’s algorithm
Select the vertex(u) with shortest distance and relax vertices connected to u

2 ∞𝟗
7
2 4
1
2
S 1
1
2
6 ∞

4 5
3 5
3
4 3 ∞

07/26/2022 NEENU R, AP, CSE 60


Dijkstra’s algorithm
Select the vertex(u) with shortest distance and relax vertices connected to u

2 𝟗
7
2 4
1
2
S 1
1
2
6 ∞

4 5
3 5
3
3 ∞𝟔

07/26/2022 NEENU R, AP, CSE 61


Dijkstra’s algorithm
Select the vertex(u) with shortest distance and relax vertices connected to u

2 8
7
2 4
1
2
S 1
1
2
6 ∞𝟏𝟏

4 5
3 5
3
3 𝟔

07/26/2022 NEENU R, AP, CSE 62


Dijkstra’s algorithm
Select the vertex(u) with shortest distance and relax vertices connected to u

2 8
7
2 4
1
2
S 1
1
2
6 𝟏𝟏𝟗

4 5
3 5
3
3 𝟔

07/26/2022 NEENU R, AP, CSE 63


Dijkstra’s algorithm
Select the vertex(u) with shortest distance and relax vertices connected to u

2 8
7
2 4
1
2
S 1
1
2
6 𝟗

4 5
3 5
3
3 𝟔

07/26/2022 NEENU R, AP, CSE 64


Dijkstra’s algorithm
INITIALIZE-SINGLE-SOURCE(G,S)
DIJKSTRA(G, w, s )
1. for each vertex v ϵ G.V
1. INITIALIZE-SINGLE-SOURCE( G, s)
2. v. d=
2. S =
3.
3. Q = G.V
4. s. d=0;
4. while Q

5. u= EXTRACT-MIN(Q) Q: a min priority queue of vertices


keyed by distance
6. s= s

7. for each vertex RELAX (u,v,w)

8. RELAX(u, v, w). 1. if v. d > u. d + w(u,v)

2. v.d = u. d + w(u,v).

3.

07/26/2022 NEENU R, AP, CSE 65


Dijkstra’s algorithm – Exercise

07/26/2022 NEENU R, AP, CSE 66


Dijkstra’s algorithm –
Exercise
V v.d v.
0 0 NIL
DIJKSTRA(G, w, s ) 1 NIL

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

07/26/2022 NEENU R, AP, CSE 67


Dijkstra’s algorithm –
Exercise
V v.d v.

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

07/26/2022 NEENU R, AP, CSE 68


Dijkstra’s algorithm –
Exercise
V v.d v.
0 0 NIL
DIJKSTRA(G, w, s ) 1 0
1. INITIALIZE-SINGLE-
SOURCE( G, s) 4
2 1
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

07/26/2022 NEENU R, AP, CSE 69


Dijkstra’s algorithm –
Exercise
V v.d v.
0 0 NIL
DIJKSTRA(G, w, s ) 1 0

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

07/26/2022 NEENU R, AP, CSE 70


Dijkstra’s algorithm –
Exercise
V v.d v.
0 0 NIL
DIJKSTRA(G, w, s ) 1 0

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

07/26/2022 NEENU R, AP, CSE 71


Dijkstra’s algorithm –
Exercise
V v.d v.
0 0 NIL
DIJKSTRA(G, w, s ) 1 0

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

07/26/2022 NEENU R, AP, CSE 72


Dijkstra’s algorithm –
Exercise
V v.d v.
0 0 NIL
DIJKSTRA(G, w, s ) 1 0

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

07/26/2022 NEENU R, AP, CSE 73


Dijkstra’s algorithm –
Exercise
V v.d v.
0 0 NIL
DIJKSTRA(G, w, s ) 1 0

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

07/26/2022 NEENU R, AP, CSE 74


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 4 5
5. u= EXTRACT-MIN(Q)
6. s= s 5 6

7. for each vertex


6 7
8. RELAX(u, v, w).
8 9 11 7 0

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

07/26/2022 NEENU R, AP, CSE 76


Dijkstra’s algorithm –
Exercise
V v.d v.
0 0 NIL
1 0

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(

07/26/2022 NEENU R, AP, CSE 78


Dijkstra’s algorithm – Exercise 2

07/26/2022 NEENU R, AP, CSE 79


Dijkstra’s algorithm – Exercise 2
Current 2 3 4 5 6
Vertex
1 50 45 10
4 50 45 10
5 45 45 10 25
2 45 45 10 25
3 45 45 10 25

07/26/2022 NEENU R, AP, CSE 80

You might also like