Skip to content

Commit 326c392

Browse files
authored
Merge pull request #56 from TimMikeladze/master
Update build process + general updates
2 parents bbb25b0 + df75ac1 commit 326c392

13 files changed

Lines changed: 16341 additions & 6033 deletions

File tree

.github/workflows/main.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Main CI workflow
2+
3+
on: [push]
4+
5+
jobs:
6+
run-ci:
7+
name: Run Type Check & Linters
8+
runs-on: ubuntu-latest
9+
timeout-minutes: 10
10+
11+
steps:
12+
- name: Checkout repository
13+
uses: actions/checkout@v3
14+
with:
15+
fetch-depth: 0
16+
17+
- name: Set up Node
18+
uses: actions/setup-node@v3
19+
with:
20+
node-version: 18
21+
22+
- name: Install dependencies (with cache)
23+
uses: bahmutov/npm-install@v1
24+
25+
- name: Check types
26+
run: npm run type-check
27+
28+
- name: Test
29+
run: npm run test
30+
31+
- name: Build
32+
run: npm run build

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
*.swp
33
graph-data-structure.js
4+
dist

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
save-exact = true

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist

README.md

Lines changed: 41 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
# graph-data-structure
1+
# graph-data-structure
22

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).
44

55
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.
66

77
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.
88

99
**Table of Contents**
1010

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)
1616

1717
## Installing
1818

