This repository has been archived by the owner on Oct 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dijkstra.h
169 lines (150 loc) · 2.89 KB
/
dijkstra.h
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
#ifndef dijkstra_h
#define dijkstra_h
#include <iostream>
#include <vector>
using namespace std;
#define max 100000000
class Node
{
public:
string label;
int cost;
Node() {}
Node(string s)
{
label = s;
cost = max;
}
Node(string s, int cst)
{
label = s;
cost = cst;
}
};
class Distance
{
public:
string pred;
string label;
int cost;
Distance()
{
cost = max;
}
};
class Graph
{
public:
vector< vector<Node> > graph;
Distance* shortest_path;
int n;
Graph()
{
string s;
cout <<" Enter Number Of Vertices : ";
cin >>n;
string t;
for (int i = 0; i < n; i++)
{
vector<Node> singleList;
cout<<"\n Details For Vertex "<<i + 1 <<"\n";
cout <<"\n Enter Label Of Node : ";
cin >>s;
singleList.push_back(Node(s, 0));
cout <<"\n Enter Number of Adjacent Nodes : ";
int n2, cst;
cin >>n2;
for (int j = 0; j < n2; j++)
{
cout<<"\n Details For Adjacent Node "<<j + 1<<"\n";
cout <<"\n Enter Label Of Adjacent Node : ";
cin>>s;
cout<<"\n Enter Cost : ";
cin>>cst;
singleList.push_back(Node(s, cst));
}
graph.push_back(singleList);
}
shortest_path = new Distance[n];
}
int display()
{
cout<<"\n\n Displaying Graph Details \n\n";
for (int i = 0; i < n; i++)
{
int s = graph[i].size();
for (int j = 0; j < s; j++)
{
cout <<" "<<graph[i][j].label<<" "<< graph[i][j].cost<<"\n";
}
}
cout<<"\n\n";
return 0;
}
int findNode(string l)
{
//int x = graph.size();
for (int i = 0; i < n; i++)
{
if (graph[i][0].label == l)return i;
}
return -1;
}
int dijkstra(string x)
{
cout<<"\n\n The Shortest Path Is \n\n";
bool visited[1001] = { false };
int y = findNode(x);
for (int i = 0; i < n; i++)
{
if (i == y)
{
shortest_path[i].pred = x;
shortest_path[i].label = graph[i][0].label;
shortest_path[i].cost = 0;
}
else
{
shortest_path[i].pred = x;
shortest_path[i].label = graph[i][0].label;
shortest_path[i].cost = max;
}
}
for (int i = 0; i < n; i++)
{
cout << y << endl;
int s = graph[y].size();
visited[y] = true;
for (int j = 1; j < s; j++)
{
int c = findNode(graph[y][j].label);
if (visited[c] == false)
if (graph[y][j].cost + shortest_path[y].cost < shortest_path[c].cost)
{
shortest_path[c].cost = graph[y][j].cost + shortest_path[y].cost;
shortest_path[c].pred = graph[y][0].label;
}
}
int min = max;
int minpos = -1;
for (int j = 0; j < n; j++)
{
if (shortest_path[j].cost < min && visited[j]==false)
{
min = shortest_path[j].cost;
minpos = j;
}
}
if (minpos == -1) break;
y = minpos;
}
for (int i = 0; i < n; i++)
{
cout <<" "<<shortest_path[i].label<< " - " <<shortest_path[i].pred;
if (shortest_path[i].cost == max)cout<< " - inf "<< endl;
else cout<<" - " <<shortest_path[i].cost<< endl;
}
return 0;
}
};
#endif