Java Program to Implement Adjacency List

«
»
This Java program,implements Adjacency list.In graph theory and computer science, an adjacency list representation of a graph is a collection of unordered lists, one for each vertex in the graph. Each list describes the set of neighbors of its vertex.

Here is the source code of the Java program to display a linked list in reverse. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. import java.util.HashMap;
  2. import java.util.InputMismatchException;
  3. import java.util.LinkedList;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Scanner;
  7.  
  8. public class AdjacencyList
  9. {
  10.     private  Map<Integer,List<Integer>> Adjacency_List;	
  11.  
  12.     public AdjacencyList(int number_of_vertices)
  13.     {
  14.         Adjacency_List = new HashMap<Integer, List<Integer>>();	
  15.         for (int i = 1; i <= number_of_vertices;  i++)
  16.         {
  17.             Adjacency_List.put(i, new LinkedList<Integer>());
  18.         }
  19.     }
  20.  
  21.     public void setEdge(int source, int destination)
  22.     {
  23.         if (source > Adjacency_List.size() || destination > Adjacency_List.size())
  24.         {
  25.             System.out.println("the vertex entered in not present ");
  26.             return;
  27.         }
  28.         List<Integer> slist = Adjacency_List.get(source);
  29.         slist.add(destination);
  30.         List<Integer> dlist = Adjacency_List.get(destination);
  31.         dlist.add(source);
  32.     }
  33.  
  34.     public List<Integer> getEdge(int source)
  35.     {
  36.         if (source > Adjacency_List.size())
  37.         {
  38.             System.out.println("the vertex entered is not present");
  39.             return null;
  40.         }
  41.         return Adjacency_List.get(source);
  42.     }
  43.  
  44.     public static void main(String...arg)
  45.     {
  46.         int source, destination;
  47.         int number_of_edges, number_of_vertices;
  48.         int count = 1;
  49.         Scanner scan = new Scanner(System.in);
  50.         try
  51.         {
  52.             System.out.println("Enter the number of vertices and edges in graph");
  53.             number_of_vertices = scan.nextInt();
  54.             number_of_edges = scan.nextInt();
  55.  
  56.             AdjacencyList adjacencyList = new AdjacencyList(number_of_vertices);
  57.             System.out.println("Enter the edges in graph Format : <source index> <destination index>");
  58.             while (count <= number_of_edges)
  59.             {
  60.                 source = scan.nextInt();
  61.                 destination = scan.nextInt();
  62.                 adjacencyList.setEdge(source, destination);
  63.                 count++;
  64.             }
  65.             System.out.println("the given Adjacency List for the graph \n");
  66.             for (int i = 1; i <= number_of_vertices; i++)
  67.             {
  68.                 System.out.print(i+"->");
  69.                 List<Integer> edgeList = adjacencyList.getEdge(i);
  70.                 for (int j = 1; ; j++ )
  71.                 {
  72.                     if (j != edgeList.size())
  73.                         System.out.print(edgeList.get(j - 1 ) + "->");
  74.                     else
  75.                     {
  76.                         System.out.print(edgeList.get(j - 1 ));
  77.                         break;
  78.                     }						 
  79.                 }
  80.                 System.out.println();					
  81.             }
  82.         }catch (InputMismatchException inputMismatch)
  83.         {
  84.              System.out.println("Error in Input Format. \nFormat : <source index> <destination index>");
  85.         }
  86.         scan.close();
  87.     }
  88. }


advertisement
$javac AdjacencyList.java
$java AdjacencyList
Enter the number of vertices and edges in graph
4 5
Enter the edges in graph Format : <source index> <destination index>
1 2
2 3
3 4
4 1
1 3
the given Adjacency List for the graph 
 
1->2->4->3
2->1->3
3->2->4->1
4->3->1

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.