-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphBellmanFord.c
executable file
·199 lines (161 loc) · 5.44 KB
/
GraphBellmanFord.c
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
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include "generics/List.h"
#include "Graph.h"
//Data Structure
struct vertexBellmanFord {
int keyVertex;
int keyPrev;
int weight;
};
struct bellmanFord {
List * vertexes; //List of vertexBellmanFord
List * edges; // List of Key
};
typedef struct vertexBellmanFord VertexBellmanFord;
typedef struct bellmanFord BellmanFord;
//Declaracoes
BellmanFord * initAlgorithms(Graph * g,int keyVertexSource);
void relax(VertexBellmanFord * source, VertexBellmanFord * dest, int w);
VertexBellmanFord * findVertexBf(BellmanFord * bf, int key);
int bellmanFord(Graph * g, BellmanFord * bf, int keyFrom);
List * mountPath(BellmanFord * bf, int keySrc, int keyDest);
int weightInfinity (BellmanFord * bf);
/*
| ---=== Bellman Ford ===--- |
*/
// Public methods
List * minPath(Graph * g, int keyFrom, int keyTo) {
//Imprimir o caminho
BellmanFord * bf = initAlgorithms(g, keyFrom);
if(bellmanFord(g, bf, keyFrom)){
//Pega os anteriores a partir do destino
return mountPath(bf,keyFrom,keyTo);
}
return NULL;
}
List * weightVertex(Graph * g) {
List * vertexesList = getVertexList(g);
int i = 0, len = length(vertexesList);
List * result = list();
for (i = 0; i < len; i++) {
int * key = (int *) malloc(sizeof(int));
*key = getValueInt(vertexesList, i);
BellmanFord * bf = initAlgorithms(g, *key);
if(bellmanFord(g, bf, *key)){
if(weightInfinity(bf)){
listAdd(result, key);
}
}
}
return result;
}
// Private methods
int weightInfinity (BellmanFord * bf) {
int i = 0, len = length(bf->vertexes);
for (i = 0; i < len; i++) {
VertexBellmanFord * act = (VertexBellmanFord *) getValue(bf->vertexes, i);
if(act->weight == INT_MAX) {
return 0;
}
}
return 1;
}
List * mountPath(BellmanFord * bf, int keySrc, int keyDest) {
int keyAct = keyDest;
List * path = list();
int * v = (int *) malloc(sizeof(int));
*v = keyDest;
listAdd(path, v);
int weightDest = findVertexBf(bf,keyDest)->weight;
if(weightDest != INT_MAX) {
while (keySrc != keyAct) {
VertexBellmanFord * act = findVertexBf(bf,keyAct);
int * v = (int *) malloc(sizeof(int));
*v = act->keyPrev;
listAdd(path, v);
keyAct = act->keyPrev;
}
reverseList(path);
return path;
}
return NULL;
}
int bellmanFord(Graph * g, BellmanFord * bf, int keyFrom) {
List * vertexes = bf->vertexes;
int i, lenVertex = length(vertexes);
List * edges = bf->edges;
int lenEdge = length(edges);
for (i = 0; i < lenVertex; i++) { // Roda a quantidade de vertice
//Roda a quantidade de arestas
int e;
for (e = 0; e < lenEdge; e++) {
//VertexBellmanFord * vbf = findVertex(bf, );
int edgeId = getValueInt(edges, e);
int srcId = getVertexByFromKeyEdge(g, edgeId), destId = getVertexByToKeyEdge(g,edgeId);
VertexBellmanFord * src = findVertexBf(bf,srcId);
VertexBellmanFord * dest = findVertexBf(bf,destId);
//Relaxar o programa
relax(src,dest, getWeightEdge(g,edgeId));
}
}
//Verificar se há ciclo
for (i = 0; i < lenEdge; i++) {
int edgeId = getValueInt(edges, i);
int srcId = getVertexByFromKeyEdge(g, edgeId), destId = getVertexByToKeyEdge(g,edgeId);
VertexBellmanFord * src = findVertexBf(bf,srcId);
VertexBellmanFord * dest = findVertexBf(bf,destId);
if(dest->weight > (src->weight == INT_MAX ? INT_MAX : src->weight + getWeightEdge(g,edgeId))) {
printf("| %d |",dest->weight);
return 0;
}
}
return 1;
}
VertexBellmanFord * findVertexBf(BellmanFord * bf, int key){
int len = length(bf->vertexes);
int i;
for (i = 0; i < len; i++) {
VertexBellmanFord * act = (VertexBellmanFord *) getValue(bf->vertexes,i);
if(act->keyVertex == key)
return act;
}
return NULL;
}
BellmanFord * initAlgorithms(Graph * g, int keyVertexSource) {
BellmanFord * bf = (BellmanFord *) malloc(sizeof(struct bellmanFord));
bf->vertexes = list();
bf->edges = list();
List * vertexesList = getVertexList(g);
int i, len = length(vertexesList);
//Seta para infinito os nos e a origem seta para 0
for (i = 0; i < len; i++) {
int keyVertex = getValueInt(vertexesList, i);
VertexBellmanFord * vertexBf = (VertexBellmanFord *) malloc(sizeof(struct vertexBellmanFord));
vertexBf->keyVertex = keyVertex;
vertexBf->keyPrev = -1;
if(keyVertexSource == keyVertex) {
vertexBf->weight = 0;
} else {
vertexBf->weight = INT_MAX;
}
listAdd(bf->vertexes, vertexBf);
}
List * edgesList = getEdgeList(g);
len = length(edgesList);
//Transfere os ids dos o struct
for (i = 0; i < len; i++) {
int * keyEdge = (int *) malloc(sizeof(int)); //TODO: Implementar na TAD LISTA
*keyEdge = getValueInt(edgesList, i);
listAdd(bf->edges, keyEdge);
}
return bf;
}
void relax(VertexBellmanFord * source, VertexBellmanFord * dest, int w) {
if(dest->weight > (source->weight == INT_MAX ? INT_MAX : source->weight + w)) {
dest->weight = source->weight + w;
dest->keyPrev = source->keyVertex;
}
}