graph-curry
Version:
a Map based implementation of canonical graph data algorithms
383 lines (247 loc) • 16.1 kB
HTML
<html>
<head>
<title>graph.js</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
<link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
<div id="container">
<div id="background"></div>
<ul id="jump_to">
<li>
<a class="large" href="javascript:void(0);">Jump To …</a>
<a class="small" href="javascript:void(0);">+</a>
<div id="jump_wrapper">
<div id="jump_page_wrapper">
<div id="jump_page">
<a class="source" href="components.html">
components.js
</a>
<a class="source" href="contract.html">
contract.js
</a>
<a class="source" href="cut.html">
cut.js
</a>
<a class="source" href="graph.html">
graph.js
</a>
<a class="source" href="index.html">
index.js
</a>
<a class="source" href="path.html">
path.js
</a>
<a class="source" href="reducers.html">
reducers.js
</a>
<a class="source" href="search.html">
search.js
</a>
<a class="source" href="show.html">
show.js
</a>
<a class="source" href="tree.html">
tree.js
</a>
</div>
</div>
</li>
</ul>
<ul class="sections">
<li id="title">
<div class="annotation">
<h1>graph.js</h1>
</div>
</li>
<li id="section-1">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-1">¶</a>
</div>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">import</span> { addBinMap, asMap, get, hasK, removeBin, spreadK, triple, tuple, uniteMap, } <span class="hljs-keyword">from</span> <span class="hljs-string">'fenugreek-collections'</span>;
<span class="hljs-keyword">import</span> { addEdgeBin, addNodeBin, disconnectNodeBin, mergeEdgesBin, removeEdgeBin, resetNodeBin, }
<span class="hljs-keyword">from</span> <span class="hljs-string">'./reducers'</span>;</pre></div></div>
</li>
<li id="section-2">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-2">¶</a>
</div>
<p><strong>mergeNeighbors</strong> <code>:: Map<edge> -> node -> Map<edge></code>
resets the nodes adjacency list to an empty map</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> mergeNeighbors = uniteMap;</pre></div></div>
</li>
<li id="section-3">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-3">¶</a>
</div>
<p><strong>graph</strong> <code>:: [Node] -> Map<edge></code>
adds {node: adjacencyList} pairs ot an Edgelist</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> graph = <span class="hljs-function">(<span class="hljs-params">...elems</span>) =></span> elems.reduce(addNodeBin, <span class="hljs-keyword">new</span> <span class="hljs-built_in">Map</span>);</pre></div></div>
</li>
<li id="section-4">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-4">¶</a>
</div>
<p><strong>nodes</strong> `:: Map<edge> -> [node]
returns an array of the nodes</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> nodes = <span class="hljs-function"><span class="hljs-params">edges</span> =></span> spreadK(asMap(edges));</pre></div></div>
</li>
<li id="section-5">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-5">¶</a>
</div>
<p><strong>copy</strong> <code>:: Map<edge> -> Map<edge></code>
creates a copy of a Edgelist</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> copy = <span class="hljs-function"><span class="hljs-params">edges</span> =></span> nodes(edges).reduce(addNodeBin, asMap(edges));</pre></div></div>
</li>
<li id="section-6">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-6">¶</a>
</div>
<p><strong>adj</strong> <code>:: Map<edge> -> node -> Map<{node: Number}></code>
returns the nodes adjacency list</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> adj = <span class="hljs-function"><span class="hljs-params">edges</span> =></span> src => asMap(get(edges)(src));</pre></div></div>
</li>
<li id="section-7">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-7">¶</a>
</div>
<p><strong>neighbors</strong> <code>:: Map<edge> -> node -> [node]</code>
returns the nodes neighbors</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> neighbors = <span class="hljs-function"><span class="hljs-params">edges</span> =></span> src => nodes(adj(edges)(src));</pre></div></div>
</li>
<li id="section-8">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-8">¶</a>
</div>
<p><strong>contains</strong> <code>:: Map<edge> -> node -> Boolean</code>
checks for the presence of a node in an edgelist</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> contains = <span class="hljs-function"><span class="hljs-params">edges</span> =></span> node => hasK(edges)(node);</pre></div></div>
</li>
<li id="section-9">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-9">¶</a>
</div>
<p><strong>isAdjacent</strong> <code>:: Map<edge> -> node -> Map<edge></code>
checks for the presence of a neighbor in a node’s adjacency list</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> isAdjacent = <span class="hljs-function"><span class="hljs-params">edges</span> =></span> src => <span class="hljs-function"><span class="hljs-params">nb</span> =></span> contains(adj(edges)(src))(nb);</pre></div></div>
</li>
<li id="section-10">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-10">¶</a>
</div>
<p><strong>addNodes</strong> <code>:: Map<edge> -> ...node -> Map<edge></code>
adds nodes to an Edgelist</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> addNodes = <span class="hljs-function"><span class="hljs-params">edges</span> =></span> (...srcs) => srcs.reduce(addNodeBin, edges);</pre></div></div>
</li>
<li id="section-11">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-11">¶</a>
</div>
<p><strong>resetNodes</strong> <code>:: Map<edge> -> ...node -> Map<edge></code>
resets the adjacency lists of given nodes to an empty map</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> resetNodes = <span class="hljs-function"><span class="hljs-params">edges</span> =></span> (...src) => src.reduce(resetNodeBin, edges);</pre></div></div>
</li>
<li id="section-12">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-12">¶</a>
</div>
<p><strong>addEdges</strong> <code>:: Map<edge> -> (node, Number) -> ...node -> Map<edge></code>
creates edges between a node and multiple other nodes</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> addEdges = <span class="hljs-function"><span class="hljs-params">edges</span> =></span> (src, w = <span class="hljs-number">0</span>) => <span class="hljs-function">(<span class="hljs-params">...nabes</span>) =></span>
nabes.map(triple(w)(src)).reduce(addEdgeBin, edges);</pre></div></div>
</li>
<li id="section-13">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-13">¶</a>
</div>
<p><strong>removeEdges</strong> <code>:: Map<edge> -> node -> ...node -> Map<edge></code>
removes edges between a node and select other nodes</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> removeEdges = <span class="hljs-function"><span class="hljs-params">edges</span> =></span> src => <span class="hljs-function">(<span class="hljs-params">...nabes</span>) =></span>
nabes.map(tuple(src)).reduce(removeEdgeBin, edges);</pre></div></div>
</li>
<li id="section-14">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-14">¶</a>
</div>
<p><strong>disconnectNodes</strong> <code>:: Map<edge> -> ...node -> Map<edge></code>
resets the nodes adjacency list to an empty map</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> disconnectNodes = <span class="hljs-function"><span class="hljs-params">edges</span> =></span> (...srcs) =>
srcs.reduce(disconnectNodeBin, copy(edges));</pre></div></div>
</li>
<li id="section-15">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-15">¶</a>
</div>
<p><strong>removeNodes</strong> <code>:: Map<edge> -> ...node -> Map<edge></code>
resets the nodes adjacency list to an empty map</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> removeNodes = <span class="hljs-function"><span class="hljs-params">edges</span> =></span> (...srcs) =>
srcs.reduce(removeBin, disconnectNodes(edges)(...srcs));</pre></div></div>
</li>
<li id="section-16">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-16">¶</a>
</div>
<p><strong>mergeEdges</strong> <code>:: Map<edge> -> ...{node:adjacency} -> Map<edge></code>
resets the nodes adjacency list to an empty map</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> mergeEdges = <span class="hljs-function"><span class="hljs-params">edges</span> =></span> (...alt) => alt.reduce(mergeEdgesBin, edges);</pre></div></div>
</li>
<li id="section-17">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-17">¶</a>
</div>
<p><strong>addNeighbor</strong> <code>:: Map<edge> -> ...{node:adjacency} -> Map<edge></code>
resets the nodes adjacency list to an empty map</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> addNeighbor = <span class="hljs-function"><span class="hljs-params">edges</span> =></span> src => <span class="hljs-function">(<span class="hljs-params">n, w = <span class="hljs-number">0</span></span>) =></span>
addBinMap(adj(edges)(src), [ n, w ]);</pre></div></div>
</li>
<li id="section-18">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-18">¶</a>
</div>
<p><strong>addEntry</strong> <code>:: Map<{node:Number}> -> [node, Number] -> Map<edge></code>
resets the nodes adjacency list to an empty map</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> addEntry = <span class="hljs-function"><span class="hljs-params">nabes</span> =></span> ([ n, w = <span class="hljs-number">0</span> ]) => addBinMap(nabes, [ n, w ]);</pre></div></div>
</li>
</ul>
</div>
</body>
</html>