This repository has been archived by the owner on Nov 22, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphy.go
358 lines (286 loc) · 7.34 KB
/
graphy.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// Copyright © 2019 Developer Network, LLC
//
// This file is subject to the terms and conditions defined in
// file 'LICENSE', which is part of this source code package.
package graph
import (
"context"
"fmt"
"sync"
"devnw.com/validator"
"github.com/pkg/errors"
)
// Graphy is the graphy struct for building and searching the graph
type Graphy struct {
Directional bool
Weighted bool
nodes sync.Map
size int
sizeMutty sync.Mutex
edges sync.Map
}
// DAG determines if the graph that's build is a Directed Acyclic Graph
func (g *Graphy) DAG() (isDAG bool) {
return isDAG
}
// Strong determines if the graph has strong connectivity
func (g *Graphy) Strong() (isStrong bool) {
return isStrong
}
// Bipartite determines if the graph is bipartite
func (g *Graphy) Bipartite() (isBipartite bool) {
return isBipartite
}
// Mutual accepts two nodes of the graph and determines if they're mutually reachable
func (g *Graphy) Mutual(n1 Node, n2 Node) (mutual bool) {
// TODO: validate inputs
return mutual
}
// Node adds a new node to the graph if it
// doesn't already exist and returns the node
// if the node already exists then it returns
// the node object from the map
func (g *Graphy) Node(value interface{}) (node *Node, err error) {
if validator.IsValid(value) {
n := &Node{
Value: value,
}
v, loaded := g.nodes.LoadOrStore(value, n)
// increment size
if !loaded {
g.sizeMutty.Lock()
g.size++
g.sizeMutty.Unlock()
}
var ok bool
if node, ok = v.(*Node); !ok {
err = errors.Errorf("unable to assert node type for value [%v]", v)
}
} else {
err = errors.Errorf("value [%v] is invalid and not added to graph", value)
}
return node, err
}
func (g *Graphy) AddNode(node *Node) (err error) {
if validator.IsValid(node) {
_, loaded := g.nodes.LoadOrStore(node.Value, node)
// increment size
if !loaded {
g.sizeMutty.Lock()
g.size++
g.sizeMutty.Unlock()
}
} else {
// TODO:
}
return err
}
// Size returns the current size of the graph
func (g *Graphy) Size() int {
g.sizeMutty.Lock()
defer g.sizeMutty.Unlock()
s := g.size
return s
}
// Nodes returns a full set of the nodes in the graph with their associated edges
func (g *Graphy) Nodes(ctx context.Context) <-chan *Node {
nodes := make(chan *Node)
go func(nodes chan<- *Node) {
defer close(nodes)
g.nodes.Range(func(key, value interface{}) bool {
if n, ok := value.(*Node); ok {
if n != nil {
// Push the node onto the channel
select {
case <-ctx.Done():
// Break the loop
return false
case nodes <- n:
}
} else {
// TODO:
}
}
// Always loop to completion
return true
})
}(nodes)
return nodes
}
// RemoveNode removes a node from the graph and removes all edges that reference that node
func (g *Graphy) RemoveNode(value interface{}) (err error) {
// TODO: validate inputs
g.sizeMutty.Lock()
defer g.sizeMutty.Unlock()
g.size--
// TODO: Implement removal
return err
}
// AddEdge adds a new edge to the graph between two nodes
func (g *Graphy) AddEdge(parent, child *Node, value interface{}, weight float64) (err error) {
// TODO: validate inputs
// TODO: Track most connected node
var edge Edge
// Build the edge based on the type of graph
if g.Directional {
if g.Weighted {
edge = &dwedgy{
parent: parent,
child: child,
value: value,
weight: weight,
}
} else {
edge = &dedgy{
parent: parent,
child: child,
value: value,
}
}
} else {
if g.Weighted {
edge = &wedgy{
parent: parent,
child: child,
value: value,
weight: weight,
}
} else {
edge = &edgy{
parent: parent,
child: child,
value: value,
}
}
if parent != child {
// TODO: Register the edge in the child node
// TODO: Register the edge in the edge map for the child index
if err = child.AddEdge(parent, edge); err == nil {
// TODO: deal with the sync map here. Need to take into account a possible duplicate edge...
if _, loaded := g.edges.LoadOrStore(child, edge); !loaded {
// TODO:
} else {
// TODO: Error here because the edge already existed
}
}
}
}
//TODO: Register the edge in the parent node
//TODO: Register the edge in the edge map for the parent index
if err = parent.AddEdge(child, edge); err == nil {
// TODO: deal with the sync map here. Need to take into account a possible duplicate edge...
if _, loaded := g.edges.LoadOrStore(parent, edge); !loaded {
// TODO:
} else {
// TODO: Error here because the edge already existed
}
}
return err
}
// UpdateEdge updates the information in an edge for the graph
func (g *Graphy) UpdateEdge(parent *Node, child *Node, value interface{}, weight int) (err error) {
return err
}
// RemoveEdge removes an edge from the graph
func (g *Graphy) RemoveEdge(parent *Node, child *Node) (err error) {
return err
}
func (g *Graphy) String(ctx context.Context) string {
var output = ""
nodes := g.Nodes(ctx)
// Setup function literal to break out of when the loop completes
func() {
for {
select {
case <-ctx.Done():
return
case n, ok := <-nodes:
if ok {
output = fmt.Sprintf("%s%s\n", output, n.String(ctx))
} else {
return
}
}
}
}()
return output
}
func (g *Graphy) Export(ctx context.Context) string {
direction := "undirected"
if g.Directional {
direction = "directed"
}
weighted := "unweighted"
if g.Weighted {
weighted = "weighted"
}
output := fmt.Sprintf("%s %s\n", direction, weighted)
filter := make(map[interface{}]bool)
g.nodes.Range(func(key, value interface{}) bool {
if n, ok := value.(*Node); ok {
func() {
edges := n.Edges(ctx)
for {
select {
case <-ctx.Done():
return
case e, ok := <-edges:
if ok {
if !filter[e.Child().Value] && !filter[e.Parent().Value] {
edge := fmt.Sprintf("%s%v=%v\n", output, e.Parent().Value, e.Child().Value)
if we, ok := e.(WeightedEdge); ok {
edge = fmt.Sprintf("%s%v=%v=%v\n", output, e.Parent().Value, e.Child().Value, we.Weight())
}
output = edge
}
} else {
return
}
}
}
}()
filter[n.Value] = true
}
return true
})
// g.edges.Range(func(key, value interface{}) bool {
// if e, ok := value.(Edge); ok {
// if e != nil {
// key := fmt.Sprintf("%v%v", e.Child().Value, e.Parent().Value)
// key2 := fmt.Sprintf("%v%v", e.Parent().Value, e.Child().Value)
// fmt.Println(key, key2, !filter[key] && !filter[key2])
// if !filter[key] && !filter[key2] {
// edge := fmt.Sprintf("%s%v=%v\n", output, e.Parent().Value, e.Child().Value)
// if we, ok := e.(WeightedEdge); ok {
// edge = fmt.Sprintf("%s%v=%v=%v\n", output, e.Parent().Value, e.Child().Value, we.Weight())
// }
// output = edge
// // Update the filter that this edge has been seen
// filter[key] = true
// filter[key2] = true
// }
// } else {
// // TODO:
// }
// }
// // Always loop to completion
// return true
// })
// nodes := g.Nodes(ctx)
// // Setup function literal to break out of when the loop completes
// func() {
// for {
// select {
// case <-ctx.Done():
// return
// case n, ok := <-nodes:
// if ok {
// output = fmt.Sprintf("%s%s\n", output, n.Export(ctx))
// } else {
// return
// }
// }
// }
// }()
return output
}