Check If a Graph Can Be Constructed for a Given Degree Sequence in C++



We are given an array of integers containing the degree of each vertex in a graph. Our task is to check if it is possible to construct a graph with the given degree sequence.

Example:

int degrees[] = {3, 2, 2, 0}
Output: Not Possible

Explanation: The first vertex has degree 3, which means it must be connected to three other vertices. But the last vertex has degree 0, meaning it cannot be connected to any other vertex. Hence, it is impossible to construct a graph with this degree sequence.

To implement this in C++, we can use the Havel-Hakimi algorithm. The algorithm works as follows:

Havel-Hakimi Algorithm to Check Degree Sequence of a Graph

The Havel-Hakimi algorithm is a method to determine if a given degree sequence can represent a simple graph. The steps of the algorithm are as follows:

  • Take the degrees of the graph in a vector.
  • Start a while loop that is true forever. This loop will continue until we either return true or false.
  • First remove all zeros from the vector and if it becomes empty, return true. End the program.
  • Next, sort the vector in descending order.
  • Now, store the first element of the vector in a variable d and remove it from the vector.
  • Check if d is greater than the size of the vector. If it is, return false.
  • Subtract 1 from the first d elements of the vector.
  • While doing this, if any element becomes negative, return false.

C++ Code to Check Degree Sequence of a Graph

The following C++ code implements the Havel-Hakimi algorithm to check if a given degree sequence can form a simple graph.

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

bool havelHakimi(vector<int> degree) {
    while (true) {
        // Remove all zeros
        degree.erase(remove(degree.begin(), degree.end(), 0), degree.end());

        // If list is empty, it's graphical
        if (degree.empty())
            return true;

        // Sort in descending order
        sort(degree.begin(), degree.end(), greater<int>());

        // Take the first degree
        int d = degree[0];
        degree.erase(degree.begin());

        // Check if d is valid
        if (d > degree.size())
            return false;

        // Subtract 1 from next d degrees
        for (int i = 0; i < d; ++i) {
            degree[i]--;
            if (degree[i] < 0)
                return false;
        }
    }
}

int main() {
    vector<int> degrees = {4, 3, 3, 3, 3};  // Example

    if (havelHakimi(degrees))
        cout << "The sequence is graphical (a simple graph can be formed).\n";
    else
        cout << "The sequence is NOT graphical.\n";

    return 0;
}

The output of the above code will be:

The sequence is graphical (a simple graph can be formed).

Time and Space Complexity

Time Complexity: The time complexity of this algorithm is O(n^2 log n), where n is the number of vertices in the graph. This is due to the sorting operation and the while loop that iterates through the degree sequence.

Space Complexity:The space complexity is O(n), where n is the number of vertices in the graph, as we are using a vector to store the degree sequence.

Farhan Muhamed
Farhan Muhamed

No Code Developer, Vibe Coder

Updated on: 2025-06-12T17:23:27+05:30

152 Views

Kickstart Your Career

Get certified by completing the course

Get Started