-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.c
188 lines (153 loc) · 4.11 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
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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "graph.h"
/***************************************************************************/
void edge_to_string(const edge_t *edge, char *str, unsigned size)
{
assert(str != NULL);
assert(size > 0);
str[0] = '\0';
}
/***************************************************************************/
bool list_is_empty(const adjacency_list_t *list)
{
assert(list != NULL);
return false;
}
/***************************************************************************/
unsigned list_size(const adjacency_list_t *list)
{
assert(list != NULL);
return 0;
}
/***************************************************************************/
void list_prepend(adjacency_list_t *list, edge_t *edge)
{
assert(list != NULL);
assert(edge != NULL);
}
/***************************************************************************/
bool list_contains(const adjacency_list_t *list, unsigned tail, unsigned head)
{
assert(list != NULL);
return false;
}
/***************************************************************************/
bool graph_initialise(graph_t *graph, unsigned vertex_count)
{
assert(graph != NULL);
return false;
}
/***************************************************************************/
void graph_print(const graph_t *graph)
{
assert(graph != NULL);
}
/***************************************************************************/
void graph_release(graph_t *graph)
{
assert(graph != NULL);
}
/***************************************************************************/
bool
graph_connect(graph_t *graph, unsigned tail, unsigned head, unsigned weight)
{
assert(graph != NULL);
return false;
}
/***************************************************************************/
void graph_disconnect(graph_t *graph, unsigned tail, unsigned head)
{
assert(graph != NULL);
}
/***************************************************************************/
unsigned graph_indegree(const graph_t *graph, unsigned id)
{
assert(graph != NULL);
unsigned result = 0;
if (id < graph->vertex_count)
{
for (size_t i=0; i < graph->vertex_count; i++)
{
const adjacency_list_t *list = &graph->adjacency_lists[i];
for (edge_t *edge = list->first; edge != NULL; edge = edge->next)
{
if (edge->head == id)
{
result++;
}
}
}
}
return result;
}
/***************************************************************************/
unsigned graph_outdegree(const graph_t *graph, unsigned id)
{
assert(graph != NULL);
return 0;
}
/***************************************************************************/
void graph_build_from_file(graph_t *graph, const char *pathname)
{
assert(graph != NULL);
assert(pathname != NULL);
FILE *fp = fopen(pathname, "r");
if (fp != NULL)
{
unsigned vertex_count;
if (fscanf(fp, "%u", &vertex_count) == 1)
{
if (graph_initialise(graph, vertex_count))
{
while (! feof(fp))
{
unsigned tail;
unsigned head;
unsigned weight;
int n = fscanf(fp, "%u %u %u", &tail, &head, &weight);
if (! feof(fp))
{
if (n == 3)
{
if (graph_connect(graph, tail, head, weight))
{
fprintf(stderr, "Failed to connect: %d->%d\n", tail, head);
}
}
else
{
/* Bad format */
}
}
}
}
}
else
{
/* Bad format */
}
(void) fclose(fp);
}
}
/***************************************************************************/
void graph_to_dot(const graph_t *graph, const char *pathname)
{
assert(graph != NULL);
assert(pathname != NULL);
FILE *fp = fopen(pathname, "w");
if (fp != NULL)
{
fprintf(fp, "digraph {\n");
for (size_t i=0; i < graph->vertex_count; i++)
{
const adjacency_list_t *list = &graph->adjacency_lists[i];
for (edge_t *edge = list->first; edge != NULL; edge = edge->next)
{
fprintf(fp, "%d -> %d\n", edge->tail, edge->head);
}
}
fprintf(fp, "}\n");
}
}