Repository navigation
Expand file tree
/
Copy pathgraphmatrix.h
More file actions
196 lines (180 loc) · 6.34 KB
/
Copy pathgraphmatrix.h
File metadata and controls
196 lines (180 loc) · 6.34 KB
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
192
193
194
195
196
#ifndef GRAPHMATRIX_JL
#define GRAPHMATRIX_JL
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#include "../../../LinearDataStructures/DynamicDataStructure/LinkedQueue/linkedqueue.h"
#include "../../../LinearDataStructures/DynamicDataStructure/LinkedStack/linkedstack.h"
#define GRAPH_MATRIX_MAX_VERTICES 20 // The maximum number of vertices that our graph can have.
// Define how our (non linear and static) graph with an adjacency matrix is structured.
// matrix[i][j] stores the weight of the edge from i to j, or 0 if there's no edge. The vertices are 0..vertices-1.
typedef struct{
int matrix[GRAPH_MATRIX_MAX_VERTICES][GRAPH_MATRIX_MAX_VERTICES]; // The weights of the edges.
int vertices; // The number of vertices of our graph.
int directed; // 1 if the edges have a direction or 0 if they go both ways.
} GraphMatrix;
/********************/
/* FUNCTIONS */
/********************/
// Set Up the Graph with a Number of Vertices and no Edges. Returns 1 if successful or 0 if the number of vertices is invalid.
int initGraphMatrix(GraphMatrix *graph, int vertices, int directed){
if(vertices < 1 || vertices > GRAPH_MATRIX_MAX_VERTICES){
return 0;
}
else{
graph->vertices = vertices;
graph->directed = directed;
for(int i = 0; i < GRAPH_MATRIX_MAX_VERTICES; i++){
for(int j = 0; j < GRAPH_MATRIX_MAX_VERTICES; j++){
graph->matrix[i][j] = 0;
}
}
return 1;
}
}
// Check if a Vertex Exists. Returns 1 if it exists or 0 if it doesn't.
int isValidVertexMatrix(GraphMatrix *graph, int vertex){
if(vertex >= 0 && vertex < graph->vertices){
return 1;
}
else{
return 0;
}
}
// Add an Edge with a Weight (use 1 for unweighted graphs). If it already exists, its weight is updated.
// Returns 1 if successful or 0 if a vertex is invalid or the weight is not positive.
int addEdgeMatrix(GraphMatrix *graph, int from, int to, int weight){
if(!isValidVertexMatrix(graph, from) || !isValidVertexMatrix(graph, to) || weight <= 0){
return 0;
}
else{
graph->matrix[from][to] = weight;
if(!graph->directed){
graph->matrix[to][from] = weight;
}
return 1;
}
}
// Remove an Edge. Returns 1 if successful or 0 if the edge doesn't exist.
int removeEdgeMatrix(GraphMatrix *graph, int from, int to){
if(!isValidVertexMatrix(graph, from) || !isValidVertexMatrix(graph, to) || graph->matrix[from][to] == 0){
return 0;
}
else{
graph->matrix[from][to] = 0;
if(!graph->directed){
graph->matrix[to][from] = 0;
}
return 1;
}
}
// Check if there's an Edge from one Vertex to Another. Returns 1 if there is or 0 if there isn't.
int hasEdgeMatrix(GraphMatrix *graph, int from, int to){
if(isValidVertexMatrix(graph, from) && isValidVertexMatrix(graph, to) && graph->matrix[from][to] != 0){
return 1;
}
else{
return 0;
}
}
// Get the Weight of an Edge. Returns 1 if successful or 0 if the edge doesn't exist.
int getWeightMatrix(GraphMatrix *graph, int from, int to, int *weight){
if(!hasEdgeMatrix(graph, from, to)){
return 0;
}
else{
*weight = graph->matrix[from][to];
return 1;
}
}
// Count the Edges that Leave a Vertex (its degree, or its out-degree if the graph is directed). Returns -1 if the vertex is invalid.
int degreeMatrix(GraphMatrix *graph, int vertex){
if(!isValidVertexMatrix(graph, vertex)){
return -1;
}
int degree = 0;
for(int i = 0; i < graph->vertices; i++){
if(graph->matrix[vertex][i] != 0){
degree++;
}
}
return degree;
}
// Breadth-First Search: visit the start vertex, then its neighbours, then their neighbours... using a Queue.
// Writes the visited vertices in order. Returns 1 if successful or 0 if the start vertex is invalid or there's no memory.
int bfsGraphMatrix(GraphMatrix *graph, int start, int *order, int *count){
*count = 0;
if(!isValidVertexMatrix(graph, start)){
return 0;
}
int visited[GRAPH_MATRIX_MAX_VERTICES] = {0};
LinkedQueue queue;
initLinkedQueue(&queue);
visited[start] = 1;
if(!enQueueLinked(&queue, start)){
return 0;
}
int vertex;
while(deQueueLinked(&queue, &vertex)){
order[(*count)++] = vertex;
for(int i = 0; i < graph->vertices; i++){
if(graph->matrix[vertex][i] != 0 && !visited[i]){
visited[i] = 1;
if(!enQueueLinked(&queue, i)){
freeLinkedQueue(&queue);
return 0;
}
}
}
}
return 1;
}
// Depth-First Search: go as deep as possible before going back, using a Stack. The smallest neighbour is visited first.
// Writes the visited vertices in order. Returns 1 if successful or 0 if the start vertex is invalid or there's no memory.
int dfsGraphMatrix(GraphMatrix *graph, int start, int *order, int *count){
*count = 0;
if(!isValidVertexMatrix(graph, start)){
return 0;
}
int visited[GRAPH_MATRIX_MAX_VERTICES] = {0};
LinkedStack stack;
initLinkedStack(&stack);
if(!pushLinkedStack(&stack, start)){
return 0;
}
int vertex;
while(popLinkedStack(&stack, &vertex)){
if(visited[vertex]){
continue;
}
visited[vertex] = 1;
order[(*count)++] = vertex;
// We push the neighbours from the greatest to the smallest, so the smallest one is popped first.
for(int i = graph->vertices-1; i >= 0; i--){
if(graph->matrix[vertex][i] != 0 && !visited[i]){
if(!pushLinkedStack(&stack, i)){
freeLinkedStack(&stack);
return 0;
}
}
}
}
return 1;
}
// Print the Adjacency Matrix. Displays one row per vertex with the weight of each edge (0 if there's no edge).
void printGraphMatrix(GraphMatrix *graph){
printf(" ");
for(int j = 0; j < graph->vertices; j++){
printf("%3d", j);
}
printf("\n");
for(int i = 0; i < graph->vertices; i++){
printf(" %2d:", i);
for(int j = 0; j < graph->vertices; j++){
printf("%3d", graph->matrix[i][j]);
}
printf("\n");
}
}
#endif //GRAPHMATRIX_JL