A weighted graph

A graph is composed of GraphNode and GraphArc objects.

Example:

var o = new polygonal.ds.Graph<String>();
var a = o.addNode(o.createNode("a"));
var b = o.addNode(o.createNode("b"));
var c = o.addNode(o.createNode("c"));
o.addSingleArc(a, b);
o.addSingleArc(b, a);
o.addMutualArc(a, c);
trace(o); //outputs:

[ Graph size=3
  c -> a
  b -> a
  a -> c,b
]

Constructor

new ()

Variables

@:value(false)autoClearMarks:Bool = false

If true, automatically clears the mark-flag on all graph nodes prior to starting a new traversal.

Default is false.

@:value(HashKey.next())read onlykey:Int = HashKey.next()

A unique identifier for this object.

A hash table transforms this key into an index of an array element by using a hash function.

@:value(false)reuseIterator:Bool = false

If true, reuses the iterator object instead of allocating a new one when calling this.iterator().

The default is false.

If this value is true, nested iterations will fail as only one iteration is allowed at a time.

read onlysize:Int

The total number of elements in this graph.

Equals the number of graph nodes.

borrowArc:GraphNode<T> ‑> Float ‑> GraphArc<T>

If specified, this.borrowArc() is called in order to create GraphArc objects.

Useful for pooling GraphArc objects.

Default is null.

returnArc:GraphArc<T> ‑> Void

A function pointer responsible for returning GraphArc objects.

Required if this.borrowArc is specified.

Default is null.

Methods

add (val:T):GraphNode<T>

Wraps val in a GraphNode object and adds the newly created node to this graph.

Shortcut for:

var node = new GraphNode<String>("value");
myGraph.addNode(node);

Returns:

the GraphNode object storing val.

addMutualArc (source:GraphNode<T>, target:GraphNode<T>):Graph<T>

Creates an uni-directional link between two nodes. This creates two arcs; an arc pointing from source to target and vv.

The newly created arcs can be accessed via source.arcList (pointing to target) and target.arcList (pointing to source).

addNode (node:GraphNode<T>):GraphNode<T>

Adds the node node to this graph and returns node.

Silently fails if the node was already added to this graph.

addSingleArc (source:GraphNode<T>, target:GraphNode<T>):Graph<T>

Creates an uni-directional link between two nodes. This creates an arc pointing from source to target.

The newly created arc can be accessed via source.arcList.

arcIterator ():Itr<GraphArc<T>>

Returns a new GraphArcIterator object to iterate over all GraphArc objects in this graph.

The arcs are visited in a random order.

See:

@:value({ userData : null, process : null, seed : null, preflight : false })bfs (preflight:Bool = false, ?seed:GraphNode<T>, ?process:GraphNode<T> ‑> Bool ‑> Dynamic ‑> Bool, ?userData:Dynamic):Graph<T>

Performs an iterative breadth-first search (BFS).

Parameters:

preflight

if true, an extra traversal is performed before the actual traversal runs. The first pass visits all elements and calls element.visit() with the preflight parameter set to true. In this pass the return value determines whether the element will be processed (true) or excluded (false) from the final traversal, which is the second pass (preflight parameter set to false). The same applies when using a process function.

seed

the starting point of the traversal. If omitted, the first node in the list of graph nodes is used.

process

a function that is invoked for every traversed node.
The parameters are:

  • a reference to the visited node.
  • the preflight flag.
  • custom data specified by the userData parameter (default is null).
Once process returns false, the traversal stops immediately and no further nodes are examined (termination condition). If omitted, element.visit() is used.
In this case the elements of all nodes have to implement Visitable.

userData

custom data that is passed to every visited node via process or element.visit(). If omitted, null is used.

@:value({ gc : false })clear (gc:Bool = false):Void

Removes all elements.

Parameters:

gc

if true, explicitly nullifies nodes and elements upon removal so the garbage collector can reclaim used memory.

clearMarks ():Graph<T>

Clears the mark-flag on all graph nodes that were set in a BFS/DFS traversal.

Call this method to start a fresh traversal.

clearParent ():Graph<T>

Clears the parent pointers on all graph nodes.

@:value({ copier : null, byRef : true })clone (byRef:Bool = true, ?copier:T ‑> T):Collection<T>

Creates and returns a shallow copy (structure only - default) or deep copy (structure & elements) of this graph.

If byRef is true, primitive elements are copied by value whereas objects are copied by reference.

If byRef is false, the copier function is used for copying elements. If omitted, clone() is called on each element assuming all elements implement Cloneable.

contains (val:T):Bool

Returns true if this graph contains a node storing val.

@:value({ recursive : false, userData : null, process : null, seed : null, preflight : false })dfs (preflight:Bool = false, ?seed:GraphNode<T>, ?process:GraphNode<T> ‑> Bool ‑> Dynamic ‑> Bool, ?userData:Dynamic, recursive:Bool = false):Graph<T>

Performs an iterative depth-first search (DFS).

Parameters:

preflight

if true, an extra traversal is performed before the actual traversal runs. The first pass visits all elements and calls element.visit() with the preflight parameter set to true. In this pass the return value determines whether the element will be processed (true) or excluded (false) from the final traversal, which is the second pass (preflight parameter set to false). The same applies when using a process function.

seed

the starting point of the traversal. If omitted, the first node in the list of graph nodes is used.

process

a function that is invoked for every traversed node.
The parameters are:

  • a reference to the visited node.
  • the preflight flag.
  • custom data specified by the userData parameter (default is null).
Once process returns false, the traversal stops immediately and no further nodes are examined (termination condition). If omitted, element.visit() is used.
In this case the elements of all nodes have to implement Visitable.

userData

custom data that is passed to every visited node via process or element.visit(). If omitted, null is used.

recursive

if true, performs a recursive traversal (default traversal style is iterative).

@:value({ userData : null, process : null, seed : null, preflight : false })dlbfs (maxDepth:Int, preflight:Bool = false, ?seed:GraphNode<T>, ?process:GraphNode<T> ‑> Bool ‑> Dynamic ‑> Bool, ?userData:Dynamic):Graph<T>

Performs an iterative depth-limited breadth-first search (DLBFS).

Parameters:

maxDepth

a maxDepth value of 1 means that only all direct neighbors of seed are visited.

preflight

if true, an extra traversal is performed before the actual traversal runs. The first pass visits all elements and calls element.visit() with the preflight parameter set to true. In this pass the return value determines whether the element will be processed (true) or excluded (false) from the final traversal, which is the second pass (preflight parameter set to false). The same applies when using a process function.

seed

the starting point of the traversal. If omitted, the first node in the list of graph nodes is used.

process

a function that is invoked for every traversed node. The parameters are:

  • a reference to the visited node.
  • the preflight flag.
  • custom data specified by the userData parameter (default is null).
Once process returns false, the traversal stops immediately and no further nodes are examined (termination condition). If omitted, element.visit() is used. In this case the elements of all nodes have to implement Visitable.

userData

custom data that is passed to every visited node via process or element.visit(). If omitted, null is used.

findNode (val:T):GraphNode<T>

Finds and returns the node storing val or null if such a node does not exist.

free ():Void

Destroys this object by explicitly nullifying all nodes, elements and pointers for GC'ing used resources.

Improves GC efficiency/performance (optional).

inlinegetNodeList ():GraphNode<T>

The graph nodes stored as a doubly linked list of GraphNode objects.

Returns:

the first node in a list of GraphNode objects or null if the graph is empty.

inlineisEmpty ():Bool

Returns true only if this.size is 0.

inlineiter (f:T ‑> Void):Graph<T>

Calls 'f` on all elements in preorder.

iterator ():Itr<T>

Returns a new GraphIterator object to iterate over all elements stored in the graph nodes of this graph.

The nodes are visited in a random order.

See:

nodeIterator ():Itr<GraphNode<T>>

Returns a new GraphNodeIterator object to iterate over all GraphNode objects in this graph.

The nodes are visited in a random order.

See:

remove (val:T):Bool

Removes all nodes storing val.

Nodes and elements are nullified.

Returns:

true if at least one node storing val was removed.

removeNode (node:GraphNode<T>):Graph<T>

Removes node from this graph.

This clears all outgoing and incoming arcs and removes node from the node list. Silently fails if node was already removed from this graph.

serialize (getVal:T ‑> Dynamic):{vals:Array<Dynamic>, arcs:Array<Int>}

Serializes the graph, outputting two arrays: the first one stores all node values, while the second one contains a list of indices describing how the nodes are connected via arcs.

Example:

class Element {
    public var name:String;
    public function new(name:String) {
        this.name = name;
    }
}

...

var graph = new Graph<Element>();
var a = graph.createNode(new Element("a"));
var b = graph.createNode(new Element("b"));
var c = graph.createNode(new Element("c"));
graph.addNode(a);
graph.addNode(b);
graph.addNode(c);
graph.addMutualArc(a, b);
graph.addMutualArc(b, c);
graph.addMutualArc(a, c);

//serialize
var data = graph.serialize(function(nodeValue:Element) return nodeValue.name); //only store name property
trace(data.arcs); //[0,2,0,1,1,0,1,2,2,0,2,1]
trace(data.vals); //["c","b","a"]

//unserialize
var graph = new Graph<Element>();
graph.unserialize(data, function(val:String) return new Element(val));

toArray ():Array<T>

Returns an unordered array containing all elements stored in the graph nodes of this graph.

toString ():String

Prints out all elements.

unlink (node:GraphNode<T>):GraphNode<T>

Isolates node from this graph by unlinking it from all outgoing and incoming arcs.

The size remains unchanged as the node is not removed from the graph. Silently fails if node was not added to this graph.

Returns:

the disconnected graph node.

unserialize (data:{vals:Array<Dynamic>, arcs:Array<Int>}, setVal:Dynamic ‑> T):Void

See this.serialize.