JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 424
  • Score
    100M100P100Q87764F
  • License Apache-2.0

A library for modeling and traversing graphs

Package Exports

  • cleargraph

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (cleargraph) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Cleargraph

Cleargraph is a graph library offering:

  • An abstraction over graphs that supports generic data types
  • Traversal over successors and predecessors
  • Use of user-defined filters on nodes and edges information and traversal
  • Strictly-typed implementation

Installation

npm install cleargraph
yarn add cleargraph

Getting started

The nodes and edges in the graph are represented by key-value pairs where the keys are strings, and the generics N and E represent the node value and edge value respectively.

When instantiating the graph, specify the values of N and E. In addition, in order to allow graph serialization, N and E should implement stringify(). If it's not implemented, the graph will call its own default serialization method that might not work for complex objects.

Here is an example of N (Node Data) and E (Edge Data) classes:

class Orb { // a node in the graph
    name: string;
    radius: number;
    constructor(name:string, radius:number){
        this.name = name;
        this.radius = radius;
    }
    stringify(){ // Add a specific stringify() implementation if your class will not stringify correctly with just JSON.stringiy when serializing the graph
        return JSON.stringify({name: this.name, radius: this.radius});
    }
}

class OrbRelation{ // an edge in the graph
    relationType: string;
    proximity: number;
    constructor(relationType: string, proximity: number){
        this.relationType = relationType;
        this.proximity = proximity;
    }
    stringify(){
        return JSON.stringify({relationType: this.relationType, proximity: this.proximity});
    }
}

Using these classes to implement a graph:

let g = new Graph<Orb, OrbRelation>();

g.setNode('earth', new Orb('earth', 6371));
g.setNode('moon', new Orb('moon', 1737));
g.setNode('sun', new Orb('sun', 696340));
g.setEdge('moon','earth', new OrbRelation('orbits', 384400));
g.setEdge('earth','sun', new OrbRelation('orbits', 147240000));

Some uses of the graph:

g.node('moon');
// Orb{name: 'moon', radius: 1737}
g.edge('earth', 'sun');
//OrbRelation{relationType: 'orbits', proximity: 147240000}
g.succssors('moon'); // returns the immediate nodes the given node point to
// Map 
// {"earth" => Orb} {key: "earth", value: Orb}
//     key:"earth"
//     value:Orb {name: "earth", radius: 6371}
g.successorsArray('moon'); // returns an array of all the nodes the given node points to *recursively*
// Array(2) [Orb, Orb]
// 0:Orb {name: "earth",radius: 6371}
// 1:Orb {name: "sun", radius: 696340}
g.toposort(); // performs a topological sort on the graph
// Array(3) [Orb, Orb, Orb]
// 0:Orb {name: "moon",radius: 1737}
// 1:Orb {name: "earth",radius: 6371}
// 2:Orb {name: "sun", radius: 696340}

Contributing

Contributions are always welcome, no matter how large or small.

License

Apache license, version 2.0