aboutsummaryrefslogtreecommitdiff
path: root/sem1/algo/mm10/bfs.c
blob: 982ecef0c950c65e24d7620ee834f0878f02447b (plain)
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
#include <stdio.h>
#include <stdlib.h>

#define COL_WHITE 0
#define COL_GRAY  1
#define COL_BLACK 2

typedef struct list_node_t_struct {
	struct list_node_t_struct *next;
	void *val;
} list_node_t;

typedef struct vertex_struct {
	char ref;
	int color;
	int dist;
	struct vertex_struct *p;
	list_node_t *adj;
} vertex_t;

vertex_t *create_vertex(char ref) {
	// Get some space TODO check for null
	vertex_t *v = malloc(sizeof(vertex_t));

	// Set values
	v->ref = ref;
	v->color = COL_WHITE;
	v->dist = -1;
	v->p = NULL;
	v->adj = NULL;
}

vertex_t *add_adj(vertex_t *v, vertex_t *add) {
	list_node_t *oldN = v->adj;

	// Create new list node
	list_node_t *n = malloc(sizeof(list_node_t));
	n->val = add;
	n->next = oldN;

	v->adj = n;

	return add;
}

void print_adj(vertex_t *v) {
	list_node_t *n = v->adj;
	printf("[ ");
	while(n) {
		printf("%c ", *((char *)n->val));
		n = n->next;
	}
	printf("]\n");
}

vertex_t *vertexes[128];

void add_edge(char a, char b) {
	// Does a exists
	if( vertexes[a] == NULL ) {
		vertexes[a] = create_vertex(a);
	}
	// What about b
	if( vertexes[b] == NULL ) {
		vertexes[b] = create_vertex(b);
	}

	// Add edge
	add_adj(vertexes[a], vertexes[b]);
}

typedef struct {
	unsigned int len;
	list_node_t *in;
	list_node_t *out;
} queue_t;

void enqueue(queue_t *q, vertex_t *v) {
	list_node_t *n = malloc(sizeof(list_node_t));
	n->val = v;

	// Add at end
	list_node_t *old = q->in;
	q->in = n;
	if( old ) {
		old->next = n;
	}

	// Make sure we have an out
	if( q->out == NULL ) {
		q->out = n;
	}

	q->len++;
}

vertex_t *dequeue(queue_t *q) {
	list_node_t *n = q->out;
	if( n == NULL ) {
		return NULL;
	}
	vertex_t *v = (vertex_t *)n->val;

	// Move out forward
	q->out = n->next;
	q->len--;

	// Free node and return
	free(n);
	return v;
}

void bfs(vertex_t *s) {
	queue_t q = {0, NULL, NULL };
	enqueue(&q, s);
	s->dist = 0;
	
	while( q.len ) {
		vertex_t *u = dequeue(&q);
		list_node_t *n = u->adj;
		while( n ) {
			vertex_t *v = (vertex_t *)n->val;
			if( v->color == COL_WHITE ) {
				v->color = COL_GRAY;
				v->dist = u->dist + 1;
				v->p = u;
				enqueue(&q, v);
			}
			n = n->next;
		}
		u->color = COL_BLACK;
	}
	
}

int main() {
	add_edge('s', 'd'); // S
	add_edge('s', 'c');
	add_edge('s', 'a');
	add_edge('d', 'e'); // D
	add_edge('d', 'c');
	add_edge('e', 'g'); // E
	add_edge('e', 'f');
	add_edge('c', 's'); // C
	add_edge('c', 'g');
	add_edge('g', 'f'); // G

	//print_adj(vertexes['d']);
	bfs(vertexes['s']);

	char c;
	while( (c = getchar()) != EOF) {
		if( vertexes[c] != NULL ) {
			print_adj(vertexes[c]);
			printf("%d\n", vertexes[c]->dist);
		} else if (c == 10) {
		} else {
			printf("Not found\n");
		}
	}


}