-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q10_MinCostTreeFromLeafValues.cpp
123 lines (92 loc) · 3.29 KB
/
Q10_MinCostTreeFromLeafValues.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
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
#include<iostream>
#include<vector>
#include<limits.h>
#include<map>
using namespace std;
int byRecursion(vector<int>&arr, map<pair<int,int>, int>&maxi , int left , int right){
//base case
if(left == right){
return 0;
}
//initializing the ans;
int ans = INT_MAX;
for(int i = left ; i < right; i++){
ans = min(ans ,
maxi[{left, i}] * maxi[{i+1 , right}]
+byRecursion(arr, maxi , left, i)
+byRecursion(arr, maxi , i+1, right ));
}
return ans;
}
int byTopDown(vector<int>&arr , map<pair<int, int > ,int> &maxi , int left , int right , vector<vector<int> > &dp){
// base cases
if(left == right){
return 0;
}
//step3 : cheking it the ans is already present
if(dp[left][right] != -1){
return dp[left][right];
}
//initialilzing the ans
int ans = INT_MAX;
for(int i = left; i<right ;i++){
ans = min(ans ,
maxi[{left , i}] * maxi[{i+1 , right}]
+byTopDown(arr , maxi , left , i ,dp)
+byTopDown(arr, maxi , i+1, right , dp));
}
//step2: storing the answer to the dp
dp[left][right] = ans;
return dp[left][right];
}
int byTabulation(vector<int>&arr , map<pair<int, int > , int> &maxi ){
//finding n
int n = arr.size();
//step1 : creainng the DP array
vector<vector<int> > dp(n+1 , vector<int>(n+1, 0));
// flow of code
for(int left = n-1 ; left >= 0; left--){
for(int right = 0 ; right <= n-1; right++){
//base case
if(left >= right){ //invalid case
continue;
}
else{ //valid case
//initialilzing the ans
int ans = INT_MAX;
for(int i = left; i<right ;i++){
ans = min(ans ,
maxi[{left , i}] * maxi[{i+1 , right}]
+dp[left] [i]
+dp [i+1] [right]);
}
//step2: storing the answer to the dp
dp[left][right] = ans;
}
}
}
return dp[0][n-1];
}
int main(){
vector<int>arr{6,2,4}; //in order traversal
// some precomputation using maps
map<pair<int,int>, int> maxi;
for(int i = 0 ;i<arr.size();i++){
maxi[{i,i}] = arr[i]; //storing all ranges to the map
for(int j = i+1 ; j<arr.size(); j++){
maxi[{i, j}] = max(arr[j] , maxi[{i, j-1}]);
}
}
int n = arr.size();
int ans = byRecursion(arr, maxi , 0 , n-1);
cout<<"Answer using recursion is : "<<ans<<endl;
//optimsing the solution via Memoisation
// step1 : creating the 2d dp aray
vector<vector<int> > dp(n+1 ,vector<int>(n+1 , -1));
int ansViaMemoisation = byTopDown(arr , maxi , 0 , n-1 , dp);
cout<<"Answer using Memoisation "<<ansViaMemoisation<<endl;
//solution using tablulation method
int ansUsingTabulation = byTabulation(arr , maxi );
cout<<"Answer using Tabulation: "<<ansUsingTabulation<<endl;
return 0;
}