Documentation
¶
Overview ¶
Example ¶
g, err := LoadWGraph(testDir + "mst.yml") if err != nil { panic(err) } mst := g.Prim() //mst := g.LazyPrim() //mst := g.Kruskal() fmt.Println(mst.String()) // possible output: // 0 : 2 7 // 1 : 7 // 2 : 0 3 6 // 3 : 2 // 4 : 5 // 5 : 7 4 // 6 : 2 // 7 : 0 1 5
Index ¶
- Constants
- Variables
- type BFS
- type Cycle
- type Digraph
- func (dg *Digraph) AddEdge(from, to int)
- func (dg *Digraph) Adjacent(v int) (adj []int)
- func (dg Digraph) BFS(src int) *BFS
- func (dg *Digraph) Bipartite() (colors []bool)
- func (dg *Digraph) DelEdge(src, dst int)
- func (dg *Digraph) FindCycle() *Cycle
- func (dg *Digraph) FindCycleFrom(v int) *Path
- func (dg *Digraph) FindNegativeEdge() (src, dst int)
- func (dg *Digraph) FindNegativeEdgeFrom(start int) (src int, dst int)
- func (dg *Digraph) GetWeight(from, to int) float64
- func (dg *Digraph) HasEdge(from, to int) bool
- func (dg *Digraph) HasVert(v int) bool
- func (dg *Digraph) IsSameWith(other Digraph) bool
- func (dg *Digraph) IterAdjacent(v int, fn func(int) bool)
- func (dg *Digraph) IterBDFSFrom(src int, fn func(int) bool)
- func (dg *Digraph) IterEdge(fn func(int, int) bool)
- func (dg *Digraph) IterEdgeFrom(src int, fn func(int, int) bool)
- func (dg *Digraph) IterVertDFS(src int, fn func(int) bool)
- func (dg *Digraph) IterVetBDFS(fn func(int) bool)
- func (dg *Digraph) IterWAdjacent(v int, fn func(int, float64) bool)
- func (dg *Digraph) IterWEdge(fn func(int, int, float64) bool)
- func (dg *Digraph) IterWEdgeFrom(src int, fn func(int, int, float64) bool)
- func (dg *Digraph) Marshal() ([]byte, error)
- func (dg *Digraph) NumEdge() uint
- func (dg *Digraph) NumVert() uint
- func (dg Digraph) Reachable() Reachable
- func (dg *Digraph) ReachableBits(src int) []bool
- func (dg *Digraph) ReachableSlice(src int) []int
- func (dg *Digraph) Reverse() *Digraph
- func (dg Digraph) SCC() *SCC
- func (dg *Digraph) String() string
- func (dg *Digraph) Topological() (order *basic.Stack[int])
- func (dg *Digraph) TotalWeight() float64
- type Graph
- func (g *Graph) AddEdge(a, b int) error
- func (g *Graph) DelEdge(a, b int)
- func (g *Graph) HasCycle() bool
- func (g *Graph) IterEdge(fn func(int, int) bool)
- func (g *Graph) IterEdgeFrom(src int, fn func(int, int) bool)
- func (g *Graph) IterWEdge(fn func(int, int, float64) bool)
- func (g *Graph) IterWEdgeFrom(src int, fn func(int, int, float64) bool)
- func (g *Graph) NumEdge() uint
- func (g *Graph) SubGraphs() *SubGraphs
- func (g *Graph) TotalWeight() float64
- type Path
- type PathTree
- type Reachable
- type SCC
- type Searcher
- type SubGraphs
- type Symbol
- type WDigraph
- func (g *WDigraph) AddEdge(src, dst int, w float64)
- func (g *WDigraph) Searcher() (*Searcher, error)
- func (g *WDigraph) SearcherBellmanFord() (*Searcher, error)
- func (g *WDigraph) SearcherDijkstra() (*Searcher, error)
- func (g *WDigraph) SearcherTopological() (*Searcher, error)
- func (g *WDigraph) ShortestPathTree(src int, alg int) (*PathTree, error)
- type WGraph
Examples ¶
Constants ¶
const ( Auto = iota Dijkstra Topological BellmanFord )
Variables ¶
Functions ¶
This section is empty.
Types ¶
type BFS ¶
type BFS struct {
// contains filtered or unexported fields
}
Example ¶
dg, err := LoadDigraph(testDir + "no_cycle.yml") if err != nil { panic(err) } bfs := dg.BFS(1) fmt.Println(bfs.CanReach(5)) fmt.Println(bfs.ShortestPathTo(2).Str(nil)) // false // [TotalDistance=3] 7->2(1.00) 3->7(1.00) 1->3(1.00)
func (*BFS) ShortestPathTo ¶
ShortestPathTo get the shortest path to dst (ignore weight)
type Digraph ¶
type Digraph struct { *Symbol // contains filtered or unexported fields }
func LoadDigraph ¶
func NewDigraph ¶
func (*Digraph) Bipartite ¶
Bipartite put two colors on all nodes while any connected nodes have different color
Example ¶
dg, err := LoadDigraph(testDir + "no_cycle.yml") if err != nil { panic(err) } fmt.Println(dg.Bipartite())
Output: []
func (*Digraph) FindCycle ¶
FindCycle find any directed cycle in dg
Example ¶
// (0)-------->(2) // | ^ ^ // | \ | // | ------ | // | \ | // v \| // (1)-------->(3) g := NewDigraph(4) g.AddEdge(0, 1) g.AddEdge(0, 2) g.AddEdge(1, 3) g.AddEdge(3, 0) g.AddEdge(3, 2) c := g.FindCycle() fmt.Println(c.Error()) // [TotalDistance=3] 0->1(1.00) 1->3(1.00) 3->0(1.00)
func (*Digraph) FindCycleFrom ¶
FindCycleFrom find any directed cycle from vertical v in dg But not include cycle that can not be accessed from v
func (*Digraph) FindNegativeEdge ¶
FindNegativeEdge find a negative edge. If here is no negative edge, (-1, -1) will be returned
func (*Digraph) FindNegativeEdgeFrom ¶
FindNegativeEdgeFrom find a reachable negative edge from the specified start vertical If here is no negative edge, (-1, -1) will be returned
func (*Digraph) GetWeight ¶
GetWeight get the weight of edge Zero will be returned if edge not exist
func (*Digraph) IsSameWith ¶
IsSameWith check whether dg is the same with other
func (*Digraph) IterAdjacent ¶
IterAdjacent iterate all adjacent vertices of v
func (*Digraph) IterBDFSFrom ¶
IterBDFSFrom iterate all reachable vertices from vertical src in RDFS order
func (*Digraph) IterEdgeFrom ¶
IterEdgeFrom iterate all reachable edges from vertical src
func (*Digraph) IterVertDFS ¶
IterVertDFS iterate all reachable vertices from vertical src in DFS order
func (*Digraph) IterVetBDFS ¶
IterVetBDFS iterate all vertices in Back-DFS order
func (*Digraph) IterWAdjacent ¶
IterWAdjacent iterate all adjacent vertices and weight of v
func (*Digraph) IterWEdgeFrom ¶
IterWEdgeFrom iterate all reachable edges and their weight from vertical src
func (*Digraph) ReachableBits ¶
ReachableBits get a bit-map contains all reachable vertices from src
func (*Digraph) ReachableSlice ¶
ReachableSlice get a slice contains all reachable vertices from src
func (*Digraph) Topological ¶
Topological return a stack that will pop vertices in topological order
Example ¶
dg, err := LoadDigraph(testDir + "no_cycle.yml") if err != nil { panic(err) } for _, vet := range dg.Topological().ToSlice() { fmt.Printf("%d->", vet) }
Output: 5->1->3->6->4->7->0->2->
func (*Digraph) TotalWeight ¶
TotalWeight sum the weight of all edges
type Graph ¶
type Graph struct {
*Digraph
}
Graph has no direction
func (*Graph) IterEdgeFrom ¶
IterEdgeFrom iterate all reachable edges from vertical src
func (*Graph) IterWEdgeFrom ¶
IterWEdgeFrom iterate all reachable edges and their weight from vertical src
func (*Graph) TotalWeight ¶
TotalWeight sum the weight of all edges
type PathTree ¶
type PathTree struct {
// contains filtered or unexported fields
}
PathTree is shortest path tree
func (*PathTree) DistanceTo ¶
DistanceTo get the distance from src to dst
type Reachable ¶
type Reachable [][]bool
Example ¶
dg, err := LoadDigraph(testDir + "no_cycle.yml") if err != nil { panic(err) } reach := dg.Reachable() fmt.Println(reach.CanReach(5, 2)) fmt.Println(reach.CanReach(2, 5))
Output: true false
type SCC ¶
type SCC struct {
// contains filtered or unexported fields
}
SCC is strong connected components of digraph vertices in the same component can access each other
Example ¶
g := NewDigraph(13) g.AddEdge(0, 1) g.AddEdge(0, 5) g.AddEdge(5, 4) g.AddEdge(4, 3) g.AddEdge(4, 2) g.AddEdge(3, 2) g.AddEdge(2, 3) g.AddEdge(2, 0) g.AddEdge(6, 0) g.AddEdge(6, 4) g.AddEdge(6, 9) g.AddEdge(9, 10) g.AddEdge(10, 12) g.AddEdge(12, 9) g.AddEdge(9, 11) g.AddEdge(11, 12) g.AddEdge(11, 4) g.AddEdge(7, 6) g.AddEdge(7, 8) g.AddEdge(8, 7) g.AddEdge(8, 9) scc := g.SCC() fmt.Println("amount of strongly connected component:", scc.NumComponents()) var vertices []int scc.IterComponent(scc.Comp(0), func(v int) bool { vertices = append(vertices, v) return true }) sort.Shell(vertices) fmt.Println("vertices strongly connected with 0:", vertices) fmt.Println(scc.IsStronglyConn(0, 6)) // amount of strongly connected component: 5 // vertices strongly connected with 0: [0 2 3 4 5] // false
func (*SCC) IsStronglyConn ¶
IsStronglyConn check whether src is strongly connected with dst
func (*SCC) NumComponents ¶
NumComponents get the number of components
type Searcher ¶
type Searcher struct {
// contains filtered or unexported fields
}
func (*Searcher) GetDistance ¶
type SubGraphs ¶
type SubGraphs struct {
// contains filtered or unexported fields
}
func (*SubGraphs) NumSubGraph ¶
NumSubGraph get the number of sub-graphs
type Symbol ¶
type Symbol struct {
// contains filtered or unexported fields
}
Example ¶
g, _ := LoadGraph("./testdata/symbol_graph.yml") bfs := g.BFS(g.VetOf("姜文")) fmt.Println(bfs.ShortestPathTo(g.VetOf("梁朝伟")).Str(g.Symbol)) fmt.Println(bfs.ShortestPathTo(g.VetOf("宋慧乔")).Str(g.Symbol)) fmt.Println(bfs.ShortestPathTo(g.VetOf("郎雄")).Str(g.Symbol)) fmt.Println(bfs.ShortestPathTo(g.VetOf("周星驰")).Str(g.Symbol)) fmt.Println(bfs.ShortestPathTo(g.VetOf("梁家辉")).Str(g.Symbol)) //(distance=12): 姜文->《让子弹飞》, 《让子弹飞》->刘嘉玲, 刘嘉玲->《阿飞正传》, 《阿飞正传》->张学友, 张学友->《东邪西毒》, 《东邪西毒》->梁朝伟, //(distance=12): 姜文->《让子弹飞》, 《让子弹飞》->周润发, 周润发->《卧虎藏龙》, 《卧虎藏龙》->章子怡, 章子怡->《一代宗师》, 《一代宗师》->宋慧乔, //(distance=8): 姜文->《让子弹飞》, 《让子弹飞》->周润发, 周润发->《卧虎藏龙》, 《卧虎藏龙》->郎雄, //(distance=12): 姜文->《让子弹飞》, 《让子弹飞》->刘嘉玲, 刘嘉玲->《阿飞正传》, 《阿飞正传》->张曼玉, 张曼玉->《家有喜事》, 《家有喜事》->周星驰, //(distance=8): 姜文->《让子弹飞》, 《让子弹飞》->周润发, 周润发->《赌神2》, 《赌神2》->梁家辉,
func NewSymbolGraph ¶
func NewSymbolGraph() *Symbol
type WDigraph ¶
type WDigraph struct {
*Digraph
}
WDigraph is edge weighted digraph
Example ¶
g, _ := LoadWDigraph(testDir + "no_cycle.yml") searcher, _ := g.SearcherDijkstra() // searcher, _ := g.SearcherTopological() // searcher, _ := g.SearcherBellmanFord() fmt.Println(searcher.GetPath(1, 2).Str(nil)) // [TotalDistance=1.02] 1->3(0.29) 3->7(0.39) 7->2(0.34)
func LoadWDigraph ¶
func NewWDigraph ¶
func (*WDigraph) SearcherBellmanFord ¶
func (*WDigraph) SearcherDijkstra ¶
func (*WDigraph) SearcherTopological ¶
type WGraph ¶
type WGraph struct {
*Graph
}
func LoadWGraph ¶
func (*WGraph) Kruskal ¶
Kruskal gets the minimum spanning tree by Kruskal algorithm. g MUST be a connected graph
func (*WGraph) LazyPrim ¶
LazyPrim gets the minimum spanning tree by Lazy-Prim algorithm. g MUST be a connected graph
func (*WGraph) Prim ¶
Prim gets the minimum spanning tree by Prim algorithm. g MUST be a connected graph