Skip to content

Commit 0d5ce32

Browse files
deep first search (#87)
* undirected graph * deep first search * fixed LGTM
1 parent 6c393a3 commit 0d5ce32

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <assert.h>
2+
#include <stdio.h>
3+
#include <stdbool.h>
4+
5+
#define VERTEX_NUM 5
6+
typedef char ElemType;
7+
typedef struct {
8+
ElemType vertex[VERTEX_NUM];
9+
int edge[VERTEX_NUM][VERTEX_NUM];
10+
} Graph;
11+
12+
bool visited[VERTEX_NUM];
13+
ElemType dfsPath[VERTEX_NUM];
14+
int count = 0;
15+
void deepFirstSearch(Graph *g, int start) {
16+
dfsPath[count++] = g->vertex[start];
17+
visited[start] = true;
18+
for (int i = 0; i < VERTEX_NUM; ++i) {
19+
if (g->edge[start][i] == 1 && !visited[i]) {
20+
deepFirstSearch(g, i);
21+
}
22+
}
23+
}
24+
25+
void test() {
26+
Graph graph;
27+
/* init vertexes */
28+
for (int i = 0; i < VERTEX_NUM; ++i) {
29+
graph.vertex[i] = 'A' + i;
30+
}
31+
32+
/* init edges: see images/adjacency_matrix.png */
33+
int matrix[VERTEX_NUM][VERTEX_NUM] = {
34+
{0, 1, 0, 1, 0},
35+
{1, 0, 1, 1, 0},
36+
{0, 1, 0, 0, 1},
37+
{1, 1, 0, 0, 1},
38+
{0, 0, 1, 1, 0}
39+
};
40+
for (int i = 0; i < VERTEX_NUM; ++i) {
41+
for (int j = 0; j < VERTEX_NUM; ++j) {
42+
graph.edge[i][j] = matrix[i][j];
43+
}
44+
}
45+
46+
ElemType path[] = {'A', 'B', 'C', 'E', 'D'};
47+
deepFirstSearch(&graph, 0);
48+
for (int i = 0; i < VERTEX_NUM; ++i) {
49+
assert(path[i] == dfsPath[i]);
50+
}
51+
}
52+
53+
int main() {
54+
test();
55+
return 0;
56+
}
+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <assert.h>
2+
#include <stdio.h>
3+
4+
#define VERTEX_NUM 5
5+
typedef char ElemType;
6+
typedef struct {
7+
ElemType vertex[VERTEX_NUM];
8+
int edge[VERTEX_NUM][VERTEX_NUM];
9+
} Graph;
10+
11+
void initGraph(Graph *graph) {
12+
/* init vertex */
13+
for (int i = 0; i < VERTEX_NUM; ++i) {
14+
graph->vertex[i] = '\0';
15+
}
16+
17+
/* init edge */
18+
for (int i = 0; i < VERTEX_NUM; ++i) {
19+
for (int j = 0; j < VERTEX_NUM; ++j) {
20+
graph->edge[i][j] = 0;
21+
}
22+
}
23+
}
24+
25+
int getDegree(Graph *graph, ElemType vertex) {
26+
int vertexIndex = -1;
27+
for (int i = 0; i < VERTEX_NUM; ++i) {
28+
if (vertex == graph->vertex[i]) {
29+
vertexIndex = i;
30+
break;
31+
}
32+
}
33+
if (vertexIndex == -1) {
34+
perror("vertex not found.");
35+
}
36+
int sum = 0;
37+
for (int i = 0; i < VERTEX_NUM; ++i) {
38+
sum = sum + graph->edge[vertexIndex][i];
39+
}
40+
return sum;
41+
}
42+
43+
void test() {
44+
Graph graph;
45+
initGraph(&graph);
46+
47+
/* init vertexes */
48+
for (int i = 0; i < VERTEX_NUM; ++i) {
49+
graph.vertex[i] = 'A' + i;
50+
}
51+
52+
/* init edges: see images/adjacency_matrix.png */
53+
int matrix[VERTEX_NUM][VERTEX_NUM] = {
54+
{0, 1, 0, 1, 0},
55+
{1, 0, 1, 1, 0},
56+
{0, 1, 0, 0, 1},
57+
{1, 1, 0, 0, 1},
58+
{0, 0, 1, 1, 0}
59+
};
60+
for (int i = 0; i < VERTEX_NUM; ++i) {
61+
for (int j = 0; j < VERTEX_NUM; ++j) {
62+
graph.edge[i][j] = matrix[i][j];
63+
}
64+
}
65+
66+
assert(2 == getDegree(&graph, 'A'));
67+
assert(3 == getDegree(&graph, 'B'));
68+
assert(2 == getDegree(&graph, 'C'));
69+
assert(3 == getDegree(&graph, 'D'));
70+
assert(2 == getDegree(&graph, 'E'));
71+
}
72+
73+
int main() {
74+
test();
75+
return 0;
76+
}
6.32 KB
Loading

0 commit comments

Comments
 (0)