项目作者: bserdar

项目描述 :
Go directed labeled/unlabeled graph data structure with constant-time accessors
高级语言: Go
项目地址: git://github.com/bserdar/digraph.git
创建时间: 2021-05-17T01:07:19Z
项目社区:https://github.com/bserdar/digraph

开源协议:Apache License 2.0

下载


Digraph

GoDoc
Go Report

This package provides the digraph package that implements a directed
graph data structure. Nodes and edges may be labeled. The graph
supports application-defined structs as nodes and edges.

The graph structure is designed so that nodes know the outgoing edges,
and edges know both the source and target nodes. The graph structure
itself knows only “some” of the nodes, so retrieving all the nodes of
the graph or accessing nodes by label requires an intermediate
structure, the NodeIndex. A NodeIndex discovers all nodes when
requested, and then provides indexes access to the nodes. A
NodeIndex only sees the nodes that were accessible from the Graph
when it is created, thus it does not provide a dynamic view of the
graph.

Digraph is not thread-safe.

Example

Construct a graph, and add nodes:

  1. g:=digraph.New()
  2. node1:=digraph.NewBasicNode("node1",nil)
  3. node2:=digraph.NewBasicNode("node2",nil)

Connect the nodes with edges:

  1. // edge node1 --edge1--> node2
  2. edge:=g.NewBasicEdge("edge1",nil)
  3. digraph.Connect(node1,node2,edge)

Get the nodes accessible via a label:

  1. n:=node1.Next("edge1")

If there are multiple, iterate:

  1. edges:=node1.GetAllOutgoingEdgesWithLabel("edge1")
  2. for edges.HasNext() {
  3. edge:=edges.Next()
  4. }

The BasicNode and BasicEdge contains an application-defined
Payload field. This allows for container/list style graph
container. It is also possible to use application-specific node and
edge objects.

  1. type CustomNode struct {
  2. digraph.NodeHeader
  3. // other fields
  4. }
  5. type CustomEdge struct {
  6. digraph.EdgeHeader
  7. // other fields
  8. }

The *CustomNode and *CustomEdge instances can be added to the
graph. The embeded NodeHeader and EdgeHeader connects the object
to the underlying graph.