You are on page 1of 8

Dijkstra's Shortest Path Algorithm

Finding the shortest path

Prepared By: Rosales, Eldhie Ann Sabanal, Karen Balala, Kvin

Graph
a

c b d

Algorithm
Print out the graph with its vertices name. Input the value of its edges. Show the possible path . Print out the value of the shortest path.

Flow Chart

START

Declare variables

Print out the graph

Input the values

Print out the possible path and the total value

Print out the shortest path

END

Source Code

#include<iostream> using namespace std; int main() { int e1, e2, e3, e4, e5,e6; int p1, p2, p3, p4; cout<<"Dijkstra's Shortest Path Algorithm cout<<" c * cout<<" * * * cout<<" * * * cout<<" * * * cout<<" * * * cout<<" a * * * b cout<<" * * * cout<<" * * * * * cout<<" * * * * * cout<<" * * * * * cout<<" * * * * * cout<<" * * * * * cout<<" * d* * cout<<" * * cout<<" * * * * * *

\n\n"; \n"; \n"; \n"; \n"; \n"; \n"; \n"; \n"; \n"; \n"; \n"; \n"; \n"; \n"; \n\n";

cout<<" The program will find the shortest possible path in the graph from a to b.\n"; cout<<"Enter the value of edges:\n"; cout<<"\ta->c: "; cin>>e1; cout<<"\tc->b: "; cin>>e2; cout<<"\ta->d: "; cin>>e3; cout<<"\tc->d: "; cin>>e4; cout<<"\td->b: "; cin>>e5; cout<<"\ta->b: "; cin>>e6;
p1=e1+e2; p2=e3+e5; p3=e1+e4+e5; p4=e2+e3+e4;

vertices

vertices

vertices

vertices

vertices }

cout<<"There are five possible path:\n"; cout<<" a, c, b\n"; cout<<" a, d, b\n"; cout<<" a, c, d, b\n"; cout<<" a, d, c. b\n"; cout<<" a to b\n\n"; if ((p1<p2)&&(p1<p3)&&(p1<p4)&&(p1<e6)) cout<<"The shortest path is "<<p1<<".It passes through the of a, c, b."<<endl; else if ((p2<p1)&&(p2<p3)&&(p2<p4)&&(p2<e6)) cout<<"The shortest path is "<<p2<<".It passes through the of a, d, b."<<endl; else if ((p3<p1)&&(p3<p2)&&(p3<p4)&&(p3<e6)) cout<<"The shortest path is "<<p3<<". It passes through the of a, c, d, b."<<endl; else if ((p4<p1)&&(p4<p2)&&(p4<p3)&&(p4<e6)) cout<<"The shortest path is "<<p4<<". It passes through the of a, d, c. b."<<endl; else if ((e6<p1)&&(e6<p2)&&(e6<p3)&&(e6<p4)) cout<<"The shortest path is "<<e6<<".It passes through the of a to b."<<endl; return 0;

Output

You might also like