document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  * Tugas : Graph Adjacency List
  3.  *
  4.  * Rafael Asi Kristanto Tambunan
  5.  * 5025201168
  6.  * Teknik Informatika
  7.  */
  8.  
  9. class Node {
  10.     int n;
  11.     String name;
  12.  
  13.     Node(int n, String name){
  14.         this.n = n;
  15.         this.name = name;
  16.     }
  17. }
  18.  
  19. class Graph {
  20.     private HashMap<Node, LinkedList<Node>> adjacencyMap;
  21.     private boolean directed;
  22.  
  23.     public Graph(boolean directed) {
  24.         this.directed = directed;
  25.         adjacencyMap = new HashMap<>();
  26.     }
  27.  
  28.     public void addEdgeHelper(Node a, Node b) {
  29.         LinkedList<Node> tmp = adjacencyMap.get(a);
  30.  
  31.         if (tmp != null) {
  32.             tmp.remove(b);
  33.         }
  34.         else tmp = new LinkedList<>();
  35.         tmp.add(b);
  36.         adjacencyMap.put(a,tmp);
  37.     }
  38.  
  39.     public void addEdge(Node source, Node destination) {
  40.         if (!adjacencyMap.keySet().contains(source))
  41.             adjacencyMap.put(source, null);
  42.  
  43.         if (!adjacencyMap.keySet().contains(destination))
  44.             adjacencyMap.put(destination, null);
  45.  
  46.         addEdgeHelper(source, destination);
  47.  
  48.         if (!directed) {
  49.             addEdgeHelper(destination, source);
  50.         }
  51.     }
  52.  
  53.     public void printEdges() {
  54.         for (Node node : adjacencyMap.keySet()) {
  55.             System.out.print("The " + node.name + " has an edge towards: ");
  56.             if (adjacencyMap.get(node) != null) {
  57.                 for (Node neighbor : adjacencyMap.get(node)) {
  58.                     System.out.print(neighbor.name + " ");
  59.                 }
  60.                 System.out.println();
  61.             }
  62.             else {
  63.                 System.out.println("none");
  64.             }
  65.         }
  66.     }
  67.  
  68.     public boolean hasEdge(Node source, Node destination) {
  69.         return adjacencyMap.containsKey(source) && adjacencyMap.get(source) != null && adjacencyMap.get(source).contains(destination);
  70.     }
  71. }
  72.  
  73. public class AdjacencyList {
  74.     public static void main(String[] args) {
  75.         Graph graph = new Graph(true);
  76.         Node a = new Node(0, "A");
  77.         Node b = new Node(1, "B");
  78.         Node c = new Node(2, "C");
  79.         Node d = new Node(3, "D");
  80.         Node e = new Node(4, "E");
  81.  
  82.         graph.addEdge(a,b);
  83.         graph.addEdge(b,c);
  84.         graph.addEdge(b,d);
  85.         graph.addEdge(c,e);
  86.         graph.addEdge(b,a);
  87.  
  88.         graph.printEdges();
  89.  
  90.         System.out.println( );
  91.        
  92.         System.out.println(graph.hasEdge(a,b));
  93.         System.out.println(graph.hasEdge(d,a));
  94.     }
  95. }
');