-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.c
191 lines (176 loc) · 4.3 KB
/
search.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
#include "search.h"
#include "graph.h"
#include "config.h"
#include "util.h"
#include "priority_queue.h"
const char alg_table[][MAX_ALG_LEN] = {"DFS", "BFS", "Dijkstra"};
const int alg_num = sizeof(alg_table) / MAX_ALG_LEN;
static int matchAlg(char *c);
static int *int_new(int value);
static const int inf = 0x3f3f3f3f;
uint8 visited[MAXV] = {0 };
static int *int_new(int value)
{
int *p = (int *)malloc(sizeof(int));
*p = value;
return p;
}
int Dijkstra(AdjGraph g, int u, int w, int* path)
{
int dis[MAXV];
for (int i = 0; i < MAXV; i++) {
dis[i] = inf;
}
PriorityQueue *pq = priority_queue_new(PRIORITY_MIN);
dis[u] = 0;
path[u] = u;
KeyValue *kv = key_value_new(dis[u], int_new(u));
priority_queue_enqueue(pq, kv);
while (!priority_queue_empty(pq)) {
Anode *p;
kv = priority_queue_dequeue(pq);
u = *(int *)kv->_value;
//dis[u] = kv->_key > dis[u] ? dis[u] : kv->_key;
if (u == w) {
return dis[u];
}
p = g->adj[u].firstarc;
while (p != NULL) {
int v = p->no;
if (dis[v] > dis[u] + p->weight) {
dis[v] = dis[u] + p->weight;
path[v] = u;
kv = key_value_new(dis[v], int_new(v));
priority_queue_enqueue(pq, kv);
}
p = p->nextarc;
}
}
}
void BFS(AdjGraph g, int u, int v, int *path)
{
int *qu = (int *)malloc(sizeof(int)*MAXV);
int *weight = (int*)malloc(sizeof(int)*MAXV);
int rear = -1;
int front = -1;
qu[++rear] = u;
visited[u] = 1;
int w = u;
weight[u] = 1;
while (front != rear) {
w = qu[++front];
if (w == v) {
return;
}
if (weight[w] != 1) {
qu[++rear] = w;
weight[w]--;
continue;
}
Anode *p;
p = g->adj[w].firstarc;
while (p != NULL) {
if (visited[p->no] == 1) {
if (weight[p->no] > p->weight) {
weight[p->no] = p->weight;
path[p->no] = w;
}
p = p->nextarc;
continue;
}
weight[p->no] = p->weight;
qu[++rear] = p->no;
visited[p->no] = 1;
path[p->no] = w;
p = p->nextarc;
}
}
}
void DFS(AdjGraph g, int u, int v,int *path)
{
Anode *p;
visited[u] = 1;
if (u == v) {
return;
}
p = g->adj[u].firstarc;
while (p != NULL) {
if (visited[p->no] == 0) {
path[p->no] = u;
DFS(g, p->no, v, path);
}
p = p->nextarc;
}
}
char* processPath(int *path, int u, int v)
{
char *s = (char *)malloc(sizeof(char) * MAX_PATH_LEN);
char con[] = " >- ";
char *b; char *a;
if (path[v] == 0) {
printf("path doesn't exist!!!\n");
return NULL;
}
int i = v;
long j = 0;
while (i != path[i])
{
b = itos(i);
while (*b) {
s[j++] = *b++;
}
a = con;
while (*a) {
s[j++] = *a++;
}
i = path[i];
}
b = itos(i);
while (*b) {
s[j++] = *b++;
}
s[j] = '\0';
reverse(s, 0, j - 1);
return s;
}
char* shortestPath(int u, int v, char algorithm[], char name[])
{
AdjGraph g;
g = createAdj(name);
id_type path[MAX_PATH_LEN];
int cost;
//char path[MAX_PATH_LEN] = {0, };
if (g->adj[u].outDegree == -1 || g->adj[v].outDegree == -1) {
printf("vertices not exist!!!\n");
exit(1);
}
path[u] = u;
switch (matchAlg(algorithm))
{
case 0 : //DFS
DFS(g, u, v, path);
return processPath(path, u, v);
break;
case 1 :
BFS(g, u, v, path);
return processPath(path, u, v);
break;
case 2 : //DIJ
cost = Dijkstra(g, u, v, path);
//printf("cost: %d\n", cost);
return processPath(path, u, v);
break;
default:
printf("unsupported alg: %s\n", algorithm);
break;
}
}
static int matchAlg(char *c)
{
for (int i = 0; i < alg_num; i++) {
if (strcmp(c, (char *)alg_table[i]) == 0) {
return i;
}
}
return -1;
}