-
Notifications
You must be signed in to change notification settings - Fork 3
/
question17.c
138 lines (116 loc) · 3.28 KB
/
question17.c
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
Largest sum independent set in a binary tree
Independent set means no two nodes are adjacent to each other. Eg:
10
5 6
4 3 7 8
9 12 13 14 15 16 19 21
In the above tree if I choose 10, I cannot choose 5 and 6 as they are adjacent to 10.
If I choose 5, I cannot choose 10, 4 and 3 and so on.
METHOD:
Naive approach: Here there are two cases for every tree or subtree present (either to include the node
or to not include it)
Therefore LIS(root) = max {
cost(root) + cost of all its grandchildren,
cost of all its children of root
}
Therefore for each node the number of possibilities increase as we go down the tree. Hence,
exponential time complexity.
WATCH VIDEO SECOND LAST DP
*/
//naive approach implementation
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
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 findMax(int a,int b){
return (a>b)?a:b;
}
int LISS(struct node *root){
if(!root){
return 0;
}
//size excluding the current node
int size_excl = LISS(root->left) + LISS(root->right);
//size including
int size_incl = 1;
if (root->left)
size_incl += LISS(root->left->left) + LISS(root->left->right);
if (root->right)
size_incl += LISS(root->right->left) + LISS(root->right->right);
return findMax(size_excl, size_incl);
}
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);
int size = LISS(root);
printf("size of independent set with max sum is %d\n", size);
return 0;
}
//dynamic programming approach
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
int liss;
struct node *left;
struct node *right;
};
struct node *newNode(int data){
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = data;
temp->liss = 0;
temp->left = temp->right = NULL;
return temp;
}
int findMax(int a,int b){
return (a>b)?a:b;
}
int LISS(struct node *root){
if(!root){
return 0;
}
if(root->liss){
return root->liss;
}
if(!root->left && !root->right){
return (root->liss = 1);
}
//size excluding the current node
int size_excl = LISS(root->left) + LISS(root->right);
//size including
int size_incl = 1;
if (root->left)
size_incl += LISS(root->left->left) + LISS(root->left->right);
if (root->right)
size_incl += LISS(root->right->left) + LISS(root->right->right);
return findMax(size_excl, size_incl);
}
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);
int size = LISS(root);
printf("size of independent set with max sum is %d\n", size);
return 0;
}