forked from MirceaOvidiu/CleanCoding-GIT-PA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample1.c
More file actions
165 lines (137 loc) · 4.06 KB
/
Copy pathexample1.c
File metadata and controls
165 lines (137 loc) · 4.06 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
#include <stdio.h>
#include <stdlib.h>
// Structura pentru un nod (folosita si pentru liste de adiacenta, si pentru coada)
typedef struct Node {
int data;
struct Node *next;
} NODE;
// Structura pentru graf
typedef struct Graph {
int vertices;
int *visited;
NODE **adjacency_lists;
} GPH;
// --- FUNCTII UTILS ---
// Creeaza un nod nou
NODE *create_node(int v) {
NODE *new_node = malloc(sizeof(NODE));
new_node->data = v;
new_node->next = NULL;
return new_node;
}
// Initializeaza graful
GPH *create_graph(int vertices) {
GPH *graph = malloc(sizeof(GPH));
graph->vertices = vertices;
graph->adjacency_lists = malloc(vertices * sizeof(NODE *));
graph->visited = malloc(vertices * sizeof(int));
for (int i = 0; i < vertices; i++) {
graph->adjacency_lists[i] = NULL;
graph->visited[i] = 0;
}
return graph;
}
// Adauga o muchie neorientata
void add_edge(GPH *graph, int src, int dest) {
NODE *new_node = create_node(dest);
new_node->next = graph->adjacency_lists[src];
graph->adjacency_lists[src] = new_node;
new_node = create_node(src);
new_node->next = graph->adjacency_lists[dest];
graph->adjacency_lists[dest] = new_node;
}
// Citeste muchiile de la tastatura
void insedg(int nr_of_vertices, int nr_of_edges, GPH *graph) {
int src, dest;
printf("Adauga %d muchii (noduri de la 0 la %d):\n", nr_of_edges, nr_of_vertices - 1);
for (int i = 0; i < nr_of_edges; i++) {
printf("Muchia %d (sursa destinatie): ", i + 1);
scanf("%d %d", &src, &dest);
add_edge(graph, src, dest);
}
}
// --- FUNCTII COADA ---
int is_empty(NODE *queue) {
return queue == NULL;
}
void enqueue(NODE **queue, int data) {
NODE *new_node = create_node(data);
if (is_empty(*queue)) {
*queue = new_node;
} else {
NODE *temp = *queue;
while (temp->next) {
temp = temp->next;
}
temp->next = new_node;
}
}
int dequeue(NODE **queue) {
int data = (*queue)->data;
NODE *temp = *queue;
*queue = (*queue)->next;
free(temp);
return data;
}
void wipe_visited_list(GPH *graph, int nr_of_vertices) {
for (int i = 0; i < nr_of_vertices; i++) {
graph->visited[i] = 0;
}
}
// --- PARCURGERI ---
// DFS (Adancime)
void DFS(GPH *graph, int vertex_nr) {
NODE *temp = graph->adjacency_lists[vertex_nr];
graph->visited[vertex_nr] = 1;
printf("%d ", vertex_nr);
while (temp != NULL) {
int connected_vertex = temp->data;
if (graph->visited[connected_vertex] == 0) {
DFS(graph, connected_vertex);
}
temp = temp->next;
}
}
// BFS (Latime)
void BFS(GPH *graph, int start) {
NODE *queue = NULL;
graph->visited[start] = 1;
enqueue(&queue, start);
while (!is_empty(queue)) {
int current = dequeue(&queue);
printf("%d ", current);
NODE *temp = graph->adjacency_lists[current];
while (temp) {
int adj_vertex = temp->data;
if (graph->visited[adj_vertex] == 0) {
graph->visited[adj_vertex] = 1;
enqueue(&queue, adj_vertex);
}
temp = temp->next;
}
}
}
// --- MAIN ---
int main() {
int nr_of_vertices;
int nr_of_edges;
int starting_vertex;
printf("Cate noduri are graful? ");
scanf("%d", &nr_of_vertices);
printf("Cate muchii are graful? ");
scanf("%d", &nr_of_edges);
GPH *graph = create_graph(nr_of_vertices);
insedg(nr_of_vertices, nr_of_edges, graph);
printf("\nDe unde plecam in DFS? ");
scanf("%d", &starting_vertex);
printf("Parcurgere cu DFS: ");
DFS(graph, starting_vertex);
printf("\n");
wipe_visited_list(graph, nr_of_vertices);
printf("De unde plecam in BFS? ");
scanf("%d", &starting_vertex);
printf("Parcurgere cu BFS: ");
BFS(graph, starting_vertex);
printf("\n");
return 0;
}