UNPKG

graph-curry

Version:

a Map based implementation of canonical graph data algorithms

383 lines (247 loc) 16.1 kB
<!DOCTYPE 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 &hellip;</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">&#182;</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">&#182;</a> </div> <p><strong>mergeNeighbors</strong> <code>:: Map&lt;edge&gt; -&gt; node -&gt; Map&lt;edge&gt;</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">&#182;</a> </div> <p><strong>graph</strong> <code>:: [Node] -&gt; Map&lt;edge&gt;</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>) =&gt;</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">&#182;</a> </div> <p><strong>nodes</strong> `:: Map<edge> -&gt; [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> =&gt;</span> spreadK(asMap(edges));</pre></div></div> </li> <li id="section-5"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p><strong>copy</strong> <code>:: Map&lt;edge&gt; -&gt; Map&lt;edge&gt;</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> =&gt;</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">&#182;</a> </div> <p><strong>adj</strong> <code>:: Map&lt;edge&gt; -&gt; node -&gt; Map&lt;{node: Number}&gt;</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> =&gt;</span> src =&gt; asMap(get(edges)(src));</pre></div></div> </li> <li id="section-7"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-7">&#182;</a> </div> <p><strong>neighbors</strong> <code>:: Map&lt;edge&gt; -&gt; node -&gt; [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> =&gt;</span> src =&gt; nodes(adj(edges)(src));</pre></div></div> </li> <li id="section-8"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-8">&#182;</a> </div> <p><strong>contains</strong> <code>:: Map&lt;edge&gt; -&gt; node -&gt; 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> =&gt;</span> node =&gt; hasK(edges)(node);</pre></div></div> </li> <li id="section-9"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-9">&#182;</a> </div> <p><strong>isAdjacent</strong> <code>:: Map&lt;edge&gt; -&gt; node -&gt; Map&lt;edge&gt;</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> =&gt;</span> src =&gt; <span class="hljs-function"><span class="hljs-params">nb</span> =&gt;</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">&#182;</a> </div> <p><strong>addNodes</strong> <code>:: Map&lt;edge&gt; -&gt; ...node -&gt; Map&lt;edge&gt;</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> =&gt;</span> (...srcs) =&gt; srcs.reduce(addNodeBin, edges);</pre></div></div> </li> <li id="section-11"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-11">&#182;</a> </div> <p><strong>resetNodes</strong> <code>:: Map&lt;edge&gt; -&gt; ...node -&gt; Map&lt;edge&gt;</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> =&gt;</span> (...src) =&gt; src.reduce(resetNodeBin, edges);</pre></div></div> </li> <li id="section-12"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-12">&#182;</a> </div> <p><strong>addEdges</strong> <code>:: Map&lt;edge&gt; -&gt; (node, Number) -&gt; ...node -&gt; Map&lt;edge&gt;</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> =&gt;</span> (src, w = <span class="hljs-number">0</span>) =&gt; <span class="hljs-function">(<span class="hljs-params">...nabes</span>) =&gt;</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">&#182;</a> </div> <p><strong>removeEdges</strong> <code>:: Map&lt;edge&gt; -&gt; node -&gt; ...node -&gt; Map&lt;edge&gt;</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> =&gt;</span> src =&gt; <span class="hljs-function">(<span class="hljs-params">...nabes</span>) =&gt;</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">&#182;</a> </div> <p><strong>disconnectNodes</strong> <code>:: Map&lt;edge&gt; -&gt; ...node -&gt; Map&lt;edge&gt;</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> =&gt;</span> (...srcs) =&gt; 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">&#182;</a> </div> <p><strong>removeNodes</strong> <code>:: Map&lt;edge&gt; -&gt; ...node -&gt; Map&lt;edge&gt;</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> =&gt;</span> (...srcs) =&gt; 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">&#182;</a> </div> <p><strong>mergeEdges</strong> <code>:: Map&lt;edge&gt; -&gt; ...{node:adjacency} -&gt; Map&lt;edge&gt;</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> =&gt;</span> (...alt) =&gt; alt.reduce(mergeEdgesBin, edges);</pre></div></div> </li> <li id="section-17"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-17">&#182;</a> </div> <p><strong>addNeighbor</strong> <code>:: Map&lt;edge&gt; -&gt; ...{node:adjacency} -&gt; Map&lt;edge&gt;</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> =&gt;</span> src =&gt; <span class="hljs-function">(<span class="hljs-params">n, w = <span class="hljs-number">0</span></span>) =&gt;</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">&#182;</a> </div> <p><strong>addEntry</strong> <code>:: Map&lt;{node:Number}&gt; -&gt; [node, Number] -&gt; Map&lt;edge&gt;</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> =&gt;</span> ([ n, w = <span class="hljs-number">0</span> ]) =&gt; addBinMap(nabes, [ n, w ]);</pre></div></div> </li> </ul> </div> </body> </html>