Documentation ¶
Overview ¶
Package graph implements solvers based on Graph theory
Index ¶
- func BuildIndicatorMatrix(nv int, pth []int) (x [][]int)
- func CheckIndicatorMatrix(source, target int, x [][]int, verbose bool) (errPath, errLoop int)
- func CheckIndicatorMatrixRowMaj(source, target, nv int, xmat []int) (errPath, errLoop int)
- func MetisPartition(npart, nvert int, xadj, adjncy []int32, recursive bool) (objval int32, parts []int32)
- func PrintIndicatorMatrix(x [][]int) (l string)
- type Graph
- func (o *Graph) CalcDist()
- func (o *Graph) GetAdjacency() (xadj, adjncy []int32)
- func (o *Graph) GetEdge(i, j int) (k int)
- func (o *Graph) HashEdgeKey(i, j int) (edge int)
- func (o *Graph) Init(edges [][]int, weightsE []float64, verts [][]float64, weightsV []float64)
- func (o *Graph) Nverts() int
- func (o *Graph) Path(s, t int) (p []int)
- func (o *Graph) ShortestPaths(method string)
- func (o *Graph) StrDistMatrix() (l string)
- type MaskType
- type Munkres
- type Plotter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildIndicatorMatrix ¶
BuildIndicatorMatrix builds indicator matrix
func CheckIndicatorMatrix ¶
CheckIndicatorMatrix checks indicator matirx
func CheckIndicatorMatrixRowMaj ¶
CheckIndicatorMatrixRowMaj checks indicator matrix in row-major format
func MetisPartition ¶
func MetisPartition(npart, nvert int, xadj, adjncy []int32, recursive bool) (objval int32, parts []int32)
MetisPartition performs graph partitioning using METIS
func PrintIndicatorMatrix ¶
PrintIndicatorMatrix prints indicator matrix
Types ¶
type Graph ¶
type Graph struct { // input Edges [][]int // [nedges][2] edges (connectivity) WeightsE []float64 // [nedges] weights of edges. can be <nil> Verts [][]float64 // [nverts][ndim] vertices. can be <nil> WeightsV []float64 // [nverts] weights of vertices. can be <nil> // auxiliary Key2edge map[int]int // maps (i,j) vertex to edge index Dist [][]float64 // [nverts][nverts] distances Next [][]int // [nverts][nverts] next tree connection. -1 means no connection }
Graph defines a graph structure
func ReadGraphTable ¶
ReadGraphTable reads data and allocate graph
func (*Graph) CalcDist ¶
func (o *Graph) CalcDist()
CalcDist computes distances beetween all vertices and initialises 'Next' matrix
func (*Graph) GetAdjacency ¶
GetAdjacency returns adjacency list as a compressed storage format for METIS
func (*Graph) GetEdge ¶
GetEdge performs a lookup on Key2edge map and returs id of edge for given nodes ides
func (*Graph) HashEdgeKey ¶
HashEdgeKey creates a unique hash key identifying an edge
func (*Graph) Init ¶
Init initialises graph
Input: edges -- [nedges][2] edges (connectivity) weightsE -- [nedges] weights of edges. can be <nil> verts -- [nverts][ndim] vertices. can be <nil> weightsV -- [nverts] weights of vertices. can be <nil>
func (*Graph) Path ¶
Path returns the path from source (s) to destination (t)
Note: ShortestPaths method must be called first
func (*Graph) ShortestPaths ¶
ShortestPaths computes the shortest paths in a graph defined as follows
[10] 0 ––––––→ 3 numbers in brackets | ↑ indicate weights [5] | | [1] ↓ | 1 ––––––→ 2 [3] ∞ means that there are no connections from i to j graph: j= 0 1 2 3 ----------- i= 0 5 ∞ 10 | 0 ⇒ w(0→1)=5, w(0→3)=10 ∞ 0 3 ∞ | 1 ⇒ w(1→2)=3 ∞ ∞ 0 1 | 2 ⇒ w(2→3)=1 ∞ ∞ ∞ 0 | 3 Input: method -- FW: Floyd-Warshall method
func (*Graph) StrDistMatrix ¶
StrDistMatrix returns a string representation of Dist matrix
type Munkres ¶
type Munkres struct { // main C [][]float64 // [nrow][ncol] cost matrix Cori [][]float64 // [nrow][ncol] original cost matrix Links []int // [nrow] will contain links/assignments after Run(), where j := o.Links[i] means that i is assigned to j. -1 means no assignment/link Cost float64 // total cost after Run() and links are established // auxiliary M [][]MaskType // [nrow][ncol] mask matrix. If Mij==1, then Cij is a starred zero. If Mij==2, then Cij is a primed zero // contains filtered or unexported fields }
Munkres (Hungarian algorithm) method to solve the assignment problem
based on code by Bob Pilgrim from http://csclab.murraystate.edu/bob.pilgrim/445/munkres.html Note: this method runs in O(n²), in the worst case; therefore is not efficient for large matrix Example: $ | Clean Sweep Wash -------|-------------------- Fry | [2] 3 3 Leela | 3 [2] 3 Bender | 3 3 [2] minimum cost = 6 Note: cost will be minimised
func (*Munkres) Run ¶
func (o *Munkres) Run()
Run runs the iterative algorithm
Output: o.Links -- will contain assignments, where len(assignments) == nrow and j := o.Links[i] means that i is assigned to j -1 means no assignment/link o.Cost -- will have the total cost by following links
func (*Munkres) SetCostMatrix ¶
SetCostMatrix sets cost matrix by copying from C to internal o.C
Note: costs must be positive
func (*Munkres) StrCostMatrix ¶
StrCostMatrix returns a representation of cost matrix with masks and covers
type Plotter ¶
type Plotter struct { G *Graph // the graph Parts []int32 // [nverts] partitions VertsLabels map[int]string // [nverts] labels of vertices. may be nil EdgesLabels map[int]string // [nedges] labels of edges. may be nil FcParts []string // [nverts] face colors of circles indicating partitions. may be nil Radius float64 // radius of circles. can be 0 Width float64 // distance between two edges. can be 0 Gap float64 // gap between text and edges; e.g. width/2. can be 0 ArgsVertsTxt *plt.A // arguments for vertex ids. may be nil ArgsEdgesTxt *plt.A // arguments for edge ids. may be nil ArgsVerts *plt.A // arguments for vertices. may be nil ArgsEdges *plt.A // arguments for edges. may be nil }
Plotter draws graphs