Graph
Class Graph
java.lang.Object
|
+--Graph.Graph
- public class Graph
- extends Object
Represents a graph...in this case, the connections between cities.
Includes static methods to test the class.
Could be generalized.
Implement the connectedComponents() method to find the connected components
of the graph.
Warning: You may find this challenging!
If you want more:
Implement the findPath method that returns a path between 2 vertices.
|
Constructor Summary |
Graph()
|
| Methods inherited from class java.lang.Object |
,
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
registerNatives,
wait,
wait,
wait |
adjacency
private Map adjacency
Graph
public Graph()
addVertex
public void addVertex(Vertex v)
- Adds a new vertex.
addEdge
public void addEdge(Vertex v1,
Vertex v2)
- Adds a new edge.
addAdjacency
private void addAdjacency(Vertex v1,
Vertex v2)
- Adds the edge.
connectedComponents
public List connectedComponents()
- You should implement the algorithm described in the notes
findPath
public Path findPath(Vertex v1,
Vertex v2)
- Returns a path from v1 to v2.
toString
public String toString()
- Convert to a String. The Graph is represented as its ajavency list.
- Overrides:
- toString in class Object
main
public static void main(String[] args)
- Tests the Graph class. Builds a graph, finds connected components,
and a path.
generateGraph
private static Graph generateGraph(List vertices)
- Builds a sample Graph object. Builds a graph, finds connected
components, and a path.