patgraph

Source   Edit  

Types

Direction = enum
  Outgoing, Incoming
Source   Edit  
Graph[N; E] = object
  
A graph datastructure using an adjacency list representation Source   Edit  

Consts

invalid = 2147483647'i32
An invalid index used to denote absence of an edge, for example to end an adjacency list. Source   Edit  

Procs

proc `[]`[N, E](self: Graph[N, E]; a: Natural): N
Access the weight for node a. Source   Edit  
proc `[]`[N, E](self: var Graph[N, E]; a: Natural): var N
Access the weight for node a, mutably. Source   Edit  
proc `[]=`[N, E](self: var Graph[N, E]; a: Natural; v: N)
Set the weight for node a. Source   Edit  
proc addEdge[N, E](self: var Graph[N, E]; a, b: Natural; weight: E)
Add an edge from a to b to the graph, with its associated data weight. Source   Edit  
proc addNode[N, E](self: var Graph[N, E]; weight: N): int
Add a node (also called vertex) with associated data weight to the graph. Return the index of the new node. Source   Edit  
proc extendWithEdges[N, E](self: var Graph[N, E];
                           iterable: openArray[(int, int)])
Source   Edit  
proc extendWithEdges[N, E](self: var Graph[N, E];
                           iterable: openArray[(int, int, E)])

Extend the graph from an iterable of edges.

Node weights N are set to default values. Edge weights E may either be specified in the list, or they are filled with default values.

Nodes are inserted automatically to match the edges.

Source   Edit  
proc graphFromEdges[N, E](iterable: openArray[(int, int)]): Graph[N, E]
Source   Edit  
proc graphFromEdges[N, E](iterable: openArray[(int, int, E)]): Graph[N, E]

Create a new Graph from an iterable of edges.

Node weights N are set to default values. Edge weights E may either be specified in the list, or they are filled with default values.

Nodes are inserted automatically to match the edges.

import graph

let graph2 = graphFromEdges[int, int](@[
   (0, 1), (0, 2), (0, 3),
   (1, 2), (1, 3),
   (2, 3)])
Source   Edit  
proc len[N, E](self: Graph[N, E]): int
Return the number of nodes (vertices) in the graph. Source   Edit  
proc updateEdge[N, E](self: var Graph[N, E]; a, b: Natural; weight: E)

Add or update an edge from a to b. If the edge already exists, its weight is updated.

Return the index of the affected edge.

Computes in O(e') time, where e' is the number of edges connected to a.

Source   Edit  

Iterators

iterator edges[N, E](self: Graph[N, E]; a: Natural; dir = Outgoing): (int, E)

Return all neighbors that have an edge between them and a, in the specified direction.

Neighbors are listed in reverse order of their addition to the graph, so the most recently added edge's neighbor is listed first.

Source   Edit  
iterator neighbors[N, E](self: Graph[N, E]; a: Natural; dir = Outgoing): int

Return all neighbors that have an edge between them and a, in the specified direction.

Neighbors are listed in reverse order of their addition to the graph, so the most recently added edge's neighbor is listed first.

Source   Edit