JavaGenes, NASA Ames. Written largely by Al Globus

gov.nasa.javaGenes.graph
Class Graph

java.lang.Object
  extended bygov.nasa.javaGenes.core.Evolvable
      extended bygov.nasa.javaGenes.graph.Graph
All Implemented Interfaces:
java.lang.Cloneable, java.io.Serializable
Direct Known Subclasses:
DirectedGraph, Molecule

public class Graph
extends Evolvable

A information theoretic graph, in other words, a set of vertices and a set of edges. Each edge connects two vertices. This implementation supports genetic software with a crossover operator and a distance function that uses all-pairs-shortest-path.

See Also:
Vertice, Edge, apsp, Serialized Form

Nested Class Summary
protected static class Graph.TrailKey
          An object that efficiently represents a path for use in as a key Hashtables and KeyCounters.
 
Field Summary
protected  ExtendedVector edges
           
protected  KeyCounter nodeTypePairsShortestTrailCounts
          cache the results of all-pairs-shortest-path since this operation is compute intensive.
protected  ExtendedVector vertices
           
 
Fields inherited from class gov.nasa.javaGenes.core.Evolvable
childMakersUsed
 
Constructor Summary
Graph()
           
Graph(VertexAndEdgeProvider provider, int numberOfVertices, int numberOfCycles)
          Generates a random graph.
 
Method Summary
 void add(Edge e)
           
 void add(Graph g)
          add actual (not copies) vertices and edges of g to this.
 void add(Vertex v)
           
 boolean canMate(Graph graph)
          can I perform crossover with graph?
protected  void changed()
          Must be called when the graph changes.
 java.lang.Object clone()
          copy the graph and make copies of all edges and vertices
 Graph deepCopyGraph()
          copy the graph and make copies of all edges and vertices
 void delete(Edge e)
          Removes Edge e from graph, removes the vertices from e, and removes e from its vertices.
 double distanceFrom(Evolvable other)
          Subclasses will almost always override this.
 void executeOnEdges(Procedure p)
           
 void executeOnVertices(Procedure p)
          Execute Procedure p on all vertices
 Graph getConnectedSubgraph(Vertex v)
          Walks the graph starting at Vertex v adding copies of all edges and vertices to the new graph.
 int[][] getConnectionTable()
           
 Edge getEdge(int i)
           
 EdgeIterator getEdgeIterator()
           
 int getEdgesSize()
           
 Edge getEquivalent(Graph graph, Edge edge)
           
 Vertex getEquivalent(Graph graph, Vertex vertex)
           
 KeyCounter getExtendedVertexTypesCounter()
           
 KeyCounter getNodeTypePairsShortestTrailCounts()
           
 int getNumberOfCycles()
           
 Edge getRandomEdge()
           
 Edge getRandomEdge(Predicate p)
           
 Vertex getRandomVertex()
           
 Vertex getRandomVertex(Predicate p)
           
 int getSize()
           
 java.lang.String getStringKey(Vertex v1, Vertex v2, int count)
          Represents a path with a string.
 Trail getTrailBetween(Vertex v1, Vertex v2)
           
 Vertex getVertex(int i)
           
 VertexAndTrailIterator getVertexAndTrailIterator(Vertex v)
           
 VertexIterator getVertexIterator()
           
 int getVerticesSize()
           
 boolean hasVertexPairCloserThan(double distance)
           
 boolean isConnected()
           
 boolean isLegal()
           
 void numberVertices(int start)
          Number the vertices starting with int start.
 void numberVerticesReverse(int start)
           
 void remove(Edge e)
          Removes Edge e from graph.
 void remove(Vertex v)
          Removes Vertex v from graph.
 void removeLastNVertices(int n)
           
 void replaceEdge(Edge oldEdge, Edge newEdge)
           
 void replaceVertex(Vertex oldVertex, Vertex newVertex)
           
 void setAllMarks(boolean m)
          set marks on all edges and vertices
 void setEdgeMarks(boolean m)
           
 void setVertexMarks(boolean m)
           
 java.lang.Object shallowClone()
          copy the graph and don't make copies of all edges and vertices
 java.lang.String toString()
           
 
Methods inherited from class gov.nasa.javaGenes.core.Evolvable
addChildMakers, copyForEvolution, getChildMakersUsed, getSmallest, prepareForEvaluator, stateSave
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

vertices

protected ExtendedVector vertices

edges

protected ExtendedVector edges

nodeTypePairsShortestTrailCounts

protected KeyCounter nodeTypePairsShortestTrailCounts
cache the results of all-pairs-shortest-path since this operation is compute intensive.

Constructor Detail

Graph

public Graph()

Graph

public Graph(VertexAndEdgeProvider provider,
             int numberOfVertices,
             int numberOfCycles)
Generates a random graph. First creates a random spanning tree then randomly inserts numberOfCycles edges.

Parameters:
provider - will provide random edges and vertices for the graph
Method Detail

deepCopyGraph

public Graph deepCopyGraph()
copy the graph and make copies of all edges and vertices


clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
copy the graph and make copies of all edges and vertices

