-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.c
113 lines (95 loc) · 2.51 KB
/
Graph.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
// Graph.c
// Seung Hoon Park
// Type Synonums
typedef unsigned int nat;
typedef int enat; // negative numbers will be treated as infinity for this purpose
// typedef float ereal; // the INFINITY constnat in math.h will be used for infinity
typedef nat IVertex;
typedef nat IEdge_Id;
typedef nat ICost;
typedef enat IDist; // use enat for now to avoid messy float calculations
typedef enat IPEdge;
typedef enat INum;
typedef struct IEdge {
IVertex first;
IVertex second;
} IEdge;
typedef struct IGraph {
nat num_vertices;
nat num_edges;
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;
nat i;
for(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, IDist *dist, ICost *c) {
IEdge_Id edge_id;
for(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, IDist *dist, ICost *c, IVertex s, INum *enu, IPEdge *pred) {
IEdge_Id edge_id;
IVertex v;
for(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, IDist *dist, INum *enu) {
for(IVertex 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, ICost *c) {
IEdge_Id edge_id;
for(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, IDist *dist, ICost *c, IVertex s, INum *enu, IPEdge *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, IDist *dist, ICost *c, IVertex s, INum *enu, IPEdge *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;
}