![]() |
VOOZH | about |
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 :
- Take the input of the number of vertexes and their corresponding degree.
- Declare adjacency matrix, mat[ ][ ] to store the graph.
- To create the graph, create the first loop to connect each vertex āiā.
- Second nested loop to connect the vertex āiā to the 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.
Based on the above explanation, below are implementations:
(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).
Space complexity : O(n^2) because it creates a 2-dimensional array (matrix) of size n * n, where n is the number of vertices in the graph.