peersim.graph
Class ConstUndirGraph

java.lang.Object
  extended by peersim.graph.ConstUndirGraph
All Implemented Interfaces:
Graph
Direct Known Subclasses:
FastUndirGraph

public class ConstUndirGraph
extends java.lang.Object
implements Graph

This class is an adaptor making any Graph an undirected graph by making its edges bidirectional. The graph to be made undirected is passed to the constructor. Only the reference is stored. However, at construction time the incoming edges are stored for each node, so if the graph passed to the constructor changes over time then methods getNeighbours(int) and degree(int) become inconsistent (but only those). The upside of this inconvenience is that getNeighbours(int) will have constant time complexity.

See Also:
UndirectedGraph

Field Summary
protected  Graph g
           
protected  java.util.List<java.lang.Integer>[] in
           
 
Constructor Summary
ConstUndirGraph(Graph g)
          Initialization based on given graph.
 
Method Summary
 boolean clearEdge(int i, int j)
          not supported
 int degree(int i)
          Returns the degree of the given node.
 boolean directed()
          Returns true if the graph is directed otherwise false.
 java.lang.Object getEdge(int i, int j)
          If there is an (i,j) edge, returns that, otherwise if there is a (j,i) edge, returns that, otherwise returns null.
 java.util.Collection<java.lang.Integer> getNeighbours(int i)
          Uses sets as collection so does not support multiple edges now, even if the underlying directed graph does.
 java.lang.Object getNode(int i)
          Returns the node from the underlying graph
protected  void initGraph()
          Finds and stores incoming edges
 boolean isEdge(int i, int j)
          Returns true if there is a directed edge between node i and node j.
 boolean setEdge(int i, int j)
          not supported
 int size()
          The number of nodes in the graph.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

g

protected final Graph g

in

protected final java.util.List<java.lang.Integer>[] in
Constructor Detail

ConstUndirGraph

public ConstUndirGraph(Graph g)
Initialization based on given graph. Stores the graph and if necessary (if the graph is directed) searches for the incoming edges and stores them too. The given graph is stored by reference (not cloned) so it should not be modified while this object is in use.

Method Detail

initGraph

protected void initGraph()
Finds and stores incoming edges


isEdge

public boolean isEdge(int i,
                      int j)
Description copied from interface: Graph
Returns true if there is a directed edge between node i and node j.

Specified by:
isEdge in interface Graph

getNeighbours

public java.util.Collection<java.lang.Integer> getNeighbours(int i)
Uses sets as collection so does not support multiple edges now, even if the underlying directed graph does.

Specified by:
getNeighbours in interface Graph

getNode

public java.lang.Object getNode(int i)
Returns the node from the underlying graph

Specified by:
getNode in interface Graph

getEdge

public java.lang.Object getEdge(int i,
                                int j)
If there is an (i,j) edge, returns that, otherwise if there is a (j,i) edge, returns that, otherwise returns null.

Specified by:
getEdge in interface Graph

size

public int size()
Description copied from interface: Graph
The number of nodes in the graph.

Specified by:
size in interface Graph

directed

public boolean directed()
Description copied from interface: Graph
Returns true if the graph is directed otherwise false.

Specified by:
directed in interface Graph

setEdge

public boolean setEdge(int i,
                       int j)
not supported

Specified by:
setEdge in interface Graph

clearEdge

public boolean clearEdge(int i,
                         int j)
not supported

Specified by:
clearEdge in interface Graph

degree

public int degree(int i)
Description copied from interface: Graph
Returns the degree of the given node. If the graph is directed, returns out degree.

Specified by:
degree in interface Graph