You are on page 1of 13

Data Structure & Algorithm

ICT-5102

Homework Assignment 2

To Honorable:
Dr. Hussain Asiful Mustafa
Assistant Professor
IICT-BUET.

Submitted By:
Md.Dedarul Hasan
Reg.Id: 0417311011
PGD in IT, IICT-BUET.
4/9/2019

IICT, BUET
TO FIND THE LONGEST PATH FOR THAT GRAPH BY DIJKSTRA ALGORITHM

Distance (A)=0

Source=’A’

A
6 5

5
∞ B C

2 3

3
F 2
3 1

D
E ∞
∞ 4

Fig : 1 .given graph for longest path find

Dijkstra’s algorithm to find the Longest path between A and B. It picks the unvisited vertex with the
lowest distance, calculates the distance through it to each unvisited neighbor and updates the neighbors
distance if smaller. Mark visited when done with neighbors.

But for that requirement of Dijkstra’s algorithm to find out the longest path we will find pick the
unvisited vertex with the highest distance & calculate the distance through it to each unvisited neighbor &
updates the neighbors distance if greater. Mark visited when done with the neighbors.

Modification of that graph by Dijkstra Algorithm- step by step for longest path:

 First Source ‘A’ is ‘0’ & others vertex is ∞


 We have picked highest distance for unvisited vertex & calculated through it to each unvisited neighbor
and updated the neighbor distance for greater.
ALGORITHM FOR THAT GRAPH TO FIND LONGEST PATH

STEP1:

Distance(B)=6

Distance(C)=5

A
6 5

5
6 B C
5
2 3

3
F 2
3 1

D
E ∞
∞ 4

Fig: 2Pick vertex in List with maximum distance, i.e., B


STEP2:

Distance(C)=6+5=11

A
6 5

5
6 B C
11
2 3

3
F 2
3 1
2
D
E ∞
3 4

Fig: 3
STEP3:

Distance(F)=11+3=14

Distance(D)=11+2=13

A
6 5

5
6 B C
11
2 3

3
F 2
3 1
14
E D
13
3 4

Fig: 4
STEP4:

Distance(E)=14+3=17

Distance(D)=14+1=15

A
6 5

5
6 B C
11
2 3

3
F 2
3 1
14
E D
15
17 4

Fig :5
STEP5:

Distance(D)=17+4=21

A
6 5

5
6 B C
11
2 3

3
F 2
3 1
14
E D
21
17 4

Fig: 6
STEP6:

Longest Path Cost is= 6+11+14+17+21=69

A
6 5

5
6 B C
11
2 3

3
F 2
3 1
14
E D
21
17 4

Fig: 7
STEP7:

The Final Graph for longest path

6 B C 11

14 F

21

17 E D

Fig: 7 –Longest Path Final for the graph G(V,E)


PSEUDOCODES FOR THAT LONGEST PATH USING DIJKSTRA’S ALGORITHM

Dijkstra():

for each vertex v:

v's distance:=infinity.

v's previous:=none.

v1's distance:=0.

List:={all vertics}

while List is not empty:

v:=remove List vertex with MAXIMUM distance.

Mark v as known.

for each unknown neighbor n of v:

dist:=v's distance+edge(v,n)'s weight.

if dist is GREATER than n's distance:

n's distance:=dist.

n's previous:=v.

Reconstruct path from v2 back to v1, following previous pointers.


IMPLEMENTATIONS CODE

We have assigned that graph which has to be converted as a tree node structure: [input array]

From Step 1-7:

A B C D E F
A 0 6 5 0 0 0
B 0 0 5 0 3 2
C 0 5 0 2 0 3
D 0 0 0 0 0 1
E 0 0 0 4 0 0
F 0 0 0 0 3 0

Programming Code Using c :


EXECUTION OF PROGRAM IMPLEMENTATION & OUTPUT GRAPH WHERE SOURCE IS ‘A’

Output:
RESULT as Graph

C
B

E D

METARIALS

CodeBlocks
Sublime Text Editor
Ms Word To Draw Graph

CONCLUSION

Total Longest Path Graph is: A B C F E D

REFERENCE

Class Lectures
Stack Overflow
Dynamic Programming For Longest Path Solution Sequence

You might also like