-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_dense.go
240 lines (218 loc) · 7.11 KB
/
graph_dense.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
package graph
//DenseGraph is a data structure representing a simple undirected labelled graph.
//DenseGraph stores the number of vertices, the number of edges, the degree sequence of the graph and stores the edges in a []byte array which has an indicator of an edge being present. The edges are in the order 01, 02, 12, 03, 13, 23... so the edge ij with i < j is in the (j*(j-1))/2 + i place.
//Adding or removing edges are quick operations. Adding a vertex may be quick if the backing array doesn't have to grow but may require copying the entire adjacency matrix. Removing a vertex is generally slow.
//*DenseGraph implements the Graph interface.
type DenseGraph struct {
NumberOfVertices int
NumberOfEdges int
DegreeSequence []int
Edges []byte
}
//NewDense returns a pointer to a DenseGraph representation of the graph with n vertices and the edges as given in edges.
//The edges are in the order 01, 02, 12, 03, 13, 23... so the edge ij with i < j is in the (j*(j-1))/2 + i place. The *DenseGraph uses its own copy of edges and modifications to edges won't change the current graph.
//*DenseGraph implements the Graph interface.
func NewDense(n int, edges []byte) *DenseGraph {
if edges == nil {
edges = make([]byte, (n*(n-1))/2)
return &DenseGraph{NumberOfVertices: n, NumberOfEdges: 0, DegreeSequence: make([]int, n), Edges: edges}
}
if len(edges) != (n*(n-1))/2 {
panic("Wrong number of edges")
}
degrees := make([]int, n)
m := 0
copyOfEdges := make([]byte, len(edges))
copy(copyOfEdges, edges)
index := 0
for j := 0; j < n; j++ {
for i := 0; i < j; i++ {
if edges[index] > 0 {
degrees[i]++
degrees[j]++
m++
}
index++
}
}
return &DenseGraph{NumberOfVertices: n, NumberOfEdges: m, DegreeSequence: degrees, Edges: edges}
}
//N returns the number of vertices in the graph.
func (g DenseGraph) N() int {
return g.NumberOfVertices
}
//M returns the number of vertices in the graph.
func (g DenseGraph) M() int {
return g.NumberOfEdges
}
//IsEdge returns true if the undirected edge (i, j) is present in the graph and false otherwise.
func (g DenseGraph) IsEdge(i, j int) bool {
if i >= g.NumberOfVertices || j >= g.NumberOfVertices || i < 0 || j < 0 {
return false
}
if i < j && g.Edges[(j*(j-1))/2+i] > 0 {
return true
} else if i > j && g.Edges[(i*(i-1))/2+j] > 0 {
return true
}
return false
}
//Neighbours returns the neighbours of v i.e. the vertices u such that (u,v) is an edge.
func (g DenseGraph) Neighbours(v int) []int {
degrees := g.DegreeSequence
r := make([]int, 0, degrees[v])
tmp := (v * (v - 1)) / 2
for i := 0; i < v; i++ {
index := tmp + i
if g.Edges[index] > 0 {
r = append(r, i)
}
}
for i := v + 1; i < g.N(); i++ {
index := (i*(i-1))/2 + v
if g.Edges[index] > 0 {
r = append(r, i)
}
}
return r
}
//Degrees returns the slice containing the degrees (number of edges incident with the vertex) of each vertex.
func (g DenseGraph) Degrees() []int {
tmpDegreeSequence := make([]int, len(g.DegreeSequence))
copy(tmpDegreeSequence, g.DegreeSequence)
return tmpDegreeSequence
}
//AddEdge modifies the graph by adding the edge (i, j) if it is not already present.
//If the edge is already present (or i == j), this does nothing.
func (g *DenseGraph) AddEdge(i, j int) {
if i == j || g.IsEdge(i, j) {
return
}
g.DegreeSequence[i]++
g.DegreeSequence[j]++
g.NumberOfEdges++
if i < j {
g.Edges[(j*(j-1))/2+i] = 1
} else if i > j {
g.Edges[(i*(i-1))/2+j] = 1
}
}
//RemoveEdge modifies the graph by removing the edge (i, j) if it is present.
//If the edge is not already present, this does nothing.
func (g *DenseGraph) RemoveEdge(i, j int) {
if !g.IsEdge(i, j) {
return
}
if i < j {
g.Edges[(j*(j-1))/2+i] = 0
} else if i > j {
g.Edges[(i*(i-1))/2+j] = 0
}
g.DegreeSequence[i]--
g.DegreeSequence[j]--
g.NumberOfEdges--
}
//AddVertex modifies the graph by appending one new vertex with edges from the new vertex to the vertices in neighbours.
func (g *DenseGraph) AddVertex(neighbours []int) {
oldSize := (g.NumberOfVertices * (g.NumberOfVertices - 1)) / 2
newSize := oldSize + g.NumberOfVertices
if cap(g.Edges) >= newSize {
g.Edges = g.Edges[:newSize]
for i := oldSize; i < newSize; i++ {
g.Edges[i] = 0
}
} else {
tmp := make([]byte, newSize)
copy(tmp, g.Edges)
g.Edges = tmp
}
for _, v := range neighbours {
g.Edges[oldSize+v] = 1
g.DegreeSequence[v]++
}
g.DegreeSequence = append(g.DegreeSequence, len(neighbours))
g.NumberOfVertices++
g.NumberOfEdges += len(neighbours)
}
//RemoveVertex modifies the graph by removing the speicified vertex. The index of a vertex u > v becomes u - 1 while the index of u < v is unchanged.
func (g *DenseGraph) RemoveVertex(v int) {
if v >= g.NumberOfVertices {
panic("No such vertex")
}
//Update the degree sequences and number of edges.
g.NumberOfEdges -= g.DegreeSequence[v]
tmp := (v * (v - 1)) / 2
for i := 0; i < v; i++ {
index := tmp + i
if g.Edges[index] > 0 {
g.DegreeSequence[i]--
}
}
for i := v + 1; i < g.N(); i++ {
index := (i*(i-1))/2 + v
if g.Edges[index] > 0 {
g.DegreeSequence[i]--
}
}
copy(g.DegreeSequence[v:], g.DegreeSequence[v+1:])
g.DegreeSequence = g.DegreeSequence[:len(g.DegreeSequence)-1]
//Update the backing array.
oldIndex := (v*(v+1))/2 - 1
newIndex := (v * (v - 1)) / 2
for j := v + 1; j < g.NumberOfVertices; j++ {
tmp := (j*(j-1))/2 + v
newIndex += copy(g.Edges[newIndex:], g.Edges[oldIndex+1:tmp])
oldIndex = tmp
}
copy(g.Edges[newIndex:], g.Edges[oldIndex+1:])
g.NumberOfVertices--
g.Edges = g.Edges[:(g.NumberOfVertices*(g.NumberOfVertices-1))/2]
}
//InducedSubgraph returns a deep copy of the induced subgraph of g with vertices given in order by V.
//This can also be used to return relabellings of the graph if len(V) = g.N().
func (g *DenseGraph) InducedSubgraph(V []int) EditableGraph {
n := len(V)
m := 0
degrees := make([]int, n)
edges := make([]byte, (n*(n-1))/2)
index := 0
for j := 1; j < len(V); j++ {
for i := 0; i < j; i++ {
if g.IsEdge(V[i], V[j]) {
edges[index] = 1
m++
degrees[i]++
degrees[j]++
}
index++
}
}
return &DenseGraph{NumberOfVertices: n, NumberOfEdges: m, DegreeSequence: degrees, Edges: edges}
}
//Copy returns a deep copy of the graph g.
func (g *DenseGraph) Copy() EditableGraph {
newEdges := make([]byte, len(g.Edges))
copy(newEdges, g.Edges)
newDegrees := make([]int, len(g.DegreeSequence))
copy(newDegrees, g.DegreeSequence)
return &DenseGraph{NumberOfVertices: g.NumberOfVertices, NumberOfEdges: g.NumberOfEdges, DegreeSequence: newDegrees, Edges: newEdges}
}
//Helper functions for implementing the required functions
//String returns a human readable representation of the graph.
// func (g DenseGraph) String() string {
// var buffer bytes.Buffer
// buffer.WriteString(fmt.Sprintf("Degree: %v \n", g.NumberOfVertices))
// for i := 0; i < g.NumberOfVertices; i++ {
// for j := 0; j < g.NumberOfVertices; j++ {
// if j < i {
// buffer.WriteString(" ")
// } else if j == i {
// buffer.WriteString("0 ")
// } else {
// buffer.WriteString(fmt.Sprintf("%v ", g.Edges[(j*(j-1))/2+i]))
// }
// }
// buffer.WriteString("\n")
// }
// return buffer.String()
// }