Documentation ¶
Index ¶
- type Graph
- func (g Graph) AddEdge(from, to string)
- func (g Graph) AddNode(node string)
- func (g Graph) GetNodes() []string
- func (g Graph) GetRoots() []string
- func (g Graph) GetTargets(from string) []string
- func (g Graph) HasEdge(from, to string) bool
- func (g Graph) HasNode(node string) bool
- func (g Graph) RemoveNode(node string)
- func (g Graph) Size() int
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Graph ¶
Graph represent directed acyclic graph of strings
func (Graph) AddEdge ¶
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 ¶
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) GetRoots ¶
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 ¶
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) HasNode ¶
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 ¶
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
Click to show internal directories.
Click to hide internal directories.