edu.stanford.nlp.graph
Class DirectedMultiGraph<V,E>

java.lang.Object
  extended by edu.stanford.nlp.graph.DirectedMultiGraph<V,E>
Type Parameters:
V - Type of vertices
E - Type of edges.
All Implemented Interfaces:
Graph<V,E>, Serializable

public class DirectedMultiGraph<V,E>
extends Object
implements Graph<V,E>

Simple graph library; this is directed for now. This class focuses on time efficiency rather than memory efficiency.

Author:
sonalg, John Bauer
See Also:
Serialized Form

Constructor Summary
DirectedMultiGraph()
           
 
Method Summary
 void add(V source, V dest, E data)
          adds vertices (if not already in the graph) and the edge between them
 boolean addVertex(V v)
          For adding a zero degree vertex
 void clear()
          clears the graph, removes all edges and nodes
 boolean containsVertex(V v)
           
 List<E> convertPath(List<V> nodes, boolean directionSensitive)
           
 Iterable<E> edgeIterable()
           
 Iterator<E> edgeIterator()
           
 boolean equals(Object that)
           
 List<E> getAllEdges()
           
 Set<V> getAllVertices()
           
 Set<V> getChildren(V vertex)
          for undirected graph, it is just the neighbors
 List<Set<V>> getConnectedComponents()
           
 List<E> getEdges(V source, V dest)
           
 List<E> getIncomingEdges(V v)
          for undirected graph, it is just the edges from the node
 int getInDegree(V vertex)
          for undirected graph, it should just be the degree
 Set<V> getNeighbors(V v)
          Gets both parents and children nodes
 int getNumEdges()
           
 int getNumVertices()
           
 int getOutDegree(V vertex)
           
 List<E> getOutgoingEdges(V v)
          for undirected graph, it is just the edges from the node
 Set<V> getParents(V vertex)
          for undirected graph, it is just the neighbors
 List<V> getShortestPath(V node1, V node2)
          direction insensitive (the paths can go "up" or through the parents)
 List<V> getShortestPath(V node1, V node2, boolean directionSensitive)
          can specify the direction sensitivity
 List<E> getShortestPathEdges(V node1, V node2)
           
 List<E> getShortestPathEdges(V node1, V node2, boolean directionSensitive)
           
 int hashCode()
          Be careful hashing these.
 Iterable<E> incomingEdgeIterable(V vertex)
           
 Iterator<E> incomingEdgeIterator(V vertex)
           
 boolean isEdge(V source, V dest)
          only checks if there is an edge from source to dest.
 boolean isEmpty()
          False if there are any vertices in the graph, true otherwise.
 boolean isNeighbor(V source, V dest)
           
 Iterable<E> outgoingEdgeIterable(V vertex)
           
 Iterator<E> outgoingEdgeIterator(V vertex)
           
 boolean removeEdge(V source, V dest, E data)
           
 boolean removeEdges(V source, V dest)
           
 boolean removeVertex(V vertex)
          remove a vertex (and its edges) from the graph.
 boolean removeVertices(Collection<V> vertices)
           
 void removeZeroDegreeNodes()
          Deletes nodes with zero incoming and zero outgoing edges
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DirectedMultiGraph

public DirectedMultiGraph()
Method Detail

hashCode

public int hashCode()
Be careful hashing these. They are mutable objects, and changing the object will throw off the hash code, messing up your hash table

Overrides:
hashCode in class Object

equals

public boolean equals(Object that)
Overrides:
equals in class Object

addVertex

public boolean addVertex(V v)
For adding a zero degree vertex

Specified by:
addVertex in interface Graph<V,E>
Parameters:
v -

add

public void add(V source,
                V dest,
                E data)
adds vertices (if not already in the graph) and the edge between them

Specified by:
add in interface Graph<V,E>
Parameters:
source -
dest -
data -

removeEdges

public boolean removeEdges(V source,
                           V dest)
Specified by:
removeEdges in interface Graph<V,E>

removeEdge

public boolean removeEdge(V source,
                          V dest,
                          E data)
Specified by:
removeEdge in interface Graph<V,E>

removeVertex

public boolean removeVertex(V vertex)
remove a vertex (and its edges) from the graph.

Specified by:
removeVertex in interface Graph<V,E>
Parameters:
vertex -
Returns:
true if successfully removes the node

