Find if a Degree Sequence can form a Simple Graph | Havel-Hakimi Algorithm


A degree chain in graph theory indicates the order of vertices' degrees. It is crucial to ascertain whether a degree order can result in a simple graph, or a graph without parallel or self−looping edges. We will examine three approaches to resolving this issue in this blog, concentrating on the Havel−Hakimi algorithm. We'll go over the algorithm used by each technique, offer related code representations with the appropriate headers, and show off each approach's unique results.

Methods Used

  • Havel−Hakimi Algorithm

  • Sort & check

  • Direct Count

Havel− Hakimi Algorithm

The Havel−Hakimi algorithm is a popular technique for figuring out whether a degree sequence can result in a straightforward graph. Up until an initial case is attained, degrees are eliminated one at a time.

Algorithm

  • Arrange the degree series in descending order using the following algorithm.

  • Return true if the degree chain is zero (it creates a simple graph).

  • Return false (it cannot form a simple graph) if the initial degree is unfavourable or higher than the sum of the remainder degrees.

  • Subtract the initial degree from the list.

  • Deduct 1 from the following 'k' degrees, where 'k' is the deleted degree's value.

  • Perform from step 1−5 again.

Example

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

bool havelHakimi(vector<int>& degrees) {
    sort(degrees.rbegin(), degrees.rend()); // Sort in non-increasing order

    while (!degrees.empty() && degrees.front() > 0) {
        int firstDegree = degrees.front();
        degrees.erase(degrees.begin()); // Remove the first degree

        if (firstDegree > degrees.size()) {
            return false;
        }

        for (int i = 0; i < firstDegree; ++i) {
            degrees[i]--;
        }

        sort(degrees.rbegin(), degrees.rend()); // Re-sort the sequence
    }

    return degrees.empty(); // Return true if the sequence is empty
}

int main() {
    // Test the havelHakimi function
    vector<int> degrees = {3, 1, 2, 3, 1, 0};
    bool result = havelHakimi(degrees);

    if (result) {
        cout << "The sequence can represent a valid graph." << endl;
    } else {
        cout << "The sequence cannot represent a valid graph." << endl;
    }

    return 0;
}

Output

The sequence cannot represent a valid graph.

Sort & Check

The second approach is sorting the degree series in a non−decreasing order and repeatedly determining if the prerequisites for a straightforward graph are met.

Algorithm

  • Sort the degree series in descending order using the following algorithm.

  • Repeat this process for every degree in the series.

  • Return false if the present level is unfavourable or more numerous than the number of available degrees.

  • If every degree passes the tests, return true (it creates a straightforward graph).

Example

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

bool havelHakimiAlgorithm(vector<int>& degrees) {
    // Sort the degree sequence in non-increasing order
    sort(degrees.rbegin(), degrees.rend());

    while (!degrees.empty() && degrees[0] > 0) {
        int highestDegree = degrees[0];
        int n = degrees.size();

        // Check if the highest degree is greater than the number of remaining vertices
        if (highestDegree >= n) {
            return false;
        }

        // Remove the highest degree vertex
        degrees.erase(degrees.begin());

        // Decrease the degrees of its neighbors
        for (int i = 0; i < highestDegree; ++i) {
            degrees[i]--;
        }

        // Sort the degree sequence again
        sort(degrees.rbegin(), degrees.rend());
    }

    // If all degrees are zero, the degree sequence can form a simple graph
    return degrees.empty();
}

int main() {
    // Example degree sequence
    vector<int> degrees = {3, 3, 2, 2, 1};

    // Check if the degree sequence can form a simple graph
    bool canFormGraph = havelHakimiAlgorithm(degrees);

    if (canFormGraph) {
        cout << "The degree sequence can form a simple graph." << endl;
    } else {
        cout << "The degree sequence cannot form a simple graph." << endl;
    }

    return 0;
}

Output

The degree sequence cannot form a simple graph.

Direct Count

The fourth approach simply measures the number of levels meeting predetermined criteria to ascertain whether the sequence may be represented as a simple graph.

Algorithm

  • Determine the number of degrees that are more than or equal to 0 and save the result in 'n'.

  • Return false if 'n' is an odd number (it cannot form a simple graph).

  • Measure and keep in 'k' the number of non−zero degrees that are more than the number of left degrees.

  • Return false if 'k' is higher than the number of left degrees.

  • Return true if all requirements are satisfied (it creates a basic graph).

Example

#include <iostream>
#include <vector>

using namespace std;

bool directCount(vector<int>& degrees) {
    int n = 0;
    int k = 0;

    for (int degree : degrees) {
        if (degree >= 0) {
            n++;
            if (degree > degrees.size() - n) {
                k++;
            }
        }
    }

    return (n % 2 == 0) && (k <= n);
}

int main() {
    // Test the directCount function
    vector<int> degrees = {3, 1, 2, 3, 1, 0};
    bool result = directCount(degrees);

    if (result) {
        cout << "The sequence can represent a valid graph." << endl;
    } else {
        cout << "The sequence cannot represent a valid graph." << endl;
    }

    return 0;
}

Output

The sequence can represent a valid graph.

Conclusion

In this post, we looked at three distinct approaches to figuring out whether a particular degree sequence can result in a straightforward graph. We talked about the Havel−Hakimi algorithm, which employs a method of gradual reduction to determine whether the formation of a graph is feasible. We also looked at the degree array approach, the straightforward count method, and the sort and check method, each of which had a different strategy for validating a basic graph's conditions. You can quickly decide whether or not a graph can be created from a particular degree sequence by comprehending and using these techniques. The method chosen will depend on the particular specifications and features of the current degree sequence.

Updated on: 14-Jul-2023

169 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements