
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
Generate a Graph for a Given Fixed Degree Sequence in C++
In this article, we will understand how to generate a graph for a given fixed-degree sequence. The degree of each node is given in the form of an array. The graph generated will be an undirected graph where the degree of each node will correspond to the given degree array.
Example
The following example generates a graph and adjacency matrix for the given degree sequence:
Input: Degree Sequence: {3, 2, 3, 2} => Degree(Node a) = 3, Degree(Node b) = 2, Degree(Node c) = 3, Degree(Node d) = 2 Output: Adjacency matrix of the graph for the given degree sequence: a b c d a 0 1 1 1 b 1 0 1 0 c 1 1 0 1 d 1 0 1 0 Graph: (a)- - - -(b) | \ | | \ | | \ | | \| (d)- - - -(c)
The following figure demonstrates the graph and its respective adjacency matrix generated from the given fixed degree sequence:
Steps to Generate Graph for Fixed Degree Sequence
The steps are mentioned below where we have generated an undirected graph with the given degree sequence.
- First, we sort the degree sequence in descending order using the sort() function.
- Then, we check if the given degree sequence can be represented in a graph or not. We have checked that the degree of the node does not exceed the available number of nodes.
- Then we iterate over the degree sequence using a for loop, and choose a node of the highest degree (u), and connect it with a node that has the second highest degree (v).
- After that, we decrease the degree of each node i.e. the highest degree node and the nodes to which it is connected. Then we remove the highest degree node using the erase() function.
- We repeat the above two steps until the degree of each node becomes 0.
- At last, the adjacency matrix of the graph is printed using the printMatrix() function.
Generating Graph with Fixed Degree Sequence in C++
The code implementation of the above steps for generating a graph with the given degree is given below:
#include <iostream> #include <iomanip> #include <vector> #include <algorithm> using namespace std; void printMatrix(int matrix[][20], int n) { cout << "\n "; for (int i = 0; i < n; i++) cout << setw(4) << i + 1; cout << "\n\n"; for (int i = 0; i < n; i++) { cout << setw(4) << i + 1; for (int j = 0; j < n; j++) { cout << setw(4) << matrix[i][j]; } cout << "\n\n"; } } int main() { int N = 5; int degSeq[5] = {3, 3, 2, 2, 2}; int graphMatrix[20][20] = {0}; vector<pair<int, int>> degree_list; for (int i = 0; i < N; i++) { degree_list.push_back({degSeq[i], i}); } // Havel-Hakimi process while (true) { // Sorting the degrees in descending order sort(degree_list.begin(), degree_list.end(), greater<>()); if (degree_list[0].first == 0) break; int d = degree_list[0].first; int v = degree_list[0].second; // Removing the highest degree node from the list degree_list.erase(degree_list.begin()); // Checking if the degree is valid and does //not exceed the number of remaining nodes if (d > degree_list.size()) { cout << "Error: Invalid Input.\n"; return 0; } for (int i = 0; i < d; i++) { int u = degree_list[i].second; graphMatrix[v][u] = graphMatrix[u][v] = 1; degree_list[i].first--; if (degree_list[i].first < 0) { cout << "Error: Invalid Input.\n"; return 0; } } } cout << "Adjacency matrix of the graph for the given degree sequence:\n"; printMatrix(graphMatrix, N); cout << "\n"; }
The output of the given code is as follows:
Adjacency matrix of the graph for the given degree sequence: 1 2 3 4 5 1 0 1 1 1 0 2 1 0 0 1 1 3 1 0 0 0 1 4 1 1 0 0 0 5 0 1 1 0 0
Complexity of Generating Graph with Given Degree Sequence
- Time Complexity: The time complexity for generating a graph with given degree sequence is O(N^2 log N).
- Space Complexity: The space complexity for generating a graph with given degree sequence is O(N^2).
Advertisements