-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph_copy.c
98 lines (82 loc) · 2.2 KB
/
Graph_copy.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
// Graph.c
// Seung Hoon Park
// Type Synonums
typedef struct IEdge {
unsigned int first;
unsigned int second;
} IEdge;
typedef struct IGraph {
unsigned int num_edges;
unsigned int num_vertices;
IEdge *arcs;
} IGraph;
// Abbreviations
#define ivertex_cnt(g) g->num_vertices
#define iedge_cnt(g) g->num_edges
#define iarcs(g, e) g->arcs[e]
// Procedures
int is_wellformed(IGraph *g) {
IEdge e;
for(unsigned int i = 0; i < iedge_cnt(g); i++) {
e = iarcs(g, i);
if(ivertex_cnt(g) <= e.first) return 0;
if(ivertex_cnt(g) <= e.second) return 0;
}
return 1;
}
int trian(IGraph *g, int *dist, unsigned int *c) {
for(unsigned int edge_id = 0; edge_id < iedge_cnt(g); edge_id++) {
if (dist[iarcs(g, edge_id).second] > dist[iarcs(g, edge_id).first] + c[edge_id]) return 0;
}
return 1;
}
int just(IGraph *g, int *dist, unsigned int *c, unsigned int s, int *enu, int *pred) {
unsigned int edge_id;
for(unsigned int v = 0; v < ivertex_cnt(g); v++) {
edge_id = pred[v];
if(v != s) {
if(enu[v] >= 0) {
if(edge_id >= iedge_cnt(g)) return 0;
if(iarcs(g, edge_id).second != v) return 0;
if(dist[v] != dist[iarcs(g, edge_id).first] + c[edge_id]) return 0;
if(enu[v] != enu[iarcs(g, edge_id).first] + 1) return 0; // onum
}
}
}
return 1;
}
int no_path(IGraph *g, int *dist, int *enu) {
for(unsigned int v = 0; v < ivertex_cnt(g); v++) {
if(dist[v] < 0) {
if(enu[v] >= 0) return 0;
}
else {
if(enu[v] < 0) return 0;
}
}
return 1;
}
int pos_cost(IGraph *g, unsigned int *c) {
for(unsigned int edge_id = 0; edge_id < iedge_cnt(g); edge_id++) {
if(c[edge_id] < 0) return 0;
}
return 1;
}
int check_basic_just_sp(IGraph *g, int *dist, unsigned int *c, unsigned int s, int *enu, int *pred) {
if(!is_wellformed(g)) return 0;
if(dist[s] > 0) return 0;
if(!trian(g, dist, c)) return 0;
if(!just(g, dist, c, s, enu, pred)) return 0;
return 1;
}
int check_sp(IGraph *g, int *dist, unsigned int *c, unsigned int s, int *enu, int *pred) {
if(!check_basic_just_sp(g, dist, c, s, enu, pred)) return 0;
if(s >= ivertex_cnt(g)) return 0;
if(dist[s] != 0) return 0;
if(!no_path(g, dist, enu)) return 0;
if(!pos_cost(g, c)) return 0;
return 1;
}
int main(int argc, char **argv) {
return 0;
}