Graph Representation using Incidence List in Java

This is a java program to represent graph as a incidence list. The incidence matrix of G is a n × m matrix (b_{ij}), where n and m are the numbers of vertices and edges respectively, such that b_{ij} = 1 if the vertex v_i and edge x_j are incident and 0 otherwise. If this relationship is represented by a list, list is known as incidence list.

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

Output:

$ javac Represent_Graph_Incidence_List.java
$ java Represent_Graph_Incidence_List
 
Enter the number of vertices: 
5
Enter the number of edges: 
5
Enter the edges in the graph : <edgenumber> <to> <from>
1 1 2
2 2 4 
3 5 4
4 4 3
5 5 1
The Incidence List Representation of the graph is: 
Vertex   EdgeNumber
1	:1	5
2	:1	2
3	:4
4	:2	3	4
5	:3	5

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

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

If you find any mistake above, kindly email to [email protected]

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.