|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||
java.lang.Object | +--DataStructures.WeightedGraph
A class implementing a weighted directed graph.
Graphs are created empty, for example:
WeightedGraph g = new WeightedGraph();after which weighted directed arcs can be added by
g.add(tail, head, weight)where tail is the source node of the arc, head is the destination node of the arc and weight is an integer.
Graph descriptions can be read from and written to files.
Nodes and arcs can be enumerated using an iterator, e.g.
for (Iterator i = g.nodes(); i.hasNext();) {
System.out.println(i.next());
}
The remove method is implemented for arcs but not for nodes.
The arcs iterator returns an object in the
WeightedGraph.Arc class.
WeightedGraph.Arc| Inner Class Summary | |
static class |
WeightedGraph.Arc
A public class returned by the arcs() iteration over arcs. |
protected class |
WeightedGraph.Node
|
protected class |
WeightedGraph.Tip
|
| Field Summary | |
protected LookupTable |
nodeMap
|
protected int |
version
|
| Constructor Summary | |
WeightedGraph()
Constructs an empty weighted graph |
|
| Method Summary | |
void |
add(java.lang.Object tail,
java.lang.Object head,
int weight)
Adds a weighted directed arc into the graph. |
java.util.Iterator |
arcs()
Iterates over the arcs in the graph. |
boolean |
contains(java.lang.Object label)
Tests whether a node is present in the graph. |
java.util.Iterator |
nodes()
Iterates over the nodes in the graph. |
int |
nrArcs()
Returns the number of arcs in the graph |
int |
nrNodes()
Returns the number of nodes in the graph |
void |
readGraphTxtFile(java.lang.String fileName)
Reads a graph description from the text file fileName. |
void |
writeGraphTxtFile(java.lang.String fileName)
Writes a graph description to the text file fileName. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
protected LookupTable nodeMap
protected int version
| Constructor Detail |
public WeightedGraph()
| Method Detail |
public boolean contains(java.lang.Object label)
label - the label of the node to be searched for.public int nrNodes()
public int nrArcs()
public void add(java.lang.Object tail,
java.lang.Object head,
int weight)
tail - the label of the source node,
head the label of the destination node and
weight the weight attached to the arc.public java.util.Iterator nodes()
java.util.NoSuchElementException - if the next
method is called when there is no next element.UnsupportedOperatonException - if the remove
method is called.public java.util.Iterator arcs()
WeightedGraph.Arcpublic void readGraphTxtFile(java.lang.String fileName)
Reads a graph description from the text file fileName. Entries read from the file are added to the graph.
File entries must be of form
T H W
where T and H are strings and W is an integer. Each entry
represents a weighted arc where T is the tail or source node,
H is the head or destination node and W is the weight.
All entries must be on separate lines. Empty lines are skipped.
public void writeGraphTxtFile(java.lang.String fileName)
Writes a graph description to the text file fileName.
Files are written in the same form in which readGraphTxtFile expects to read them.
Note: it is the applications's responsibility to protect files which ought not to be overwritten.
|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||