nick.graph
Class AdjacencyListGraph<E>
java.lang.Object
nick.graph.AdjacencyListGraph<E>
- Type Parameters:
E
- The type for this class would typically be numerical (Integer or
Double). In some applications it might be some other class, but it
is important that those classes override both the hashCode() and
equals() methods.
- All Implemented Interfaces:
- GraphInterface<E>
public class AdjacencyListGraph<E>
- extends java.lang.Object
- implements GraphInterface<E>
Let G = (V, E) be a simple weighted graph. An adjacency list is implemented
by keeping track of list of vertices. For each vertex we keep a list of
adjacent vertices. Because we are modeling a simple graph, self edges and
parallel edges are not allowed. The graph may be connected or disconnected.
See below graph for a basic example:
Adjacency list:
A adjacent to B (2), C (6)
B adjacent to A (2), C (4)
C adjacent to A (6), B (4)
Here is the source code to create the above graph:
AdjacencyListGraph<Integer> alg = new AdjacencyListGraph<Integer>();
alg.addVertex("A");
alg.addVertex("B");
alg.addVertex("C");
alg.addEdge("A", "B", 2);
alg.addEdge("A", "C", 6);
alg.addEdge("B", "C", 4);
- Version:
- March 25, 2009
- Author:
- Nick Aschenbach
Method Summary |
void |
addEdge(java.lang.String vertex1,
java.lang.String vertex2,
E edge_value)
|
void |
addEdge(java.lang.String vertex1,
java.lang.String vertex2,
E edge_value,
java.lang.String edge_name)
|
void |
addVertex(java.lang.String vertex_name)
|
void |
addVertex(java.lang.String vertex_name,
E vertex_value)
|
java.util.Set<E> |
edges()
|
java.lang.String |
getAVertex()
|
java.util.Set<E> |
getEdges(java.lang.String vertex)
|
java.util.Set<java.lang.String> |
getNeighbors(java.lang.String vertex)
|
java.util.Set<java.lang.String> |
vertices()
|
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
AdjacencyListGraph
public AdjacencyListGraph()
addVertex
public void addVertex(java.lang.String vertex_name)
- Specified by:
addVertex
in interface GraphInterface<E>
addVertex
public void addVertex(java.lang.String vertex_name,
E vertex_value)
- Specified by:
addVertex
in interface GraphInterface<E>
addEdge
public void addEdge(java.lang.String vertex1,
java.lang.String vertex2,
E edge_value)
- Specified by:
addEdge
in interface GraphInterface<E>
addEdge
public void addEdge(java.lang.String vertex1,
java.lang.String vertex2,
E edge_value,
java.lang.String edge_name)
- Specified by:
addEdge
in interface GraphInterface<E>
vertices
public java.util.Set<java.lang.String> vertices()
- Specified by:
vertices
in interface GraphInterface<E>
edges
public java.util.Set<E> edges()
- Specified by:
edges
in interface GraphInterface<E>
getNeighbors
public java.util.Set<java.lang.String> getNeighbors(java.lang.String vertex)
- Specified by:
getNeighbors
in interface GraphInterface<E>
getEdges
public java.util.Set<E> getEdges(java.lang.String vertex)
- Specified by:
getEdges
in interface GraphInterface<E>
getAVertex
public java.lang.String getAVertex()
- Specified by:
getAVertex
in interface GraphInterface<E>