-
Notifications
You must be signed in to change notification settings - Fork 0
/
hypercube.cpp
56 lines (49 loc) · 1017 Bytes
/
hypercube.cpp
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
//Create a hypercube graph for any given degree
#include<bits/stdc++.h>
using namespace std;
bool differbyone(int a, int b){
int x=a^b;
if(x && (!(x & (x-1))))
return true;
else
return false;
}
void print(vector<int> g[],int m)
{
cout<<"The adjacency list representation of the graph is:"<<endl;
for(int i=0;i<m;i++)
{
int degree=0;
cout<<i<<": ";
for(int j=0;j<g[i].size();j++)
{
cout<<" -> ";
degree++;
cout<<g[i][j]<<" ";
}
cout<<endl;
cout<<"Degree : "<<degree<<endl;
}
}
int main(){
int deg,size;
cout<<"Enter the degree: "<<endl;
cin>>deg;
size=pow(2,deg);
cout<<size<<endl;
vector<int> g[size];
set<int> s;
for(int i=0;i<size;i++){
s.insert(i);
}
for(int i=0;i<size;i++){
int n1=i;
set<int> ::iterator itr;
for(itr=s.begin();itr!=s.end();itr++){
if(differbyone(n1,*(itr))){
g[n1].push_back(*itr);
}
}
}
print(g,size);
}