-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrafo3.0.c
144 lines (129 loc) · 2.56 KB
/
grafo3.0.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
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
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 50000
typedef struct adj_list adj_list;
typedef struct queue queue;
typedef struct graph graph;
struct queue{
int current_size;
int head;
int tail;
int items[MAX_SIZE];
};
struct adj_list
{
int item;
adj_list *next;
};
struct graph{
adj_list *vertices[MAX_SIZE];
int visited[MAX_SIZE];
};
queue* create_queue()
{
queue *new_queue = (queue*) malloc(sizeof(queue));
new_queue->current_size = 0;
new_queue->head = 0;
new_queue->tail = MAX_SIZE - 1;
return new_queue;
}
int is_empty(queue *queue)
{
if(queue->current_size==0)
return 1;
else
return 0;
}
void enqueue(queue *queue, int item)
{
if (queue->current_size >= MAX_SIZE)
{
printf("Queue overflow");
}
else
{
queue->tail = (queue->tail + 1) % MAX_SIZE;
queue->items[queue->tail] = item;
queue->current_size++;
}
}
int dequeue(queue *queue)
{
if (is_empty(queue))
{
printf("Queue underflow");
return -1;
}
else
{
int dequeued = queue->items[queue->head];
queue->head = (queue->head + 1) % MAX_SIZE;
queue->current_size--;
return dequeued;
}
}
adj_list* create_adj_list(int item)
{
adj_list *new=(adj_list*)malloc(sizeof(adj_list));
new->item=item;
new->next=NULL;
return new;
}
graph* create_graph()
{
graph *grafo = (graph*) malloc(sizeof(graph));
int i;
for(i=1;i<=MAX_SIZE-1;i++)
{
grafo->vertices[i]=NULL;
grafo->visited[i]=0;
}
return grafo;
}
void add_edge(graph *graph,int v1,int v2)
{
adj_list *vertex = create_adj_list(v2);
vertex->next =graph->vertices[v1];
graph->vertices[v1]=vertex;
//Undirected graph. Edge to the other direction as well.
vertex = create_adj_list(v1);
vertex->next = graph->vertices[v2];
graph->vertices[v2] = vertex;
}
void bfs(graph *graph, int source)
{
queue *q=create_queue();
int dequeued;
adj_list *adj_l= graph->vertices[source];
graph->visited[source]=1;
enqueue(q,source);
while(!is_empty(q))
{
dequeued=dequeue(q);
adj_l = graph->vertices[dequeued];
while(adj_l != NULL)
{
if(!graph->visited[adj_l->item])
{
//printf("%d\n", adj_l->item);
graph->visited[adj_l->item] = 1;
enqueue(q,adj_l->item);
}
adj_l=adj_l->next;
}
}
}
void dfs(graph *graph, int source)
{
graph->visited[source] = 1;
printf("%d\n", source);
adj_list *adj_list = graph->vertices[source];
while (adj_list != NULL)
{
if (!graph->visited[adj_list->item])
{
dfs(graph, adj_list->item);
}
adj_list = adj_list->next;
}
}