-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.h
More file actions
329 lines (285 loc) · 8.24 KB
/
graph.h
File metadata and controls
329 lines (285 loc) · 8.24 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/**
* ================================================
*
* Copyright 2025 Manoel Vilela
*
* Author: Manoel Vilela
* Contact: manoel_vilela@engineer.com
* Organization: ITA
*
* ===============================================
*/
#ifndef GRAPH_H
#define GRAPH_H
#include <stdbool.h>
#include "../set/set.h"
typedef struct Graph Graph;
typedef enum edgeType {
TREE,
CROSS,
FORWARD,
BACK,
} EdgeType;
static inline const char* graph_edge_type_name(EdgeType edge_type) {
switch (edge_type) {
case TREE: return "TREE";
case CROSS: return "CROSS";
case FORWARD: return "FORWARD";
case BACK: return "BACK";
}
return "?";
}
/**
* @brief Creates a new directed graph.
* @return A pointer to the new graph.
* @ingroup DataStructureMethods
*/
Graph* graph_create();
/**
* @brief Creates a new undirected graph.
* @return A pointer to the new graph.
* @ingroup DataStructureMethods
*/
Graph* graph_undirected_create();
/**
* @brief Creates a new tarjan graph.
* @param directed flag if graph should be directed or not.
* @return A pointer to the new graph.
* @ingroup DataStructureMethods
*/
Graph* graph_tarjan_create(bool directed);
/**
* @brief Get the number of nodes.
* @return number of nodes on the graph.
* @ingroup DataStructureMethods
*/
size_t graph_size(Graph *g);
/**
* @brief Check if graph is weighted.
* @return true if is weighted, false otherwise.
* @ingroup DataStructureMethods
*/
bool graph_is_directed(Graph *g);
/**
* @brief Check if graph is weighted.
* @return true if is weighted, false otherwise.
* @ingroup DataStructureMethods
*/
bool graph_is_weighted(Graph *g);
/**
* @brief Adds a node to the graph.
* @param g The graph.
* @param node The node to be added.
* @ingroup DataStructureMethods
*/
void graph_add_node(Graph *g, int node);
/**
* @brief Adds an edge to the graph.
* @param g The graph.
* @param u The source node.
* @param v The destination node.
* @ingroup DataStructureMethods
*/
void graph_add_edge(Graph *g, int u, int v);
/**
* @brief Adds a weighted edge to the graph.
* @param g The graph.
* @param u The source node.
* @param v The destination node.
* @param weight The weight of the edge.
* @ingroup DataStructureMethods
*/
void graph_add_edge_with_weight(Graph *g, int u, int v, int weight);
/**
* @brief Gets the weight of an edge.
* @param g The graph.
* @param u The source node.
* @param v The destination node.
* @return The weight of the edge, or -1 if the edge does not exist.
* @ingroup DataStructureMethods
*/
int graph_get_edge_weight(Graph *g, int u, int v);
/**
* @brief Removes an edge from the graph.
* @param g The graph.
* @param u The source node.
* @param v The destination node.
* @ingroup DataStructureMethods
*/
void graph_remove_edge(Graph *g, int u, int v);
/**
* @brief Removes a node from the graph.
* @param g The graph.
* @param node The node to be removed.
* @ingroup DataStructureMethods
*/
void graph_remove_node(Graph *g, int node);
/**
* @brief Checks if an edge exists in the graph.
* @param g The graph.
* @param u The source node.
* @param v The destination node.
* @return True if the edge exists, false otherwise.
* @ingroup DataStructureMethods
*/
bool graph_has_edge(Graph *g, int u, int v);
/**
* @brief Checks if a node exists in the graph.
* @param g The graph.
* @param u The node.
* @return True if the node exists, false otherwise.
* @ingroup DataStructureMethods
*/
bool graph_has_node(Graph *g, int u);
/**
* @brief Gets the neighbors of a node.
* @param g The graph.
* @param node The node.
* @return A set containing the neighbors of the node.
* @ingroup DataStructureMethods
*/
Set* graph_get_neighbors(Graph *g, int node);
/**
* @brief Frees the memory allocated for the graph.
* @param g The graph.
* @ingroup DataStructureMethods
*/
void graph_free(Graph *g);
/**
* @brief Prints the graph.
* @param g The graph.
* @ingroup DataStructureMethods
*/
void graph_print(Graph *g);
/**
* @brief Exports the graph to a DOT file for visualization with Graphviz.
* @param g The graph.
* @param filename The name of the output DOT file.
* @ingroup DataStructureMethods
*/
void graph_export_to_dot(Graph *g, const char* filename);
/**
* @brief Performs a Breadth-First Search on a graph.
* @param g The graph to traverse.
* @param start_node The node to start the traversal from.
* @return A node iterator
* @ingroup DataStructureMethods
*/
Iterator* graph_bfs(Graph *g, int start_node);
/**
* @brief Performs a Depth-First Search on a graph.
* @param g The graph to traverse.
* @param start_node The node to start the traversal from.
* @return A node iterator
* @ingroup DataStructureMethods
*/
Iterator* graph_dfs(Graph *g, int start_node);
/**
* @brief Iterate over nodes of the graph.
* @param g The graph to traverse.
* @ingroup DataStructureMethods
*/
Iterator* graph_nodes_iterator(Graph *g);
/**
* @brief List with edges of the graph with (key,data).
* @param g The graph to traverse.
* @ingroup DataStructureMethods
*/
List* graph_edges(Graph *g);
/**
* @brief List with edges of the graph with (key,data) ordered ascending.
* @param g The graph to traverse.
* @ingroup DataStructureMethods
*/
List* graph_edges_ordered(Graph *g);
/**
* @brief Sum of the edge weights. If undirected, calculate (u, v) == (v, u) only once.
* @param g The graph to traverse.
* @ingroup DataStructureMethods
*/
int graph_edges_sum(Graph *g);
/**
* @brief Get the maximum node id on the graph.
* @return maximum node id on the graph.
* @ingroup DataStructureMethods
*/
int graph_max_node_id(Graph *g);
/**
* @brief Check if graph has cycles.
* @param g The graph to traverse.
* @return true if has any cycle, false otherwise.
* @ingroup DataStructureMethods
*/
bool graph_acyclical(Graph *g);
/**
* @brief Check if graph can be classified as Directed Acyclical Graph.
* @param g The graph to traverse.
* @return true if is directed and acyclical , false otherwise.
* @ingroup DataStructureMethods
*/
bool graph_is_dag(Graph *g);
/**
* @brief Create a new graph with tarjan arc classification as weight of the edges.
* @param g The graph to traverse.
* @return new graph.
* @ingroup DataStructureMethods
*/
Graph* graph_tarjan(Graph *g);
/**
* @brief Create a array of strong components using tarjan algorithm.
* @param g The graph to traverse.
* @return array of componentes indexed by node id.
* @ingroup DataStructureMethods
*/
int* graph_strong_components(Graph *g);
/**
* This method is defined in acyclical.c because it inherits part of the acyclical code.
*
* @brief Creat a list with the nodes in topological sort.
* @param g The graph to traverse.
* @return a list with the nodes that represents the topological sort. Return NULL if graph cannot be sort.
* @ingroup DataStructureMethods
*/
List* graph_topological_sort(Graph *g);
/**
* @brief Run dijkstra algorithm on the graph.
* @param g The graph to traverse.
* @param source The source node to start.
* @return a new graph with the shortest path tree.
* @ingroup DataStructureMethods
*/
Graph* graph_dijkstra(Graph* g, int source);
/**
* @brief Run Kruskal algorithm to get the minimum-span tree.
* @param g The graph to traverse.
* @return a new graph with the minimum span tree.
* @ingroup DataStructureMethods
*/
Graph* graph_kruskal(Graph* g);
/**
* @brief Run Prim algorithm to get the minimum-span tree.
* @param g The graph to traverse.
* @param start Initial node to start.
* @return a new graph with the minimum span tree.
* @ingroup DataStructureMethods
*/
Graph* graph_prim(Graph* g, int start);
/**
* @brief Run dijkstra algorithm and calculate the minimum distance.
* @param g The graph to traverse.
* @param source The source node to start.
* @param source The destination node.
* @return a new graph with the shortest path tree.
* @ingroup DataStructureMethods
*/
int graph_minimum_distance(Graph* g, int source, int destination);
/**
* @brief Run dijkstra algorithm and return the shortest path.
* @param g The graph to traverse.
* @param source The source node to start.
* @param source The destination node.
* @return a list with nodes as the shortest path.
* @ingroup DataStructureMethods
*/
List* graph_shortest_path(Graph* g, int source, int destination);
#endif /* GRAPH_H */