Graph

A linked uni-directional weighted graph structure.

The Graph class manages all graph nodes.  Each graph node has an array of arcs, pointing to different nodes.

Summary
GraphA linked uni-directional weighted graph structure.
Variables
nodesThe graph’s nodes.
Functions and Properties
GraphCreates an empty graph.
maxSizeThe number of nodes the graph can store.
depthFirstPerforms an iterative depth-first traversal starting at a given node.
breadthFirstPerforms a breadth-first traversal starting at a given node.
addNodeAdds a node at a given index to the graph.
removeNodeRemoves a node from the graph at a given index.
getArcFinds an arc pointing to the node at the ‘from’ index to the node at the ‘to’ index.
addArcAdds an arc pointing to the node located at the ‘from’ index to the node at the ‘to’ index.
removeArcRemoves an arc pointing to the node located at the ‘from’ index to the node at the ‘to’ index.
clearMarksMarks are used to keep track of the nodes that have been visited during a depth-first or breadth-first traversal.
contains@inheritDoc
clear@inheritDoc
getIterator@inheritDoc
size@inheritDoc
isEmpty@inheritDoc
toArray@inheritDoc

Variables

nodes

public var nodes: Array

The graph’s nodes.

Functions and Properties

Graph

public function Graph(size: int)

Creates an empty graph.

Parameters

sizeThe total number of nodes the graph can hold. 

maxSize

public function get maxSize():int

The number of nodes the graph can store.

depthFirst

public function depthFirst(node: GraphNode,
visit: Function):void

Performs an iterative depth-first traversal starting at a given node.  @example The following code shows an example callback function.  The graph traversal runs until the value ‘5’ is found in the data property of a node instance.  <listing version=”3.0”> var visitNode:Function = function(node:GraphNode):Boolean { if (node.data == 5) return false; //terminate traversal return true; } myGraph.depthFirst(graph.nodes[0], visitNode); </listing>

Parameters

nodeThe graph node at which the traversal starts.
visitA callback function which is invoked every time a node is visited.  The visited node is accessible through the function’s first argument.  You can terminate the traversal by returning false in the callback function. 

breadthFirst

public function breadthFirst(node: GraphNode,
visit: Function):void

Performs a breadth-first traversal starting at a given node.  @example The following code shows an example callback function.  The graph traversal runs until the value ‘5’ is found in the data property of a node instance.  <listing version=”3.0”> var visitNode:Function = function(node:GraphNode):Boolean { if (node.data == 5) return false; //terminate traversal return true; } myGraph.breadthFirst(graph.nodes[0], visitNode); </listing>

Parameters

nodeThe graph node at which the traversal starts.
visitA callback function which is invoked every time a node is visited.  The visited node is accessible through the function’s first argument.  You can terminate the traversal by returning false in the callback function. 

addNode

public function addNode(obj: *,
i: int):Boolean

Adds a node at a given index to the graph.

Parameters

objThe data to store in the node.
iThe index the node is stored at. 

Returns

True if a node was added, otherwise false. 

removeNode

public function removeNode(i: int):Boolean

Removes a node from the graph at a given index.

Parameters

indexThe node’s index. 

Returns

True if removal was successful, otherwise false. 

getArc

public function getArc(from: int,
to: int):GraphArc

Finds an arc pointing to the node at the ‘from’ index to the node at the ‘to’ index.

Parameters

fromThe originating graph node index.
toThe ending graph node index. 

Returns

A GraphArc object or null if it doesn’t exist. 

addArc

public function addArc(from: int,  
to: int,  
weight: int = 1):Boolean

Adds an arc pointing to the node located at the ‘from’ index to the node at the ‘to’ index.

Parameters

fromThe originating graph node index.
toThe ending graph node index.
weightThe arc’s weight

Returns

True if the arc was added, otherwise false. 

removeArc

public function removeArc(from: int,
to: int):Boolean

Removes an arc pointing to the node located at the ‘from’ index to the node at the ‘to’ index.

Parameters

fromThe originating graph node index.
toThe ending graph node index. 

Returns

True if the arc was removed, otherwise false. 

clearMarks

public function clearMarks():void

Marks are used to keep track of the nodes that have been visited during a depth-first or breadth-first traversal.  You must call this method to clear all markers before starting a new traversal.

contains

public function contains(obj: *):Boolean

@inheritDoc

clear

public function clear():void

@inheritDoc

getIterator

public function getIterator():Iterator

@inheritDoc

size

public function get size():int

@inheritDoc

isEmpty

public function isEmpty():Boolean

@inheritDoc

toArray

public function toArray():Array

@inheritDoc

A ‘java-style’ collection interface.
public var nodes: Array
The graph’s nodes.
public function Graph(size: int)
Creates an empty graph.
public function get maxSize():int
The number of nodes the graph can store.
public function depthFirst(node: GraphNode,
visit: Function):void
Performs an iterative depth-first traversal starting at a given node.
public function breadthFirst(node: GraphNode,
visit: Function):void
Performs a breadth-first traversal starting at a given node.
public function addNode(obj: *,
i: int):Boolean
Adds a node at a given index to the graph.
public function removeNode(i: int):Boolean
Removes a node from the graph at a given index.
public function getArc(from: int,
to: int):GraphArc
Finds an arc pointing to the node at the ‘from’ index to the node at the ‘to’ index.
public function addArc(from: int,  
to: int,  
weight: int = 1):Boolean
Adds an arc pointing to the node located at the ‘from’ index to the node at the ‘to’ index.
public function removeArc(from: int,
to: int):Boolean
Removes an arc pointing to the node located at the ‘from’ index to the node at the ‘to’ index.
public function clearMarks():void
Marks are used to keep track of the nodes that have been visited during a depth-first or breadth-first traversal.
public function contains(obj: *):Boolean
@inheritDoc
public function clear():void
@inheritDoc
public function getIterator():Iterator
@inheritDoc
public function get size():int
@inheritDoc
public function isEmpty():Boolean
@inheritDoc
public function toArray():Array
@inheritDoc
Close