package graph

Types

NodeProperties

type NodeProperties struct
	Title string `json:"title,omitempty"`
	Link string `json:"link,omitempty"`
}

NodeProperties contains optional metadata associated with a node.

Fields:

  • Title: An optional title or name for the node.

  • Link: An optional URL or reference link to retrive the resource represented by the node.

Example:

props := graph.NodeProperties{
	Title: "John Doe",
	Link: "https://example.com/johndoe",
}

Node

type Node struct {
	Id         UUID           `json:"id"`
	Labels     []string       `json:"labels,omitempty"`
	Properties NodeProperties `json:"properties"`
}

Node represents a vertex in the graph. Each node has a unique identifier, an optional list of labels, and associated properties.

Fields:

  • Id: A unique identifier for the node.

  • Labels: An optional list of string labels or tags associated with the node.

  • Properties: Metadata and additional information about the node.

Example:

g := graph.NewGraph()
node := g.NewNode()
node.Labels = append(node.Labels, "Person", "Developer")
node.Properties.Title = "Alice"

EdgeProperties

type EdgeProperties struct {
	Label string `json:"label,omitempty"`
}

EdgeProperties contains optional metadata associated with an edge.

Fields:

  • Label: An optional label or description for the edge.

Example:

props := graph.EdgeProperties{
	Label: "collaborates with",
}

Edge

type Edge struct {
	Id         UUID           `json:"id"`
	Type       string         `json:"type,omitempty"`
	From       UUID           `json:"from"`
	To         UUID           `json:"to"`
	Properties EdgeProperties `json:"properties"`
}

Edge represents a directed connection between two nodes in the graph. Each edge has a unique identifier, connects a source node to a destination node, an optional type, and associated properties.

Fields:

  • Id: A unique identifier for the edge.

  • Type: An optional type or category for the edge.

  • From: The unique identifier of the source node.

  • To: The unique identifier of the destination node.

  • Properties: Metadata and additional information about the edge.

Example:

g := graph.NewGraph()
a := g.NewNode()
b := g.NewNode()
edge, err := g.NewEdge(a, b)
if err == nil {
	edge.Type = "knows"
	edge.Properties.Label = "since 2020"
}

Graph

type Graph struct {
	adjacencyList map[UUID][]UUID
	nodes         map[UUID]*Node
	edges         map[UUID]map[UUID]*Edge
}

Graph represents a directed acyclic graph (DAG) structure composed of nodes and edges. It provides methods for creating nodes, adding edges between nodes, and querying paths.

The Graph type maintains internal data structures to efficiently store and retrieve nodes and edges. Use NewGraph to create a new graph instance.

Example:

// Create a new graph
g := graph.NewGraph()

// Add nodes
a := g.NewNode()
b := g.NewNode()
c := g.NewNode()

// Add edges
e1, _ := g.NewEdge(a, b)
e2, _ := g.NewEdge(b, c)

// Find a path
path, _ := g.GetPath(a, c) // returns [e1, e2]

// Marshal to JSON
data, _ := g.MarshalJSON()
fmt.Println(string(data))

Functions

graph.NewGraph

func NewGraph() *Graph

Creates a new empty graph.

Returns:

  • *Graph: A pointer to a new empty graph instance.

Example:

g := graph.NewGraph()

graph.NewNode

func (g *Graph) NewNode() *Node

Creates a new node in the graph.

Returns:

  • A pointer to the newly created node.

Example:

g := NewGraph()
node := g.NewNode()

graph.GetNodes

func (g *Graph) GetNodes() []*Node

Returns a slice of all nodes in the graph.

Returns:

  • A slice of pointers to all nodes in the graph.

Example:

g := NewGraph()
a := g.NewNode()
b := g.NewNode()
nodes := g.GetNodes() // returns []*Node{a, b}

graph.ContainsNode

func (g *Graph) ContainsNode(v *Node) bool

Checks if a given node is present in the graph.

Parameters:

  • v: A pointer to the node to check.

Returns:

  • true if the node is present in the graph, false otherwise.

Example:

g := NewGraph()
a := g.NewNode()
b := &Node{Id: NewUUID()}
existsA := g.ContainsNode(a) // returns true
existsB := g.ContainsNode(b) // returns false

graph.NewEdge

func (g *Graph) NewEdge(start, end *Node) (*Edge, error)

Creates a new edge between the source node and the destination node in the graph.

Parameters:

  • start: The source node from which the edge originates.

  • end: The destination node to which the edge points.

Returns:

  • A pointer to the newly created edge if successful.

  • An error if the source or destination node is nil, if they are the same node (loop), or if either node is not present in the graph.

Example:

g := NewGraph()
a := g.NewNode()
b := g.NewNode()
e, err := g.NewEdge(a, b)

	if err != nil {
		// Handle error
	}

graph.ContainsEdge

func (g *Graph) ContainsEdge(e *Edge) bool

Checks if a given edge is present in the graph.

Parameters:

  • e: A pointer to the edge to check.

Returns:

  • true if the edge is present in the graph, false otherwise.

Example:

g := NewGraph()
a := g.NewNode()
b := g.NewNode()
e, _ := g.NewEdge(a, b)

	if g.ContainsEdge(e) {
		fmt.Println("Edge exists in graph")
	}

graph.GetPath

func (g *Graph) GetPath(start, end *Node) ([]*Edge, error)

GetPath returns a slice of edges that represents a path from the source node to the destination node.

Parameters:

  • start: The source node from which the path starts.

  • end: The destination node to which the path leads.

Returns:

  • A slice of edges that represents the path from the source node to the destination node.

  • An error if no path exists or if either the source or destination node is not in the graph.

Example:

g := NewGraph()
a := g.NewNode()
b := g.NewNode()
c := g.NewNode()
g.NewEdge(a, b)
g.NewEdge(b, c)
path, err := g.GetPath(a, c)

	if err != nil {
		// Handle error
	}

graph.MarshalJSON

func (g *Graph) MarshalJSON() ([]byte, error)

Encodes the graph into a JSON representation.

Returns:

  • A byte slice containing the JSON representation of the graph.

  • An error if the graph cannot be marshaled.

Example:

g := NewGraph()
a := g.NewNode()
b := g.NewNode()
g.NewEdge(a, b)
data, err := g.MarshalJSON()

	if err != nil {
		// Handle error
	}

fmt.Println(string(data)) // {"nodes":[{"id":"..."}],"edges":[{"id":"...","from":"...","to":"..."}]}

graph.UnmarshalJSON

func (g *Graph) UnmarshalJSON(data []byte) error

Decodes a JSON representation of a graph into a Graph type.

Parameters:

  • data: A byte slice containing the JSON representation of a graph.

Returns:

  • An error if the graph cannot be unmarshaled.

Example:

g := NewGraph()
data := []byte(`{"nodes":[{"id":"..."},{"id":"..."}],"edges":[{"id":"...","from":"...","to":"..."}]}`)

	if err := g.UnmarshalJSON(data); err != nil {
		// Handle error
	}