-
Notifications
You must be signed in to change notification settings - Fork 10
/
graph.go
213 lines (180 loc) · 4.12 KB
/
graph.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package graphs
import (
"fmt"
"sort"
)
// A Vertex can be just anything.
type Vertex interface {
comparable
}
// An Edge connects two vertices with a cost.
type Edge[T Vertex] struct {
Start T
End T
Cost float64
}
// A Halfedge is an edge where just the end vertex is
// stored. The start vertex is inferred from the context.
type Halfedge[T Vertex] struct {
End T
Cost float64
}
// A Graph is defined by its vertices and edges stored as
// an adjacency set.
type Graph[T Vertex] struct {
Adjacency map[T]*Set[Halfedge[T]]
Directed bool
}
// NewGraph creates a new empty graph.
func NewGraph[T Vertex]() *Graph[T] {
return &Graph[T]{
Adjacency: map[T]*Set[Halfedge[T]]{},
Directed: false,
}
}
// NewDigraph creates a new empty directed graph.
func NewDigraph[T Vertex]() *Graph[T] {
graph := NewGraph[T]()
graph.Directed = true
return graph
}
// AddVertex adds the given vertex to the graph.
func (g *Graph[T]) AddVertex(v T) {
if _, exists := g.Adjacency[v]; !exists {
g.Adjacency[v] = NewSet[Halfedge[T]]()
}
}
// AddEdge adds an edge to the graph. The edge connects
// vertex v1 and vertex v2 with cost c.
func (g *Graph[T]) AddEdge(v1, v2 T, c float64) {
g.AddVertex(v1)
g.AddVertex(v2)
g.Adjacency[v1].Add(Halfedge[T]{
End: v2,
Cost: c,
})
if !g.Directed {
g.Adjacency[v2].Add(Halfedge[T]{
End: v1,
Cost: c,
})
}
}
// Dump prints all edges with their cost to stdout.
func (g *Graph[T]) Dump() {
g.EachEdge(func(e Edge[T], _ func()) {
fmt.Printf("(%v,%v,%f)\n", e.Start, e.End, e.Cost)
})
}
// NVertices returns the number of vertices.
func (g *Graph[T]) NVertices() int {
return len(g.Adjacency)
}
// NEdges returns the number of edges.
func (g *Graph[T]) NEdges() int {
n := 0
for _, v := range g.Adjacency {
n += v.Len()
}
// Don’t count a-b and b-a edges for undirected graphs
// as two separate edges.
if !g.Directed {
n /= 2
}
return n
}
// Equals returns whether the graph is equal to the given graph.
// Two graphs are equal of their adjacency is equal.
func (g *Graph[T]) Equals(g2 *Graph[T]) bool {
// Two graphs with different number of vertices aren’t equal.
if g.NVertices() != g2.NVertices() {
return false
}
// Some for number of edges.
if g.NEdges() != g2.NEdges() {
return false
}
// Check if the adjacency for each vertex is equal
// for both graphs.
a1 := g.Adjacency
a2 := g2.Adjacency
for k, v := range a1 {
if !v.Equals(a2[k]) {
return false
}
}
return true
}
// EachVertex calls f for every vertex.
func (g *Graph[T]) EachVertex(f func(T, func())) {
var stopped bool
stop := func() { stopped = true }
for k, _ := range g.Adjacency {
f(k, stop)
if stopped {
break
}
}
}
// SortedEdges is an array of edges that can be sorted
// by their cost.
type SortedEdges[T Vertex] []Edge[T]
func (se SortedEdges[T]) Len() int {
return len(se)
}
func (se SortedEdges[T]) Less(i, j int) bool {
return se[i].Cost < se[j].Cost
}
func (se SortedEdges[T]) Swap(i, j int) {
se[i], se[j] = se[j], se[i]
}
// SortedEdges returns an array of edges sorted by their cost.
func (g *Graph[T]) SortedEdges() SortedEdges[T] {
set := NewSet[Edge[T]]()
for v := range g.Adjacency {
g.EachHalfedge(v, func(he Halfedge[T], _ func()) {
set.Add(Edge[T]{
Start: v,
End: he.End,
Cost: he.Cost,
})
})
}
edges := make(SortedEdges[T], set.Len())
set.Each(func(e Edge[T], _ func()) {
edges = append(edges, e)
})
sort.Sort(&edges)
return edges
}
// EachEdge calls f for every edge.
func (g *Graph[T]) EachEdge(f func(Edge[T], func())) {
var stopped bool
stop := func() { stopped = true }
for v, s := range g.Adjacency {
s.Each(func(he Halfedge[T], innerStop func()) {
edge := Edge[T]{v, he.End, he.Cost}
f(edge, stop)
if stopped {
innerStop()
}
})
if stopped {
break
}
}
}
// EachHalfedge calls f for every halfedge for
// the given start vertex.
func (g *Graph[T]) EachHalfedge(v T, f func(Halfedge[T], func())) {
if s, exists := g.Adjacency[v]; exists {
var stopped bool
stop := func() { stopped = true }
s.Each(func(he Halfedge[T], innerStop func()) {
f(he, stop)
if stopped {
innerStop()
}
})
}
}