/
simple.go
136 lines (111 loc) · 3.13 KB
/
simple.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
/*
Copyright 2017 Julian Griggs
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Package graph provides graph implementations. Currently, this includes an
undirected simple graph.
*/
package graph
import (
"errors"
"sync"
)
var (
// ErrVertexNotFound is returned when an operation is requested on a
// non-existent vertex.
ErrVertexNotFound = errors.New("vertex not found")
// ErrSelfLoop is returned when an operation tries to create a disallowed
// self loop.
ErrSelfLoop = errors.New("self loops not permitted")
// ErrParallelEdge is returned when an operation tries to create a
// disallowed parallel edge.
ErrParallelEdge = errors.New("parallel edges are not permitted")
)
// SimpleGraph is a mutable, non-persistent undirected graph.
// Parallel edges and self-loops are not permitted.
// Additional description: https://en.wikipedia.org/wiki/Graph_(discrete_mathematics)#Simple_graph
type SimpleGraph struct {
mutex sync.RWMutex
adjacencyList map[interface{}]map[interface{}]struct{}
v, e int
}
// V returns the number of vertices in the SimpleGraph
func (g *SimpleGraph) V() int {
g.mutex.RLock()
defer g.mutex.RUnlock()
return g.v
}
// E returns the number of edges in the SimpleGraph
func (g *SimpleGraph) E() int {
g.mutex.RLock()
defer g.mutex.RUnlock()
return g.e
}
// AddEdge will create an edge between vertices v and w
func (g *SimpleGraph) AddEdge(v, w interface{}) error {
g.mutex.Lock()
defer g.mutex.Unlock()
if v == w {
return ErrSelfLoop
}
g.addVertex(v)
g.addVertex(w)
if _, ok := g.adjacencyList[v][w]; ok {
return ErrParallelEdge
}
g.adjacencyList[v][w] = struct{}{}
g.adjacencyList[w][v] = struct{}{}
g.e++
return nil
}
// Adj returns the list of all vertices connected to v
func (g *SimpleGraph) Adj(v interface{}) ([]interface{}, error) {
g.mutex.RLock()
defer g.mutex.RUnlock()
deg, err := g.Degree(v)
if err != nil {
return nil, ErrVertexNotFound
}
adj := make([]interface{}, deg)
i := 0
for key := range g.adjacencyList[v] {
adj[i] = key
i++
}
return adj, nil
}
// Degree returns the number of vertices connected to v
func (g *SimpleGraph) Degree(v interface{}) (int, error) {
g.mutex.RLock()
defer g.mutex.RUnlock()
val, ok := g.adjacencyList[v]
if !ok {
return 0, ErrVertexNotFound
}
return len(val), nil
}
func (g *SimpleGraph) addVertex(v interface{}) {
mm, ok := g.adjacencyList[v]
if !ok {
mm = make(map[interface{}]struct{})
g.adjacencyList[v] = mm
g.v++
}
}
// NewSimpleGraph creates and returns a SimpleGraph
func NewSimpleGraph() *SimpleGraph {
return &SimpleGraph{
adjacencyList: make(map[interface{}]map[interface{}]struct{}),
v: 0,
e: 0,
}
}