-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximal_Independent_Set.cpp
More file actions
64 lines (57 loc) · 1.27 KB
/
Copy pathMaximal_Independent_Set.cpp
File metadata and controls
64 lines (57 loc) · 1.27 KB
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
59
60
61
62
63
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
int dp;
struct node *left;
struct node *right;
};
struct node* newNode(int data)
{
struct node *temp = (struct node*)malloc(sizeof(struct node));
temp->data= data;
temp->left= temp->right = NULL;
return temp;
}
int recur_LISS(struct node *root)
{
if(root ==NULL)
{
return 0;
}
if(root->dp)
{
return root->dp;
}
if(root->left==NULL && root->right==NULL)
{
root->dp=1;
}
//if root is't LISS
int size_excl = recur_LISS(root->left)+recur_LISS(root->right);
int size_incl =1;
if(root->right)
{
size_incl+=(recur_LISS(root->right->right)+recur_LISS(root->right->left));
}
if(root->left)
{
size_incl+=recur_LISS(root->left->right)+recur_LISS(root->left->left);
}
root->dp = max(size_excl,size_incl);
return root->dp;
}
int main()
{
struct node *root = newNode(20);
root->left = newNode(8);
root->left->left = newNode(4);
root->left->right = newNode(12);
root->left->right->left = newNode(10);
root->left->right->right = newNode(14);
root->right = newNode(22);
root->right->right = newNode(25);
printf ("Size of the Largest Independent Set is %d ", recur_LISS(root));
return 0;
}