UndirectedGraph

UndirectedGraph

undirected graph based on adjacent list concept.
the graph structure edge is made by basic Object, whick is key - value mapping class.
one example is like {n1: {n2: w12, n3: w13}, n2: {n3: w23}, n3: {n1: w31}};

Constructor

new UndirectedGraph(node, edge, otherGraph)

Version:
  • v1.0

Create undirected graph.
If it is not given any parameter, empty graph.
when otherGraph parameter are given, this deepcopy otherGraph.
If node, edge are given, it made graph by given params.

Parameters:
Name Type Default Description
node null | Array null

array of string because Object key is string value.

edge null | Array null

array of [startnode : string, endnode : string, weight], w can be omitted.

otherGraph null | DirectedGraph null

other DirectedGraph object.

Methods

nodeSize() → {number}

return the number of node.

Returns:
  • the number of node.
Type
number

edgeSize() → {number}

return the number of edge.

Returns:
  • the number of edge.
Type
number

getNodes() → {array}

Get every node of graph by array.

Returns:
  • nodes of graph.
Type
array

getEdges() → {array}

Get every edge of graph by array.

Returns:
  • edges of graph. [[startnode,endnode,weight],...] structure.
Type
array

getWeight(startNode, endNode) → {*}

Get weight of a edge.

Parameters:
Name Type Default Description
startNode string null

edge start node.

endNode string null

edge end node.

Returns:
  • weight of the edge.
Type
*

setIterType(type)

Set iterator type.
'dfs', 'bfs' or 'dijkstra'

Parameters:
Name Type Description
type string

iterator type.

setIterStart(node)

Set where to start iterating.

Parameters:
Name Type Description
node string

start node.

setWeightAddFunc(func)

Set weight add function.

Parameters:
Name Type Description
func function

add two weight.

setWeightCompFunc(func)

Set weight compare function.

Parameters:
Name Type Description
func function

compare two weight.

setWeight(startNode, endNode, newWeight) → {boolean}

Change the weight of a edge.

Parameters:
Name Type Default Description
startNode string null

edge start node.

endNode string null

edge end node.

newWeight string null

new weight of edge.

Returns:
  • if edge doesn't exist, return false.
Type
boolean

mapWeight(func) → {boolean}

Change all the edge by given function.
the function parameter are three, startnode name, endnode name, weight of edge.

Parameters:
Name Type Default Description
func function null

change edge.

Returns:

false if given param is not a function.

Type
boolean

eraseNode(node) → {boolean}

erase a node.
all edges relative to node are also erased.

Parameters:
Name Type Default Description
node string null

node name

Returns:
  • false if node is not exist.
Type
boolean

eraseEdge(startNode, endNode) → {boolean}

erase a edge

Parameters:
Name Type Default Description
startNode string null
endNode string null
Returns:

false if edge is not exist.

Type
boolean

insertNode(node) → {boolean}

insert a node.

Parameters:
Name Type Default Description
node string null
Returns:

false if node is already exist.

Type
boolean

insertEdge(startNode, endNode, weight) → {boolean}

insert a edge.

Parameters:
Name Type Default Description
startNode string null
endNode string null
weight * null
Returns:

false if nodes are not exist or edge is already exist.

Type
boolean

isCycle() → {boolean}

check there is a cycle in the graph.

Returns:
  • whether a cycle is in or not.
Type
boolean

isTree() → {boolean}

check whether the graph is tree or not.

Returns:
  • whether the graph is tree or not.
Type
boolean

isNegativeWeight(func) → {boolean}

check whether negative edge exists.
this can get function parameter to decide the result of arbitrary weight.

Parameters:
Name Type Description
func function

return true if weight is semantically negative.

Returns:
  • true if negative weight exists.
Type
boolean

isAllWeightEqual() → {boolean}

check the weight of every edge in graph is equal.

Returns:
  • whether edges are same or not.
Type
boolean

disjointSet() → {Array}

find the disjoint sets in graph.
In a disjoinset, all nodes in set can travel to others.

Returns:
  • an array of disjoint set node array. [[n1, n2, n3], [n4, n5], ...]
Type
Array

copy() → {UndirectedGraph}

return a copy of this object.

Returns:
Type
UndirectedGraph