Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Quiz 2 with Answers - Analysis of Algorithms | CSIS 385, Quizzes of Algorithms and Programming

Material Type: Quiz; Class: Analysis of Algorithms; Subject: Computer Science; University: Siena College; Term: Unknown 1989;

Typology: Quizzes

Pre 2010

Uploaded on 08/09/2009

koofers-user-03s
koofers-user-03s 🇺🇸

10 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSIS-385: Quiz 2 Name____________________________________
1. Which of the 7 algorithm properties is not
satisfied in the following algorithm?
Precision (or Input/Output not specified
correctly)
Input: two integers a and b.
Output: maximum of a and b.
max (a,b,c,d) {
compare a, b, c, and d;
return the one that’s the
biggest.
}
2. Which of the 7 algorithm properties is not
satisfied in the following algorithm?
Finiteness (or Correctness)
Input: an array of n integers
Output: True or False
True if there exist two adjacent
integers that are the same,
otherwise return false
adj_dup (A[], n) {
x = 0;
while (x < n-1) {
if (A[x] == A[x+1])
return true;
}
return false;
}
3. Express the exact number of operations
as a summation, then solve the summation.
for (x = 0 to n-1) {
3 operations; only count these operations
}
Answer:
)(33
1
nOn
n
x
4. Prove the following using Mathematical
Induction. You must use Mathematical
Induction! No other proof method will be
accepted. Show all three steps.
Step 1:
1122)1(2
2
1
1
x
Step 2: Assume this is true
n
x
nnx
1
2
2
Step 3:
1)1(2
2
1
1
nnx
n
x
1122)1(2
2
1
nnnxn
n
x
23)1(2
22
nnnnn
2323
22
nnnn
5. Express the exact number of operations as
a summation then solve the summation.
Hint: remember you can change the loop
bounds to an equivalent loop.
for (z = 0 to n-1) {
for (y = 1 to z) {
for (x = y to n) {
1 operation;
}
}
}
This loop is equivalent to
for (z = 1 to n)
for (y = 1 to z)
for (x = 1 to y)
1 operation;
n
z
n
z
z
y
n
z
z
y
y
x
zz
y
11 11 1 1
2
)1(
1
6
23
26
32
2
123223 nnnnnnnn

Partial preview of the text

Download Quiz 2 with Answers - Analysis of Algorithms | CSIS 385 and more Quizzes Algorithms and Programming in PDF only on Docsity!

CSIS-385: Quiz 2 Name____________________________________

  1. Which of the 7 algorithm properties is not satisfied in the following algorithm? Precision (or Input/Output not specified correctly) Input: two integers a and b. Output: maximum of a and b. max (a,b,c,d) { compare a, b, c, and d; return the one that’s the biggest. }
  2. Which of the 7 algorithm properties is not satisfied in the following algorithm? Finiteness (or Correctness) Input: an array of n integers Output: True or False True if there exist two adjacent integers that are the same, otherwise return false adj_dup (A[], n) { x = 0; while (x < n-1) { if (A[x] == A[x+1]) return true; } return false; }
  3. Express the exact number of operations as a summation, then solve the summation. for (x = 0 to n-1) { 3 operations;  only count these operations } Answer: 3 3 ( ) 1 n O n n x

 ^ 

  1. Prove the following using Mathematical Induction. You must use Mathematical Induction! No other proof method will be accepted. Show all three steps. Step 1: 2 (^1 )^2211 2 1 1

   ^ 

x

Step 2: Assume this is true 

n x x n n 1

Step 3: 2 (^1 )^1 2 1 1

   ^ 

  x n n n x 2 ( 1 ) 2 2 2 1 1 1

n x n n n n x 2 ( n  1 ) n^2  nn^2  3 n  2 n^2  3 n  2  n^2  3 n  2

  1. Express the exact number of operations as a summation then solve the summation. Hint: remember you can change the loop bounds to an equivalent loop. for (z = 0 to n-1) { for (y = 1 to z) { for (x = y to n) { 1 operation; } } } This loop is equivalent to for (z = 1 to n) for (y = 1 to z) for (x = 1 to y) 1 operation;  

  ^  ^ 

      n z n z z y n z z y y x z z y 1 1 1 1 1 1 2 ( 1 ) 1

^ 

 ^2

1 1

2 nn n nn

z z

n z n z 6 3 2 6 2 2 3 2 1 n^3^ n^2 n n^2 n n^3  n^2  n ^       (^)    