nct.graph.basic
Class BasicGraph<NodeType extends java.lang.Comparable<? super NodeType>,WeightType extends java.lang.Comparable<? super WeightType>>

java.lang.Object
  extended by nct.graph.basic.BasicGraph<NodeType,WeightType>
All Implemented Interfaces:
java.lang.Cloneable, java.lang.Comparable<Graph<NodeType,WeightType>>, Graph<NodeType,WeightType>
Direct Known Subclasses:
BasicDistanceGraph, BasicKPartiteGraph, CompatibilityGraph

public class BasicGraph<NodeType extends java.lang.Comparable<? super NodeType>,WeightType extends java.lang.Comparable<? super WeightType>>
extends java.lang.Object
implements Graph<NodeType,WeightType>, java.lang.Comparable<Graph<NodeType,WeightType>>, java.lang.Cloneable

A generic implementation of the Graph interface.


Field Summary
protected  boolean assumeGraphFinished
          Used to indicate whether the graph has been updated (ie node or edge added) since the last time a value based on the graph (eg numEdges) was calculated.
protected  java.util.Map<NodeType,java.util.Map<NodeType,java.lang.String>> descMap
          A mapping of nodes to nodes to edge descriptions.
protected  java.lang.String id
          The id of the graph.
protected  int numEdges
          The number of edges in the graph.
protected  WeightType score
          The score of the graph.
protected  java.util.Map<NodeType,java.util.Map<NodeType,WeightType>> weightMap
          A mapping of nodes to nodes to edge weights.
 
Constructor Summary
BasicGraph()
          Constructor.
BasicGraph(java.lang.String id)
          Constructor.
 
Method Summary
 boolean addEdge(NodeType nodeA, NodeType nodeB, WeightType weight)
          Adds an edge to the graph.
 boolean addEdge(NodeType nodeA, NodeType nodeB, WeightType weight, java.lang.String desc)
          Adds an edge to the graph.
 boolean addNode(NodeType node)
          Adds a node to the graph.
 BasicGraph<NodeType,WeightType> clone()
          Returns a clone of this graph.
 int compareTo(Graph<NodeType,WeightType> g)
          Returns the compareTo value of the score of this graph.
 int degreeOfNode(NodeType node)
          Returns the degree (number of neighbors) of the node.
 Edge<NodeType,WeightType> getEdge(NodeType nodeA, NodeType nodeB)
          Returns the specified edge.
 java.lang.String getEdgeDescription(NodeType nodeA, NodeType nodeB)
          Returns a description of the edge implied by the two specified nodes.
 java.util.Set<Edge<NodeType,WeightType>> getEdges()
          Returns a list of all edges in the graph.
 WeightType getEdgeWeight(NodeType nodeA, NodeType nodeB)
          Returns the weight of the edge implied by the two specified nodes.
 java.lang.String getId()
          Returns the id of the graph, in this case the filename of the input file that specifies the graph.
 java.util.Set<NodeType> getNeighbors(NodeType node)
          Returns a set all nodes adjacent to the specified node.
 java.util.Set<NodeType> getNodes()
          Returns a set containing all nodes in the graph.
 WeightType getScore()
          Returns the score of the graph.
 boolean isEdge(NodeType nodeA, NodeType nodeB)
          Indicates whether the specified nodes refer to a valid edge in the graph.
 boolean isNode(NodeType node)
          Indicates whether the specified node is contained in the graph.
 int numberOfEdges()
          Returns the number of edges in the graph.
 int numberOfNodes()
          Returns the number of nodes in the graph.
 boolean removeEdge(NodeType nodeA, NodeType nodeB)
          Removes the specified edge from the graph.
 boolean removeNode(NodeType node)
          Removes the specified node from the graph and all of its adjacent edges.
 boolean setEdgeDescription(NodeType nodeA, NodeType nodeB, java.lang.String desc)
          Sets the description for the specified edge.
 boolean setEdgeWeight(NodeType nodeA, NodeType nodeB, WeightType weight)
          Sets the edge weight of an existing edge.
 void setScore(WeightType score)
          Sets the score of the graph.
 java.lang.String toString()
          Returns a String representation of the graph format.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

weightMap

protected java.util.Map<NodeType extends java.lang.Comparable<? super NodeType>,java.util.Map<NodeType extends java.lang.Comparable<? super NodeType>,WeightType extends java.lang.Comparable<? super WeightType>>> weightMap
A mapping of nodes to nodes to edge weights.


descMap

protected java.util.Map<NodeType extends java.lang.Comparable<? super NodeType>,java.util.Map<NodeType extends java.lang.Comparable<? super NodeType>,java.lang.String>> descMap
A mapping of nodes to nodes to edge descriptions. This is not populated by default.


id

protected java.lang.String id
The id of the graph.


score

protected WeightType extends java.lang.Comparable<? super WeightType> score
The score of the graph.


assumeGraphFinished

protected boolean assumeGraphFinished
Used to indicate whether the graph has been updated (ie node or edge added) since the last time a value based on the graph (eg numEdges) was calculated.


numEdges

