mirror of
https://github.com/anvaka/ngraph.path.git
synced 2026-01-18 15:13:12 +00:00
Working on docs
This commit is contained in:
parent
ec71971cba
commit
ad8260cd6d
37
README.md
37
README.md
@ -2,6 +2,43 @@
|
||||
|
||||
Path finding in a graph
|
||||
|
||||
# usage
|
||||
|
||||
## Basic usage
|
||||
|
||||
This is a basic example, which finds a path between arbitrary
|
||||
two nodes in arbitrary graph
|
||||
|
||||
``` js
|
||||
let path = require('ngraph.path');
|
||||
let pathFinder = path.aStar(graph);
|
||||
|
||||
// now we can find a path between two nodes:
|
||||
let fromNodeId = 40;
|
||||
let toNodeId = 42;
|
||||
let foundPath = pathFinder.find(fromNodeId, toNodeId);
|
||||
// foundPath is array of nodes in the graph
|
||||
```
|
||||
|
||||
Example above works for any graph, and it's equivalent to unweighted [Dijkstra's algorithm](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm).
|
||||
|
||||
## Weighted graph
|
||||
|
||||
Let's say we have the following graph:
|
||||
|
||||
``` js
|
||||
let createGraph = require('ngraph.graph');
|
||||
let graph = createGraph();
|
||||
|
||||
graph.addLink('a', 'b', {weight: 10});
|
||||
graph.addLink('a', 'c', {weight: 10});
|
||||
graph.addLink('c', 'd', {weight: 5});
|
||||
graph.addLink('b', 'd', {weight: 10});
|
||||
```
|
||||
|
||||

|
||||
|
||||
|
||||
# license
|
||||
|
||||
MIT
|
||||
|
||||
50
docs/weighted.svg
Normal file
50
docs/weighted.svg
Normal file
@ -0,0 +1,50 @@
|
||||
<svg width="354pt" height="98pt" viewBox="0.00 0.00 354.00 98.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<g id="graph1" class="graph" transform="scale(1 1) rotate(0) translate(4 94)">
|
||||
<title>Weighted demo</title>
|
||||
<polygon fill="white" stroke="white" points="-4,5 -4,-94 351,-94 351,5 -4,5"></polygon>
|
||||
<!-- a -->
|
||||
<g id="node1" class="node"><title>a</title>
|
||||
<ellipse fill="none" stroke="black" cx="27" cy="-46" rx="27" ry="18"></ellipse>
|
||||
<text text-anchor="middle" x="27" y="-41.8" font-family="Times,serif" font-size="14.00">a</text>
|
||||
</g>
|
||||
<!-- b -->
|
||||
<g id="node3" class="node"><title>b</title>
|
||||
<ellipse fill="none" stroke="black" cx="173" cy="-72" rx="27" ry="18"></ellipse>
|
||||
<text text-anchor="middle" x="173" y="-67.8" font-family="Times,serif" font-size="14.00">b</text>
|
||||
</g>
|
||||
<!-- a->b -->
|
||||
<g id="edge2" class="edge"><title>a->b</title>
|
||||
<path fill="none" stroke="black" d="M53.4128,-50.5884C76.5471,-54.7655 110.927,-60.9729 136.721,-65.6301"></path>
|
||||
<polygon fill="black" stroke="black" points="136.378,-69.1248 146.841,-67.4574 137.622,-62.2362 136.378,-69.1248"></polygon>
|
||||
<text text-anchor="middle" x="100" y="-67.2" font-family="Times,serif" font-size="14.00">weight 10</text>
|
||||
</g>
|
||||
<!-- c -->
|
||||
<g id="node5" class="node"><title>c</title>
|
||||
<ellipse fill="none" stroke="black" cx="173" cy="-18" rx="27" ry="18"></ellipse>
|
||||
<text text-anchor="middle" x="173" y="-13.8" font-family="Times,serif" font-size="14.00">c</text>
|
||||
</g>
|
||||
<!-- a->c -->
|
||||
<g id="edge4" class="edge"><title>a->c</title>
|
||||
<path fill="none" stroke="black" d="M52.1002,-39.0646C58.5342,-37.3428 65.5044,-35.5934 72,-34.2 93.1836,-29.6558 117.136,-25.7338 136.286,-22.8874"></path>
|
||||
<polygon fill="black" stroke="black" points="136.991,-26.3218 146.384,-21.4196 135.984,-19.3946 136.991,-26.3218"></polygon>
|
||||
<text text-anchor="middle" x="100" y="-39.2" font-family="Times,serif" font-size="14.00">weight 10</text>
|
||||
</g>
|
||||
<!-- d -->
|
||||
<g id="node7" class="node"><title>d</title>
|
||||
<ellipse fill="none" stroke="black" cx="319" cy="-46" rx="27" ry="18"></ellipse>
|
||||
<text text-anchor="middle" x="319" y="-41.8" font-family="Times,serif" font-size="14.00">d</text>
|
||||
</g>
|
||||
<!-- b->d -->
|
||||
<g id="edge8" class="edge"><title>b->d</title>
|
||||
<path fill="none" stroke="black" d="M199.413,-67.4116C222.547,-63.2345 256.927,-57.0271 282.721,-52.3699"></path>
|
||||
<polygon fill="black" stroke="black" points="283.622,-55.7638 292.841,-50.5426 282.378,-48.8752 283.622,-55.7638"></polygon>
|
||||
<text text-anchor="middle" x="246" y="-67.2" font-family="Times,serif" font-size="14.00">weight 10</text>
|
||||
</g>
|
||||
<!-- c->d -->
|
||||
<g id="edge6" class="edge"><title>c->d</title>
|
||||
<path fill="none" stroke="black" d="M199.616,-21.4196C219.898,-24.3025 248.893,-28.8143 274,-34.2 277.248,-34.8967 280.614,-35.6824 283.973,-36.5092"></path>
|
||||
<polygon fill="black" stroke="black" points="283.343,-39.961 293.9,-39.0646 285.088,-33.182 283.343,-39.961"></polygon>
|
||||
<text text-anchor="middle" x="246" y="-39.2" font-family="Times,serif" font-size="14.00">weight 5</text>
|
||||
</g>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 3.0 KiB |
Loading…
x
Reference in New Issue
Block a user