This repository has been archived by the owner on Mar 25, 2024. It is now read-only.
generated from gomu-gomu/starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.c
74 lines (61 loc) · 1.36 KB
/
graph.c
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
#include <stdlib.h>
#include <stdbool.h>
#include "graph.h"
Graph *creerGraph(int sommets, int aretes)
{
Graph *graph = (Graph *)malloc(sizeof(Graph));
graph->aretes = aretes;
graph->sommets = sommets;
graph->edge = (Edge *)malloc(aretes * sizeof(Edge));
graph->composant = (int *)malloc(aretes * sizeof(int));
return graph;
}
void detruireGraph(Graph *graph)
{
if (graph != NULL)
{
graph->aretes = 0;
graph->sommets = 0;
if (graph->edge != NULL)
{
free(graph->edge);
}
if (graph->composant != NULL)
{
free(graph->composant);
}
free(graph);
}
}
void ajouterArete(Graph *graph, int src, int dest, int i)
{
graph->edge[i].src = src;
graph->edge[i].dest = dest;
}
void DFS(Graph *graph, int sommet, int numComposant, bool visite[])
{
visite[sommet] = true;
graph->composant[sommet] = numComposant;
for (int i = 0; i < graph->aretes; i++)
{
if (graph->edge[i].src == sommet && !visite[graph->edge[i].dest])
DFS(graph, graph->edge[i].dest, numComposant, visite);
}
}
void trouverComposantsConnexes(Graph *graph)
{
int visite[graph->sommets];
for (int i = 0; i < graph->sommets; i++)
{
visite[i] = false;
}
int numComposant = 0;
for (int i = 0; i < graph->sommets; i++)
{
if (!visite[i])
{
DFS(graph, i, numComposant, visite);
numComposant++;
}
}
}