• ¿Documento equivocado? Cámbialo gratis
  • Escrito por estudiantes que aprobaron
  • Inmediatamente disponible después del pago
  • Leer en línea o como PDF
Vender
¿Dónde estudias?
Tu idioma
Document preview thumbnail
Vista previa 3 fuera de 26 páginas
Examen

Algorithms Test 2 Questions With Answers

Document preview thumbnail
Vista previa 3 fuera de 26 páginas

Algorithms Test 2 Questions With Answers. Complete set of exam questions with correct answers for Algorithm Design and Analysis exam preparation and revision. Ideal for reviewing key concepts and practicing before your test.

Vista previa del contenido

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;

Información del documento

Subido en
2 de diciembre de 2026
Número de páginas
26
Escrito en
2026/2027
Tipo
Examen
Contiene
Preguntas y respuestas
$14.99

¿Documento equivocado? Cámbialo gratis Dentro de los 14 días posteriores a la compra y antes de descargarlo, puedes elegir otro documento. Puedes gastar el importe de nuevo.
Escrito por estudiantes que aprobaron
Inmediatamente disponible después del pago
Leer en línea o como PDF

Seller avatar
Los indicadores de reputación están sujetos a la cantidad de artículos vendidos por una tarifa y las reseñas que ha recibido por esos documentos. Hay tres niveles: Bronce, Plata y Oro. Cuanto mayor reputación, más podrás confiar en la calidad del trabajo del vendedor.
Favorgrades
3.8
(200)
Vendido
865
Seguidores
376
Artículos
64193
Última venta
1 día hace



Por qué los estudiantes eligen Stuvia

Creado por compañeros estudiantes, verificado por reseñas

Calidad en la que puedes confiar: escrito por estudiantes que aprobaron y evaluado por otros que han usado estos resúmenes.

¿No estás satisfecho? Elige otro documento

¡No te preocupes! Puedes elegir directamente otro documento que se ajuste mejor a lo que buscas.

Paga como quieras, empieza a estudiar al instante

Sin suscripción, sin compromisos. Paga como estés acostumbrado con tarjeta de crédito y descarga tu documento PDF inmediatamente.

Student with book image

“Comprado, descargado y aprobado. Así de fácil puede ser.”

Alisha Student

Preguntas frecuentes