A graph is a collection of nodes and edges (circles and lines)
vargraph = newGraph()
Graphs don't exist in 2D space (or any space), they are an abstract map showing connections between nodes.
§ Nodes and Edges
nodes = // array of GraphNode
edges = // array of GraphEdge
This makes a new node. You can store a reference to it.
varnode =graph. newNode() // node is a GraphNode
A new edge needs to know the 2 nodes its connecting.
varedge =graph. newEdge( node1, node2) // edge is a GraphEdge
nodes[ 0] // the first node
nodes.length // total number of nodes
This graph is interactive with the code below
nodes[ ]. adjacentNodes()
edges[ ]. adjacentNodes()
Two nodes are adjacent if they are connected by an edge
An edge connects two nodes
edges[ 0] // the first edge
edges.length // total number of edges
nodes[ ]. adjacentEdges()
edges[ ]. adjacentEdges()
Ways that edges can be invalid:
- CIRCULAR an edge cannot connect a node to the same node
- DUPLICATE the same 2 nodes cannot have more than 1 edge between them
When removing a node, any edges which share the node will be removed also.
Removing an edge will do simply that and the nodes remain untouched.
clean() // # of edges removed
Cleaning removes duplicate and circular edges. You can also target specific edges:
removeEdge( edge) // # of edges removed
removeEdgeBetween( node1, node2) // # of edges removed
removeNode( node) // # of nodes removed
removeIsolatedNodes() // # of nodes removed
Isolated nodes are nodes that aren't connected to an edge.
varnode =graph. mergeNodes( node1, node2) // node is a GraphNode