Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Graph<E> {
- /* You must use the following maps in your implementation */
- private HashMap<String, HashMap<String, Integer>> adjacencyMap;
- private HashMap<String, E> dataMap;
- private class Node{
- int weight;
- String prev;
- private Node(String predecessor, int cost){
- this.prev = predecessor;
- this.weight = cost;
- }
- private void setPrev(String name, int cost){
- this.prev = name;
- this.weight = cost;
- }
- private int getWeight(){
- return this.weight;
- }
- private String getPrev(){
- return this.prev;
- }
- }
- public Graph(){
- this.adjacencyMap = new HashMap<String, HashMap<String, Integer>>();
- this.dataMap = new HashMap<String, E>();
- }
- public void addVertex(String vertexName, E data){
- if(this.adjacencyMap.containsKey(vertexName)){
- throw new IllegalArgumentException("Already a vertex");
- }
- this.adjacencyMap.put(vertexName, new HashMap<String, Integer>());
- this.dataMap.put(vertexName, data);
- }
- public void addDirectedEdge(String startVertexName, String endVertexName, int cost){
- if(this.adjacencyMap.containsKey(startVertexName) == false ||
- this.adjacencyMap.containsKey(endVertexName) == false){
- throw new IllegalArgumentException("Check if vertex is in the graph");
- }
- this.adjacencyMap.get(startVertexName).put(endVertexName, cost);
- }
- public Map<String, Integer> getAdjacentVertices(String vertexName){
- return this.adjacencyMap.get(vertexName);
- }
- public int getCost(String startVertex, String endVertex){
- return this.adjacencyMap.get(startVertex).get(endVertex);
- }
- public E getData(String vertex){
- if(this.adjacencyMap.containsKey(vertex) == false){
- throw new IllegalArgumentException("No vertex with that name");
- }
- return this.dataMap.get(vertex);
- }
- public Set<String> getVertices(){
- return this.adjacencyMap.keySet();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement