-
Notifications
You must be signed in to change notification settings - Fork 32
/
extra_ShortestPathFile.cpp
124 lines (105 loc) · 2.51 KB
/
extra_ShortestPathFile.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
116
117
118
119
120
121
122
123
124
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <fstream>
using namespace std;
// structure to represent a vertex
struct node
{
int cost;
bool visited;
vector <int> neighborsCost;
};
void shortestPath(vector <node*>& graph, int source)
{
queue <node*> list;
graph[source - 1]->cost = 0;
list.push(graph[source - 1]);
// applying a simple BFS
while (!list.empty())
{
node* current = list.front();
list.pop();
current->visited = true;
// checking the neighbors of our current node
// we are always updating the distance to neighbors if we find a more optimal value
for (int x = 0; x < current->neighborsCost.size(); x++)
{
int cost = current->neighborsCost[x];
// condition that checks if there is a connection and if the current connection has not yet been visited
if (cost != -1 && graph[x]->visited == false)
{
// doing edge relaxation
// analyze the current cost it take from current to that node
// if the current path there is the best choice, we update its cost
int newCost = cost + current->cost;
if (newCost < graph[x]->cost)
{
graph[x]->cost = newCost;
}
// adding the neighbor to our queue
list.push(graph[x]);
}
}
}
}
int main()
{
int n;
int source;
string filename;
cout << "enter file name (include the extension)" << endl;
cin >> filename;
ifstream readFile(filename);
readFile >> n; // get the number of vertices
readFile >> source; // get the source node
// setting up our graph
vector <node*> graph;
for (int x = 0; x < n; x++)
{
graph.push_back(new node);
graph[x]->cost = 1000*100; // by default we set them all to infinity in this case just setting a high value
graph[x]->visited = false;
}
// loop to extract cost of each distance from current node
int index = 0;
int count = 0;
while (index < n)
{
string temp;
readFile >> temp;
int num;
// condition utilize to convert to an int data type
if (temp != "-")
{
num = stoi(temp);
}
else
{
num = -1;
}
// adding it to current list
// note no connections as signaled as -1
graph[index]->neighborsCost.push_back(num);
// keeping track of which node we are modifying
count++;
if (count == n)
{
count = 0;
index++;
}
}
// calling our shortest path method
shortestPath(graph, source);
// outputting the result
for (int x = 0; x < graph.size(); x++)
{
if (source == x + 1)
{
continue;
}
cout << "(" << source << " -> " << x + 1 << ") = " << graph[x]->cost << endl;
}
return 0;
}