protected int numEdges
The number of edges in the graph.

Constructor Detail

BasicGraph

public BasicGraph()
Constructor.


BasicGraph

public BasicGraph(java.lang.String id)
Constructor.

Parameters:
id - The graph id.
Method Detail

addNode

public boolean addNode(NodeType node)
Adds a node to the graph.

Specified by:
addNode in interface Graph<NodeType extends java.lang.Comparable<? super NodeType>,WeightType extends java.lang.Comparable<? super WeightType>>
Parameters:
node - The node to add.
Returns:
true if node successfully added, false otherwise.

addEdge

public boolean addEdge(NodeType nodeA,
                       NodeType nodeB,
                       WeightType weight)
Adds an edge to the graph. Returns false if the nodes are bad or if the the nodes have not already been added to the graph.

Specified by:
addEdge in interface Graph<NodeType extends java.lang.Comparable<? super NodeType>,WeightType extends java.lang.Comparable<? super WeightType>>
Parameters:
nodeA - The beginning node of the edge to add.
nodeB - The ending node of the edge to add.
weight - The edge weight.
Returns:
true if edge successfully added, false otherwise.

addEdge

public boolean addEdge(NodeType nodeA,
                       NodeType nodeB,
                       WeightType weight,
                       java.lang.String desc)
Adds an edge to the graph. Returns false if the nodes are bad or if the the nodes have not already been added to the graph.

Specified by:
addEdge in interface Graph<NodeType extends java.lang.Comparable<? super NodeType>,WeightType extends java.lang.Comparable<? super WeightType>>
Parameters:
nodeA - The beginning node of the edge to add.
nodeB - The ending node of the edge to add.
weight - The edge weight.
desc - The edge description.
Returns:
true if edge successfully added, false otherwise.

isEdge

public boolean isEdge(NodeType nodeA,
                      NodeType nodeB)
Indicates whether the specified nodes refer to a valid edge in the graph.

Specified by:
isEdge in interface Graph<NodeType extends java.lang.Comparable<? super NodeType>,WeightType extends java.lang.Comparable<? super WeightType>>
Parameters:
nodeA - The beginning node of the edge to check.
nodeB - The ending node of the edge to check.
Returns:
true if edge exists, false otherwise.

isNode

public boolean isNode(NodeType node)
Indicates whether the specified node is contained in the graph.

Specified by:
isNode in interface Graph<NodeType extends java.lang.Comparable<? super NodeType>,WeightType extends java.lang.Comparable<? super WeightType>>
Parameters:
node - The node to check.
Returns:
true if node exists, false otherwise.

getEdgeWeight

public WeightType getEdgeWeight(NodeType nodeA,
                                NodeType nodeB)
Returns the weight of the edge implied by the two specified nodes.

Specified by:
getEdgeWeight in interface Graph<NodeType extends java.lang.Comparable<? super NodeType>,WeightType extends java.lang.Comparable<? super WeightType>>
Parameters:
nodeA - The source node of the edge.
nodeB - The target node of the edge.
Returns:
The weight of the edge.

getEdge

public Edge<NodeType,WeightType> getEdge(NodeType nodeA,
                                         NodeType nodeB)
Returns the specified edge. Returns null if the edge doesn't exist.

Specified by:
getEdge in interface Graph<NodeType extends java.lang.Comparable<? super NodeType>,WeightType extends java.lang.Comparable<? super WeightType>>
Parameters:
nodeA - The source node of the edge.
nodeB - The target node of the edge.
Returns:
The edge specified.

getNodes

public java.util.Set<NodeType> getNodes()
Returns a set containing all nodes in the graph.

Specified by:
getNodes in interface Graph<NodeType extends java.lang.Comparable<? super NodeType>,WeightType extends java.lang.Comparable<? super WeightType>>
Returns:
A set of all nodes in the graph.

getNeighbors

public java.util.Set<NodeType> getNeighbors(NodeType node)
Returns a set all nodes adjacent to the specified node.

Specified by:
getNeighbors in interface Graph<NodeType extends java.lang.Comparable<? super NodeType>,WeightType extends java.lang.Comparable<? super WeightType>>
Parameters:
node - The node whose neighbors we're requesting.
Returns:
A set of all neighbor nodes.

getId

public java.lang.String getId()
Returns the id of the graph, in this case the filename of the input file that specifies the graph.

Specified by:
getId in interface Graph<NodeType extends java.lang.Comparable<? super NodeType>,WeightType extends java.lang.Comparable<? super WeightType>>
Returns:
The id of the graph.

numberOfNodes

public int numberOfNodes()
Returns the number of nodes in the graph.

Specified by:
numberOfNodes in interface Graph<NodeType extends java.lang.Comparable<? super NodeType>,WeightType extends java.lang.Comparable<? super WeightType>>
Returns:
The number of nodes in the graph.

numberOfEdges

public int numberOfEdges()
Returns the number of edges in the graph.

Specified by:
numberOfEdges in interface Graph<NodeType extends java.lang.Comparable<? super NodeType>,WeightType extends java.lang.Comparable<? super WeightType>>
Returns:
The number of edges in the graph.

