forked from Anirudh-1149/CS204-datastructures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortest_path.cpp
102 lines (93 loc) · 1.74 KB
/
shortest_path.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, a, b) for(ll i=a;i<b;i++)
#define fi(i, a, b) for(ll i=b-1;i>=a;i--)
#define pb push_back
#define VI vector<int>
#define Vll vecotr<long long
class Graph
{
int V;
list<int> *adj;
public:
Graph(int V);
void AddEdge(int v,int w);
void Get_Path(int s,int d);
};
Graph::Graph(int V){
this->V=V;
adj= new list<int>[V];
}
void Graph::AddEdge(int v,int w)
{
adj[v].push_back(w);
adj[w].push_back(v);
}
void Graph::Get_Path(int s,int d){
int pred[V],dist[V];
bool *visited = new bool[V];
f(i,0,V)
{
visited[i] = false;
dist[i] = INT_MAX;
pred[i] = -1;
}
list<int>queue;
visited[s]=true;
queue.push_back(s);
dist[s]=0;
list<int>::iterator i;
int flag=1;
while(!queue.empty())
{
int u=queue.front();
queue.pop_front();
for(i=adj[u].begin();i!=adj[u].end();i++)
{
if (visited[*i] == false) {
visited[*i] = true;
dist[*i] = dist[u] + 1;
pred[*i] = u;
queue.push_back(*i);
if (*i == d){
flag=0;
break;
}
}
}
if(!flag)
break;
}
if(flag)
cout<<"The vertecis are not connected";
else
{
vector<int> path;
int crawl = d;
path.push_back(crawl);
while (pred[crawl] != -1) {
path.push_back(pred[crawl]);
crawl = pred[crawl];
}
cout << "Shortest path length is : "<< dist[d];
cout << "\nPath is::\n";
for (int i = path.size() - 1; i >= 0; i--)
cout << path[i] << " ";
}
}
int main()
{
int n,e;
cin>>n>>e;
Graph g(n);
f(i,0,e)
{
int x,y;
cin>>x>>y;
g.AddEdge(x,y);
}
int src,dest;
cin>>src>>dest;
g.Get_Path(src,dest);
}