-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_traversal.c
201 lines (158 loc) · 4.31 KB
/
graph_traversal.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
200
201
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "errc.h"
#include "graph.h"
#include "graph_traversal.h"
#include "stack.h"
struct graph_dfs_iter {
struct graph * g;
struct stack st_nd;
};
int
graph_dfs_create(struct graph* g, struct graph_dfs_iter ** it, uint32_t first_nd) {
assert(g != NULL);
assert(it != NULL);
assert(first_nd < graph_nodes_count(g));
*it = malloc(sizeof(**it));
if (*it == NULL) {
return GRAPH_MALLOC_ERROR;
}
if (stack_init(&(*it)->st_nd, graph_nodes_count(g))) {
return ERROR;
}
stack_push(&(*it)->st_nd, first_nd);
(*it)->g = g;
nodes_reset_seen(g);
edges_reset_seen(g);
return SUCCESS;
}
void
graph_dfs_destroy(struct graph_dfs_iter * it) {
assert(it != NULL);
nodes_reset_seen(it->g);
edges_reset_seen(it->g);
stack_clean(&it->st_nd);
free(it);
}
int
graph_dfs_next(struct graph_dfs_iter * it,
uint32_t * ndret,
uint32_t ndsz) {
uint32_t nd, nh;
uint32_t ndret_count;
assert(it != NULL);
assert(ndret != NULL);
assert(ndsz > 0);
ndret_count = 0;
while (!stack_is_empty(&it->st_nd) && ndret_count < ndsz) {
nd = stack_pop(&it->st_nd);
/* mark node as seen */
if ((it->g->nds)[nd].seen) {
continue;
}
(it->g->nds)[nd].seen = 1;
/* push each neighbour in stack */
graph_foreach_neighbor(it->g, nd, nh) {
stack_push(&it->st_nd, nh);
}
/* return node */
ndret[ndret_count] = nd;
ndret_count++;
}
return ndret_count;
}
/*
* Topological sort
*/
struct graph_tsort_iter {
struct graph * g; /* original graph, use "seen" variables */
struct graph g_rev; /* partial graph containing reversed edges */
uint32_t * adj_cnt; /* array containing counter of adjacent nodes */
};
int
graph_tsort_create(struct graph* g, struct graph_tsort_iter ** it)
{
uint32_t nd, nh;
uint32_t i;
struct graph_tsort_iter * it_tmp;
assert(g != NULL);
assert(it != NULL);
/* Prerequisite : graph without cycle */
for (i = 0; i < graph_nodes_count(g); i++) {
if (graph_is_cyclic(g, i)) {
return 1;
}
}
it_tmp = malloc(sizeof(*it_tmp));
if (it_tmp == NULL) {
return 1;
}
it_tmp->adj_cnt = malloc(graph_nodes_count(g) * sizeof(*(it_tmp->adj_cnt)));
if (it_tmp->adj_cnt == NULL) {
return 1;
}
/* Initialize adjacent nodes count and
* initialize reversed edges graph
*/
graph_init(&(it_tmp->g_rev), graph_nodes_count(g), g->edgs_count);
graph_nodes_count(&it_tmp->g_rev) = graph_nodes_count(g);
/* 1. count adjacent nodes to nd
* 2. reverse edges : (nd -> nh) become (nh -> nd) in g_rev graph*/
for (nd = 0 ; nd < graph_nodes_count(g) ; nd++) {
(it_tmp->adj_cnt)[nd] = 0;
graph_foreach_neighbor(g, nd, nh) {
(it_tmp->adj_cnt)[nd] += 1;
graph_add_edge(&(it_tmp->g_rev), nh, nd);
}
}
it_tmp->g = g;
*it = it_tmp;
return 0;
}
uint32_t
graph_tsort_next(struct graph_tsort_iter * it,
uint32_t * ndret,
uint32_t ndsz)
{
uint32_t nd, nh;
uint32_t nds_cnt;
struct graph_node * nds;
assert(ndret != NULL);
assert(it != NULL);
nds = it->g->nds;
for (nd = GRAPH_ND_FIRST, nds_cnt = 0
; nd < graph_nodes_count(&it->g_rev) && nds_cnt < ndsz
; nd++) {
/* if it remains some dependencies or if node is already seen
* don't return it */
if (nds[nd].seen || (it->adj_cnt)[nd] > 0) {
continue;
}
nds[nd].seen = 1;
/* return at most ndsz elements */
ndret[nds_cnt++] = nd;
}
/* for each neighbour of elements in ndret array,
* decrement dependency count */
for (nd = 0; nd < nds_cnt ; nd++) {
graph_foreach_neighbor(&(it->g_rev), ndret[nd], nh) {
if ((it->adj_cnt)[nh] > 0) {
(it->adj_cnt)[nh] -= 1;
}
}
}
return nds_cnt;
}
void
graph_tsort_destroy(struct graph_tsort_iter * it) {
struct graph * g;
assert(it != NULL);
g = it->g;
nodes_reset_seen(g);
free(it->adj_cnt);
graph_clean(&(it->g_rev));
free(it);
}