Professional Documents
Culture Documents
Supervised By
Daw Nann Seint Seint Soe
Lecture
Group member
a b c d e
Cost 0 inf inf inf Inf
Visited 0 0 0 0 0
Parent
Node
Shortest Path Algorithm Explanation
• Step – 1
Pointer -a a b c d e
Cost 0 7 5 inf Inf
Visited 1 0 0 0 0
Parent _ a a
Node
Cost If (0 + 7 < inf) {b = 7;} true
Updated If (0 + 5 < inf) {c = 5;} true
Next min(b,c) = min(7,5) = c
Pointer
Shortest Path Algorithm Explanation
• Step-2
Pointer - c a b c d e
Cost 0 7 5 13 7
Visited 1 0 1 0 0
Parent _ a a c c
Node
Cost If(5 + 3 < 7) {b = 7;} false
Updated If(5 + 8 < inf ) {d = 13;} true
If(5 + 2 < inf ) {e = 7;} true
Next min (b,d,e) = min(7,13,7) = b
Pointer
Shortest Path Algorithm Explanation
• Step-3
Pointer - c a b c d e
Cost 0 7 5 10 7
Visited 1 1 1 0 0
Parent _ a a b c
Node
Cost If(7 + 3 < 13) {d = 10;} true
Updated
Next min (d) = min(10) = d
Pointer
Shortest Path Algorithm Explanation
• Step-4
Pointer - d a b c d e
Cost 0 7 5 10 7
Visited 1 1 1 1 0
Parent _ a a b c
Node
Cost If(10 + 4 < 7) {e = 7;} false
Updated
Next min (e) = min(7) = e
Pointer
Shortest Path Algorithm Explanation
• Step-5
Pointer - d a b c d e
Cost 0 7 5 10 7
Visited 1 1 1 1 1
Parent _ a a b c
Node
Cost If(7 + 10 < 0) {a = 0;} false
Updated
Next Null, all nodes are visited.
Pointer
Shortest Path Algorithm Explanation
• Path finding(a to other)
Pointer - a b c d e
d
Cost 0 7 5 10 7
Visited 1 1 1 1 1
Parent _ a a b c
Node
Shortest Path Algorithm Explanation
• Path finding (a to other)
‘a’ to ‘b’ = a b
‘a’ to ‘c’ = a c
‘a’ to ‘d’ = a b d
‘a’ to ‘e’ = a c e
Shortest Path Algorithm
• Once the algorithm is over, we use backtrack from the destination
vertex to the source vertex to find the path.
• A minimum priority queue can be used to efficiently receive the vertex
with the least path distance.
Shortest Path Algorithm
•Function shortestPath(G,S) unction Dijkstra(Graph, source):
•2: for each vertex v in Graph: // Initialization
•3: dist[v] := infinity// initial distance from source to vertex v is set to infinite
•4: previous[v] := undefined // Previous node in optimal path from source
•5: dist[source] := 0 // Distance from source to source
•6: Q := the set of all nodes in Graph // all nodes in the graph are unoptimized - thus are in Q
•7: while Q is not empty: // main loop
•8: u := node in Q with smallest dist[ ]
for each vertex V in G
distance[V] <- infinite
previous[V] <- Null
if V != S, add V to Priority Queue Q
distance[S] <- 0
while Q IS NOT EMPTY
U <- Extract MIN from Q
Shortest Path Algorithm
9: remove u from Q
10:for each neighbor v of u:// where v has not yet been removed from Q.
11:alt := dist[u] + dist_between(u, v)
12:if alt < dist[v] // Relax (u,v)
13:dist[v] := alt
14:previous[v] := u
15:return previous[ ]
Shortest Path Algorithm
• Let distance of start vertex from vertex = 0
• Let distance of all vertices from start = ∞ (infinity)
Repeat
Visit the unvisited vertex with the smallest known distance from the
start vertex
For the current vertex, examine its unvisited neighbors
For the current vertex, calculate distance of each neighbors from
start vertex
If the calculate distance of this vertex is less than the known distance,
update the shortest distance
Shortest Path Algorithm
Update the previous vertex for each of the updated distances
Add the current vertex for each of the updated distances
Until all vertices visited
Shortest Path Algorithm
• Let distance of start vertex from vertex = 0
• Let distance of all vertices from start = ∞ (infinity)
WHILE vertices remain unvisited
Visit the unvisited vertex with the smallest known distance from the
start vertex(call this ‘current vertex’)
FOR each unvisited neighbor of the current vertex
Calculate the distance from start vertex
If the calculate distance of this vertex is less than the known distance
Shortest Path Algorithm
• Update shortest distance to this vertex
Update the previous vertex with the current vertex
end if
NEXT unvisited neighbor
Add the current vertex to the list of visited vertices
END WHILE
Problem
4 A
B 1
c
7
6
2
D
E
Java Code
Java Code
Java Code
Program output
Result of output
• Distance from source to Destination is
• 0------->0
• 1------->4
• 2------->5
• 3------->6
• 4------->7
Conclusion
• Among many, we have discussed the Dijkstra(a shortest path)
algorithm used for finding the shortest path, however, one of the
obstacles while implementing the algorithm on the internet is to
provide a full representation of the graph to execute the algorithm as
an individual router has a complete outline for all the routers on the
internet.
References
Shortest Path Algorithm Explanation from
MIG-Myanmar Innovative Group: http://programmingknowledge.blogspot.com/