Graphviz 13.0.0~dev.20241220.2304
Loading...
Searching...
No Matches
bfs.c
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11
12/******************************************
13
14 Breadth First Search
15 Computes single-source distances for
16 unweighted graphs
17
18******************************************/
19
20#include <neatogen/bfs.h>
21#include <stdbool.h>
22#include <stdlib.h>
23#include <util/alloc.h>
24
25void bfs(int vertex, vtx_data *graph, int n, DistType *dist)
26 /* compute vector 'dist' of distances of all nodes from 'vertex' */
27{
28 int closestVertex, neighbor;
29 DistType closestDist = INT_MAX;
30
31 /* initial distances with edge weights: */
32 for (int i = 0; i < n; i++)
33 dist[i] = -1;
34 dist[vertex] = 0;
35
36 Queue Q;
37 mkQueue(&Q, n);
38 initQueue(&Q, vertex);
39
40 if (graph[0].ewgts == NULL) {
41 while (deQueue(&Q, &closestVertex)) {
42 closestDist = dist[closestVertex];
43 for (size_t i = 1; i < graph[closestVertex].nedges; i++) {
44 neighbor = graph[closestVertex].edges[i];
45 if (dist[neighbor] < -0.5) { /* first time to reach neighbor */
46 dist[neighbor] = closestDist + 1;
47 enQueue(&Q, neighbor);
48 }
49 }
50 }
51 } else {
52 while (deQueue(&Q, &closestVertex)) {
53 closestDist = dist[closestVertex];
54 for (size_t i = 1; i < graph[closestVertex].nedges; i++) {
55 neighbor = graph[closestVertex].edges[i];
56 if (dist[neighbor] < -0.5) { /* first time to reach neighbor */
57 dist[neighbor] =
58 closestDist +
59 (DistType) graph[closestVertex].ewgts[i];
60 enQueue(&Q, neighbor);
61 }
62 }
63 }
64 }
65
66 /* For dealing with disconnected graphs: */
67 for (int i = 0; i < n; i++)
68 if (dist[i] < -0.5) /* 'i' is not connected to 'vertex' */
69 dist[i] = closestDist + 10;
70
71 freeQueue(&Q);
72}
73
74void mkQueue(Queue * qp, int size)
75{
76 qp->data = gv_calloc(size, sizeof(int));
77 qp->queueSize = size;
78 qp->start = qp->end = 0;
79}
80
81void freeQueue(Queue * qp)
82{
83 free(qp->data);
84}
85
86void initQueue(Queue * qp, int startVertex)
87{
88 qp->data[0] = startVertex;
89 qp->start = 0;
90 qp->end = 1;
91}
92
93bool deQueue(Queue * qp, int *vertex)
94{
95 if (qp->start >= qp->end)
96 return false; /* underflow */
97 *vertex = qp->data[qp->start++];
98 return true;
99}
100
101bool enQueue(Queue * qp, int vertex)
102{
103 if (qp->end >= qp->queueSize)
104 return false; /* overflow */
105 qp->data[qp->end++] = vertex;
106 return true;
107}
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
Definition alloc.h:26
void bfs(int vertex, vtx_data *graph, int n, DistType *dist)
Definition bfs.c:25
void initQueue(Queue *qp, int startVertex)
Definition bfs.c:86
void freeQueue(Queue *qp)
Definition bfs.c:81
void mkQueue(Queue *qp, int size)
Definition bfs.c:74
bool deQueue(Queue *qp, int *vertex)
Definition bfs.c:93
bool enQueue(Queue *qp, int vertex)
Definition bfs.c:101
static double dist(int dim, double *x, double *y)
void free(void *)
node NULL
Definition grammar.y:163
Agraph_t * graph(char *name)
Definition gv.cpp:30
#define neighbor(t, i, edim, elist)
Definition make_map.h:41
int DistType
Definition sparsegraph.h:37
Definition bfs.h:21
int start
Definition bfs.h:25
int * data
Definition bfs.h:22
int end
Definition bfs.h:24
int queueSize
Definition bfs.h:23
Definition legal.c:31