Skip to content

Commit 2b97561

Browse files
adjacency matrix graph (#57)
1 parent fa148d2 commit 2b97561

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ add_subdirectory(ciphers)
99
add_subdirectory(conversions)
1010
add_subdirectory(datastructures/array)
1111
add_subdirectory(datastructures/binarytree)
12+
add_subdirectory(datastructures/graph)
1213
add_subdirectory(datastructures/hash)
1314
add_subdirectory(datastructures/stack)
1415
add_subdirectory(datastructures/tree)

datastructures/graph/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
2+
# with full pathname. RELATIVE may makes it easier to extract an executable name
3+
# automatically.
4+
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c )
5+
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
6+
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
7+
foreach( testsourcefile ${APP_SOURCES} )
8+
# I used a simple string replace, to cut off .cpp.
9+
string( REPLACE ".c" "" testname ${testsourcefile} )
10+
add_executable( ${testname} ${testsourcefile} )
11+
install(TARGETS ${testname} DESTINATION "bin/graph")
12+
13+
endforeach( testsourcefile ${APP_SOURCES} )
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <assert.h>
2+
#define VERTEX_NUM 5
3+
typedef char ElemType;
4+
typedef struct {
5+
ElemType vertex[VERTEX_NUM];
6+
int edge[VERTEX_NUM][VERTEX_NUM];
7+
}Graph;
8+
9+
void initVertex(Graph *graph) {
10+
for (int i = 0; i < VERTEX_NUM; ++i) {
11+
graph->vertex[i] = '\0';
12+
}
13+
}
14+
15+
void initEdge(Graph *graph) {
16+
for (int i = 0; i < VERTEX_NUM; ++i) {
17+
for (int j = 0; j < VERTEX_NUM; ++j) {
18+
graph->edge[i][j] = 0;
19+
}
20+
}
21+
}
22+
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) {
33+
for (int i = 0; i < VERTEX_NUM; ++i) {
34+
graph->vertex[i] = 'A' + i;
35+
}
36+
}
37+
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+
};
50+
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];
53+
}
54+
}
55+
}
56+
57+
void test() {
58+
Graph graph;
59+
initGraph(&graph);
60+
insertVertex(&graph);
61+
insertEdge(&graph);
62+
63+
for (int i = 0; i < VERTEX_NUM; ++i) {
64+
assert(graph.vertex[i] == 'A' + i);
65+
}
66+
67+
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}
73+
};
74+
for (int i = 0; i < VERTEX_NUM; ++i) {
75+
for (int j = 0; j < VERTEX_NUM; ++j) {
76+
assert(graph.edge[i][j] == matrix[i][j]);
77+
}
78+
}
79+
}
80+
81+
int main() {
82+
test();
83+
return 0;
84+
}
85+

images/adjacency_matrix_graph.jpeg

25.6 KB
Loading

0 commit comments

Comments
 (0)