-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijeskstraGraph.cpp
More file actions
169 lines (142 loc) · 4.08 KB
/
DijeskstraGraph.cpp
File metadata and controls
169 lines (142 loc) · 4.08 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <iostream>
#include <climits>
#include <limits>
#include <stack>
using namespace std;
int getOnlyInteger() {
int x;
while(1) {
cin>>x;
if(cin.fail() || x<0) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout<<"Please give positive integer. ";
}
else return x;
}
}
int main() {
int order;
cout << "Enter no. of vertices: ";
order = getOnlyInteger();
if(order==0) {
cout<<"Graph is empty! "<<endl;
return 0;
}
int** graph = new int*[order];
for (int i = 0; i < order; i++) {
graph[i] = new int[order]();
}
for(int i=0;i<order;i++) {
for(int j=0;j<order;j++) {
graph[i][j]=0;
}
}
//taking input of all vertex values
string mapping[order];
for(int i=0;i<order;i++) {
cout<<"Enter value of vertex "<<i+1<<": ";
cin>>mapping[i];
} //value input taken
for (int i = 0; i < order; i++) {
for (int j = i + 1; j < order; j++) {
int temp;
cout << "Enter weight of edge " << mapping[i] << ", " << mapping[j] << ": ";
temp = getOnlyInteger();
graph[i][j] = temp;
graph[j][i] = temp;
}
int zeros = 0;
for (int k = 0; k < order; k++) {
if (graph[i][k] == 0) zeros++;
}
if (zeros == order) {
cout << "Graph not connected. Node " << mapping[i] << " has no edges." << endl;
return 0;
}
}
int operation;
while(1) {
cout<<"Enter the operation: 1- Find Shortest Path, 2- Show graph, 0- Exit: ";
operation = getOnlyInteger();
if(operation==0) {
return 0;
}
else if(operation==1) {
string source, desti;
cout<<"Enter start and end: ";
cin>>source>>desti;
int start=-1, end=-1;
for(int i=0;i<order;i++) {
if(mapping[i]==source) start = i;
if(mapping[i]==desti) end = i;
}
while(1) {
if(start==-1 || end==-1) {
cout<<"Enter valid start and end: ";
cin>>source>>desti;
}
else {
break;
}
}
int nodes[order]; //distance
bool visited[order]; //visited or not
int predecessor[order]; //predecessor
for (int i = 0; i < order; i++) {
nodes[i] = INT_MAX; //distances infinity initially
visited[i] = false; //all nodes unvisited
predecessor[i] = -1; //no predecessor at starting
}
nodes[start] = 0;
for (int count = 0; count < order; count++) {
int minDistance = INT_MAX;
int currentNode = -1;
for (int i = 0; i < order; i++) {
if (!visited[i] && nodes[i] < minDistance) {
minDistance = nodes[i];
currentNode = i;
}
}
if (currentNode == -1) break;
visited[currentNode] = true;
for (int i = 0; i < order; i++) {
if (graph[currentNode][i] != 0 && !visited[i]) {
int newDistance = nodes[currentNode] + graph[currentNode][i];
if (newDistance < nodes[i]) {
nodes[i] = newDistance;
predecessor[i] = currentNode;
}
}
}
}
cout << "Shortest path from node "<< mapping[start] <<" to node "<< mapping[end] <<" is:"<< endl;
if (nodes[end] == INT_MAX) {
cout << "No path found." << endl;
} else {
stack<int> path;
int currentNode = end;
while (currentNode != -1) {
path.push(currentNode);
currentNode = predecessor[currentNode];
}
cout << "Path: ";
while (!path.empty()) {
cout << mapping[path.top()] << " ";
path.pop();
}
cout<<endl<< "Minimum distance: " << nodes[end] << endl;
}
}
else if(operation==2) {
for(int i=0;i<order;i++) {
for(int j=0;j<order;j++) {
cout<<graph[i][j]<<" ";
}
cout<<endl;
}
}
else cout<<"Enter valid operation! "<<endl;
}
return 0;
}