-
Notifications
You must be signed in to change notification settings - Fork 0
/
BellmanFord.cpp
162 lines (129 loc) · 3.6 KB
/
BellmanFord.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
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
#include <stdio.h>
#include <limits>
#include <string>
#include <math.h>
using namespace std;
#define EXTRA_CURRENCY "0"
struct Vertex
{
string name;
};
struct Edge
{
int fromId;
int toId;
double cost;
};
struct Graph
{
Vertex *vertices;
Edge *edges;
};
void SetVertex(Vertex* vertex, string name)
{
vertex->name = name;
}
void SetEdge(Edge* edge, int sourceId, int destinationId, double cost)
{
edge->fromId = sourceId;
edge->toId = destinationId;
edge->cost = cost;
}
double NegativeLogRate(double rate)
{
return -log(rate);
}
Graph* GetInputGraphProfit(int &numberOfVertices, int &numberOfEdges)
{
numberOfVertices = 4;
numberOfEdges = 6;
Graph *graph = new Graph;
graph->vertices = new Vertex[numberOfVertices];
graph->edges = new Edge[numberOfEdges];
SetVertex(&graph->vertices[0], EXTRA_CURRENCY);
SetVertex(&graph->vertices[1], "1");
SetVertex(&graph->vertices[2], "2");
SetVertex(&graph->vertices[3], "3");
SetEdge(&graph->edges[0], 0, 1, 0);
SetEdge(&graph->edges[1], 0, 2, 0);
SetEdge(&graph->edges[2], 0, 3, 0);
SetEdge(&graph->edges[3], 1, 2, 20);
SetEdge(&graph->edges[4], 1, 3, 40);
SetEdge(&graph->edges[5], 3, 2, -30);
return graph;
}
void InitializeDistance(double *distance, int numberOfVertices)
{
for(int i=0; i<numberOfVertices; i++)
distance[i] = numeric_limits<double>::max();
}
void InitializePredecessor(int *predecessor, int numberOfVertices)
{
for(int i=0; i<numberOfVertices; i++)
predecessor[i] = -1;
}
bool IsEqual(double f, double s)
{
return std::fabs(f - s) < std::numeric_limits<double>::epsilon();
}
bool Relax (Edge *e, double* distance, int* predecessor)
{
if(!IsEqual(numeric_limits<double>::max(), distance[e->fromId])
&& distance[e->toId] > distance[e->fromId] + e->cost)
{
distance[e->toId] = distance[e->fromId] + e->cost;
predecessor[e->toId] = e->fromId;
return true;
}
return false;
}
bool BellmanFord(Graph *graph, int numberOfVertices, int numberOfEdges, int source, double* distance, int* predecessor, int &edgeWithNegativeCycle)
{
distance[source] = 0;
bool valueChanged;
for(int i=0; i<numberOfVertices-1; i++)
{
valueChanged = false;
for(int j=0; j<numberOfEdges; j++)
valueChanged = Relax(&graph->edges[j], distance, predecessor) || valueChanged;
if(!valueChanged)
return true;
}
for(int j=0; j<numberOfEdges; j++)
if(Relax(&graph->edges[j], distance, predecessor))
{
edgeWithNegativeCycle = j;
return false;
}
}
void PrintCycle(Graph* graph, int *predecessor, int currentVertex, int firstVertex)
{
currentVertex = predecessor[currentVertex];
if(currentVertex == firstVertex)
return;
PrintCycle(graph, predecessor, currentVertex, firstVertex);
printf("-> %s ", graph->vertices[currentVertex].name.c_str());
}
void PrintResult(bool success, double* distance, int numberOfVertices)
{
if(!success)
return;
for(int i=1; i<numberOfVertices; i++)
printf("[%d]=%f\n", i, distance[i]);
}
void Call(Graph *graph, int numberOfVertices, int numberOfEdges)
{
double *distance = new double[numberOfVertices];
int *predecessor = new int[numberOfVertices];
InitializeDistance(distance, numberOfVertices);
InitializePredecessor(predecessor, numberOfVertices);
int edgeWithNegativeCycle;
bool success = BellmanFord(graph, numberOfVertices, numberOfEdges, 0, distance, predecessor, edgeWithNegativeCycle);
PrintResult(success, distance, numberOfVertices);
}
int main()
{
int numberOfVertices, numberOfEdges;
Graph* graph = GetInputGraphProfit(numberOfVertices, numberOfEdges);
Call(graph, numberOfVertices, numberOfEdges);
}