From e7c8e83ce048b48dd4da6cefb1096f79de5da602 Mon Sep 17 00:00:00 2001 From: nihonium Date: Fri, 4 Feb 2022 21:07:01 +0300 Subject: [PATCH] BFS --- README.md | 5 ++++- graphs/main.c | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e69960c..9ab2d8a 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,5 @@ -# mipt_clang +# MIPT C + +Код из лекций Бабичева по алгоритмам и структурам данных. +Несколько решенных задач для контестов с Ejudge и горка всякой мелочевки. diff --git a/graphs/main.c b/graphs/main.c index f6db864..a8fd070 100644 --- a/graphs/main.c +++ b/graphs/main.c @@ -1,6 +1,26 @@ #include "queue.c" #include "graph.c" +void graph_bfs(graph *t, int s, int *d) { + color *c = (color *)calloc(sizeof(color),t->N); + for (int i = 0; i < t->N; i++) d[i] = -1; + queue *q = queue_new(t->N*t->N); + queue_enqueue(q, s); + d[s] = 0; c[s] = GREY; + while (!queue_empty(q)) { + int u = queue_dequeue(q); + for (int z = 0; z < t->N; z++) { + if (t->neibs[u*t->N+z] == 0 || c[z] != WHITE) continue; + d[z] = d[u] + 1; + c[z] = GREY; + queue_enqueue(q,z); + } + c[u] = BLACKl + } + queue_delete(q); + free(c); +} + int main() { queue *q = queue_new(5); }