Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Represent Graph Using Linked List
What is Linked List?
A linked list is a linear data structure which can store a collection of "nodes" connected together via links i.e. pointers. Linked lists nodes are not stored at a contiguous location, rather they are linked using pointers to the different memory locations.
A linked list can be used for graph representations. In the graph representation:
- Each vertex has its own linked list that stores its adjacent vertices.
- This forms an adjacency list using linked lists instead of vectors or arrays.
Graph
The image below represent a simple undirected graph with 6 vertices and 8 edges.
Linked List
The linked list representation of the above graph is shown below.
0 -> 3 4 1 -> 2 4 5 2 -> 1 3 5 3 -> 0 2 5 4 -> 0 1 5 5 -> 1 2 3 4
Algorithm
The following are the steps to represent a graph using linked lists:
- Step 1: Start
- Step 2: Input the number of vertices V and number of edges E
- Step 3: Define a structure for a node containing integer to store vertex number and pointer to next node
- Step 4: Create an array of head pointers (graph[V]), initialized to NULL
- Step 5: For each edge, repeat the steps 6,7 and 8.
- Step 6: Input the pair of vertices (u, v)
- Step 7: Create a new node for v and insert it at the beginning of u's list
- Step 8: If the graph is undirected, also create a node for u and insert it at the beginning of v's list
- Step 9: Display the adjacency list for each vertex
- Step 10: End
C++ Program for Linked List Representation of Graph
The following C++ program demonstrates how to represent a graph using linked lists. The program allows the user to input the number of vertices and edges, and then it displays the adjacency list.
#include <iostream>
using namespace std;
// Step 3: Node structure
struct Node {
int vertex;
Node* next;
};
// Function to create a new node
Node* createNode(int v) {
Node* newNode = new Node;
newNode->vertex = v;
newNode->next = nullptr;
return newNode;
}
int main() {
int V, E;
bool isDirected;
// Step 2: Input number of vertices and edges
cout << "Enter number of vertices: ";
cin >> V;
cout << "Enter number of edges: ";
cin >> E;
cout << "Is the graph directed? (1 for Yes, 0 for No): ";
cin >> isDirected;
// Step 4: Array of head pointers
Node* graph[V];
for (int i = 0; i < V; i++) {
graph[i] = nullptr;
}
// Step 5: Add edges
for (int i = 0; i < E; i++) {
int u, v;
cout << "Enter edge " << i + 1 << " (u v): ";
cin >> u >> v;
// Add v to u's list
Node* newNode = createNode(v);
newNode->next = graph[u];
graph[u] = newNode;
// If undirected, also add u to v's list
if (!isDirected) {
newNode = createNode(u);
newNode->next = graph[v];
graph[v] = newNode;
}
}
// Step 9: Display adjacency list
cout << "\nGraph Representation using Linked List:\n";
for (int i = 0; i < V; i++) {
cout << i << " ? ";
Node* temp = graph[i];
while (temp != nullptr) {
cout << temp->vertex << " ";
temp = temp->next;
}
cout << endl;
}
// Step 7: End
return 0;
}
Sample Output
The output of the above program is as follows:
