util

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 16, 2020 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package util provides useful utilities for working with graphs.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clipped added in v0.4.0

func Clipped(err error) int

Clipped returns the clipping if err is a ClipError. Otherwise it returns 0.

func CpWeights

func CpWeights(dst groph.WGraph, src groph.RGraph) (dstout groph.WGraph, err error)

CpWeights copies the edge weights from one graph to another. Vertices are identified by their index, i.e. the user has to take care of the vertex order. If the number of vertices in the graph differs the smaller graph determines how many edge weights are copied.

func CpXWeights

func CpXWeights(
	dst groph.WGraph,
	src groph.RGraph,
	xf func(in interface{}) interface{},
) (dstout groph.WGraph, err error)

CpXWeights “transfers” the edge weights from src Graph to dst Graph with the same vertex restrictions as CpWeights. CpXWeights applies the transformation function xf() to each edge weight.

Panic of xf will be recovered and returned as error.

func FirstEdge added in v0.5.0

func FirstEdge(ofIdx int, akku, w interface{}) (sum interface{}, stop bool)

func MergeEqual

func MergeEqual(w1, w2 interface{}) (interface{}, error)

func MergeWeights

func MergeWeights(
	g groph.RGraph,
	merge func(w1, w2 interface{}) (merged interface{}, err error),
) func(u, v groph.VIdx) (interface{}, error)

func MustCp

func MustCp(g groph.WGraph, err error) groph.WGraph

MustCp panics when err is not nil. Otherwiese it returns g.

func ParseI32 added in v0.4.0

func ParseI32(s string) (interface{}, error)

func ReadGraph added in v0.4.0

func ReadGraph(into groph.WGraph, rd io.Reader, wParse WeightParse) error

func ReorderPath added in v0.5.0

func ReorderPath(slice interface{}, path []groph.VIdx)

TODO can this be done in place?

Example
data := []string{"a", "b", "c", "d", "e"}
path := []groph.VIdx{1, 3, 0, 4, 2}
ReorderPath(data, path)
fmt.Println(data)
Output:

[b d a e c]

func WeightOr added in v0.4.0

func WeightOr(g groph.RGraph, u, v groph.VIdx, or interface{}) interface{}

WeightOr returns parameter 'or' when the edge (u,v) is not in graph g. Otherwise the weight of edge (u,v) is returned.

func WriteSparse added in v0.4.0

func WriteSparse(wr io.Writer, g groph.RGraph) error
Example
g := sliceofmaps.NewDInt32(4, nil)
g.SetEdge(0, 1, 1)
g.SetEdge(1, 2, 2)
g.SetEdge(2, 3, 3)
g.SetEdge(3, 0, 0)
g.SetEdge(0, 2, -1)
WriteSparse(os.Stdout, g)
Output:

groph directed edges order=4
0 1 1
0 2 -1
1 2 2
2 3 3
3 0 0

Types

type ClipError added in v0.4.0

type ClipError int

ClipError is returned when the order of src and dst in Cp*Weights does not match. If err is < 0 there were -err vertices ignored from src. If err > 0 then err vertices in dst were not covered.

func (ClipError) Error added in v0.4.0

func (err ClipError) Error() string

type MergeError

type MergeError struct {
	// contains filtered or unexported fields
}

func (MergeError) Error

func (e MergeError) Error() string

func (MergeError) Unwrap

func (e MergeError) Unwrap() error

type PointsNDist added in v0.4.0

type PointsNDist struct {
	// contains filtered or unexported fields
}

PointsNDist implements a metric RUndirected graph based on a slice of vertices of some type V and a symmetric function f(u V, v V) → W that computes the weight of type W for any edge (u, v).

From this use e.g. CpWeights or CpXWeights to initialize an other WGraph.

func NewPointsNDist added in v0.4.0

func NewPointsNDist(vertexSlice interface{}, measure interface{}) *PointsNDist

func (*PointsNDist) Check added in v0.4.0

func (g *PointsNDist) Check() (*PointsNDist, error)

Check does type checking on g. It always returns g. Only if everything is OK the returned error is nil.

func (*PointsNDist) Must added in v0.4.0

func (g *PointsNDist) Must() *PointsNDist

Verify call Check on g and panics if Check returns an error.

func (*PointsNDist) Order added in v0.4.0

func (g *PointsNDist) Order() groph.VIdx

func (*PointsNDist) Vertex added in v0.4.0

