Graph Representation using Incidence List in C++

This is a C++ program to represent graph using incidence list.

Problem Description

1. This algorithm represent a graph using incidence list.
2. The time complexity of this algorithm is O(e).

Problem Solution

1. This algorithm takes the input of the number of vertex and edges.
2. Then it takes the input of connected vertex pairs.
3. Print the incidence list.
4. Exit.

Program/Source Code

C++ program to represent graph using incidence list.
This program is successfully run on Dev-C++ using TDM-GCC 4.9.2 MinGW compiler on a Windows system.

#include<iostream>
 
using namespace std;
 
int main()
{
	int i, v, e, j, count;
 
	// take the input of the number of edges.
	cout<<"Enter the number of vertexes of the graph: ";
	cin>>v;
	cout<<"\nEnter the number of edges of the graph: ";
	cin>>e;
	int edge[e][2];
 
	// Take the input of the adjacent vertex pairs of the given graph.
	for(i = 0; i < e; i++)
	{
		cout<<"\nEnter the vertex pair for edge "<<i+1;
		cout<<"\nV(1): ";
		cin>>edge[i][0];
		cout<<"V(2): ";
		cin>>edge[i][1];
	}
 
	// Print the incidence list representation of the graph.
	cout<<"\n\nThe incidence list representation for the given graph: ";
	for(i = 0; i < e; i++)
	{
		// For each vertex print, its adjacent vertex.
		cout<<"\n\tE("<<i+1<<") -> { ";
		cout<<"V("<<edge[i][0]<<") , "<<"V("<<edge[i][1]<<")";
		cout<<" }";
	}
}
Program Explanation

1. Take the input of the number of vertex ‘v’ and edges ‘e’.
2. Take the input of ‘e’ pairs of vertexes of the given graph in edge[][].
3. Now for each edge print the corresponding vertex involved in that connection.
4. Exit.

advertisement
advertisement
Runtime Test Cases
Case 1:
Enter the number of vertexes of the graph: 5
 
Enter the number of edges of the graph: 8
 
Enter the vertex pair for edge 1
V(1): 1
V(2): 3
 
Enter the vertex pair for edge 2
V(1):1
V(2): 4
 
Enter the vertex pair for edge 3
V(1): 1
V(2): 5
 
Enter the vertex pair for edge 4
V(1): 2
V(2): 3
 
Enter the vertex pair for edge 5
V(1): 2
V(2): 5
 
Enter the vertex pair for edge 6
V(1): 3
V(2): 4
 
Enter the vertex pair for edge 7
V(1): 3
V(2): 5
 
Enter the vertex pair for edge 8
V(1): 4
V(2): 5
 
 
The incidence list representation for the given graph:
        E(1) -> { V(1) , V(3) }
        E(2) -> { V(1) , V(4) }
        E(3) -> { V(1) , V(5) }
        E(4) -> { V(2) , V(3) }
        E(5) -> { V(2) , V(5) }
        E(6) -> { V(3) , V(4) }
        E(7) -> { V(3) , V(5) }
        E(8) -> { V(4) , V(5) }	
Case 2:
Enter the number of vertexes of the graph: 3
 
Enter the number of edges of the graph: 2
 
Enter the vertex pair for edge 1
V(1): 1
V(2): 3
 
Enter the vertex pair for edge 2
V(1): 2
V(2): 3
 
 
The incidence list representation for the given graph:
        E(1) -> { V(1) , V(3) }
        E(2) -> { V(2) , V(3) }

Sanfoundry Global Education & Learning Series – C++ Algorithms.
To practice all C++ Algorithms, here is complete set of 1000 C++ 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.