• source-docco.js

  • ¶
    /**
     * Directed Graph
     * --------------
     *
     * Provides data structure for directed graphs
     * Author: Mr. Bean <[email protected]>
     * License MIT
     * Cpyright 2013 Mr. Bean
     */
  • ¶

    A Graph object with the basic properties

    var Graph = function () {
  • ¶

    Object for holding vertices of the Graph

        this.vertices = {};
  • ¶

    Array of edges of the Graph

        this.edges = [];
  • ¶

    Length of the Graph, initially set to 0

        this.length = 0;
    };
  • ¶

    Constructor for a vertex of Graph

    Graph.Vertex = function (name, value) {
  • ¶

    Store the name argument as a property

        this.name = name;
  • ¶

    Create an empty array for edges

        this.edges = [];
  • ¶

    Store the value argument as a property

        this.value = value;
    };
  • ¶

    Prototype function which returns the number of edges of a Graph.Vertex

    Graph.Vertex.prototype.degree = function () {
        return this.edges.length;
    };
  • ¶

    Constructor for edge of a Graph

    Graph.Edge = function (tail, head) {
        this.tail = tail;
        this.head = head;
        tail.edges.push(this);
        head.edges.push(this);
    };
  • ¶

    Add a new Graph.Vertex or updates the value of an existing one example var newVertex = Graph.addVertex("vertex1", 20);

    Graph.prototype.addVertex = function (name, value) {
        if (!this.vertices[name]) {
            this.vertices[name] = new Graph.Vertex(name, value);
            this.length++;
    
        }
        else if (value) {
            this.vertices[name].value = value;
        }
        return this.vertices[name];
    };
  • ¶

    Method for adding new {@link Graph.Edge}

    Graph.prototype.addEdge = function (tail, head) {
        return new Graph.Edge(this.addVertex(tail), this.addVertex(head));
    };
  • ¶

    Exports the {@link Graph}

    module.exports = Graph;