This is a C++ program to generate a graph for a given fixed degree sequence.This algorithm generates a undirected graph for the given degree sequence.It does not include self-edge and multiple edges.

Examples:

Input : degrees[] = {2, 2, 1, 1} Output : (0) (1) (2) (3) (0) 0 1 1 0 (1) 1 0 0 1 (2) 1 0 0 0 (3) 0 1 0 0 Explanation : We are given that there are four vertices with degree of vertex 0 as 2, degree of vertex 1 as 2, degree of vertex 2 as 1 and degree of vertex 3 as 1. Following is graph that follows given conditions. (0)----------(1) | | | | | | (2) (3)

**Approach : **

1- Take the input of the number of vertexes and their corresponding degree.

2- Declare adjacency matrix, mat[ ][ ] to store the graph.

3- To create the graph, create the first loop to connect each vertex ‘i’.

4- Second nested loop to connect the vertex ‘i’ to the every valid vertex ‘j’, next to it.

5- If the degree of vertex ‘i’ and ‘j’ are more than zero then connect them.

6- Print the adjacency matrix.

Based on the above explanation, below are implementations:

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

*chevron_right*

*filter_none*

Output:

(0) (1) (2) (3) (4) (0) 0 1 1 0 0 (1) 1 0 0 1 0 (2) 1 0 0 0 0 (3) 0 1 0 0 0 (4) 0 0 0 0 0

**Time Complexity: **O(v*v).

## Recommended Posts:

- Breadth First Search or BFS for a Graph
- Find if there is a path between two vertices in a directed graph
- Depth First Search or DFS for a Graph
- Detect Cycle in a Directed Graph
- Disjoint Set (Or Union-Find) | Set 1 (Detect Cycle in an Undirected Graph)
- Graph and its representations
- Transitive closure of a graph
- Check whether a given graph is Bipartite or not
- Shortest Path in Directed Acyclic Graph
- Bridges in a graph
- Articulation Points (or Cut Vertices) in a Graph
- Biconnected graph
- Check if a graph is strongly connected | Set 1 (Kosaraju using DFS)
- Eulerian path and circuit for undirected graph
- Longest Path in a Directed Acyclic Graph

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.