graph

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2020 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckCycles

func CheckCycles(g *Graph) error

CheckCycles checks graph g to not to be cyclic.

func Tarjan

func Tarjan(g *Graph) [][]ID

Tarjan finds the strongly connected components. In the mathematics, a directed graph is "strongly connected" if every vertex is reachable from every other node. Therefore, a graph is strongly connected if there is a path in each direction between each pair of node of a graph. Then a pair of vertices u and v is strongly connected to each other because there is a path in each direction. "Strongly connected components" of an arbitrary graph partition into sub-graphs that are themselves strongly connected. That is, "strongly connected component" of a directed graph is a sub-graph that is strongly connected. Formally, "Strongly connected components" of a graph is a maximal set of vertices C in G.V such that for all u, v ∈ C, there is a path both from u to v, and from v to u. (https://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm)

  1. Tarjan(G): 1.
  2. globalIndex = 0 // smallest unused index
  3. let S be a stack
  4. result = [][] 5.
  5. for each vertex v in G:
  6. if v.index is undefined:
  7. tarjan(G, v, globalIndex, S, result) 9.
  8. return result 11. 12.
  9. tarjan(G, v, globalIndex, S, result): 14.
  10. v.index = globalIndex
  11. v.lowLink = globalIndex
  12. globalIndex++
  13. S.push(v) 19.
  14. for each child vertex w of v: 21.
  15. if w.index is undefined:
  16. recursively tarjan(G, w, globalIndex, S, result)
  17. v.lowLink = min(v.lowLink, w.lowLink) 25.
  18. else if w is in S:
  19. v.lowLink = min(v.lowLink, w.index) 28.
  20. // if v is the root
  21. if v.lowLink == v.index: 31.
  22. // start a new strongly connected component
  23. component = [] 34.
  24. while True: 36.
  25. u = S.pop()
  26. component.push(u) 39.
  27. if u == v:
  28. result.push(component)
  29. break

Types

type ErrCycleDetected

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

ErrCycleDetected causes when graph is cyclic.

func (ErrCycleDetected) Error

func (e ErrCycleDetected) Error() string

Error is a error implementation.

type Graph

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

Graph describes the methods of graph operations. It assumes that the identifier of a Node is unique. And weight values is float64.

func New

func New() *Graph

New returns a new graph.

func (*Graph) AddEdge

func (g *Graph) AddEdge(id1, id2 ID, weight float64) error

AddEdge adds an edge from nd1 to nd2 with the weight. It returns error if a node does not exist.

func (*Graph) AddNode

func (g *Graph) AddNode(nd Node) bool

AddNode adds a node to a graph, and returns false if the node already existed in the graph.

func (*Graph) DeleteEdge

func (g *Graph) DeleteEdge(id1, id2 ID) error

DeleteEdge deletes an edge from id1 to id2.

func (*Graph) DeleteNode

func (g *Graph) DeleteNode(id ID) bool

DeleteNode deletes a node from a graph. It returns true if it got deleted. And false if it didn't get deleted.

func (*Graph) Exists

func (g *Graph) Exists(id ID) bool

Exists checks existence of the node by id.

func (*Graph) Node

func (g *Graph) Node(id ID) (Node, error)

Node finds the Node.

func (*Graph) NodeCount

func (g *Graph) NodeCount() int

NodeCount returns the total number of nodes.

func (*Graph) Nodes

func (g *Graph) Nodes() map[ID]Node

Nodes returns a map from node ID to empty struct value. Graph does not allow duplicate node ID or name.

func (*Graph) Replace

func (g *Graph) Replace(node Node) bool

Replace replaces node.

func (*Graph) ReplaceEdge

func (g *Graph) ReplaceEdge(id1, id2 ID, weight float64) error

ReplaceEdge replaces an edge from id1 to id2 with the weight.

func (*Graph) Sources

func (g *Graph) Sources(id ID) (map[ID]Node, error)

Sources returns the map of parent Nodes. (Nodes that come towards the argument vertex.)

func (*Graph) String

func (g *Graph) String() string

String describes the Graph.

func (*Graph) Targets

func (g *Graph) Targets(id ID) (map[ID]Node, error)

Targets returns the map of child Nodes. (Nodes that go out of the argument vertex.)

func (*Graph) Weight

func (g *Graph) Weight(id1, id2 ID) (float64, error)

Weight returns the weight from id1 to id2.

type ID

type ID interface {
	// String returns the string ID.
	String() string
}

ID is unique identifier.

func BFS

func BFS(g *Graph, id ID) []ID

BFS does breadth-first search, and returns the list of vertices. (https://en.wikipedia.org/wiki/Breadth-first_search)

  1. BFS(G, v): 1.
  2. let Q be a queue
  3. Q.push(v)
  4. label v as visited 5.
  5. while Q is not empty: 7.
  6. u = Q.dequeue() 9.
  7. for each vertex w adjacent to u: 11.
  8. if w is not visited yet:
  9. Q.push(w)
  10. label w as visited

func DFS

func DFS(g *Graph, id ID) []ID

DFS does depth-first search, and returns the list of vertices. (https://en.wikipedia.org/wiki/Depth-first_search)

  1. DFS(G, v): 1.
  2. let S be a stack
  3. S.push(v) 4.
  4. while S is not empty: 6.
  5. u = S.pop() 8.
  6. if u is not visited yet: 10.
  7. label u as visited 12.
  8. for each vertex w adjacent to u: 14.
  9. if w is not visited yet:
  10. S.push(w)

func DFSRecursion

func DFSRecursion(g *Graph, id ID) []ID

DFSRecursion does depth-first search recursively.

  1. DFS(G, v): 1.
  2. if v is visited:
  3. return 4.
  4. label v as visited 6.
  5. for each vertex u adjacent to v: 8.
  6. if u is not visited yet:
  7. recursive DFS(G, u)

func TopologicalSort

func TopologicalSort(g *Graph) ([]ID, bool)

TopologicalSort does topological sort(ordering) with DFS. It returns true if the graph is a DAG (no cycle, with a topological sort). False if the graph is not a DAG (cycle, with no topological sort).

type Node

type Node interface {
	// ID returns the ID.
	ID() ID
	// String returns node string representation.
	String() string
}

Node is vertex. The ID must be unique within the graph.

type StringID

type StringID string

StringID is a node string identity representation.

func (StringID) String

func (s StringID) String() string

String returns string.ф

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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