forked from Turupawn/Bellman-Ford-Shortest-Path
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
40 lines (37 loc) · 827 Bytes
/
main.cpp
File metadata and controls
40 lines (37 loc) · 827 Bytes
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
#include "Test.h"
#include <limits>
#include <iostream>
#include <stdexcept>
using namespace std;
//based on https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm
int* getShortestPath(int** graph, int size, int origin)
{
int* predecessor = new int[size];
int* distance = new int[size];
int maxDist = std::numeric_limits<int>::max();
for (int i = 0; i < size; ++i)
{
predecessor[i] = -1;
distance[i] = maxDist;
}
predecessor[origin] = 0;
distance[origin] = 0;
// int i = 0;
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
if(graph[i][j]!=-1 && (distance[i] + graph[i][j] < distance[j]))
{
distance[j] = distance[i] + graph[i][j];
predecessor[j] = i;
}
}
}
return predecessor;
}
int main ()
{
test();
return 0;
}