-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHavelHakimi.h
58 lines (50 loc) · 1.54 KB
/
HavelHakimi.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#pragma once
#include<iostream>
#include<algorithm>
bool applyHavelHakimi(int *degrees, int noDegrees){
using namespace std;
cout << "Applying Havel-Hakimi" << endl;
cout << "---------------------" << endl;
for (int i = 0; i < noDegrees; i++)
{
//sort the remaining
std::sort(degrees+i, degrees+noDegrees, greater<int>());
// print the remaining
for (int i = 0; i < noDegrees; i++)
{
cout << degrees[i] << " ";
}
cout << endl;
// reduce degrees
for (int j = 0; j < degrees[i]; j++)
{
if(i+j+1 >= noDegrees){
cout << "Not enough vertices\nGraph is not possible" << endl;
return false;
}
--degrees[i+j+1];
}
// check for ending condition
bool zeros = true;
for (int j = i+1; j < noDegrees; j++)
{
if (degrees[j] < 0)
{
cout << "Negative Degree encountered\nGraph is not possible" << endl;
return false;
}
if (degrees[j] != 0) zeros = false;
}
if (zeros){
// print the remaining
for (int i = 0; i < noDegrees; i++)
{
cout << degrees[i] << " ";
}
cout << endl;
cout << "All the remaining are zero\nGraph is possible" << endl;
return true;
}
}
return true;
}