|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnick.graph.AdjacencyMatrixGraph<E>
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.public class AdjacencyMatrixGraph<E>
Let G = (V, E) be a simple weighted graph. An adjacency matrix is implemented by this class using a symmetric |V| x |V| matrix. A non-zero or non-null value represents an edge with given weight from an from vertex at a given rowi and columnj . Because we are modeling a simple graph, self edges and parallel edges are not allowed. User specified matrices must be symmetric with regards to their dimensions and information. A minimal amount of information is kept about the graph. Edge names and Vertex values are ignored by this graph representation. The graph may be connected or disconnected.
See below graph for a basic example:
Adjacency matrix:
A B C A 0 2 6 B 2 0 4 C 6 4 0
Here is the source code to create the above graph:
Integer[][] matrix = new Integer[3][3];
AdjacencyMatrixGraph<Integer> amg = new AdjacencyMatrixGraph<Integer>(matrix);
amg.addVertex("A");
amg.addVertex("B");
amg.addVertex("C");
amg.addEdge("A", "B", 2);
amg.addEdge("A", "C", 6);
amg.addEdge("B", "C", 4);
This implementation requires a user to specify how many vertices are in the graph upon instantiation. Note that vertices must be added before edges can be added.
The advantage to using a matrix as the backing store are that you get constant O(1) look up time to see if an edge exists between two vertices. This implementation is more space efficient when the graph is dense (D > 0.5). For sparse graphs (D < 0.5) it is more space efficient to use a adjacency list implementation.
For undirected, simple graphs the graph density can be defined:
D = 2 * |E| / (|V| * |V| - 1)
This evaluates to 0.0 for graphs with no edges and 1.0 for complete graphs (Kn).
Constructor Summary | |
---|---|
AdjacencyMatrixGraph(E[][] the_matrix)
Construct this object with a given class and size. |
Method Summary | |
---|---|
void |
addEdge(java.lang.String vertex1,
java.lang.String vertex2,
E edge_value)
Add an edge on the graph. |
void |
addEdge(java.lang.String vertex1,
java.lang.String vertex2,
E edge_value,
java.lang.String edge_name)
Add an edge on the graph. |
void |
addVertex(java.lang.String vertex_name)
Add a vertex with a specified name. |
void |
addVertex(java.lang.String vertex_name,
java.lang.Object vertex_value)
Add a vertex with a specified name. |
java.util.HashSet |
edges()
|
java.lang.String |
getAVertex()
|
java.util.HashSet |
getEdges(java.lang.String vertex)
Get all edges adjacent to a specified vertex. |
java.util.HashSet |
getNeighbors(java.lang.String vertex)
|
E[][] |
graph()
|
java.lang.String |
toString()
|
java.util.Set |
vertices()
|
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public AdjacencyMatrixGraph(E[][] the_matrix)
the_matrix
- Create an n x n matrix with given size.Method Detail |
---|
public void addEdge(java.lang.String vertex1, java.lang.String vertex2, E edge_value)
addEdge
in interface GraphInterface<E>
vertex1
- The first edge to connect.vertex2
- The second edge to connect.edge_value
- The value to assign to this edge.public void addEdge(java.lang.String vertex1, java.lang.String vertex2, E edge_value, java.lang.String edge_name)
addEdge
in interface GraphInterface<E>
vertex1
- The first edge to connect.vertex2
- The second edge to connect.edge_value
- The value to assign to this edge.edge_name
- The name to assign to this edge (ignored).public void addVertex(java.lang.String vertex_name)
addVertex
in interface GraphInterface<E>
vertex_name
- The name to use for this vertex.public void addVertex(java.lang.String vertex_name, java.lang.Object vertex_value)
addVertex
in interface GraphInterface<E>
vertex_name
- The name to use for this vertex.vertex_value
- The value that is ignored by this class.public java.util.HashSet edges()
edges
in interface GraphInterface<E>
public java.util.HashSet getEdges(java.lang.String vertex)
getEdges
in interface GraphInterface<E>
vertex
- The vertex to check.
public java.util.HashSet getNeighbors(java.lang.String vertex)
getNeighbors
in interface GraphInterface<E>
public java.util.Set vertices()
vertices
in interface GraphInterface<E>
public E[][] graph()
public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String getAVertex()
getAVertex
in interface GraphInterface<E>
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |