SimpleCxxLib
packageclass Graph<NodeType,ArcType>
NodeType
and ArcType
parameters indicate
the structure type or class used for nodes and arcs, respectively.
These types can contain any fields or methods required by the client,
but must contain the following fields required by the Graph
package itself:
The NodeType
definition must include:
string
field called name
Set<ArcType *>
field called arcs
The ArcType
definition must include:
NodeType *
field called start
NodeType *
field called finish
Constructor | |
Creates an empty Graph object. | |
Methods | |
addArc(n1, n2) addArc(arc) | Adds an arc to the graph. |
addNode(node) | Adds a node to the graph. |
Reinitializes the graph to be empty, freeing any heap storage. | |
getArcSet(node) getArcSet(name) | Returns the set of all arcs in the graph or, in the second and third forms, the arcs that start at the specified node, which can be indicated either as a pointer or by name. |
getNeighbors(name) | Returns the set of nodes that are neighbors of the specified node, which can be indicated either as a pointer or by name. |
Looks up a node in the name table attached to the graph and returns a pointer to that node. | |
Returns the set of all nodes in the graph. | |
isConnected(s1, s2) | Returns true if the graph contains an arc from n1 to n2 . |
Returns true if the graph is empty. | |
removeArc(n1, n2) removeArc(arc) | Removes an arc from the graph, where the arc can be specified in any of three ways: by the names of its endpoints, by the node pointers at its endpoints, or as an arc pointer. |
removeNode(node) | Removes a node from the graph, where the node can be specified either by its name or as a pointer value. |
Returns the number of nodes in the graph. | |
Converts the graph to a printable string representation. |
Graph();
Graph
object.
Usage:
Graph<NodeType,ArcType> g;
int size() const;
Usage:
int size = g.size();
bool isEmpty() const;
true
if the graph is empty.
Usage:
if (g.isEmpty()) ...
void clear();
Usage:
g.clear();
NodeType *addNode(string name); NodeType *addNode(NodeType *node);
Usage:
NodeType *node = g.addNode(name); NodeType *node = g.addNode(node);
void removeNode(string name); void removeNode(NodeType *node);
Usage:
g.removeNode(name); g.removeNode(node);
NodeType *getNode(string name) const;
getNode
returns NULL
.
Usage:
NodeType *node = g.getNode(name);
ArcType *addArc(string s1, string s2); ArcType *addArc(NodeType *n1, NodeType *n2); ArcType *addArc(ArcType *arc);
addArc
method. All three of these versions return a pointer to the arc in
case the client needs to capture this value.
Usage:
g.addArc(s1, s2); g.addArc(n1, n2); g.addArc(arc);
void removeArc(string s1, string s2); void removeArc(NodeType *n1, NodeType *n2); void removeArc(ArcType *arc);
Usage:
g.removeArc(s1, s2); g.removeArc(n1, n2); g.removeArc(arc);
bool isConnected(NodeType *n1, NodeType *n2) const; bool isConnected(string s1, string s2) const;
true
if the graph contains an arc from
n1
to n2
. As in the addArc
method, nodes can be specified either as node pointers or by name.
Usage:
if (g.isConnected(n1, n2)) ... if (g.isConnected(s1, s2)) ...
const Set<NodeType *> & getNodeSet() const;
Usage:
foreach (NodeType *node in g.getNodeSet()) ...
const Set<ArcType *> & getArcSet() const; const Set<ArcType *> & getArcSet(NodeType *node) const; const Set<ArcType *> & getArcSet(string name) const;
Usage:
foreach (ArcType *arc in g.getArcSet()) ... foreach (ArcType *arc in g.getArcSet(node)) ... foreach (ArcType *arc in g.getArcSet(name)) ...
const Set<NodeType *> getNeighbors(NodeType *node) const; const Set<NodeType *> getNeighbors(string node) const;
Usage:
foreach (NodeType *node in g.getNeighbors(node)) ... foreach (NodeType *node in g.getNeighbors(name)) ...
string toString();
Usage:
string str = g.toString();