@@ -85,56 +85,56 @@ For more detailed example code that shows more methods, have a look at the [test
8585

8686
## API Reference
8787

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)
91+
- [Querying the Graph](#querying-the-graph)
92+
- [Serialization](#serialization)
93+
- [Graph Algorithms](#graph-algorithms)
9494

9595
### Creating a Graph
9696

9797
<a name="graph" href="#graph">#</a> <b>Graph</b>([<i>serialized</i>])
9898

9999
Constructs an instance of the graph data structure.
100100

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)**.
102102

103103
### Adding and Removing Nodes
104104

105105
<a name="add-node" href="#add-node">#</a> <i>graph</i>.<b>addNode</b>(<i>node</i>)
106106

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.
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.
108108

109109
<a name="remove-node" href="#remove-node">#</a> <i>graph</i>.<b>removeNode</b>(<i>node</i>)
110110

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.
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.
112112

113113
### Adding and Removing Edges
114114

115115
<a name="add-edge" href="#add-edge">#</a> <i>graph</i>.<b>addEdge</b>(<i>u</i>, <i>v</i>[,<i>weight</i>])
116116

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.
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.
118118

119-
The last argument *weight* (optional) specifies the weight of this edge.
119+
The last argument _weight_ (optional) specifies the weight of this edge.
120120

121121
<a name="remove-edge" href="#remove-edge">#</a> <i>graph</i>.<b>removeEdge</b>(<i>u</i>, <i>v</i>)
122122

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.
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.
124124

125125
<a name="has-edge" href="#has-edge">#</a> <i>graph</i>.<b>hasEdge</b>(<i>u</i>, <i>v</i>)
126126

127-
Returns `true` if there exists an edge from node *u* to node *v*. Returns `false` otherwise.
127+
Returns `true` if there exists an edge from node _u_ to node _v_. Returns `false` otherwise.
128128

129129
### Working with Edge Weights
130130

131131
<a name="set-edge-weight" href="#set-edge-weight">#</a> <i>graph</i>.<b>setEdgeWeight</b>(<i>u</i>, <i>v</i>, <i>weight</i>)
132132

133-
Sets the *weight* (a number) of the edge from node *u* to node *v*.
133+
Sets the _weight_ (a number) of the edge from node _u_ to node _v_.
134134

135135
<a name="get-edge-weight" href="#get-edge-weight">#</a> <i>graph</i>.<b>getEdgeWeight</b>(<i>u</i>, <i>v</i>, <i>weight</i>)
136136

137-
Gets the *weight* of the edge from node *u* to node *v*. If no weight was previously set on this edge, then the value 1 is returned.
137+
Gets the _weight_ of the edge from node _u_ to node _v_. If no weight was previously set on this edge, then the value 1 is returned.
138138

139139
### Querying the Graph
140140

@@ -144,29 +144,29 @@ List all nodes in the graph. Returns an array of node identifier strings.
144144

145145
<a name="adjacent" href="#adjacent">#</a> <i>graph</i>.<b>adjacent</b>(<i>node</i>)
146146

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.
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.
148148

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.
150150

151151
<a name="indegree" href="#indegree">#</a> <i>graph</i>.<b>indegree</b>(<i>node</i>)
152152

153-
Computes the [indegree](https://en.wikipedia.org/wiki/Directed_graph#Indegree_and_outdegree) (number of incoming edges) for the specified *node*.
153+
Computes the [indegree](https://en.wikipedia.org/wiki/Directed_graph#Indegree_and_outdegree) (number of incoming edges) for the specified _node_.
154154

155155
<a name="outdegree" href="#outdegree">#</a> <i>graph</i>.<b>outdegree</b>(<i>node</i>)
156156

157-
Computes the [outdegree](https://en.wikipedia.org/wiki/Directed_graph#Indegree_and_outdegree) (number of outgoing edges) for the specified *node*.
157+
Computes the [outdegree](https://en.wikipedia.org/wiki/Directed_graph#Indegree_and_outdegree) (number of outgoing edges) for the specified _node_.
158158

159159
### Serialization
160160

161161
<a name="serialize" href="#serialize">#</a> <i>graph</i>.<b>serialize</b>()
162162

163163
Serializes the graph. Returns an object with the following properties.
164164

165-
* `nodes` An array of objects, each with an `id` property whose value is a node identifier string.
166-
* `links` An array of objects representing edges, each with the following properties.
167-
* `source` The node identifier string of the source node (**u**).
168-
* `target` The node identifier string of the target node (**v**).
169-
* `weight` The weight of the edge between the source and target nodes.
165+
- `nodes` An array of objects, each with an `id` property whose value is a node identifier string.
166+
- `links` An array of objects representing edges, each with the following properties.
167+
- `source` The node identifier string of the source node (**u**).
168+
- `target` The node identifier string of the target node (**v**).
169+
- `weight` The weight of the edge between the source and target nodes.
170170

171171
Here's example code for serializing a graph.
172172

@@ -181,11 +181,7 @@ The following will be the value of `serialized`.
181181

182182
```json
183183
{
184-
"nodes": [
185-
{ "id": "a" },
186-
{ "id": "b" },
187-
{ "id": "c" }
188-
],
184+
"nodes": [{ "id": "a" }, { "id": "b" }, { "id": "c" }],
189185
"links": [
190186
{ "source": "a", "target": "b", "weight": 1 },
191187
{ "source": "b", "target": "c", "weight": 1 }
@@ -197,20 +193,19 @@ This representation conforms to the convention of graph representation when work
197193

198194
<a name="deserialize" href="#deserialize">#</a> <i>graph</i>.<b>deserialize</b>(<i>serialized</i>)
199195

200-
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**.
201197

202198
### Graph Algorithms
203199

204-
<a name="dfs" href="#dfs">#</a> <i>graph</i>.<b>depthFirstSearch</b>([<i>sourceNodes</i>][, <i>includeSourceNodes</i>][, <i>errorOnCycle</i>])
200+
<a name="dfs" href="#dfs">#</a> <i>graph</i>.<b>depthFirstSearch</b>([<i>sourceNodes</i>][, <i>includesourcenodes</i>][, <i>errorOnCycle</i>])
205201

206202
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.
207203

208204
Arguments:
209205

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`.
214209

215210
<a name="has-cycle" href="#has-cycle">#</a> <i>graph</i>.<b>hasCycle</b>()
216211

@@ -222,14 +217,14 @@ Performs search of [Lowest common ancestors](https://en.wikipedia.org/wiki/Lowes
222217

223218
Arguments:
224219

225-
* *node1* (required) - First node.
226-
* *node2* (required) - Second node.
220+
- _node1_ (required) - First node.
221+
- _node2_ (required) - Second node.
227222

228-
<a name="topological-sort" href="#topological-sort">#</a> <i>graph</i>.<b>topologicalSort</b>([<i>sourceNodes</i>][, <i>includeSourceNodes</i>])
223+
<a name="topological-sort" href="#topological-sort">#</a> <i>graph</i>.<b>topologicalSort</b>([<i>sourceNodes</i>][, <i>includesourcenodes</i>])
229224

230225
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.
231226

232-
See **[depthFirstSearch](#dfs)** for documentation of the arguments *sourceNodes* and *includeSourceNodes*.
227+
See **[depthFirstSearch](#dfs)** for documentation of the arguments _sourceNodes_ and _includeSourceNodes_.
233228

234229
Note: this function raises a `CycleError` when the input is not a DAG.
235230

index.d.ts

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)