-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsearch.c
More file actions
314 lines (255 loc) · 6.19 KB
/
search.c
File metadata and controls
314 lines (255 loc) · 6.19 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/********************************************************************
$RCSfile: search.c,v $
$Author: alexvk $
$Revision: 1.1 $
$Date: 1997/10/15 02:52:53 $
********************************************************************/
static char rcsid[] = "$Id: search.c,v 1.1 1997/10/15 02:52:53 alexvk Exp alexvk $";
#include <stdio.h>
#include <assert.h>
#include "network.h"
typedef struct olist_t {
int order;
int element;
struct olist_t *next;
} OLIST;
OLIST *head = NULL;
int ListIsEmpty()
{
return (head == NULL);
}
void ListFree()
{
struct olist_t *temp;
while (head != NULL) {
temp = head;
head = head->next;
free ((char *) temp);
}
}
void ListAppend(order, element)
int order, element;
{
struct olist_t *temp = NULL;
struct olist_t *pnt;
/* check if the element is in the list already */
if (head != NULL) {
if (head->element == element) {
if (head->order <= order) return;
temp = head;
head = head->next;
} else {
for(pnt = head; pnt->next != NULL; pnt = pnt->next) {
if (pnt->next->element == element) {
if (pnt->next->order <= order) return;
temp = pnt->next;
pnt->next = pnt->next->next;
}
}
}
}
/* make a new element */
if (temp == NULL) {
temp = (OLIST *) malloc(sizeof(OLIST));
temp->element = element;
}
temp->order = order;
/* boundary case */
if (head == NULL || head->order >= order) {
temp->next = head;
head = temp;
return;
}
/* find new position */
for(pnt = head; pnt->next != NULL; pnt = pnt->next) {
if (pnt->next->order >= order) break;
}
/* insert the new element */
temp->next = pnt->next;
pnt->next = temp;
}
int ListGetFirst(order)
int *order;
{
struct olist_t *temp;
int retVal;
if(ListIsEmpty()) {
return EMPTY;
}
*order = head->order;
retVal = head->element;
temp = head;
head = temp->next;
free ((char *) temp);
return retVal;
}
void BFSearch(net, graphTable, node)
NETWORK *net;
char **graphTable;
int node;
{
int i, next, order;
/* initialize the marked fields */
for(i=0; i<net->numNodes; i++) {
net->nodes[i].marked = NOTSET;
}
assert(ListIsEmpty());
ListAppend(0, node);
/* do search of minimum path for all nodes */
while(!ListIsEmpty()) {
next = ListGetFirst(&order);
net->nodes[next].marked = order;
/* append the neighbors to the open list */
for(i=0; i<net->numNodes; i++) {
if(!graphTable[next][i]) continue;
if(net->nodes[i].marked != NOTSET) continue;
if(net->nodes[i].dirty) continue;
ListAppend(order + 1, i);
}
}
}
void DFSearchRecursive(net, graphTable, node, order)
NETWORK *net;
char **graphTable;
int node, *order;
{
int i;
net->nodes[node].marked = *order;
*order += 1;
for(i=0; i<net->numNodes; i++) {
if(!graphTable[node][i]) continue;
if(net->nodes[i].marked != NOTSET) continue;
if(net->nodes[i].dirty) continue;
DFSearchRecursive(net, graphTable, i, order);
}
}
void DFSearch(net, graphTable, node)
NETWORK *net;
char **graphTable;
int node;
{
int i, order = 0;
/* initialize the marked fields */
for(i=0; i<net->numNodes; i++) {
net->nodes[i].marked = NOTSET;
}
DFSearchRecursive(net, graphTable, node, &order);
}
int SearchAllParentsMarked(net, node)
NETWORK *net;
int node;
{
int i;
NODE *x = net->nodes + node;
for(i=0; i<x->numParents; i++) {
if(net->nodes[x->parentIndices[i]].marked == NOTSET)
return FALSE;
}
return TRUE;
}
void POSearch(net)
NETWORK *net;
{
int i, order = 0;
for(i=0; i<net->numNodes; i++) {
if(net->nodes[i].numParents == 0) {
net->nodes[i].marked = order++;
} else {
net->nodes[i].marked = NOTSET;
}
}
while(order < net->numNodes) {
for(i=0; i<net->numNodes; i++) {
if(net->nodes[i].marked != NOTSET) continue;
if(!SearchAllParentsMarked(net, i)) continue;
net->nodes[i].marked = order++;
}
}
}
void MCSearch(net, list, size)
NETWORK *net;
int *list;
int size;
{
int i, p = 0;
int *cardinality;
int pnt, max;
int num = 0;
NODE *x;
cardinality = (int*) calloc(net->numNodes, sizeof(int));
/* start with the nodes in the list */
for(i=0; i<size; i++) {
cardinality[list[i]] = 1;
}
while(TRUE) {
/* find the max cardinality */
for(i=0, max=1, pnt=EMPTY; i<net->numNodes; i++) {
if(max > cardinality[i]) continue;
if(max < cardinality[i]) {
max = cardinality[pnt = i];
} else {
if(pnt == EMPTY) pnt = i;
/* alternate between two branches */
if(p) {
if(net->nodes[i].marked < net->nodes[pnt].marked) pnt = i;
p = 0;
} else {
if(net->nodes[i].marked > net->nodes[pnt].marked) pnt = i;
p = 1;
}
}
}
if(pnt == EMPTY) break;
cardinality[pnt] = NOTSET;
net->scratchBuffer[num++] = pnt;
for(i=0; i<net->numNodes; i++) {
if(!net->graphTable[pnt][i]) continue;
if(cardinality[i] == NOTSET) continue;
if(net->nodes[i].dirty) continue;
cardinality[i]++;
}
}
free((char*) cardinality);
/* now scratch buffer gives MC order */
for(i=0; i<net->numNodes; i++) {
net->nodes[i].marked = NOTSET;
}
for(i=0; i<num; i++) {
net->nodes[net->scratchBuffer[i]].marked = i;
}
for(i=num; i<net->numNodes; i++) {
net->scratchBuffer[i] = NOTSET;
}
}
void searchUnmarkRecursive(net, node)
NETWORK *net;
int node;
{
int i;
NODE *x = net->nodes + node;
if(x->dirty) {
x->dirty = FALSE;
for(i=0; i<x->numParents; i++) {
searchUnmarkRecursive(net, x->parentIndices[i]);
}
}
}
void BNSearch(net, list, size)
NETWORK *net;
int *list;
int size;
{
int i, j;
/* mark all nodes dirty */
for(i=0; i<net->numNodes; i++) {
net->nodes[i].dirty = TRUE;
}
/* recursively unmark the ancestors of the nodes in the list */
for(i=0; i<size; i++) {
searchUnmarkRecursive(net, list[i]);
}
/* if there is any evidence, unmark the ancestors of the evidence nodes */
for(i=0; i<net->numNodes; i++) {
if (net->nodeEvidence[i] != NOTSET) searchUnmarkRecursive(net, i);
}
}