-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgorithmsAL.cpp
209 lines (164 loc) · 4.22 KB
/
AlgorithmsAL.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
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
202
203
204
205
206
207
208
209
#include "AlgorithmsAL.h"
Vertex *AlgorithmsAL::PrimsMST(Vertex **adjacency_list, int vertex_amount, int edge_amount, int start_vertex)
{
Vertex* result = new Vertex[vertex_amount];
VertexPriorityQueue q;
for (int i = 0; i < vertex_amount; i++)
{
Vertex v = {i, 2 * MAX_EDGE_WEIGHT};
result[i] = v;
q.add_vertex(v);
}
result[start_vertex] = {-1, 0};
q.change_key(start_vertex, 0);
while(!q.is_empty())
{
Vertex u = q.get_min();
q.pop();
for (int i = 0; adjacency_list[u.index][i].index != -1; i++)
{
int v_id = adjacency_list[u.index][i].index;
int weight = adjacency_list[u.index][i].weight;
if (q.is_in_queue(v_id) && weight < result[v_id].weight)
{
result[v_id] = {u.index, weight};
q.change_key(v_id, weight);
}
}
}
return result;
}
Vertex* AlgorithmsAL::KruskalMST(Vertex **adjacency_list, int vertex_amount, int edge_amount)
{
Vertex* result = new Vertex[vertex_amount];
for (int i = 0; i < vertex_amount; i++)
{
result[i] = {-1, 0};
}
int edge_list_size = 0;
Edge* edge_list = nullptr;
for (int u_id = 0; u_id < vertex_amount; u_id++)
{
for (int j = 0; adjacency_list[u_id][j].index != -1; j++)
{
int v_id = adjacency_list[u_id][j].index;
int weight = adjacency_list[u_id][j].weight;
if (v_id < u_id)
continue;
Edge tmp = {u_id, v_id, weight};
if (edge_list == nullptr)
{
edge_list_size++;
edge_list = new Edge[edge_list_size];
edge_list[edge_list_size - 1] = tmp;
}
else
{
edge_list_size++;
Edge* tmp_edge_list = new Edge[edge_list_size];
for (int i = 0; i < edge_list_size - 1; i++)
{
tmp_edge_list[i] = edge_list[i];
}
tmp_edge_list[edge_list_size - 1] = tmp;
delete[] edge_list;
edge_list = tmp_edge_list;
}
}
}
mergeSortEdges(edge_list, 0, edge_amount - 1);
DisjointSets disjoint_sets(vertex_amount);
for (int i = 0; i < edge_amount; i++)
{
int u = edge_list[i].u;
int v = edge_list[i].v;
int set_u = disjoint_sets.find(u);
int set_v = disjoint_sets.find(v);
if (set_u != set_v)
{
if (result[v].index == -1)
{
result[v].index = u;
result[v].weight = edge_list[i].weight;
} else {
result[u].index = v;
result[u].weight = edge_list[i].weight;
}
disjoint_sets.merge(set_u, set_v);
}
}
return result;
}
Vertex *AlgorithmsAL::DijkstraPath(Vertex **adjacency_list, int vertex_amount, int edge_amount, int start_vertex)
{
Vertex* result = new Vertex[vertex_amount];
VertexPriorityQueue q;
for (int i = 0; i < vertex_amount; i++)
{
Vertex v = {i, 2 * MAX_EDGE_WEIGHT};
result[i] = v;
q.add_vertex(v);
}
result[start_vertex] = {-1, 0};
q.change_key(start_vertex, 0);
while(!q.is_empty())
{
Vertex u = q.get_min();
q.pop();
for (int i = 0; adjacency_list[u.index][i].index != -1; i++)
{
int v_id = adjacency_list[u.index][i].index;
int weight = adjacency_list[u.index][i].weight;
if ((weight + result[u.index].weight) < result[v_id].weight)
{
result[v_id] = {u.index, weight + result[u.index].weight};
q.change_key(v_id, weight);
}
}
}
return result;
}
Vertex *AlgorithmsAL::BellmanFordPath(Vertex **adjacency_list, int vertex_amount, int edge_amount, int start_vertex)
{
Vertex* result = new Vertex[vertex_amount];
for (int i = 0; i < vertex_amount; i++)
{
result[i] = {-1, 2 * MAX_EDGE_WEIGHT};
}
result[start_vertex] = {-1, 0};
bool no_changes = false;
for (int i = 1; i < vertex_amount; i++)
{
no_changes = true;
for (int u_id = 0; u_id < vertex_amount; u_id++)
{
for (int j = 0; adjacency_list[u_id][j].index != -1; j++)
{
int v_id = adjacency_list[u_id][j].index;
int weight = adjacency_list[u_id][j].weight;
if (result[v_id].weight > result[u_id].weight + weight)
{
no_changes = false;
result[v_id] = {u_id, weight + result[u_id].weight};
}
}
}
if (no_changes)
return result;
}
// Check for negative cycle
for (int u_id = 0; u_id < vertex_amount; u_id++)
{
for (int j = 0; adjacency_list[u_id][j].index != -1; j++)
{
int v_id = adjacency_list[u_id][j].index;
int weight = adjacency_list[u_id][j].weight;
if (result[v_id].weight > result[u_id].weight + weight)
{
delete[] result;
return nullptr;
}
}
}
return result;
}