Throws:
java.lang.CloneNotSupportedException

shallowClone

public java.lang.Object shallowClone()
                              throws java.lang.CloneNotSupportedException
copy the graph and don't make copies of all edges and vertices

Throws:
java.lang.CloneNotSupportedException

toString

public java.lang.String toString()

hasVertexPairCloserThan

public boolean hasVertexPairCloserThan(double distance)

getConnectedSubgraph

public Graph getConnectedSubgraph(Vertex v)
Walks the graph starting at Vertex v adding copies of all edges and vertices to the new graph.

See Also:
Mark

getTrailBetween

public Trail getTrailBetween(Vertex v1,
                             Vertex v2)
Returns:
the shortest path between vertices v1 and v2.

getVertexAndTrailIterator

public VertexAndTrailIterator getVertexAndTrailIterator(Vertex v)
Returns:
a new iterator

distanceFrom

public double distanceFrom(Evolvable other)
Description copied from class: Evolvable
Subclasses will almost always override this.

Overrides:
distanceFrom in class Evolvable
Returns:
the Tanimoto distance between this and Evolvable other (which must be a graph). Uses all-pairs-shortest-path on extended vertex types for the distance measure. 0 is closest, 1 is farthest away.

getNodeTypePairsShortestTrailCounts

public KeyCounter getNodeTypePairsShortestTrailCounts()
Returns:
Uses all-pairs-shortest-path on extended vertex types for the distance measure.

getExtendedVertexTypesCounter

public KeyCounter getExtendedVertexTypesCounter()
Returns:
data structure containing counts of extended vertex types

getConnectionTable

public int[][] getConnectionTable()
Returns:
a connection table suitable for apsp. Assumes vertices are already numbered as desired.
See Also:
apsp

getStringKey

public java.lang.String getStringKey(Vertex v1,
                                     Vertex v2,
                                     int count)
Represents a path with a string. Not used due to inefficiency but useful for debugging.


isConnected

public boolean isConnected()
Returns:
true if the graph is completely connected (every pair of edges connected by at least one path)

canMate

public boolean canMate(Graph graph)
can I perform crossover with graph?


changed

protected void changed()
Must be called when the graph changes. Used to implement caching of data structures representing aspects of the graph that are compute intensive to create.


setAllMarks

public void setAllMarks(boolean m)
set marks on all edges and vertices

See Also:
Mark

add

public void add(Graph g)
add actual (not copies) vertices and edges of g to this. Caller can deepCopyGraph() before calling if desired


getSize

public int getSize()
Overrides:
getSize in class Evolvable
Returns:
number of vertices plus number of edges

getNumberOfCycles

public int getNumberOfCycles()

getVerticesSize

public int getVerticesSize()

add

public void add(Edge e)

remove

public void remove(Edge e)
Removes Edge e from graph.

See Also:
delete(gov.nasa.javaGenes.graph.Edge)

setEdgeMarks

public void setEdgeMarks(boolean m)
See Also:
Mark

getEdgeIterator

public EdgeIterator getEdgeIterator()

getRandomEdge

public Edge getRandomEdge()

getRandomEdge

public Edge getRandomEdge(Predicate p)
Returns:
a random edge that satisfies Predicate p

getEdge

public Edge getEdge(int i)
Returns:
the ith edge. Start counting at 0.

executeOnEdges

public void executeOnEdges(Procedure p)

getEquivalent

public Edge getEquivalent(Graph graph,
                          Edge edge)
Returns:
the equivalent to Edge edge in Graph graph. Graph graph must be isomorphic to this.

delete

public void delete(Edge e)
Removes Edge e from graph, removes the vertices from e, and removes e from its vertices.

See Also:
Edge.removeVertices()

replaceEdge

public void replaceEdge(Edge oldEdge,
                        Edge newEdge)

replaceVertex

public void replaceVertex(Vertex oldVertex,
                          Vertex newVertex)

getEdgesSize

public int getEdgesSize()

add

public void add(Vertex v)

remove

public void remove(Vertex v)
Removes Vertex v from graph.


removeLastNVertices

public void removeLastNVertices(int n)

setVertexMarks

public void setVertexMarks(boolean m)
See Also:
Mark

getVertexIterator

public VertexIterator getVertexIterator()

getRandomVertex

public Vertex getRandomVertex()

getRandomVertex

public Vertex getRandomVertex(Predicate p)
Returns:
random vertex that satisfies Predicate p

getVertex

public Vertex getVertex(int i)
Returns:
the ith vertex starting at 0

executeOnVertices

public void executeOnVertices(Procedure p)
Execute Procedure p on all vertices


getEquivalent

public Vertex getEquivalent(Graph graph,
                            Vertex vertex)
Returns:
the equivalent to Vertex vertex in Graph graph. Graph graph must be isomorphic to this. For better performance use vertex numbering.

numberVertices

public void numberVertices(int start)
Number the vertices starting with int start.

See Also:
Vertex.setNumber(int)

numberVerticesReverse

public void numberVerticesReverse(int start)

isLegal

public boolean isLegal()

JavaGenes, NASA Ames. Written largely by Al Globus