#81 javascript - graph and edge

## Diagram Information

Language: Javascript
Repository: trekhleb/javascript-algorithms  github
Commit: master
Tags: featured-js

## Description:

In computer science, a graph is an abstract data type that is meant to implement the undirected graph and directed graph concepts from mathematics, specifically the field of graph theory

A graph data structure consists of a finite (and possibly mutable) set of vertices or nodes or points, together with a set of unordered pairs of these vertices for an undirected graph or a set of ordered pairs for a directed graph. These pairs are known as edges, arcs, or lines for an undirected graph and as arrows, directed edges, directed arcs, or directed lines for a directed graph. The vertices may be part of the graph structure, or may be external entities represented by integer indices or references. Text and source code from https://github.com/trekhleb/javascript-algorithms/tree/master/src/data-structures/graph

### Class relationships

There were no relationships to reverse engineer so I added them manually with

`Graph "*" *--> GraphVertex : vertices`

`Graph "*" *--> GraphEdge : edges`

`GraphVertex "*" *--> GraphEdge : edges`

markup.

### On the literate code map additions

I've added a couple of literate code map "method" compartments containing code fragments and a little bit of commentary.

The PlantUML markup is

`class Graph {`

`  `

`    method("constructor", "isDirected = false")`

`    The constructor looks like this:`

`    ````

`    this.vertices = {};`

`    this.edges = {};`

`    this.isDirected = isDirected;`

`    ````

`    This class doesn't actually create instances`

`    of any other class - any such wiring is done`

`    by callers of this classes e.g. via addEdge etc. xref("1", "source")`

`    method("addEdge", "edge")`

`    xref("1")`

`    `

`    This method doesn't just add edges to <b>this.edges</b>`

`  `

`    ````

`    // Check if edge has been already added.`

`    if (this.edges[edge.getKey()]) {`

`      throw new Error('Edge has already been added before');`

`    } else {`

`      this.edges[edge.getKey()] = edge;`

`    }`

`    ````

`    it also adds information to <b>this.vertices</b> `

`    ````

`    // Try to find and end start vertices.`

`    let startVertex = this.getVertexByKey(edge.startVertex.getKey());`

`    let endVertex = this.getVertexByKey(edge.endVertex.getKey());`

`    // Insert start vertex if it wasn't inserted.`

`    if (!startVertex) {`

`      this.addVertex(edge.startVertex);`

`      startVertex = this.getVertexByKey(edge.startVertex.getKey());`

`    }`

`    ````

`}`

• `src/data-structures/graph/Graph.js`
• `src/data-structures/graph/GraphEdge.js`
• `src/data-structures/graph/GraphVertex.js`