forked from openshift/origin
/
dense.go
151 lines (124 loc) · 3.87 KB
/
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
// Copyright ©2014 The gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package concrete
import (
"github.com/gonum/graph"
)
// A dense graph is a graph such that all IDs are in a contiguous block from 0 to
// TheNumberOfNodes-1. It uses an adjacency matrix and should be relatively fast for both access
// and writing.
//
// This graph implements the CrunchGraph, but since it's naturally dense this is superfluous.
type DenseGraph struct {
adjacencyMatrix []float64
numNodes int
}
// Creates a dense graph with the proper number of nodes. If passable is true all nodes will have
// an edge with unit cost, otherwise every node will start unconnected (cost of +Inf).
func NewDenseGraph(numNodes int, passable bool) *DenseGraph {
g := &DenseGraph{adjacencyMatrix: make([]float64, numNodes*numNodes), numNodes: numNodes}
if passable {
for i := range g.adjacencyMatrix {
g.adjacencyMatrix[i] = 1
}
} else {
for i := range g.adjacencyMatrix {
g.adjacencyMatrix[i] = inf
}
}
return g
}
func (g *DenseGraph) NodeExists(n graph.Node) bool {
return n.ID() < g.numNodes
}
func (g *DenseGraph) Degree(n graph.Node) int {
deg := 0
for i := 0; i < g.numNodes; i++ {
if g.adjacencyMatrix[i*g.numNodes+n.ID()] != inf {
deg++
}
if g.adjacencyMatrix[n.ID()*g.numNodes+i] != inf {
deg++
}
}
return deg
}
func (g *DenseGraph) NodeList() []graph.Node {
nodes := make([]graph.Node, g.numNodes)
for i := 0; i < g.numNodes; i++ {
nodes[i] = Node(i)
}
return nodes
}
func (g *DenseGraph) DirectedEdgeList() []graph.Edge {
edges := make([]graph.Edge, 0, len(g.adjacencyMatrix))
for i := 0; i < g.numNodes; i++ {
for j := 0; j < g.numNodes; j++ {
if g.adjacencyMatrix[i*g.numNodes+j] != inf {
edges = append(edges, Edge{Node(i), Node(j)})
}
}
}
return edges
}
func (g *DenseGraph) Neighbors(n graph.Node) []graph.Node {
neighbors := make([]graph.Node, 0)
for i := 0; i < g.numNodes; i++ {
if g.adjacencyMatrix[i*g.numNodes+n.ID()] != inf ||
g.adjacencyMatrix[n.ID()*g.numNodes+i] != inf {
neighbors = append(neighbors, Node(i))
}
}
return neighbors
}
func (g *DenseGraph) EdgeBetween(n, neighbor graph.Node) graph.Edge {
if g.adjacencyMatrix[neighbor.ID()*g.numNodes+n.ID()] != inf ||
g.adjacencyMatrix[n.ID()*g.numNodes+neighbor.ID()] != inf {
return Edge{n, neighbor}
}
return nil
}
func (g *DenseGraph) Successors(n graph.Node) []graph.Node {
neighbors := make([]graph.Node, 0)
for i := 0; i < g.numNodes; i++ {
if g.adjacencyMatrix[n.ID()*g.numNodes+i] != inf {
neighbors = append(neighbors, Node(i))
}
}
return neighbors
}
func (g *DenseGraph) EdgeTo(n, succ graph.Node) graph.Edge {
if g.adjacencyMatrix[n.ID()*g.numNodes+succ.ID()] != inf {
return Edge{n, succ}
}
return nil
}
func (g *DenseGraph) Predecessors(n graph.Node) []graph.Node {
neighbors := make([]graph.Node, 0)
for i := 0; i < g.numNodes; i++ {
if g.adjacencyMatrix[i*g.numNodes+n.ID()] != inf {
neighbors = append(neighbors, Node(i))
}
}
return neighbors
}
// DenseGraph is naturally dense, we don't need to do anything
func (g *DenseGraph) Crunch() {
}
func (g *DenseGraph) Cost(e graph.Edge) float64 {
return g.adjacencyMatrix[e.Head().ID()*g.numNodes+e.Tail().ID()]
}
// Sets the cost of an edge. If the cost is +Inf, it will remove the edge,
// if directed is true, it will only remove the edge one way. If it's false it will change the cost
// of the edge from succ to node as well.
func (g *DenseGraph) SetEdgeCost(e graph.Edge, cost float64, directed bool) {
g.adjacencyMatrix[e.Head().ID()*g.numNodes+e.Tail().ID()] = cost
if !directed {
g.adjacencyMatrix[e.Tail().ID()*g.numNodes+e.Head().ID()] = cost
}
}
// Equivalent to SetEdgeCost(edge, math.Inf(1), directed)
func (g *DenseGraph) RemoveEdge(e graph.Edge, directed bool) {
g.SetEdgeCost(e, inf, directed)
}