|
1 | 1 | #include <assert.h>
|
| 2 | +#include <stdio.h> |
2 | 3 | #define VERTEX_NUM 5
|
3 |
| -typedef char ElemType; |
| 4 | +typedef int ElemType; |
4 | 5 | typedef struct {
|
5 | 6 | ElemType vertex[VERTEX_NUM];
|
6 | 7 | int edge[VERTEX_NUM][VERTEX_NUM];
|
7 | 8 | }Graph;
|
8 | 9 |
|
9 |
| -void initVertex(Graph *graph) { |
| 10 | +void initGraph(Graph *graph) { |
| 11 | + /* init vertex */ |
10 | 12 | for (int i = 0; i < VERTEX_NUM; ++i) {
|
11 |
| - graph->vertex[i] = '\0'; |
| 13 | + graph->vertex[i] = -1; |
12 | 14 | }
|
13 |
| -} |
14 | 15 |
|
15 |
| -void initEdge(Graph *graph) { |
| 16 | + /* init edge */ |
16 | 17 | for (int i = 0; i < VERTEX_NUM; ++i) {
|
17 | 18 | for (int j = 0; j < VERTEX_NUM; ++j) {
|
18 | 19 | graph->edge[i][j] = 0;
|
19 | 20 | }
|
20 | 21 | }
|
21 | 22 | }
|
22 | 23 |
|
23 |
| -void initGraph(Graph *graph) { |
24 |
| - initVertex(graph); |
25 |
| - initEdge(graph); |
26 |
| -} |
27 |
| - |
28 |
| -/** |
29 |
| - * Insert vertexes graph with example vertexes, see images/adjacency_matrix_graph.jpeg |
30 |
| - * @param graph the graph store vertexes. |
31 |
| - */ |
32 |
| -void insertVertex(Graph *graph) { |
| 24 | +int getOutOfDegree(Graph *graph, ElemType vertex) { |
| 25 | + int vertexIndex = -1; |
| 26 | + for (int i = 0; i < VERTEX_NUM; ++i) { |
| 27 | + if (vertex == graph->vertex[i]) { |
| 28 | + vertexIndex = i; |
| 29 | + break; |
| 30 | + } |
| 31 | + } |
| 32 | + if (vertexIndex == -1) { |
| 33 | + perror("vertex not found."); |
| 34 | + } |
| 35 | + int sum = 0; |
33 | 36 | for (int i = 0; i < VERTEX_NUM; ++i) {
|
34 |
| - graph->vertex[i] = 'A' + i; |
| 37 | + sum = sum + graph->edge[vertexIndex][i]; |
35 | 38 | }
|
| 39 | + return sum; |
36 | 40 | }
|
37 | 41 |
|
38 |
| -/** |
39 |
| - * Insert edges with example graph. See images/adjacency_matrix_graph.jpeg |
40 |
| - * @param graph graph the graph store edges. |
41 |
| - */ |
42 |
| -void insertEdge(Graph *graph) { |
43 |
| - int matrix[VERTEX_NUM][VERTEX_NUM] = { |
44 |
| - {0, 1, 1, 1, 0}, |
45 |
| - {1, 0, 0, 1, 1}, |
46 |
| - {1, 0, 0, 1, 0}, |
47 |
| - {1, 1, 1, 1, 1}, |
48 |
| - {0, 1, 0, 1, 0} |
49 |
| - }; |
| 42 | +int getInOfDegree(Graph *graph, ElemType vertex) { |
| 43 | + int vertexIndex = -1; |
50 | 44 | for (int i = 0; i < VERTEX_NUM; ++i) {
|
51 |
| - for (int j = 0; j < VERTEX_NUM; ++j) { |
52 |
| - graph->edge[i][j] = matrix[i][j]; |
| 45 | + if (vertex == graph->vertex[i]) { |
| 46 | + vertexIndex = i; |
| 47 | + break; |
53 | 48 | }
|
54 | 49 | }
|
| 50 | + if (vertexIndex == -1) { |
| 51 | + perror("vertex not found."); |
| 52 | + } |
| 53 | + int sum = 0; |
| 54 | + for (int i = 0; i < VERTEX_NUM; ++i) { |
| 55 | + sum = sum + graph->edge[i][vertexIndex]; |
| 56 | + } |
| 57 | + return sum; |
55 | 58 | }
|
56 | 59 |
|
57 | 60 | void test() {
|
58 | 61 | Graph graph;
|
59 | 62 | initGraph(&graph);
|
60 |
| - insertVertex(&graph); |
61 |
| - insertEdge(&graph); |
62 | 63 |
|
| 64 | + /* init vertexes */ |
63 | 65 | for (int i = 0; i < VERTEX_NUM; ++i) {
|
64 |
| - assert(graph.vertex[i] == 'A' + i); |
| 66 | + graph.vertex[i] = i; |
65 | 67 | }
|
66 | 68 |
|
| 69 | + /* init edges: see images/adjacency_matrix.png */ |
67 | 70 | int matrix[VERTEX_NUM][VERTEX_NUM] = {
|
68 |
| - {0, 1, 1, 1, 0}, |
69 |
| - {1, 0, 0, 1, 1}, |
70 |
| - {1, 0, 0, 1, 0}, |
71 |
| - {1, 1, 1, 1, 1}, |
72 |
| - {0, 1, 0, 1, 0} |
| 71 | + {0, 1, 1, 0, 0}, |
| 72 | + {0, 0, 1, 0, 1}, |
| 73 | + {0, 0, 0, 1, 0}, |
| 74 | + {0, 0, 0, 0, 1}, |
| 75 | + {0, 0, 0, 0, 0} |
73 | 76 | };
|
74 | 77 | for (int i = 0; i < VERTEX_NUM; ++i) {
|
75 | 78 | for (int j = 0; j < VERTEX_NUM; ++j) {
|
76 |
| - assert(graph.edge[i][j] == matrix[i][j]); |
| 79 | + graph.edge[i][j] = matrix[i][j]; |
77 | 80 | }
|
78 | 81 | }
|
| 82 | + |
| 83 | + assert(2 == getOutOfDegree(&graph, 0)); |
| 84 | + assert(1 == getOutOfDegree(&graph, 2)); |
| 85 | + assert(2 == getOutOfDegree(&graph, 1)); |
| 86 | + assert(1 == getOutOfDegree(&graph, 3)); |
| 87 | + assert(0 == getOutOfDegree(&graph, 4)); |
| 88 | + |
| 89 | + assert(0 == getInOfDegree(&graph, 0)); |
| 90 | + assert(1 == getInOfDegree(&graph, 1)); |
| 91 | + assert(2 == getInOfDegree(&graph, 2)); |
| 92 | + assert(1 == getInOfDegree(&graph, 3)); |
| 93 | + assert(2 == getInOfDegree(&graph, 4)); |
79 | 94 | }
|
80 | 95 |
|
81 | 96 | int main() {
|
|
0 commit comments