Graph Representation using Linked List in Java

This is a java program to represent graph as a linked list. Each node will have a linked list consisting of node to which it is connected.

Here is the source code of the Java Program to Represent Graph Using Linked List. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is a java program to represent graph as a linked list
  2. import java.util.HashMap;
  3. import java.util.LinkedList;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Scanner;
  7.  
  8. public class Represent_Graph_Linked_List 
  9. {
  10.     private Map<Integer, List<Integer>> adjacencyList;
  11.  
  12.     public Represent_Graph_Linked_List(int v) 
  13.     {
  14.         adjacencyList = new HashMap<Integer, List<Integer>>();
  15.         for (int i = 1; i <= v; i++)
  16.             adjacencyList.put(i, new LinkedList<Integer>());
  17.     }
  18.  
  19.     public void setEdge(int to, int from) 
  20.     {
  21.         if (to > adjacencyList.size() || from > adjacencyList.size())
  22.             System.out.println("The vertices does not exists");
  23.  
  24.         List<Integer> sls = adjacencyList.get(to);
  25.         sls.add(from);
  26.         List<Integer> dls = adjacencyList.get(from);
  27.         dls.add(to);
  28.     }
  29.  
  30.     public List<Integer> getEdge(int to) 
  31.     {
  32.         if (to > adjacencyList.size()) 
  33.         {
  34.             System.out.println("The vertices does not exists");
  35.             return null;
  36.         }
  37.         return adjacencyList.get(to);
  38.     }
  39.  
  40.     public static void main(String args[]) 
  41.     {
  42.         int v, e, count = 1, to, from;
  43.         Scanner sc = new Scanner(System.in);
  44.         Represent_Graph_Linked_List glist;
  45.         try 
  46.         {
  47.             System.out.println("Enter the number of vertices: ");
  48.             v = sc.nextInt();
  49.             System.out.println("Enter the number of edges: ");
  50.             e = sc.nextInt();
  51.  
  52.             glist = new Represent_Graph_Linked_List(v);
  53.  
  54.             System.out.println("Enter the edges in the graph : <to> <from>");
  55.             while (count <= e) 
  56.             {
  57.                 to = sc.nextInt();
  58.                 from = sc.nextInt();
  59.  
  60.                 glist.setEdge(to, from);
  61.                 count++;
  62.             }
  63.  
  64.             System.out
  65.                     .println("The Linked List Representation of the graph is: ");
  66.  
  67.             for (int i = 1; i <= v; i++) 
  68.             {
  69.                 System.out.print(i + "->");
  70.                 List<Integer> edgeList = glist.getEdge(i);
  71.                 for (int j = 1;; j++) 
  72.                 {
  73.                     if (j != edgeList.size())
  74.                         System.out.print(edgeList.get(j - 1) + " -> ");
  75.                     else 
  76.                     {
  77.                         System.out.print(edgeList.get(j - 1));
  78.                         break;
  79.                     }
  80.                 }
  81.                 System.out.println();
  82.             }
  83.         } 
  84.         catch (Exception E) 
  85.         {
  86.             System.out.println("Something went wrong");
  87.         }
  88.         sc.close();
  89.     }
  90. }

Output:

$ javac Represent_Graph_Linked_List.java
$ java Represent_Graph_Linked_List
 
Enter the number of vertices: 
5
Enter the number of edges: 
4
Enter the edges in the graph : <to> <from>
1 2
1 3
3 5
4 3 
The Linked List Representation of the graph is: 
1 -> 2 -> 3
2 -> 1
3 -> 1 -> 5 -> 4
4 -> 3
5 -> 3

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

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 & discussions at Telegram SanfoundryClasses.