GRASS 8 Programmer's Manual 8.6.0dev(2026)-1878fdfec5
Loading...
Searching...
No Matches
spanningtree.c
Go to the documentation of this file.
1/*!
2 \file vector/neta/spanningtree.c
3
4 \brief Network Analysis library - spanning tree
5
6 Computes minimum spanning tree in the network.
7
8 SPDX-FileCopyrightText: 2009-2010 Daniel Bundala
9 SPDX-FileCopyrightText: GRASS Development Team
10 SPDX-License-Identifier: GPL-2.0-or-later
11
12 \author Daniel Bundala (Google Summer of Code 2009)
13 */
14
15#include <stdio.h>
16#include <stdlib.h>
17#include <grass/gis.h>
18#include <grass/vector.h>
19#include <grass/glocale.h>
20#include <grass/dgl/graph.h>
21
22struct union_find {
23 int *parent;
24};
25
26static int uf_initialize(struct union_find *uf, int size)
27{
28 int i;
29
30 uf->parent = (int *)G_calloc(size, sizeof(int));
31 if (!uf->parent)
32 return 0;
33 for (i = 0; i < size; i++)
34 uf->parent[i] = i;
35 return 1;
36}
37
38static void uf_release(struct union_find *uf)
39{
40 G_free(uf->parent);
41}
42
43static int uf_find(struct union_find *uf, int v)
44{
45 int cur = v, tmp;
46
47 while (uf->parent[cur] != cur)
48 cur = uf->parent[cur];
49 while (uf->parent[v] != v) {
50 tmp = uf->parent[v];
51 uf->parent[v] = cur;
52 v = tmp;
53 }
54 return cur;
55}
56
57/*TODO: union by rank */
58static void uf_union(struct union_find *uf, int u, int v)
59{
60 int parent_u = uf_find(uf, u);
61 int parent_v = uf_find(uf, v);
62
63 if (parent_u != parent_v)
64 uf->parent[parent_u] = parent_v;
65}
66
67typedef struct {
68 dglInt32_t cost;
69 dglInt32_t *edge;
70} edge_cost_pair;
71
72static int cmp_edge(const void *pa, const void *pb)
73{
74 if (((edge_cost_pair *)pa)->cost < ((edge_cost_pair *)pb)->cost)
75 return -1;
76
77 return (((edge_cost_pair *)pa)->cost > ((edge_cost_pair *)pb)->cost);
78}
79
80/*!
81 \brief Get number of edges in the spanning forest
82
83 \param graph input graph
84 \param[out] tree_list list of edges
85
86 \return number of edges
87 \return -1 on failure
88 */
90{
91 int nnodes, edges, nedges, i, index;
92 edge_cost_pair *perm; /*permutation of edges in ascending order */
93 struct union_find uf;
95
96 /* TODO: consider closed nodes / node costs */
97
99 nedges = dglGet_EdgeCount(graph);
100 perm = (edge_cost_pair *)G_calloc(nedges, sizeof(edge_cost_pair));
101 if (!perm || !uf_initialize(&uf, nnodes + 1)) {
102 G_fatal_error(_("Out of memory"));
103 return -1;
104 }
105 /* dglGetEdge is only supported with graphs version > 1. Therefore this
106 * complicated enumeration of the edges... */
107 index = 0;
108 G_message(_("Computing minimum spanning tree..."));
110 for (i = 1; i <= nnodes; i++) {
111 G_percent(i, nnodes + nedges, 1);
112 dglInt32_t *edge;
113
115 &et, graph,
117 for (edge = dglEdgeset_T_First(&et); edge;
118 edge = dglEdgeset_T_Next(&et))
119 if (dglEdgeGet_Id(graph, edge) > 0) {
120 perm[index].edge = edge;
121 perm[index].cost = dglEdgeGet_Cost(graph, edge);
122 index++;
123 }
124
126 }
127 edges = 0;
128 qsort((void *)perm, index, sizeof(edge_cost_pair), cmp_edge);
129 for (i = 0; i < index; i++) {
130 G_percent(i + nnodes, nnodes + nedges, 1);
131 dglInt32_t head =
132 dglNodeGet_Id(graph, dglEdgeGet_Head(graph, perm[i].edge));
133 dglInt32_t tail =
134 dglNodeGet_Id(graph, dglEdgeGet_Tail(graph, perm[i].edge));
135 if (uf_find(&uf, head) != uf_find(&uf, tail)) {
136 uf_union(&uf, head, tail);
137 edges++;
139 }
140 }
141 G_percent(index, index, 1);
142 G_free(perm);
143 uf_release(&uf);
144 return edges;
145}
void G_percent(long, long, int)
Print percent complete messages.
Definition percent.c:59
void G_percent_reset(void)
Reset G_percent() to 0%; do not add newline.
Definition percent.c:115
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
#define G_calloc(m, n)
Definition defs/gis.h:137
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_message(const char *,...) __attribute__((format(printf
int Vect_list_append(struct ilist *, int)
Append new item to the end of list if not yet present.
#define _(str)
Definition glocale.h:10
int NetA_spanning_tree(dglGraph_s *graph, struct ilist *tree_list)
Get number of edges in the spanning forest.
List of integers.
Definition gis.h:712
long dglInt32_t
Definition type.h:24
dglInt32_t * dglGetNode(dglGraph_s *pGraph, dglInt32_t nNodeId)
dglInt32_t * dglNodeGet_OutEdgeset(dglGraph_s *pGraph, dglInt32_t *pnNode)
int dglEdgeset_T_Initialize(dglEdgesetTraverser_s *pT, dglGraph_s *pGraph, dglInt32_t *pnEdgeset)
dglInt32_t dglEdgeGet_Id(dglGraph_s *pGraph, dglInt32_t *pnEdge)
dglInt32_t * dglEdgeset_T_First(dglEdgesetTraverser_s *pT)
int dglGet_EdgeCount(dglGraph_s *pgraph)
dglInt32_t * dglEdgeset_T_Next(dglEdgesetTraverser_s *pT)
int dglGet_NodeCount(dglGraph_s *pgraph)
dglInt32_t dglEdgeGet_Cost(dglGraph_s *pGraph, dglInt32_t *pnEdge)
void dglEdgeset_T_Release(dglEdgesetTraverser_s *pT)
dglInt32_t * dglEdgeGet_Head(dglGraph_s *pGraph, dglInt32_t *pnEdge)
dglInt32_t * dglEdgeGet_Tail(dglGraph_s *pGraph, dglInt32_t *pnEdge)
dglInt32_t dglNodeGet_Id(dglGraph_s *pGraph, dglInt32_t *pnNode)