-
Notifications
You must be signed in to change notification settings - Fork 0
/
BellmanAlgorithm.cs
201 lines (167 loc) · 8.12 KB
/
BellmanAlgorithm.cs
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System;
using System.IO;
namespace Project
{ //zamień vertices na vertices
public class BellmanAlgorithm
{
public static void FindTheShortestPath(Graph graph, int source, bool printSolution = false)
{
int vertices = graph.NumVertices;
int edges = graph.NumEdges;
int[] distance = new int [vertices];
string[] pathToVertice = new string [vertices];
for (int i = 0; i < vertices; i++) // inicjowanie wszystkich odległości jako max
distance[i] = int.MaxValue - 10000;
distance[source] = 0; //źródla na 0
for (int i = 1; i <= vertices - 1; ++i) // Najkrótsza droga z żródła może mieć maksymalnie V-1 krawędzi.
{
for (int j = 0; j < vertices; ++j)
{
for (int k = 0; k < vertices; k++)
{
/*
edge[0] = Source
edge[1] = Destination
edge[2] = Weight
*/
int[] edge = graph.Edge(j,k);
if(edge == null ) break; //skończyły się krawędzie
if (distance[edge[0]] != int.MaxValue - 10000 && distance[edge[0]] + edge[2] < distance[edge[1]])
{
distance[edge[1]] = distance[edge[0]] + edge[2];
if (printSolution)
pathToVertice[edge[1]] = pathToVertice[edge[0]] + edge[0].ToString() + "->";
}
}
}
}
// Sprawdzenie czy nie wystapił cykl ujemny
// for (int j = 0; j < vertices; ++j)
// {
// for (int k = 0; k < vertices; k++)
// {
// /*
// edge[0] = Source
// edge[1] = Destination
// edge[2] = Weight
// */
// int[] edge = graph.Edge(j,k);
// if(edge == null ) break; //skończyły się krawędzie
// if (distance[edge[0]] != int.MaxValue - 10000 && distance[edge[0]] + edge[2] < distance[edge[1]])
// Console.WriteLine("Graph contains negative weight cycle.");
// }
// }
if(printSolution)
PrintSolution(pathToVertice, distance, vertices, source);
}
private static void PrintSolution(string[] pathToVertice, int[] distance, int numVertices, int startVertice)
{
string docPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
// Console.Write("Dokoncz sciezke pliku zawierajacego graf\n {0}",docPath);
string path = @"\Output.txt";
// StreamWriter sr = new StreamWriter(Path.Combine(docPath + path));
using (StreamWriter stream = new StreamWriter(Path.Combine(docPath + path)) )
{
for (int i = 0; i < numVertices; i++)
{
if(distance[i] != int.MaxValue - 10000)
stream.WriteLine("{0}->{1} Cost: {2}, Path:{3}{1}",startVertice,i ,distance[i] , pathToVertice[i]);
else if (distance[i] == int.MaxValue - 10000)
stream.WriteLine("{0}->{1} Cost: Infinity, Path: Don't exist.", startVertice, i);
}
}
}
public static void FindTheShortestPat(MatrixGraph graph, int source, bool printSolution = false)
{
int vertices = graph.NumVertices;
int edges = graph.NumEdges;
int[] distance = new int [vertices];
string[] pathToVertice = new string [vertices];
for (int i = 0; i < vertices; i++) // inicjowanie wszystkich odległości jako max
distance[i] = int.MaxValue - 10000;
distance[source] = 0; //źródla na 0
for (int i = 1; i <= vertices - 1; ++i) // Najkrótsza droga z żródła może mieć maksymalnie V-1 krawędzi.
{
for (int j = 0; j < vertices; ++j)
{
for (int k = 0; k < vertices; k++)
{
if (distance[j] != int.MaxValue - 10000 && distance[j] + graph.getEdgeWeight(j,k) < distance[k])
{
distance[k] = distance[j] + graph.getEdgeWeight(j,k);
if (printSolution)
pathToVertice[k] = pathToVertice[j] + j.ToString() + "->";
}
}
}
}
// Sprawdzenie czy nie wystapił cykl ujemny
// for (int j = 0; j < vertices; ++j)
// {
// for (int k = 0; k < vertices; k++)
// {
// /*
// edge[0] = Source
// edge[1] = Destination
// edge[2] = Weight
// */
// int[] edge = graph.Edge(j,k);
// if(edge == null ) break; //skończyły się krawędzie
// if (distance[edge[0]] != int.MaxValue - 10000 && distance[edge[0]] + edge[2] < distance[edge[1]])
// Console.WriteLine("Graph contains negative weight cycle.");
// }
// }
if(printSolution)
PrintSolution(pathToVertice, distance, vertices, source);
}
public static void FindTheShortestPat(ListGraph graph, int source, bool printSolution = false)
{
int vertices = graph.NumVertices;
int edges = graph.NumEdges;
int[] distance = new int [vertices];
string[] pathToVertice = new string [vertices];
int destination, weight;
EdgeNode current;
for (int i = 0; i < vertices; i++) // inicjowanie wszystkich odległości jako max
distance[i] = int.MaxValue - 10000;
distance[source] = 0; //źródla na 0
for (int i = 1; i <= vertices - 1; ++i) // Najkrótsza droga z żródła może mieć maksymalnie V-1 krawędzi.
{
for (int j = 0; j < vertices; ++j)
{
current = graph.ListVertices[j].Head;
while(current!=null)
{
destination = current.Destination;
weight = current.Weight;
if (distance[j] != int.MaxValue - 10000 && distance[j] + weight < distance[destination])
{
distance[destination] = distance[j] + weight;
if (printSolution)
pathToVertice[weight] = pathToVertice[j] + j.ToString() + "->";
}
current = current.Next;
}
}
}
//Sprawdzenie czy nie wystapił cykl ujemny
for (int j = 0; j < vertices; ++j)
{
for (int k = 0; k < vertices; k++)
{
/*
edge[0] = Source
edge[1] = Destination
edge[2] = Weight
*/
int[] edge = graph.Edge(j,k);
if(edge == null ) break; //skończyły się krawędzie
if (distance[edge[0]] != int.MaxValue - 10000 && distance[edge[0]] + edge[2] < distance[edge[1]])
Console.WriteLine("Graph contains negative weight cycle.");
}
}
if(printSolution)
PrintSolution(pathToVertice, distance, vertices, source);
}
}
}