
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Program to Find Out the Minimum Parsing Tree in C++
Suppose we have a list of unique and sorted numbers that represent breakpoints in a string. We want to create a tree out of these rules −
There are nodes that have a value (a, b) where a and b are breakpoints. This means the node spans from indices [a, b] in the string.
The root node spans over every breakpoint. (the whole string).
The spans of a node's left and right child are ordered, contiguous, and contains the parent node's span.
Leaf nodes' index of 'a' in breakpoints is 1 before the index of 'b' in breakpoints.
The cost of a tree is determined as the sum of b - a for every node in the tree. Our goal is to determine the lowest possible cost of a feasible tree.
So, if the input is like breakpoints = [1, 4, 7, 12], then the output will be 28.
To solve this, we will follow these steps −
n := size of the input array breakpoints
if n <= 1, then −
return 0
if n is same as 2, then −
return breakpoints[1] - breakpoints[0]
Define an array p[n - 1]
for initialize i := 0, when i < n - 1, update (increase i by 1), do −
p[i] := breakpoints[i + 1]
Define an array pre[n]
for initialize i := 1, when i < n, update (increase i by 1), do −
pre[i] := pre[i - 1] + p[i - 1]
Define one 2D array dp[n, n] and initialize columns with infinity.
Define one 2D array op[n, n]
for initialize i := 1, when i < n, update (increase i by 1), do −
dp[i,i] := p[i - 1], op[i,i] := i
for initialize len := 2, when len < n, update (increase len by 1), do −
for initialize i := 1, when i + len - 1 < n, update (increase i by 1), do −
j := i + len - 1
idx := i
for initialize k := maximum of(i, op[i,j-1]), when k < minimum of (j - 1, op[i + 1, j]), update (increase k by 1), do −
cost := dp[i, k] + dp[k + 1, j]
if cost < dp[i, j], then −
idx := k
dp[i, j] := cost
op[i, j] := idx
dp[i, j] := dp[i, j] + pre[j] - pre[i - 1]
return dp[1, n - 1]
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(vector<int>& breakpoints) { int n = breakpoints.size(); if (n <= 1) return 0; if (n == 2) return breakpoints[1] - breakpoints[0]; vector<int> p(n - 1); for (int i = 0; i < n - 1; ++i) p[i] = breakpoints[i + 1] - breakpoints[i]; vector<int> pre(n); for (int i = 1; i < n; ++i) pre[i] = pre[i - 1] + p[i - 1]; vector<vector<int>> dp(n, vector<int>(n, INT_MAX)); vector<vector<int>> op(n, vector<int>(n)); for (int i = 1; i < n; ++i) dp[i][i] = p[i - 1], op[i][i] = i; for (int len = 2; len < n; ++len) { for (int i = 1; i + len - 1 < n; ++i) { int j = i + len - 1; int idx = i; for (int k = max(i, op[i][j - 1]); k <= min(j - 1, op[i + 1][j]); ++k) { int cost = dp[i][k] + dp[k + 1][j]; if (cost < dp[i][j]) { idx = k; dp[i][j] = cost; } } op[i][j] = idx; dp[i][j] += pre[j] - pre[i - 1]; } } return dp[1][n - 1]; } int main(){ vector<int> breakpoints = {1, 4, 7, 12}; cout << solve(breakpoints) << endl; return 0; }
Input
{1, 4, 7, 12}
Output
28
- Related Articles
- C++ Program to Find the Minimum value of Binary Search Tree
- Program to find tree level that has minimum sum in C++
- C++ Program to find out the minimum difference value in n integer pairs
- Program to Find Out if an Edge is a Part of a Minimum Spanning Tree in Python
- Program to Find Out the Minimum Cost to Purchase All in Python
- Program to Find Out the Special Nodes in a Tree in Python
- C++ Program to find out the minimum number of operations required to defeat an enemy
- Program to find out the minimum path to deliver all letters in Python
- Golang program to find minimum spanning tree using dijkstra Algorithm
- Find maximum (or minimum) in Binary Tree in C++
- Program to Find Out the Minimum Cost Possible from Weighted Graph in Python
- Find Minimum Depth of a Binary Tree in C++
- Program to find out the node in the right in a binary tree using Python
- Program to find out the minimum size of the largest clique in a graph (Python)
- Program to find out the minimum moves in a snakes and ladders game in Python
