
- 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 Generate a Graph for a Given Fixed Degree Sequence
This is a C++ program represents a undirected graph for the given degree sequence. The time complexity of this algorithm is O(v*v) and it does not include self-edge and multiple edges.
Algorithm
Begin To create the graph, Create the first loop to connect each vertex ‘i’. Create second nested loop to connect the vertex ‘i’ to every valid vertex ‘j’, next to it. If the degree of vertex ‘i’ and ‘j’ are more than zero, then connect them. Print the adjacency matrix using PrintMatrix(). End
Example Code
#include<iostream> #include<iomanip> using namespace std; void PrintMatrix(int matrix[][20], int n) { int i, j; cout<<"\n\n"<<setw(3)<<" "; for(i = 0; i < n; i++) cout<<setw(3)<<"("<<i+1<<")"; cout<<"\n\n"; for(i = 0; i < n; i++) { cout<<setw(4)<<"("<<i+1<<")"; for(j = 0; j < n; j++) { cout<<setw(5)<<matrix[i][j]; } cout<<"\n\n"; } } int main() { int N, i, j, AdjMat[20][20] = {0}; cout<<"Enter the number of vertex in the graph: "; cin>>N; int degseq[N]; for(i = 0; i < N; i++) { cout<<"Enter the degree of "<<i+1<<" vertex: "; cin>>degseq[i]; } for(i = 0; i < N; i++) { for(j = i+1; j < N; j++) { (degseq[i] > 0 && degseq[j] > 0) { degseq[i]--; degseq[j]--; AdjMat[i][j] = 1; AdjMat[j][i] = 1; } } } PrintMatrix(AdjMat, N); }
Output
Enter the number of vertexes of the graph: 5 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): 3 V(2): 2 Enter the vertex pair for edge 3 V(1): 1 V(2): 1 Enter the vertex pair for edge 4 V(1): 3 V(2): 1 (1) (2) (3) (4) (5) (1) 0 1 1 1 1 (2) 1 0 0 1 0 (3) 1 0 0 0 0 (4) 1 1 0 0 1 (5) 1 0 0 1 0
- Related Articles
- C++ Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence
- C++ Program to Generate a Sequence of N Characters for a Given Specific Case
- C++ Program to Generate a Random UnDirected Graph for a Given Number of Edges
- C++ Program to Generate a Random Directed Acyclic Graph DAC for a Given Number of Edges
- How to generate a sequence of a date in each month for a fixed number of months using R?
- Generate a Vandermonde matrix of given degree in Python
- Generate a Pseudo-Vandermonde matrix of given degree in Python
- C++ Program to Generate Randomized Sequence of Given Range of Numbers
- 8085 program to generate Fibonacci sequence
- 8086 program to generate Fibonacci Sequence
- Degree of Vertex of a Graph
- Generate a Vandermonde matrix of given degree with float array of points in Python
- Generate a Vandermonde matrix of given degree with complex array of points in Python
- Generate a CNF for a given context free grammar
- Java Program to generate random elements from a given array

Advertisements