# GRAPHS

A graph is a collection of nodes and edges

let graph = new Graph()

This creates an empty graph with no nodes or edges

Graphs don't exist in 2D space (or any space), they are an abstract map showing connections between nodes.

## § Nodes and Edges

The graph holds onto all its nodes and edges in arrays.
The types are Graph Node and Graph Edge.

graph.nodes = []  // array of GraphNodegraph.edges = []  // array of GraphEdge

This operation makes a new node and returns a reference to it.

let node = graph.newNode()  // node is a GraphNode

A new edge needs to know the 2 nodes its connecting.

let edge = graph.newEdge(node1, node2)  // edge is a GraphEdge

You can always grab a node or an edge again

graph.newNode()let node = graph.nodes[0]  // the first node

## § Nodes

This graph is interactive

graph.nodes[].adjacentNodes()graph.edges[].adjacentNodes()

Two nodes are adjacent if they are connected by an edge

## § Edges

An edge connects two nodes

graph.edges[0]  // the first edgegraph.edges.length  // total number of edges
graph.nodes[].adjacentEdges()graph.edges[].adjacentEdges()

Invalid edges
Circular: an edge cannot connect the same node at both ends
Duplicate: the same 2 nodes cannot have more than 1 edge between them

## § Remove

graph.removeNode(node)

When removing a node, any edges which share the node will be removed also.

graph.removeEdge(edge)

Removing an edge will do simply that and the nodes remain untouched.

## § Clean

graph.clean()

Cleaning a graph removes duplicate and circular edges. You can also target specific edges:

graph.removeEdge(edge)graph.removeEdgeBetween(node1, node2)

Removing Nodes:

graph.removeNode(node)graph.removeIsolatedNodes()

Isolated nodes are nodes that aren't connected to an edge.

graph.mergeNodes(node1,node2)

Each of these functions returns a GraphClean Object

{  edges:{total:number, duplicate:number, circular:number},  nodes:{total:number, isolated:number}}

Each of the entries in a GraphClean object is the number of elements of that type removed