Graphviz 14.0.2~dev.20251015.0935
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 Breadth First Search
14 Computes single-source distances for
15 unweighted graphs
16
17******************************************/
18
19#include <neatogen/bfs.h>
20#include <util/list.h>
21
22void bfs(int vertex, vtx_data *graph, int n, DistType *dist) {
23 DistType closestDist = INT_MAX;
24
25 // initial distances with edge weights
26 for (int i = 0; i < n; i++)
27 dist[i] = -1;
28 dist[vertex] = 0;
29
30 LIST(int) Q = {0};
32
33 while (!LIST_IS_EMPTY(&Q)) {
34 const int closestVertex = LIST_POP_FRONT(&Q);
35 closestDist = dist[closestVertex];
36 for (size_t i = 1; i < graph[closestVertex].nedges; i++) {
37 const int neighbor = graph[closestVertex].edges[i];
38 if (dist[neighbor] < -0.5) { // first time to reach neighbor
39 const DistType bump = graph[0].ewgts == NULL
40 ? 1
41 : (DistType)graph[closestVertex].ewgts[i];
42 dist[neighbor] = closestDist + bump;
44 }
45 }
46 }
47
48 // for dealing with disconnected graphs
49 for (int i = 0; i < n; i++)
50 if (dist[i] < -0.5) // `i` is not connected to `vertex`
51 dist[i] = closestDist + 10;
52
53 LIST_FREE(&Q);
54}
void bfs(int vertex, vtx_data *graph, int n, DistType *dist)
compute vector dist of distances of all nodes from vertex
Definition bfs.c:22
static double dist(int dim, double *x, double *y)
node NULL
Definition grammar.y:181
Agraph_t * graph(char *name)
Definition gv.cpp:32
type-generic dynamically expanding list
#define LIST(type)
Definition list.h:55
#define LIST_POP_FRONT(list)
Definition list.h:394
#define LIST_FREE(list)
Definition list.h:370
#define LIST_IS_EMPTY(list)
Definition list.h:90
#define LIST_PUSH_BACK(list, item)
Definition list.h:384
#define neighbor(t, i, edim, elist)
Definition make_map.h:41
int DistType
Definition sparsegraph.h:38
Definition legal.c:32