degreeOfNode

public int degreeOfNode(NodeType node)
Returns the degree (number of neighbors) of the node.

Specified by:
degreeOfNode in interface Graph<NodeType extends java.lang.Comparable<? super NodeType>,WeightType extends java.lang.Comparable<? super WeightType>>
Parameters:
node - The node whose degree we're requesting.
Returns:
The degree of the node.

getEdges

public java.util.Set<Edge<NodeType,WeightType>> getEdges()
Returns a list of all edges in the graph.

Specified by:
getEdges in interface Graph<NodeType extends java.lang.Comparable<? super NodeType>,WeightType extends java.lang.Comparable<? super WeightType>>
Returns:
A list of all edges in the graph.

compareTo

public int compareTo(Graph<NodeType,WeightType> g)
Returns the compareTo value of the score of this graph.

Specified by:
compareTo in interface java.lang.Comparable<Graph<NodeType extends java.lang.Comparable<? super NodeType>,WeightType extends java.lang.Comparable<? super WeightType>>>
Specified by:
compareTo in interface Graph<NodeType extends java.lang.Comparable<? super NodeType>,WeightType extends java.lang.Comparable<? super WeightType>>
Parameters:
g - Graph to compare against this one.
Returns:
The compareTo() value of the score of this graph.

setScore

public void setScore(WeightType score)
Sets the score of the graph.

Specified by:
setScore in interface Graph<NodeType extends java.lang.Comparable<? super NodeType>,WeightType extends java.lang.Comparable<? super WeightType>>
Parameters:
score - The score to set the graph to.

getScore

public WeightType getScore()
Returns the score of the graph.

Specified by:
getScore in interface Graph<NodeType extends java.lang.Comparable<? super NodeType>,WeightType extends java.lang.Comparable<? super WeightType>>
Returns:
The score of the graph.

clone

public BasicGraph<NodeType,WeightType> clone()
Returns a clone of this graph.

Specified by:
clone in interface Graph<NodeType extends java.lang.Comparable<? super NodeType>,WeightType extends java.lang.Comparable<? super WeightType>>
Overrides:
clone in class java.lang.Object
Returns:
A clone of this graph.

getEdgeDescription

public java.lang.String getEdgeDescription(NodeType nodeA,
                                           NodeType nodeB)
Returns a description of the edge implied by the two specified nodes. Because the edge description map is not populated by default, this method will return the edge weight as a description if the edge description has not been explicitly set.

Specified by:
getEdgeDescription in interface Graph<NodeType extends java.lang.Comparable<? super NodeType>,WeightType extends java.lang.Comparable<? super WeightType>>
Parameters:
nodeA - The beginning node of the edge.
nodeB - The ending node of the edge.
Returns:
The description of the edge.

setEdgeDescription

public boolean setEdgeDescription(NodeType nodeA,
                                  NodeType nodeB,
                                  java.lang.String desc)
Sets the description for the specified edge.

Specified by:
setEdgeDescription in interface Graph<NodeType extends java.lang.Comparable<? super NodeType>,WeightType extends java.lang.Comparable<? super WeightType>>
Parameters:
nodeA - The source node of the edge.
nodeB - The target node of the edge.
desc - The description of the edge.
Returns:
Whether or not the edge description has been set.

toString

public java.lang.String toString()
Description copied from interface: Graph
Returns a String representation of the graph format.

Specified by:
toString in interface Graph<NodeType extends java.lang.Comparable<? super NodeType>,WeightType extends java.lang.Comparable<? super WeightType>>
Overrides:
toString in class java.lang.Object
Returns:
A string providing some basic identifying information about the graph.

removeEdge

public boolean removeEdge(NodeType nodeA,
                          NodeType nodeB)
Removes the specified edge from the graph. The nodes connected to the edge will NOT be removed.

Specified by:
removeEdge in interface Graph<NodeType extends java.lang.Comparable<? super NodeType>,WeightType extends java.lang.Comparable<? super WeightType>>
Parameters:
nodeA - The beginning node of the edge to remove.
nodeB - The ending node of the edge to remove.
Returns:
Whether or not the edge was successfully removed.

removeNode

public boolean removeNode(NodeType node)
Removes the specified node from the graph and all of its adjacent edges.

Specified by:
removeNode in interface Graph<NodeType extends java.lang.Comparable<? super NodeType>,WeightType extends java.lang.Comparable<? super WeightType>>
Parameters:
node - The node to be removed.
Returns:
Whether or not the node was successfully removed.

setEdgeWeight

public boolean setEdgeWeight(NodeType nodeA,
                             NodeType nodeB,
                             WeightType weight)
Sets the edge weight of an existing edge.

Specified by:
setEdgeWeight in interface Graph<NodeType extends java.lang.Comparable<? super NodeType>,WeightType extends java.lang.Comparable<? super WeightType>>
Parameters:
nodeA - The source node of the edge.
nodeB - The target node of the edge.
weight - The weight of the edge.
Returns:
Whether or not the edge description has been set.