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 Code
using System ; class GFG { static void printMat ( int [ ] degseq , int n ) { int [ , ] mat = new int [ n , n ] ; 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 ; } } } Console . Write ( " STRNEWLINE " + setw ( 3 ) + " ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( setw ( 3 ) + " ( " + i + " ) " ) ; Console . Write ( " STRNEWLINE STRNEWLINE " ) ; for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( setw ( 4 ) + " ( " + i + " ) " ) ; for ( int j = 0 ; j < n ; j ++ ) Console . Write ( setw ( 5 ) + mat [ i , j ] ) ; Console . Write ( " STRNEWLINE " ) ; } } static String setw ( int n ) { String space = " " ; while ( n -- > 0 ) space += " ▁ " ; return space ; } public static void Main ( String [ ] args ) { int [ ] degseq = { 2 , 2 , 1 , 1 , 1 } ; int n = degseq . Length ; printMat ( degseq , n ) ; } }