This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
forked from abhishekchopra13/nwoc_algorithms
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathDijkstra's Algorithm.cpp
115 lines (101 loc) · 3.15 KB
/
Dijkstra's Algorithm.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
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
Dijkstra's algorithm (or Dijkstra's Shortest Path First algorithm, SPF algorithm) is an algorithm for finding the
shortest paths between nodes in a weighted graph, which may represent, for example, mobile or road networks.
It can be implemented in C++ using :
1) Adjacency matrix representation
2) Adjacency List representation (using vector STL of C++)
3) Set STL of C++
4) PriorityQueue STL of C++
This code is implementation of Dijkstra's algorithm using Adjacency list representation for Undirected weighted graph.
When to use Dijkstra's algorithm and Floyd Warshall algorithm ?
-> Dijkstra's algorithm is for single source and multiple destinations .
-> Floyd Warshall algorithm is for multiple sources and multiple destinations .
*/
/*
SAMPLE OUTPUT :
Enter number of vertices
4
Enter number of edges
5
Undirected weighted graph
Enter all edges one by one
Format for input : vertex1 vertex2 weight
1 2 2
1 3 10
1 4 4
2 3 2
4 2 15
Enter the source vertex
2
Source Destination Shortest Distance
2 1 2
2 2 0
2 3 2
2 4 6
*/
#include<bits/stdc++.h>
using namespace std;
#define MAXN 1111
#define INF 999999999
vector< pair<int, int> >v[MAXN];
vector<int> djikstras(int source, int no_of_vertices) {
vector<int> dist(no_of_vertices, INF);
set< pair<int, int> > queue;
vector<bool> visited(no_of_vertices, false);
dist[source] = 0;
visited[source] = true;
queue.insert(make_pair(dist[source], source));
while(!queue.empty()) {
pair<int, int> front_p = *(queue.begin());
queue.erase(queue.begin());
int cur_dist = front_p.first;
int node = front_p.second;
for(int i=0; i<v[node].size(); i++) {
int to = v[node][i].first;
int weight = v[node][i].second;
if(dist[to] > cur_dist + weight) {
if(queue.find(make_pair(dist[to], to)) != queue.end()) {
queue.erase(make_pair(dist[to], to));
}
dist[to] = cur_dist + weight;
queue.insert(make_pair(dist[to], to));
}
}
}
return dist;
}
int main() {
cout<<"\nEnter number of vertices\n";
int no_of_vertices; cin >> no_of_vertices;
cout<<"\nEnter number of edges\n";
int no_of_edges; cin >> no_of_edges;
/*
Input the edges of undirected weighed graph
Format for input :
vertex1 vertex2 weight
*/
cout<<"\nUndirected weighted graph ";
cout<<"\nEnter all edges one by one "
<<"\nFormat for input : vertex1 vertex2 weight\n";
for(int i=0; i<no_of_edges; i++) {
int vertex1, vertex2, weight;
cin >> vertex1 >> vertex2 >> weight;
vertex1--, vertex2--;
v[vertex1].push_back(make_pair(vertex2, weight));
v[vertex2].push_back(make_pair(vertex1, weight));
}
/*
Source vertex is that vertex from where shortest distance to all other vertices is to be found
*/
cout<<"\nEnter the source vertex\n";
int source; cin >> source;
source --;
vector<int> dist = djikstras(source, no_of_vertices);
cout<<"\n";
cout<<setw(10)<<"Source"<<setw(15)<<"Destination"<<setw(20)<<"Shortest Distance";
cout<<"\n";
for(int i=0; i<no_of_vertices; i++) {
cout<<setw(10)<<source+1;
cout <<setw(10)<<i + 1 <<setw(20)<< dist[i] << "\n";
}
}