Skip to content
Open

Tema #113

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions example2_editat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
int data;
struct Node *next;
} NODE;

typedef struct Graph {
int vertices;
int *visited;
NODE **adjacency_lists;
} GPH;

NODE *create_node(int v) {
NODE *new_node = malloc(sizeof(NODE));
new_node->data = v;
new_node->next = NULL;
return new_node;
}

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;
}

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 insert_edges(int nr_of_edges, GPH *graph) {
int src, dest;
for (int i = 0; i < nr_of_edges; i++) {
scanf("%d%d", &src, &dest);
add_edge(graph, src, dest);
}
}

void wipe_visited_list(GPH *graph) {
for (int i = 0; i < graph->vertices; i++) {
graph->visited[i] = 0;
}
}

int is_empty(NODE *queue) {
return queue == NULL;
}

void enqueue(NODE **queue, int data) {
NODE *new_node = create_node(data);
if (*queue == NULL) {
*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 DFS(GPH *graph, int vertex) {
NODE *temp = graph->adjacency_lists[vertex];
graph->visited[vertex] = 1;
printf("%d ", vertex);

while (temp != NULL) {
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;
}
}
}

void print_graph(GPH *graph) {
for (int i = 0; i < graph->vertices; i++) {
NODE *temp = graph->adjacency_lists[i];
while (temp) {
temp = temp->next;
}
}
}

int main() {
int nr_of_vertices, nr_of_edges, starting_vertex;

scanf("%d", &nr_of_vertices);
scanf("%d", &nr_of_edges);

GPH *graph = create_graph(nr_of_vertices);
insert_edges(nr_of_edges, graph);

scanf("%d", &starting_vertex);
DFS(graph, starting_vertex);

wipe_visited_list(graph);

scanf("%d", &starting_vertex);
BFS(graph, starting_vertex);

return 0;
}