From 7a697b96ebcfcedf0c860114f2b9aafafe9b6664 Mon Sep 17 00:00:00 2001 From: Mann Mehta <44433995+mann2108@users.noreply.github.com> Date: Fri, 17 Apr 2020 23:22:10 +0530 Subject: [PATCH 01/11] Added tree_height.cpp in dynamic_programming directory --- dynamic_programming/tree_height.cpp | 67 +++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 dynamic_programming/tree_height.cpp diff --git a/dynamic_programming/tree_height.cpp b/dynamic_programming/tree_height.cpp new file mode 100644 index 000000000..2ef9f6b6b --- /dev/null +++ b/dynamic_programming/tree_height.cpp @@ -0,0 +1,67 @@ +/// C++ Program to find height of the tree using bottom0-up DP. + +/** + * Given a rooted tree with node 1. + * Task is to find the height of the tree. +**/ + +#include +#include + + +/// global declarations + +/// no of nodes max limit. +const int MAX = 1e5; + +/// adjacency list +std::vector adj[MAX]; + +std::vector visited; +std::vector dp; + +void dp_with_dfs(int u) { + visited[u] = true; + int child_height = 1; + for (int v : adj[u]) { + if (!visited[v]) { + dp_with_dfs(v); + + /// selected maximum subtree height from all childs. + child_height = std::max(child_height, dp[v]+1); + } + } + + /// assigned the max child height to current visited node. + dp[u] = child_height; +} + +int main(){ + + /// number of nodes + int n; + std::cin >> n; + + int u,v; + + /// a valid tree contains exactly n-1 edges where n denotes the nodes. + for (int i=0; i < n-1; i++) { + std::cin >> u >> v; + + /// undirected tree u -> v and v -> u. + adj[u].push_back(v); + adj[v].push_back(u); + } + + /// initialize all nodes as unvisited. + visited.assign(n+1,false); + + /// initialize depth of all nodes to 0. + dp.assign(n+1,0); + + /// call to dp_with_dfs which will initialize the height of all nodes in dp vector. + dp_with_dfs (1); + + std::cout << dp[1] << std::endl; +} + From 3235335a6065805c5860dd4c72fbcf816ceac376 Mon Sep 17 00:00:00 2001 From: Mann Mehta <44433995+mann2108@users.noreply.github.com> Date: Fri, 17 Apr 2020 23:33:28 +0530 Subject: [PATCH 02/11] Modified with typo errors --- dynamic_programming/tree_height.cpp | 35 ++++++++++++----------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/dynamic_programming/tree_height.cpp b/dynamic_programming/tree_height.cpp index 2ef9f6b6b..fb3025039 100644 --- a/dynamic_programming/tree_height.cpp +++ b/dynamic_programming/tree_height.cpp @@ -1,22 +1,24 @@ -/// C++ Program to find height of the tree using bottom0-up DP. +/// C++ Program to find height of the tree using bottom - up DP. /** * Given a rooted tree with node 1. * Task is to find the height of the tree. + * Example: - + * 4 + * 1 2 + * 1 3 + * 2 4 + * Height of the tree : - 3 **/ #include #include - /// global declarations - /// no of nodes max limit. const int MAX = 1e5; - /// adjacency list std::vector adj[MAX]; - std::vector visited; std::vector dp; @@ -31,37 +33,28 @@ void dp_with_dfs(int u) { child_height = std::max(child_height, dp[v]+1); } } - /// assigned the max child height to current visited node. dp[u] = child_height; } -int main(){ - +int main() { /// number of nodes int n; std::cin >> n; - - int u,v; - - /// a valid tree contains exactly n-1 edges where n denotes the nodes. + int u, v; + /// Tree contains exactly n-1 edges where n denotes the nodes. for (int i=0; i < n-1; i++) { std::cin >> u >> v; - /// undirected tree u -> v and v -> u. adj[u].push_back(v); adj[v].push_back(u); } - /// initialize all nodes as unvisited. - visited.assign(n+1,false); - + visited.assign(n+1, false); /// initialize depth of all nodes to 0. - dp.assign(n+1,0); - - /// call to dp_with_dfs which will initialize the height of all nodes in dp vector. + dp.assign(n+1, 0); + /// function call which will initialize the height of all nodes. dp_with_dfs (1); - - std::cout << dp[1] << std::endl; + std::cout << "Height of the Tree : " << dp[1] << std::endl; } From a3b2f4d17e24b169f3e47650284af1580662d0e0 Mon Sep 17 00:00:00 2001 From: Mann Mehta <44433995+mann2108@users.noreply.github.com> Date: Fri, 17 Apr 2020 23:35:39 +0530 Subject: [PATCH 03/11] Fixed presentation error around brackets --- dynamic_programming/tree_height.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dynamic_programming/tree_height.cpp b/dynamic_programming/tree_height.cpp index fb3025039..1207d89a6 100644 --- a/dynamic_programming/tree_height.cpp +++ b/dynamic_programming/tree_height.cpp @@ -25,7 +25,7 @@ std::vector dp; void dp_with_dfs(int u) { visited[u] = true; int child_height = 1; - for (int v : adj[u]) { + for(int v : adj[u]) { if (!visited[v]) { dp_with_dfs(v); @@ -43,7 +43,7 @@ int main() { std::cin >> n; int u, v; /// Tree contains exactly n-1 edges where n denotes the nodes. - for (int i=0; i < n-1; i++) { + for(int i=0; i < n-1; i++) { std::cin >> u >> v; /// undirected tree u -> v and v -> u. adj[u].push_back(v); From ff8a511413301d165b41d30502faa16d09e0e464 Mon Sep 17 00:00:00 2001 From: Mann Mehta <44433995+mann2108@users.noreply.github.com> Date: Fri, 17 Apr 2020 23:37:29 +0530 Subject: [PATCH 04/11] Fixed presentation error around brackets --- dynamic_programming/tree_height.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dynamic_programming/tree_height.cpp b/dynamic_programming/tree_height.cpp index 1207d89a6..fe9b48130 100644 --- a/dynamic_programming/tree_height.cpp +++ b/dynamic_programming/tree_height.cpp @@ -25,7 +25,7 @@ std::vector dp; void dp_with_dfs(int u) { visited[u] = true; int child_height = 1; - for(int v : adj[u]) { + for (int v : adj[u]) { if (!visited[v]) { dp_with_dfs(v); @@ -43,7 +43,7 @@ int main() { std::cin >> n; int u, v; /// Tree contains exactly n-1 edges where n denotes the nodes. - for(int i=0; i < n-1; i++) { + for (int i = 0; i < n-1; i++) { std::cin >> u >> v; /// undirected tree u -> v and v -> u. adj[u].push_back(v); @@ -54,7 +54,7 @@ int main() { /// initialize depth of all nodes to 0. dp.assign(n+1, 0); /// function call which will initialize the height of all nodes. - dp_with_dfs (1); + dp_with_dfs(1); std::cout << "Height of the Tree : " << dp[1] << std::endl; } From 5a2238d8ef0d2029db66751a8f6d56af463987c2 Mon Sep 17 00:00:00 2001 From: Mann Mehta <44433995+mann2108@users.noreply.github.com> Date: Fri, 17 Apr 2020 23:41:59 +0530 Subject: [PATCH 05/11] Modified comments --- dynamic_programming/tree_height.cpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/dynamic_programming/tree_height.cpp b/dynamic_programming/tree_height.cpp index fe9b48130..18a02e68e 100644 --- a/dynamic_programming/tree_height.cpp +++ b/dynamic_programming/tree_height.cpp @@ -1,6 +1,6 @@ -/// C++ Program to find height of the tree using bottom - up DP. +// C++ Program to find height of the tree using bottom - up DP. -/** +/* * Given a rooted tree with node 1. * Task is to find the height of the tree. * Example: - @@ -9,15 +9,15 @@ * 1 3 * 2 4 * Height of the tree : - 3 -**/ +*/ #include #include -/// global declarations -/// no of nodes max limit. +// global declarations +// no of nodes max limit. const int MAX = 1e5; -/// adjacency list +// adjacency list std::vector adj[MAX]; std::vector visited; std::vector dp; @@ -29,31 +29,31 @@ void dp_with_dfs(int u) { if (!visited[v]) { dp_with_dfs(v); - /// selected maximum subtree height from all childs. + // selected maximum subtree height from all child. child_height = std::max(child_height, dp[v]+1); } } - /// assigned the max child height to current visited node. + // assigned the max child height to current visited node. dp[u] = child_height; } int main() { - /// number of nodes + // number of nodes int n; std::cin >> n; int u, v; - /// Tree contains exactly n-1 edges where n denotes the nodes. + // Tree contains exactly n-1 edges where n denotes the nodes. for (int i = 0; i < n-1; i++) { std::cin >> u >> v; - /// undirected tree u -> v and v -> u. + // undirected tree u -> v and v -> u. adj[u].push_back(v); adj[v].push_back(u); } - /// initialize all nodes as unvisited. + // initialize all nodes as unvisited. visited.assign(n+1, false); - /// initialize depth of all nodes to 0. + // initialize depth of all nodes to 0. dp.assign(n+1, 0); - /// function call which will initialize the height of all nodes. + // function call which will initialize the height of all nodes. dp_with_dfs(1); std::cout << "Height of the Tree : " << dp[1] << std::endl; } From f4c936d8be2fe3cc99f7c4bbaa84c2edb262bf44 Mon Sep 17 00:00:00 2001 From: Mann Mehta Date: Sat, 18 Apr 2020 08:17:56 +0530 Subject: [PATCH 06/11] Update dynamic_programming/tree_height.cpp Co-Authored-By: Christian Clauss --- dynamic_programming/tree_height.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dynamic_programming/tree_height.cpp b/dynamic_programming/tree_height.cpp index 18a02e68e..b9142b117 100644 --- a/dynamic_programming/tree_height.cpp +++ b/dynamic_programming/tree_height.cpp @@ -29,7 +29,7 @@ void dp_with_dfs(int u) { if (!visited[v]) { dp_with_dfs(v); - // selected maximum subtree height from all child. + // select maximum sub-tree height from all children. child_height = std::max(child_height, dp[v]+1); } } @@ -57,4 +57,3 @@ int main() { dp_with_dfs(1); std::cout << "Height of the Tree : " << dp[1] << std::endl; } - From 1ff60ebf205b58b2332cf7f7da711b3d732999f9 Mon Sep 17 00:00:00 2001 From: Mann Mehta Date: Mon, 20 Apr 2020 16:45:13 +0530 Subject: [PATCH 07/11] Update dynamic_programming/tree_height.cpp Co-Authored-By: Christian Clauss --- dynamic_programming/tree_height.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/tree_height.cpp b/dynamic_programming/tree_height.cpp index b9142b117..8c5242ccd 100644 --- a/dynamic_programming/tree_height.cpp +++ b/dynamic_programming/tree_height.cpp @@ -22,7 +22,7 @@ std::vector adj[MAX]; std::vector visited; std::vector dp; -void dp_with_dfs(int u) { +void depth_first_search(int u) { visited[u] = true; int child_height = 1; for (int v : adj[u]) { From 847bfd6527620fd780b4f8d9bd569ce990586d38 Mon Sep 17 00:00:00 2001 From: Mann Mehta Date: Mon, 20 Apr 2020 17:04:32 +0530 Subject: [PATCH 08/11] Update tree_height.cpp --- dynamic_programming/tree_height.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/dynamic_programming/tree_height.cpp b/dynamic_programming/tree_height.cpp index 8c5242ccd..e9039c5a1 100644 --- a/dynamic_programming/tree_height.cpp +++ b/dynamic_programming/tree_height.cpp @@ -27,7 +27,7 @@ void depth_first_search(int u) { int child_height = 1; for (int v : adj[u]) { if (!visited[v]) { - dp_with_dfs(v); + depth_first_search(v); // select maximum sub-tree height from all children. child_height = std::max(child_height, dp[v]+1); @@ -39,21 +39,24 @@ void depth_first_search(int u) { int main() { // number of nodes - int n; - std::cin >> n; + int no_of_nodes; + std::cout << "Enter number of nodes of the tree : " << std::endl; + std::cin >> no_of_nodes; + // u,v denotes an undirected edge of tree. int u, v; // Tree contains exactly n-1 edges where n denotes the nodes. - for (int i = 0; i < n-1; i++) { + std::cout << "Enter edges of the tree : " << std::endl; + for (int i = 0; i < no_of_nodes-1; i++) { std::cin >> u >> v; // undirected tree u -> v and v -> u. adj[u].push_back(v); adj[v].push_back(u); } // initialize all nodes as unvisited. - visited.assign(n+1, false); + visited.assign(no_of_nodes+1, false); // initialize depth of all nodes to 0. - dp.assign(n+1, 0); + dp.assign(no_of_nodes+1, 0); // function call which will initialize the height of all nodes. - dp_with_dfs(1); + depth_first_search(1); std::cout << "Height of the Tree : " << dp[1] << std::endl; } From 6faf95adfd92e9cbea4d60deb61fa530531b2305 Mon Sep 17 00:00:00 2001 From: John Law Date: Mon, 20 Apr 2020 20:31:48 +0200 Subject: [PATCH 09/11] Update example --- dynamic_programming/tree_height.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/dynamic_programming/tree_height.cpp b/dynamic_programming/tree_height.cpp index e9039c5a1..095d79f7b 100644 --- a/dynamic_programming/tree_height.cpp +++ b/dynamic_programming/tree_height.cpp @@ -1,4 +1,4 @@ -// C++ Program to find height of the tree using bottom - up DP. +// C++ Program to find height of the tree using bottom-up dynamic programming. /* * Given a rooted tree with node 1. @@ -8,7 +8,14 @@ * 1 2 * 1 3 * 2 4 - * Height of the tree : - 3 + * which can be represented as + * 1 + * / \ + * 2 3 + * | + * 4 + * + * Height of the tree : - 2 */ #include @@ -42,6 +49,7 @@ int main() { int no_of_nodes; std::cout << "Enter number of nodes of the tree : " << std::endl; std::cin >> no_of_nodes; + // u,v denotes an undirected edge of tree. int u, v; // Tree contains exactly n-1 edges where n denotes the nodes. From 2893c222f078051f44d8b2b23ab21677f2aa8925 Mon Sep 17 00:00:00 2001 From: John Law Date: Mon, 20 Apr 2020 20:34:04 +0200 Subject: [PATCH 10/11] Fix cpplint --- dynamic_programming/tree_height.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/tree_height.cpp b/dynamic_programming/tree_height.cpp index 095d79f7b..a43f91a56 100644 --- a/dynamic_programming/tree_height.cpp +++ b/dynamic_programming/tree_height.cpp @@ -49,7 +49,7 @@ int main() { int no_of_nodes; std::cout << "Enter number of nodes of the tree : " << std::endl; std::cin >> no_of_nodes; - + // u,v denotes an undirected edge of tree. int u, v; // Tree contains exactly n-1 edges where n denotes the nodes. From f36be23cb238908c2e74637dac1df806cfba01ee Mon Sep 17 00:00:00 2001 From: John Law Date: Mon, 20 Apr 2020 20:37:57 +0200 Subject: [PATCH 11/11] Enhance readability --- dynamic_programming/tree_height.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/dynamic_programming/tree_height.cpp b/dynamic_programming/tree_height.cpp index a43f91a56..6acc15959 100644 --- a/dynamic_programming/tree_height.cpp +++ b/dynamic_programming/tree_height.cpp @@ -46,24 +46,24 @@ void depth_first_search(int u) { int main() { // number of nodes - int no_of_nodes; + int number_of_nodes; std::cout << "Enter number of nodes of the tree : " << std::endl; - std::cin >> no_of_nodes; + std::cin >> number_of_nodes; - // u,v denotes an undirected edge of tree. + // u, v denotes an undirected edge of tree. int u, v; - // Tree contains exactly n-1 edges where n denotes the nodes. + // Tree contains exactly n-1 edges where n denotes the number of nodes. std::cout << "Enter edges of the tree : " << std::endl; - for (int i = 0; i < no_of_nodes-1; i++) { + for (int i = 0; i < number_of_nodes - 1; i++) { std::cin >> u >> v; // undirected tree u -> v and v -> u. adj[u].push_back(v); adj[v].push_back(u); } // initialize all nodes as unvisited. - visited.assign(no_of_nodes+1, false); + visited.assign(number_of_nodes+1, false); // initialize depth of all nodes to 0. - dp.assign(no_of_nodes+1, 0); + dp.assign(number_of_nodes+1, 0); // function call which will initialize the height of all nodes. depth_first_search(1); std::cout << "Height of the Tree : " << dp[1] << std::endl;