Construct a graph from given degrees of all vertices | C ++ program to generate a graph for a given fixed degrees ; A function to print the adjacency matrix . ; n is number of vertices ; For each pair of vertex decrement the degree of both vertex . ; Print the result in specified format ; driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printMat ( int degseq [ ] , int n ) { int mat [ n ] [ n ] ; memset ( mat , 0 , sizeof ( mat ) ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( degseq [ i ] > 0 && degseq [ j ] > 0 ) { degseq [ i ] -- ; degseq [ j ] -- ; mat [ i ] [ j ] = 1 ; mat [ j ] [ i ] = 1 ; } } } cout << " STRNEWLINE " << setw ( 3 ) << " TABSYMBOL " ; for ( int i = 0 ; i < n ; i ++ ) cout << setw ( 3 ) << " ( " << i << " ) " ; cout << " STRNEWLINE STRNEWLINE " ; for ( int i = 0 ; i < n ; i ++ ) { cout << setw ( 4 ) << " ( " << i << " ) " ; for ( int j = 0 ; j < n ; j ++ ) cout << setw ( 5 ) << mat [ i ] [ j ] ; cout << " STRNEWLINE " ; } } int main ( ) { int degseq [ ] = { 2 , 2 , 1 , 1 , 1 } ; int n = sizeof ( degseq ) / sizeof ( degseq [ 0 ] ) ; printMat ( degseq , n ) ; return 0 ; }