
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Represent Graph Using Incidence List
This program represents a graph using incidence list and the time complexity of this algorithm is O(e).
Algorithm
Begin Take the input of the number of vertex ‘v’ and edges ‘e’ and also take the input of ‘e’ pairs of vertexes of the given graph in e[][]. For each edge print the corresponding vertex involved in that connection. End
Example Code
#include<iostream> using namespace std; int main() { int i, v, e, j, c; 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]; 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]; } 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<<" }"; } }
Output
Enter the number of vertexes of the graph: 3 Enter the number of edges of the graph: 4 Enter the vertex pair for edge 1 V(1): 2 V(2): 1 Enter the vertex pair for edge 2 V(1): 1 V(2): 2 Enter the vertex pair for edge 3 V(1): 3 V(2): 2 Enter the vertex pair for edge 4 V(1): 2 V(2): 3 The incidence list representation for the given graph: E(1) -> { V(2) , V(1) } E(2) -> { V(1) , V(2) } E(3) -> { V(3) , V(2) } E(4) -> { V(2) , V(3) }
- Related Articles
- C++ Program to Represent Graph Using Incidence Matrix
- C++ Program to Represent Graph Using Adjacency List
- C++ Program to Represent Graph Using Linked List
- C++ Program to Represent Graph Using 2D Arrays
- C++ Program to Represent Graph Using Adjacency Matrix
- C++ Program to Implement Queue using Linked List
- C++ Program to Implement Stack using linked list
- C++ Program to Check Cycle in a Graph using Topological Sort
- C++ Program to Check the Connectivity of Directed Graph Using DFS
- C++ Program to Check the Connectivity of Undirected Graph Using BFS
- C++ Program to Check the Connectivity of Directed Graph Using BFS
- C++ Program to Check the Connectivity of Undirected Graph Using DFS
- C++ Program to Check whether Graph is a Bipartite using BFS
- C++ Program to Check whether Graph is a Bipartite using DFS
- C++ Program to Create a Random Graph Using Random Edge Generation

Advertisements