func (g *PointsNDist) Vertex(idx groph.VIdx) interface{}

func (*PointsNDist) Weight added in v0.4.0

func (g *PointsNDist) Weight(u, v groph.VIdx) interface{}

func (*PointsNDist) WeightU added in v0.4.0

func (g *PointsNDist) WeightU(u, v groph.VIdx) interface{}

type RSubgraph added in v0.4.0

type RSubgraph struct {
	G    groph.RGraph
	VMap []groph.VIdx
}

func (RSubgraph) Order added in v0.4.0

func (g RSubgraph) Order() groph.VIdx

func (RSubgraph) Weight added in v0.4.0

func (g RSubgraph) Weight(u, v groph.VIdx) interface{}

type RUadapter

type RUadapter struct {
	G     groph.RGraph
	Merge func(u, v groph.VIdx) (merged interface{}, err error)
	Err   error
}
Example
package main

import (
	"fmt"

	"git.fractalqb.de/fractalqb/groph/adjmatrix"

	"git.fractalqb.de/fractalqb/groph"
)

var _ groph.RUndirected = (*RUadapter)(nil)

func main() {
	ws := NewWeightsSlice([]int32{1, 2, 3, 4})
	u := adjmatrix.NewUInt32(ws.Order(), adjmatrix.I32Del, nil)
	_, err := CpWeights(u, ws)
	fmt.Println("copy error:", err)
	rua := RUadapter{G: ws, Merge: MergeWeights(ws, MergeEqual)}
	_, err = CpWeights(u, &rua)
	fmt.Println("copy error:", err)
	fmt.Println("rua error:", rua.Err)
}
Output:

copy error: cannot copy from directed to undirected graph
copy error: source: edges 1, 0: not equal: '3' / '2'
rua error: edges 1, 0: not equal: '3' / '2'

func (*RUadapter) ErrState added in v0.4.0

func (rua *RUadapter) ErrState() error

func (*RUadapter) Order added in v0.4.0

func (rua *RUadapter) Order() groph.VIdx

func (*RUadapter) Weight

func (rua *RUadapter) Weight(u, v groph.VIdx) interface{}

func (*RUadapter) WeightU

func (rua *RUadapter) WeightU(u, v groph.VIdx) (res interface{})

type RUnion added in v0.5.0

type RUnion struct {
	Of        []groph.RGraph
	AddWeight func(ofIdx int, akku, w interface{}) (sum interface{}, stop bool)
}

func (RUnion) Order added in v0.5.0

func (g RUnion) Order() (res groph.VIdx)

func (RUnion) Weight added in v0.5.0

func (g RUnion) Weight(u, v groph.VIdx) (w interface{})

type SrcDstError added in v0.4.0

type SrcDstError struct {
	Src error
	Dst error
}

SrcDstError is returned when an error occurred in the src or dst graph during Cp*Weights.

func (SrcDstError) Error added in v0.4.0

func (err SrcDstError) Error() string

type WSubgraph added in v0.4.0

type WSubgraph struct {
	G    groph.WGraph
	VMap []groph.VIdx
}

func (WSubgraph) Order added in v0.4.0

func (g WSubgraph) Order() groph.VIdx

func (WSubgraph) Reset added in v0.4.0

func (g WSubgraph) Reset(order groph.VIdx)

func (WSubgraph) SetWeight added in v0.4.0

func (g WSubgraph) SetWeight(u, v groph.VIdx, w interface{})

func (WSubgraph) Weight added in v0.4.0

func (g WSubgraph) Weight(u, v groph.VIdx) interface{}

type WeightParse added in v0.4.0

type WeightParse = func(string) (interface{}, error)

type WeightsSlice added in v0.4.0

type WeightsSlice struct {
	// contains filtered or unexported fields
}

func NewWeightsSlice added in v0.4.0

func NewWeightsSlice(edgeSlice interface{}) *WeightsSlice

func (*WeightsSlice) Check added in v0.4.0

func (g *WeightsSlice) Check() (*WeightsSlice, error)

func (*WeightsSlice) Must added in v0.4.0

func (g *WeightsSlice) Must() *WeightsSlice

func (*WeightsSlice) Order added in v0.4.0

func (g *WeightsSlice) Order() groph.VIdx

func (*WeightsSlice) Weight added in v0.4.0

func (g *WeightsSlice) Weight(u, v groph.VIdx) interface{}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL