Algorithms Test 2 Questions With Answers
Question 1.
Determine an LCS of <1,0,0,1,0,1,0,1> and <0,1,0,1,1,0,1,1,0>
Correct Answer: The LCS is <1, 0, 0, 1, 1, 0> , Or It can be <1,0,1,0,1,0>
,Question 2.
Write pseudocode for the proce-
dure CONSTRUCT-OPTIMAL-BST(root) which, given the table root, outputs the structure of
an optimal binary search tree. For the example in Figure 15.8, your procedure should
print out the structure
k2 is the root
k1 is the left child of k2
d0 is the left child of k1
d1 is the right child of k1
k5 is the right child of k2
k4 is the left child of k5
k3 is the left child of k4
d2 is the left child of k3
d3 is the right child of k3 * d4 is the right child of k4
d5 is the right child of k5
Correct Answer: #include <iostream>
using namespace std;
const int MaxVal = 9999;
const int n = 5;
// Search probability root and virtual keys
double p[n + 1] = {-1,0.15,0.1,0.05,0.1,0.2}; double q[n + 1] =
{0.05,0.1,0.05,0.05,0.05,0.1};
int root[n + 1][n + 1];//Record root node
double w[n + 2][n + 2];//
Subtree probability sum
double e[n + 2][n + 2];//Subtree Expected Cost
void optimalBST(double *p,double *q,int n)
{
//Initializes only children with virtual keys
for (int i = 1;i <= n + 1;++i)
{
w[i][i - 1] = q[i - 1];
e[i][i - 1] = q[i - 1];
}
//From bottom to top, step by step from left to right for (int len = 1;len <=
n;++len)
{
for (int i = 1;i <= n - len + 1;++i)
{
int j = i + len - 1;
e[i][j] = MaxVal;
w[i][j] = w[i][j - 1] + p[j] + q[j];
,//Find the root of the subtree with the lowest cost for (int k = i;k <= j;++k)
{
double temp = e[i][k - 1] + e[k + 1][j] + w[i][j]; if (temp < e[i][j])
{
e[i][j] = temp;
root[i][j] = k;
}
}
}
}
}
//Output optimal binary search tree root of all subtrees
void printRoot()
{
cout << "The root of each subtree" << endl;
for (int i = 1;i <= n;++i)
{
for (int j = 1;j <= n;++j)
{
cout << root[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
//Print the structure of the optimal binary search tree
//Prints the [i,j] subtree, which is the left subtree and right subtree of root r
void printOptimalBST(int i,int j,int r)
{
int rootChild = root[i][j];//Subtree Root Node
if (rootChild == root[1][n])
{
//Output the root of the entire tree
cout << "k" << rootChild << "Root" << endl; printOptimalBST(i,rootChild -
1,rootChild); printOptimalBST(rootChild + 1,j,rootChild); return;
}
if (j < i - 1)
return;
else if (j == i - 1)//Encountered a virtual key
{
if (j < r)
cout << "d" << j << "Yes" << "k" << r << "Left Child" << endl;
Question 1.
Determine an LCS of <1,0,0,1,0,1,0,1> and <0,1,0,1,1,0,1,1,0>
Correct Answer: The LCS is <1, 0, 0, 1, 1, 0> , Or It can be <1,0,1,0,1,0>
,Question 2.
Write pseudocode for the proce-
dure CONSTRUCT-OPTIMAL-BST(root) which, given the table root, outputs the structure of
an optimal binary search tree. For the example in Figure 15.8, your procedure should
print out the structure
k2 is the root
k1 is the left child of k2
d0 is the left child of k1
d1 is the right child of k1
k5 is the right child of k2
k4 is the left child of k5
k3 is the left child of k4
d2 is the left child of k3
d3 is the right child of k3 * d4 is the right child of k4
d5 is the right child of k5
Correct Answer: #include <iostream>
using namespace std;
const int MaxVal = 9999;
const int n = 5;
// Search probability root and virtual keys
double p[n + 1] = {-1,0.15,0.1,0.05,0.1,0.2}; double q[n + 1] =
{0.05,0.1,0.05,0.05,0.05,0.1};
int root[n + 1][n + 1];//Record root node
double w[n + 2][n + 2];//
Subtree probability sum
double e[n + 2][n + 2];//Subtree Expected Cost
void optimalBST(double *p,double *q,int n)
{
//Initializes only children with virtual keys
for (int i = 1;i <= n + 1;++i)
{
w[i][i - 1] = q[i - 1];
e[i][i - 1] = q[i - 1];
}
//From bottom to top, step by step from left to right for (int len = 1;len <=
n;++len)
{
for (int i = 1;i <= n - len + 1;++i)
{
int j = i + len - 1;
e[i][j] = MaxVal;
w[i][j] = w[i][j - 1] + p[j] + q[j];
,//Find the root of the subtree with the lowest cost for (int k = i;k <= j;++k)
{
double temp = e[i][k - 1] + e[k + 1][j] + w[i][j]; if (temp < e[i][j])
{
e[i][j] = temp;
root[i][j] = k;
}
}
}
}
}
//Output optimal binary search tree root of all subtrees
void printRoot()
{
cout << "The root of each subtree" << endl;
for (int i = 1;i <= n;++i)
{
for (int j = 1;j <= n;++j)
{
cout << root[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
//Print the structure of the optimal binary search tree
//Prints the [i,j] subtree, which is the left subtree and right subtree of root r
void printOptimalBST(int i,int j,int r)
{
int rootChild = root[i][j];//Subtree Root Node
if (rootChild == root[1][n])
{
//Output the root of the entire tree
cout << "k" << rootChild << "Root" << endl; printOptimalBST(i,rootChild -
1,rootChild); printOptimalBST(rootChild + 1,j,rootChild); return;
}
if (j < i - 1)
return;
else if (j == i - 1)//Encountered a virtual key
{
if (j < r)
cout << "d" << j << "Yes" << "k" << r << "Left Child" << endl;