Debian Codegraph @

sets.go

raw symbols SCIP
1 package graph
2
3 import (
4 "fmt"
5 )
6
7 // Union combines two given graphs into a new graph. The vertex hashes in both
8 // graphs are expected to be unique. The two input graphs will remain unchanged.
9 //
10 // Both graphs should be either directed or undirected. All traits for the new
11 // graph will be derived from g.
12 func Union[K comparable, T any](g, h Graph[K, T]) (Graph[K, T], error) {
13 union, err := g.Clone()
14 if err != nil {
15 return union, fmt.Errorf("failed to clone g: %w", err)
16 }
17
18 adjacencyMap, err := h.AdjacencyMap()
19 if err != nil {
20 return union, fmt.Errorf("failed to get adjacency map: %w", err)
21 }
22
23 addedEdges := make(map[K]map[K]struct{})
24
25 for currentHash := range adjacencyMap {
26 vertex, properties, err := h.VertexWithProperties(currentHash) //nolint:govet
27 if err != nil {
28 return union, fmt.Errorf("failed to get vertex %v: %w", currentHash, err)
29 }
30
31 err = union.AddVertex(vertex, copyVertexProperties(properties))
32 if err != nil {
33 return union, fmt.Errorf("failed to add vertex %v: %w", currentHash, err)
34 }
35 }
36
37 for _, adjacencies := range adjacencyMap {
38 for _, edge := range adjacencies {
39 if _, sourceOK := addedEdges[edge.Source]; sourceOK {
40 if _, targetOK := addedEdges[edge.Source][edge.Target]; targetOK {
41 // If the edge addedEdges[source][target] exists, the edge
42 // has already been created and thus can be skipped here.
43 continue
44 }
45 }
46
47 err = union.AddEdge(copyEdge(edge))
48 if err != nil {
49 return union, fmt.Errorf("failed to add edge (%v, %v): %w", edge.Source, edge.Target, err)
50 }
51
52 if _, ok := addedEdges[edge.Source]; !ok {
53 addedEdges[edge.Source] = make(map[K]struct{})
54 }
55 addedEdges[edge.Source][edge.Target] = struct{}{}
56 }
57 }
58
59 return union, nil
60 }
61
62 // unionFind implements a union-find or disjoint set data structure that works
63 // with vertex hashes as vertices. It's an internal helper type at the moment,
64 // but could perhaps be exposed publicly in the future.
65 //
66 // unionFind is not related to the Union function.
67 type unionFind[K comparable] struct {
68 parents map[K]K
69 }
70
71 func newUnionFind[K comparable](vertices ...K) *unionFind[K] {
72 u := &unionFind[K]{
73 parents: make(map[K]K, len(vertices)),
74 }
75
76 for _, vertex := range vertices {
77 u.parents[vertex] = vertex
78 }
79
80 return u
81 }
82
83 func (u *unionFind[K]) add(vertex K) {
84 u.parents[vertex] = vertex
85 }
86
87 func (u *unionFind[K]) union(vertex1, vertex2 K) {
88 root1 := u.find(vertex1)
89 root2 := u.find(vertex2)
90
91 if root1 == root2 {
92 return
93 }
94
95 u.parents[root2] = root1
96 }
97
98 func (u *unionFind[K]) find(vertex K) K {
99 root := vertex
100
101 for u.parents[root] != root {
102 root = u.parents[root]
103 }
104
105 // Perform a path compression in order to optimize of future find calls.
106 current := vertex
107
108 for u.parents[current] != root {
109 parent := u.parents[vertex]
110 u.parents[vertex] = root
111 current = parent
112 }
113
114 return root
115 }
116
117 func copyVertexProperties(source VertexProperties) func(*VertexProperties) {
118 return func(p *VertexProperties) {
119 for k, v := range source.Attributes {
120 p.Attributes[k] = v
121 }
122 p.Weight = source.Weight
123 }
124 }