Advertisement
Guest User

Untitled

a guest
Nov 27th, 2015
578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.93 KB | None | 0 0
  1. public class Graph<E> {
  2.     /* You must use the following maps in your implementation */
  3.     private HashMap<String, HashMap<String, Integer>> adjacencyMap;
  4.     private HashMap<String, E> dataMap;
  5.    
  6.     private class Node{
  7.         int weight;
  8.         String prev;
  9.        
  10.         private Node(String predecessor, int cost){
  11.             this.prev = predecessor;
  12.             this.weight = cost;
  13.         }
  14.        
  15.         private void setPrev(String name, int cost){
  16.             this.prev = name;
  17.             this.weight = cost;
  18.         }
  19.        
  20.         private int getWeight(){
  21.             return this.weight;
  22.            
  23.         }
  24.        
  25.         private String getPrev(){
  26.             return this.prev;
  27.         }
  28.     }
  29.    
  30.    
  31.     public Graph(){
  32.         this.adjacencyMap = new HashMap<String, HashMap<String, Integer>>();
  33.         this.dataMap = new HashMap<String, E>();
  34.        
  35.        
  36.     }
  37.    
  38.     public void addVertex(String vertexName, E data){
  39.        
  40.         if(this.adjacencyMap.containsKey(vertexName)){
  41.             throw new IllegalArgumentException("Already a vertex");
  42.            
  43.         }
  44.        
  45.         this.adjacencyMap.put(vertexName, new HashMap<String, Integer>());
  46.         this.dataMap.put(vertexName, data);
  47.     }
  48.    
  49.     public void addDirectedEdge(String startVertexName, String endVertexName, int cost){
  50.         if(this.adjacencyMap.containsKey(startVertexName) == false ||
  51.                 this.adjacencyMap.containsKey(endVertexName) == false){
  52.            
  53.             throw new IllegalArgumentException("Check if vertex is in the graph");
  54.         }
  55.        
  56.         this.adjacencyMap.get(startVertexName).put(endVertexName, cost);
  57.     }
  58.    
  59.     public Map<String, Integer> getAdjacentVertices(String vertexName){
  60.         return this.adjacencyMap.get(vertexName);
  61.        
  62.     }
  63.    
  64.     public int getCost(String startVertex, String endVertex){
  65.         return this.adjacencyMap.get(startVertex).get(endVertex);
  66.        
  67.     }
  68.    
  69.     public E getData(String vertex){
  70.         if(this.adjacencyMap.containsKey(vertex) == false){
  71.             throw new IllegalArgumentException("No vertex with that name");
  72.            
  73.         }
  74.        
  75.         return this.dataMap.get(vertex);
  76.        
  77.     }
  78.    
  79.     public Set<String> getVertices(){
  80.         return this.adjacencyMap.keySet();
  81.        
  82.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement