/
graph.go
302 lines (253 loc) · 7.82 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
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
// This package provides a basic graph structure, along with
// some of the popular algorithms in graph theory.
package grago
import (
"regexp"
"strconv"
"strings"
"fmt"
)
type stringer string
func (s stringer) String() string {
return string(s)
}
type Link struct {
Start fmt.Stringer
End fmt.Stringer
Weighed bool
Weight int
}
func NewLink(start fmt.Stringer, end fmt.Stringer, weighed bool, weight int) *Link {
var l *Link = new(Link)
l.Start = start
l.End = end
l.Weighed = weighed
l.Weight = weight
return l
}
// A string representation of Link used for output.
func (l Link) String() string {
if l.Weighed {
return l.Start.String() + "-(" + strconv.Itoa(l.Weight) + ")->" + l.End.String()
} else {
return l.Start.String() + "--" + l.End.String()
}
}
type Node struct {
// A container for all the nodes, that this node has an
// outgoing connection to.
Adjacent map[fmt.Stringer]int
}
func NewNode() *Node {
var n *Node = new(Node)
n.Adjacent = make(map[fmt.Stringer]int)
return n
}
// Returns a slice of the nodes this node has an edge to.
func (n *Node) AdjacentNodes() []fmt.Stringer {
result := []fmt.Stringer{}
for node := range n.Adjacent {
result = append(result, node)
}
return result
}
type Graph struct {
// Whether the graph is oriented or not takes effect on the
// visual representation as well as edge addition to it.
Oriented bool
// If the graph is weighed, the weights will be shown on the
// exported visualization, otherwise an edge between two nodes
// will be represented by a weight of 1
Weighed bool
// Whether or not the graph has negative weights takes effect
// on which algorithms to use in some cases like shorted path
// finding.
HasNegativeWeights bool
// A container for all the nodes in the graph.
nodes map[fmt.Stringer]*Node
}
func NewGraph(oriented bool, weighed bool, hasNegativeWeights bool) *Graph {
var g *Graph = new(Graph)
g.Oriented = oriented
g.Weighed = weighed
g.HasNegativeWeights = hasNegativeWeights
g.nodes = make(map[fmt.Stringer]*Node)
return g
}
// Creates a graph, read from a string with an expected
// input format such as:
// <oriented> <weighed> <hasNegativeWeights> - booleans
// <node> - for adding a node
// <node> -- <node> [<weight>] - for adding a link
// If 'reversed' is true, the graph will be constructed
// with reversed edges.
func ReadGraph(in string, reversed bool) *Graph {
var g *Graph = new(Graph)
g.nodes = make(map[fmt.Stringer]*Node)
lines := strings.Split(in, "\n")
attributes := strings.Split(lines[0], " ")
g.Oriented, _ = strconv.ParseBool(attributes[0])
g.Weighed, _ = strconv.ParseBool(attributes[1])
g.HasNegativeWeights, _ = strconv.ParseBool(attributes[2])
linkRegexString := `(.+) -- (.+)`
if g.Weighed {
linkRegexString = `(.+) -- (.+) (\d+)`
}
linkRegex := regexp.MustCompile(linkRegexString)
for _, line := range lines[1:] {
linkMatch := linkRegex.FindStringSubmatch(line)
if len(linkMatch) != 0 { //this line describes a link
if g.Weighed {
weight, _ := strconv.Atoi(linkMatch[3])
if reversed {
g.AddLink(stringer(linkMatch[2]), stringer(linkMatch[1]), weight)
} else {
g.AddLink(stringer(linkMatch[1]), stringer(linkMatch[2]), weight)
}
} else {
if reversed {
g.AddLink(stringer(linkMatch[2]), stringer(linkMatch[1]), 1)
} else {
g.AddLink(stringer(linkMatch[1]), stringer(linkMatch[2]), 1)
}
}
} else { //this line describes a node
g.AddNode(stringer(line))
}
}
return g
}
// A string representation of Graph, with an output format
// like the input format:
// <oriented> <weighed> <hasNegativeWeights> - booleans
// <node> - all nodes in the graph each on a separate line
// <node> -- <node> [<weight>] - for all the links
func (g *Graph) String() string {
result := ""
result += strconv.FormatBool(g.Oriented)
result += " " + strconv.FormatBool(g.Weighed)
result += " " + strconv.FormatBool(g.HasNegativeWeights) + "\n"
for node := range g.nodes {
result += node.String() + "\n"
}
for node, adjacents := range g.nodes {
adjacentList := adjacents.AdjacentNodes()
for _, adjNode := range adjacentList {
if g.Weighed {
result += node.String() + " -- " + adjNode.String() + " " + strconv.Itoa(adjacents.Adjacent[adjNode]) + "\n"
} else {
result += node.String() + " -- " + adjNode.String() + "\n"
}
}
}
return result
}
// Tries to add a node to the graph and returns true
// if successful, otherwise returns false if the
// node already exists.
func (g *Graph) AddNode(node fmt.Stringer) bool {
if _, exists := g.nodes[node]; exists { //check if the node is already in the graph
return false
} else {
g.nodes[node] = NewNode()
return true
}
}
// Tries to add a link between two nodes and returns true
// if successful, otherwise returns false if such a link
// already exists.
//
// Note: If the graph isn't oriented adding a link from A to B
// effectively adds a link from B to A.
func (g *Graph) AddLink(startNode fmt.Stringer, endNode fmt.Stringer, weight int) bool {
g.AddNode(startNode)
g.AddNode(endNode)
if _, exists := g.nodes[startNode].Adjacent[endNode]; exists { //check if the link is already in the graph
return false
} else {
g.nodes[startNode].Adjacent[endNode] = weight
if !g.Oriented {
g.nodes[endNode].Adjacent[startNode] = weight
}
return true
}
}
// Tries to remove the node from the graph and if
// successful removes all links between it and
// other nodes and returns true, otherwise if the
// node doesn't exist, returns false.
func (g *Graph) RemoveNode(node fmt.Stringer) bool {
if _, exists := g.nodes[node]; exists { //check if the node is already in the graph
delete(g.nodes, node)
for _, otherNode := range g.nodes {
if _, existsNode := otherNode.Adjacent[node]; existsNode {
//if there is another node pointing to the one we removed, delete the link
delete(otherNode.Adjacent, node)
}
}
return true
} else {
return false
}
}
// Tries to remove the link from the graph and if
// successful returns true, otherwise if the link
// doesn't exist, returns false.
//
// Note: If the graph isn't oriented removing the link from A to B
// effectively removes the link from B to A.
func (g *Graph) RemoveLink(startNode fmt.Stringer, endNode fmt.Stringer) bool {
if _, existsNode := g.nodes[startNode]; existsNode { //check if the start node exists
if _, existsLink := g.nodes[startNode].Adjacent[endNode]; existsLink { //check if the link exists
delete(g.nodes[startNode].Adjacent, endNode)
if !g.Oriented {
delete(g.nodes[endNode].Adjacent, startNode)
}
return true
} else {
return false
}
}
return false
}
// Returns the count of the links which have as a starting
// node the one specified as a parameter.
//
// Note: If the graph isn't oriented the outgoing links
// will always match the incoming links.
func (g *Graph) OutgoingLinksCount(node fmt.Stringer) int {
return len(g.nodes[node].Adjacent)
}
// Returns the count of the links which have as an ending
// node the one specified as a parameter.
//
// Note: If the graph isn't oriented the outgoing links
// will always match the incoming links.
func (g *Graph) IncomingLinksCount(node fmt.Stringer) int {
result := 0
for _, otherNode := range g.nodes {
if _, exists := otherNode.Adjacent[node]; exists { //check if there is an incoming link from another node
result++
}
}
return result
}
// Returns a slice with all the nodes in the graph.
func (g *Graph) Nodes() []fmt.Stringer {
result := []fmt.Stringer{}
for node := range g.nodes {
result = append(result, node)
}
return result
}
// Returns a slice with the all the links
// in the graph.
func (g *Graph) Links() []Link {
result := []Link{}
for _, startNode := range g.Nodes() {
for endNode, weight := range g.nodes[startNode].Adjacent {
result = append(result, *NewLink(startNode, endNode, g.Weighed, weight))
}
}
return result
}