You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+41-46Lines changed: 41 additions & 46 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,18 +1,18 @@
1
-
# graph-data-structure
1
+
# graph-data-structure
2
2
3
-
A [graph data structure](https://en.wikipedia.org/wiki/Graph_(abstract_data_type)) with [topological sort](https://en.wikipedia.org/wiki/Topological_sorting).
3
+
A [graph data structure](<https://en.wikipedia.org/wiki/Graph_(abstract_data_type)>) with [topological sort](https://en.wikipedia.org/wiki/Topological_sorting).
4
4
5
5
This library provides a minimalist implementation of a directed graph data structure. Nodes are represented by unique strings. Internally, an [adjacency list](https://en.wikipedia.org/wiki/Adjacency_list) is used to represent nodes and edges.
6
6
7
7
The primary use case for this library is in implementing [dataflow programming](https://en.wikipedia.org/wiki/Dataflow_programming) or [reactive programming](https://en.wikipedia.org/wiki/Reactive_programming). The key algorithm necessary for these is topological sorting, to get an ordering of nodes such that for each edge (**u** -> **v**), **u** comes before **v** in the sorted order. The topological sorting algorithm exposed here has modifications useful for computing the order in which functions in a data flow graph should be executed, namely specifying source nodes for propagation and specifying to exclude the source nodes themselves from the result.
8
8
9
9
**Table of Contents**
10
10
11
-
*[Installing](#installing)
12
-
*[Examples](#examples)
13
-
*[ABC](#abc)
14
-
*[Getting Dressed](#getting-dressed)
15
-
*[API Reference](#api-reference)
11
+
-[Installing](#installing)
12
+
-[Examples](#examples)
13
+
-[ABC](#abc)
14
+
-[Getting Dressed](#getting-dressed)
15
+
-[API Reference](#api-reference)
16
16
17
17
## Installing
18
18
@@ -85,56 +85,56 @@ For more detailed example code that shows more methods, have a look at the [test
85
85
86
86
## API Reference
87
87
88
-
*[Creating a Graph](#creating-a-graph)
89
-
*[Adding and Removing Nodes](#adding-and-removing-nodes)
90
-
*[Adding and Removing Edges](#adding-and-removing-edges)
91
-
*[Querying the Graph](#querying-the-graph)
92
-
*[Serialization](#serialization)
93
-
*[Graph Algorithms](#graph-algorithms)
88
+
-[Creating a Graph](#creating-a-graph)
89
+
-[Adding and Removing Nodes](#adding-and-removing-nodes)
90
+
-[Adding and Removing Edges](#adding-and-removing-edges)
Constructs an instance of the graph data structure.
100
100
101
-
The optional argument *serialized* is a serialized graph that may have been generated by **[serialize](#serialize)**. If *serialized* is present, it is deserialized by invoking **[deserialize](#deserialize)**.
101
+
The optional argument _serialized_ is a serialized graph that may have been generated by **[serialize](#serialize)**. If _serialized_ is present, it is deserialized by invoking **[deserialize](#deserialize)**.
Adds a node to the graph. Returns *graph* to support method chaining. The argument *node* is a string identifier that uniquely identifies the node within this graph instance. If a node with the same identifier was already added to the graph, this function does nothing.
107
+
Adds a node to the graph. Returns _graph_ to support method chaining. The argument _node_ is a string identifier that uniquely identifies the node within this graph instance. If a node with the same identifier was already added to the graph, this function does nothing.
Removes the specified node. Returns *graph* to support method chaining. The argument *node* is a string identifier for the node to remove. This function also removes all edges connected to the specified node, both incoming and outgoing.
111
+
Removes the specified node. Returns _graph_ to support method chaining. The argument _node_ is a string identifier for the node to remove. This function also removes all edges connected to the specified node, both incoming and outgoing.
Adds an edge from node *u* to node *v*. Returns *graph* to support method chaining. The arguments *u* and *v* are string identifiers for nodes. This function also adds *u* and *v* as nodes if they were not already added.
117
+
Adds an edge from node _u_ to node _v_. Returns _graph_ to support method chaining. The arguments _u_ and _v_ are string identifiers for nodes. This function also adds _u_ and _v_ as nodes if they were not already added.
118
118
119
-
The last argument *weight* (optional) specifies the weight of this edge.
119
+
The last argument _weight_ (optional) specifies the weight of this edge.
Removes the edge from node *u* to node *v*. Returns *graph* to support method chaining. The arguments *u* and *v* are string identifiers for nodes. This function does not remove the nodes *u* and *v*. Does nothing if the edge does not exist.
123
+
Removes the edge from node _u_ to node _v_. Returns _graph_ to support method chaining. The arguments _u_ and _v_ are string identifiers for nodes. This function does not remove the nodes _u_ and _v_. Does nothing if the edge does not exist.
Gets the adjacent node list for the specified node. The argument *node* is a string identifier for a node. Returns an array of node identifier strings.
147
+
Gets the adjacent node list for the specified node. The argument _node_ is a string identifier for a node. Returns an array of node identifier strings.
148
148
149
-
The "adjacent node list" is the Array of nodes for which there is an incoming edge from the given node. In other words, for all edges (**u** -> **v**) where **u** is the specified node, all values for **v** are in the adjacent node list.
149
+
The "adjacent node list" is the Array of nodes for which there is an incoming edge from the given node. In other words, for all edges (**u** -> **v**) where **u** is the specified node, all values for **v** are in the adjacent node list.
Deserializes the given serialized graph. Returns *graph* to support method chaining. The argument *serialized* is a graph representation with the structure described in **[serialize](#serialize)**. This function iterates over the serialized graph and adds the nodes and links it represents by invoking **[addNode](#add-node)** and **[addEdge](#add-edge)**. The output from **[serialize](#serialize)** can be used as the input to **deserialize**.
196
+
Deserializes the given serialized graph. Returns _graph_ to support method chaining. The argument _serialized_ is a graph representation with the structure described in **[serialize](#serialize)**. This function iterates over the serialized graph and adds the nodes and links it represents by invoking **[addNode](#add-node)** and **[addEdge](#add-edge)**. The output from **[serialize](#serialize)** can be used as the input to **deserialize**.
Performs [Depth-first Search](https://en.wikipedia.org/wiki/Depth-first_search). Returns an array of node identifier strings. The returned array includes nodes visited by the algorithm in the order in which they were visited. Implementation inspired by pseudocode from Cormen et al. "Introduction to Algorithms" 3rd Ed. p. 604.
207
203
208
204
Arguments:
209
205
210
-
**sourceNodes* (optional) - An array of node identifier strings. This specifies the subset of nodes to use as the sources of the depth-first search. If *sourceNodes* is not specified, all **[nodes](#nodes)** in the graph are used as source nodes.
211
-
**includeSourceNodes* (optional) - A boolean specifying whether or not to include the source nodes in the returned array. If *includeSourceNodes* is not specified, it is treated as `true` (all source nodes are included in the returned array).
212
-
**errorOnCycle* (optional) - A boolean indicating that a `CycleError` should be thrown whenever a cycle is first encountered. Defaults to `false`.
213
-
206
+
-_sourceNodes_ (optional) - An array of node identifier strings. This specifies the subset of nodes to use as the sources of the depth-first search. If _sourceNodes_ is not specified, all **[nodes](#nodes)** in the graph are used as source nodes.
207
+
-_includeSourceNodes_ (optional) - A boolean specifying whether or not to include the source nodes in the returned array. If _includeSourceNodes_ is not specified, it is treated as `true` (all source nodes are included in the returned array).
208
+
-_errorOnCycle_ (optional) - A boolean indicating that a `CycleError` should be thrown whenever a cycle is first encountered. Defaults to `false`.
Performs [Topological Sort](https://en.wikipedia.org/wiki/Topological_sorting). Returns an array of node identifier strings. The returned array includes nodes in topologically sorted order. This means that for each visited edge (**u** -> **v**), **u** comes before **v** in the topologically sorted order. Amazingly, this comes from simply reversing the result from depth first search. Inspired by by Cormen et al. "Introduction to Algorithms" 3rd Ed. p. 613.
231
226
232
-
See **[depthFirstSearch](#dfs)** for documentation of the arguments *sourceNodes* and *includeSourceNodes*.
227
+
See **[depthFirstSearch](#dfs)** for documentation of the arguments _sourceNodes_ and _includeSourceNodes_.
233
228
234
229
Note: this function raises a `CycleError` when the input is not a DAG.
0 commit comments