forked from MirceaOvidiu/CleanCoding-GIT-PA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample2.c
More file actions
157 lines (128 loc) · 3.7 KB
/
Copy pathexample2.c
File metadata and controls
157 lines (128 loc) · 3.7 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
#include <stdlib.h>
#include <stdio.h>
typedef struct Node {
int data;
struct Node* next;
} NODE;
typedef struct {
int vertices;
int* visited;
NODE** adjacency_lists;
} GPH;
/// utils
NODE* create_node(int v) {
NODE* new_node = (NODE*)malloc(sizeof(NODE));
new_node->data = v;
new_node->next = NULL;
return new_node;
}
GPH* create_graph(int vertices) {
GPH* graph = (GPH*)malloc(sizeof(GPH));
graph->vertices = vertices;
graph->adjacency_lists = (NODE**)malloc(vertices * sizeof(NODE*));
graph->visited = (int*)malloc(vertices * sizeof(int));
for (int i = 0; i < vertices; i++) {
graph->adjacency_lists[i] = NULL;
graph->visited[i] = 0;
}
return graph;
}
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;
}
void insedg(int nr_of_vertices, int nr_of_edges, GPH* graph) {
int src, dest;
printf("Adauga %d muchii (nodurile 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: ", i + 1);
scanf("%d %d", &src, &dest);
add_edge(graph, src, dest);
}
}
/// bfs utils
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) {
for (int i = 0; i < graph->vertices; i++) {
graph->visited[i] = 0;
}
}
/// parcurgeri
void DFS(GPH* graph, int vertex) {
NODE* temp = graph->adjacency_lists[vertex];
graph->visited[vertex] = 1;
printf("%d ", vertex);
while (temp) {
int connected_vertex = temp->data;
if (graph->visited[connected_vertex] == 0) {
DFS(graph, connected_vertex);
}
temp = temp->next;
}
}
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;
}
}
}
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("De unde plecam in DFS? ");
scanf("%d", &starting_vertex);
printf("Parcurgere cu DFS: ");
DFS(graph, starting_vertex);
printf("\n");
wipe_visited_list(graph);
printf("De unde plecam in BFS? ");
scanf("%d", &starting_vertex);
printf("Parcurgere cu BFS: ");
BFS(graph, starting_vertex);
printf("\n");
return 0;
}