Skip to content

Commit 6c393a3

Browse files
update adjacency matrix
1 parent 6445a56 commit 6c393a3

File tree

3 files changed

+54
-39
lines changed

3 files changed

+54
-39
lines changed

datastructures/graph/graph_adjacency_matrix.c

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,96 @@
11
#include <assert.h>
2+
#include <stdio.h>
23
#define VERTEX_NUM 5
3-
typedef char ElemType;
4+
typedef int ElemType;
45
typedef struct {
56
ElemType vertex[VERTEX_NUM];
67
int edge[VERTEX_NUM][VERTEX_NUM];
78
}Graph;
89

9-
void initVertex(Graph *graph) {
10+
void initGraph(Graph *graph) {
11+
/* init vertex */
1012
for (int i = 0; i < VERTEX_NUM; ++i) {
11-
graph->vertex[i] = '\0';
13+
graph->vertex[i] = -1;
1214
}
13-
}
1415

15-
void initEdge(Graph *graph) {
16+
/* init edge */
1617
for (int i = 0; i < VERTEX_NUM; ++i) {
1718
for (int j = 0; j < VERTEX_NUM; ++j) {
1819
graph->edge[i][j] = 0;
1920
}
2021
}
2122
}
2223

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;
3336
for (int i = 0; i < VERTEX_NUM; ++i) {
34-
graph->vertex[i] = 'A' + i;
37+
sum = sum + graph->edge[vertexIndex][i];
3538
}
39+
return sum;
3640
}
3741

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;
5044
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;
5348
}
5449
}
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;
5558
}
5659

5760
void test() {
5861
Graph graph;
5962
initGraph(&graph);
60-
insertVertex(&graph);
61-
insertEdge(&graph);
6263

64+
/* init vertexes */
6365
for (int i = 0; i < VERTEX_NUM; ++i) {
64-
assert(graph.vertex[i] == 'A' + i);
66+
graph.vertex[i] = i;
6567
}
6668

69+
/* init edges: see images/adjacency_matrix.png */
6770
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}
7376
};
7477
for (int i = 0; i < VERTEX_NUM; ++i) {
7578
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];
7780
}
7881
}
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));
7994
}
8095

8196
int main() {

images/adjacency_matrix.png

31.9 KB
Loading

images/adjacency_matrix_graph.jpeg

-25.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)