removeVertices

public boolean removeVertices(Collection<V> vertices)
Specified by:
removeVertices in interface Graph<V,E>

getNumVertices

public int getNumVertices()
Specified by:
getNumVertices in interface Graph<V,E>

getOutgoingEdges

public List<E> getOutgoingEdges(V v)
Description copied from interface: Graph
for undirected graph, it is just the edges from the node

Specified by:
getOutgoingEdges in interface Graph<V,E>

getIncomingEdges

public List<E> getIncomingEdges(V v)
Description copied from interface: Graph
for undirected graph, it is just the edges from the node

Specified by:
getIncomingEdges in interface Graph<V,E>

getNumEdges

public int getNumEdges()
Specified by:
getNumEdges in interface Graph<V,E>

getParents

public Set<V> getParents(V vertex)
Description copied from interface: Graph
for undirected graph, it is just the neighbors

Specified by:
getParents in interface Graph<V,E>

getChildren

public Set<V> getChildren(V vertex)
Description copied from interface: Graph
for undirected graph, it is just the neighbors

Specified by:
getChildren in interface Graph<V,E>

getNeighbors

public Set<V> getNeighbors(V v)
Gets both parents and children nodes

Specified by:
getNeighbors in interface Graph<V,E>
Parameters:
v -

clear

public void clear()
clears the graph, removes all edges and nodes

Specified by:
clear in interface Graph<V,E>

containsVertex

public boolean containsVertex(V v)
Specified by:
containsVertex in interface Graph<V,E>

isEdge

public boolean isEdge(V source,
                      V dest)
only checks if there is an edge from source to dest. To check if it is connected in either direction, use isNeighbor

Specified by:
isEdge in interface Graph<V,E>
Parameters:
source -
dest -

isNeighbor

public boolean isNeighbor(V source,
                          V dest)
Specified by:
isNeighbor in interface Graph<V,E>

getAllVertices

public Set<V> getAllVertices()
Specified by:
getAllVertices in interface Graph<V,E>

getAllEdges

public List<E> getAllEdges()
Specified by:
getAllEdges in interface Graph<V,E>

isEmpty

public boolean isEmpty()
False if there are any vertices in the graph, true otherwise. Does not care about the number of edges.

Specified by:
isEmpty in interface Graph<V,E>

removeZeroDegreeNodes

public void removeZeroDegreeNodes()
Deletes nodes with zero incoming and zero outgoing edges

Specified by:
removeZeroDegreeNodes in interface Graph<V,E>

getEdges

public List<E> getEdges(V source,
                        V dest)
Specified by:
getEdges in interface Graph<V,E>

getShortestPath

public List<V> getShortestPath(V node1,
                               V node2)
direction insensitive (the paths can go "up" or through the parents)


getShortestPathEdges

public List<E> getShortestPathEdges(V node1,
                                    V node2)

getShortestPath

public List<V> getShortestPath(V node1,
                               V node2,
                               boolean directionSensitive)
can specify the direction sensitivity

Parameters:
node1 -
node2 -
directionSensitive - - whether the path can go through the parents
Returns:
the list of nodes you get through to get there

getShortestPathEdges

public List<E> getShortestPathEdges(V node1,
                                    V node2,
                                    boolean directionSensitive)

convertPath

public List<E> convertPath(List<V> nodes,
                           boolean directionSensitive)

getInDegree

public int getInDegree(V vertex)
Description copied from interface: Graph
for undirected graph, it should just be the degree

Specified by:
getInDegree in interface Graph<V,E>

getOutDegree

public int getOutDegree(V vertex)
Specified by:
getOutDegree in interface Graph<V,E>

getConnectedComponents

public List<Set<V>> getConnectedComponents()
Specified by:
getConnectedComponents in interface Graph<V,E>

incomingEdgeIterator

public Iterator<E> incomingEdgeIterator(V vertex)

incomingEdgeIterable

public Iterable<E> incomingEdgeIterable(V vertex)

outgoingEdgeIterator

public Iterator<E> outgoingEdgeIterator(V vertex)

outgoingEdgeIterable

public Iterable<E> outgoingEdgeIterable(V vertex)

edgeIterator

public Iterator<E> edgeIterator()

edgeIterable

public Iterable<E> edgeIterable()

toString

public String toString()
Overrides:
toString in class Object


Stanford NLP Group