
- 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 Linked List
The incidence matrix of a graph is another representation of a graph to store into the memory. This matrix is not a square matrix. The order of the incidence matrix is V x E. Where V is the number of vertices and E is the number of edges in the graph.
In each row of this matrix we are placing the vertices, and in each column the edges are placed. In this representation for an edge e {u, v}, it will be marked by 1 for the place u and v of column e.
The complexity of Adjacency Matrix representation
The incidence matrix representation takes O(V x E) amount of space while it is computed. For complete graph the number of edges will be V(V-1)/2. So incidence matrix takes larger space in memory.
Input:
Output:
Algorithm
add_edge(adj_list, u, v)
Input − The u and v of an edge {u,v}, and the adjacency list
Output − Adjacency List of the graph G
Begin Append v into the list at index u Append u into the list at index v End
Example Code
#include<iostream> #include<list> #include<iterator> using namespace std; void displayAdjList(list<int> adj_list[], int v) { for(int i = 0; i<v; i++) { cout << i << "--->"; list<int> :: iterator it; for(it = adj_list[i].begin(); it != adj_list[i].end(); ++it) { cout << *it << " "; } cout << endl; } } void add_edge(list<int> adj_list[], int u, int v) { //add v into the list u, and u into list v adj_list[u].push_back(v); adj_list[v].push_back(u); } main(int argc, char* argv[]) { int v = 6; //there are 6 vertices in the graph //create an array of lists whose size is 6 list<int> adj_list[v]; add_edge(adj_list, 0, 4); add_edge(adj_list, 0, 3); add_edge(adj_list, 1, 2); add_edge(adj_list, 1, 4); add_edge(adj_list, 1, 5); add_edge(adj_list, 2, 3); add_edge(adj_list, 2, 5); add_edge(adj_list, 5, 3); add_edge(adj_list, 5, 4); displayAdjList(adj_list, v); }
Output
0--->4 3 1--->2 4 5 2--->1 3 5 3--->0 2 5 4--->0 1 5 5--->1 2 3 4
- Related Articles
- C++ Program to Represent Graph Using Incidence List
- C++ Program to Represent Graph Using Adjacency List
- C++ Program to Represent Graph Using 2D Arrays
- C++ Program to Represent Graph Using Adjacency Matrix
- C++ Program to Represent Graph Using Incidence Matrix
- C++ Program to Implement Queue using Linked List
- C++ Program to Implement Stack using linked list
- C program to store the car information using dynamic linked list.
- C++ Program to Implement Singly Linked List
- C++ Program to Implement Doubly Linked List
- C program to display numbers in reverse order using single linked list
- Python Program to Implement a Stack using Linked List
- Python Program to Implement Binary Tree using Linked List
- C# program to find node in Linked List
- C++ Program to Implement Circular Singly Linked List
