graph

package
v0.0.0-...-517cbaf Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2018 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Graph

type Graph map[string]map[string]bool

Graph represent directed acyclic graph of strings

func NewGraph

func NewGraph() Graph

NewGraph makes a new Graph

func (Graph) AddEdge

func (g Graph) AddEdge(from, to string)

AddEdge adds a from --> to, if the nodes do not exist, it creates them

Example
package main

import (
	"fmt"

	"github.com/cohadar/gopkgorder/graph"
)

func main() {
	g := graph.NewGraph()
	g.AddEdge("Alfa", "Bravo")
	fmt.Println(g.HasEdge("Alfa", "Bravo"), g.HasEdge("Bravo", "Alfa"))
}
Output:

true false

func (Graph) AddNode

func (g Graph) AddNode(node string)

AddNode adds a new unconnected node into graph if it does not exist

Example
package main

import (
	"fmt"

	"github.com/cohadar/gopkgorder/graph"
)

func main() {
	g := graph.NewGraph()
	g.AddNode("Alfa")
	g.AddNode("Bravo")
	g.AddNode("Charlie")
	fmt.Println(g.GetNodes())
}
Output:

[Alfa Bravo Charlie]

func (Graph) GetNodes

func (g Graph) GetNodes() []string

GetNodes returns a sorted lice with node names

func (Graph) GetRoots

func (g Graph) GetRoots() []string

GetLeafs returns nodes with no dependencies (sorted)

Example
package main

import (
	"fmt"

	"github.com/cohadar/gopkgorder/graph"
)

func main() {
	g := graph.NewGraph()
	g.AddEdge("Charlie", "Alfa")
	g.AddEdge("Delta", "Alfa")
	g.AddEdge("Echo", "Charlie")
	g.AddEdge("Echo", "Bravo")
	fmt.Println(g.GetRoots())
}
Output:

[Alfa Bravo]

func (Graph) GetTargets

func (g Graph) GetTargets(from string) []string

GetTargets returns all nodes pointed by node from

Example
package main

import (
	"fmt"

	"github.com/cohadar/gopkgorder/graph"
)

func main() {
	g := graph.NewGraph()
	g.AddEdge("Alfa", "Bravo")
	g.AddEdge("Alfa", "Charlie")
	g.AddEdge("Alfa", "Delta")
	g.AddEdge("Echo", "Alfa")
	fmt.Println(g.GetTargets("Alfa"))
}
Output:

[Bravo Charlie Delta]

func (Graph) HasEdge

func (g Graph) HasEdge(from, to string) bool

HasEdge return true if edge from --> to exists

func (Graph) HasNode

func (g Graph) HasNode(node string) bool

HasNode return true if node exists in graph

Example
package main

import (
	"fmt"

	"github.com/cohadar/gopkgorder/graph"
)

func main() {
	g := graph.NewGraph()
	g.AddNode("Alfa")
	g.AddNode("Bravo")
	g.AddNode("Charlie")
	fmt.Println(g.HasNode("Bravo"), g.HasNode("Delta"))
}
Output:

true false

func (Graph) RemoveNode

func (g Graph) RemoveNode(node string)

RemoveNode removes node from graph

Example
package main

import (
	"fmt"

	"github.com/cohadar/gopkgorder/graph"
)

func main() {
	g := graph.NewGraph()
	g.AddEdge("Bravo", "Alfa")
	g.AddEdge("Charlie", "Bravo")
	g.RemoveNode("Bravo")
	fmt.Println(g.GetLeafs())
	fmt.Println(g.HasEdge("Charlie", "Bravo"))
}
Output:

[Alfa Charlie]
false

func (Graph) Size

func (g Graph) Size() int

Size returns number of nodes

Example
package main

import (
	"fmt"

	"github.com/cohadar/gopkgorder/graph"
)

func main() {
	g := graph.NewGraph()
	g.AddNode("Alfa")
	g.AddNode("Bravo")
	g.AddNode("Charlie")
	fmt.Println(g.Size())
}
Output:

3

Jump to

Keyboard shortcuts

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