CS 2420

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What are some 2^N complexity problems?

Traveling Salesperson problem, Towers of Hanoi (where n is the number of disks), Bin Packing, largest subset sum

when performing fibonacci iteratively, why did runtime improve?

We didn't have to recompute the same values over and over again.

Since we have very fast machines, why do we bother trying to make an algorithm more efficient?

When n becomes large, the time required for an inefficient algorithm could become prohibitive.

In Big O notation, we ignore constant factors. Why?

1. It is difficult to account for every constant as we would have to know a lot about operator costs. 2. If we move to another machine, all the constants would change. 3. As n gets large, the constant becomes unimportant.

How many distinct binary search trees can be created out of 4 keys?

14

What is the maximum height of a binary tree with 16 elements? (Height of a tree with one node is zero)

15

The pre-order traversal sequence of a Binary Search Tree is 30, 20, 10, 15, 25, 23, 39, 35, 42. Which one of the following is the post-order traversal sequence of the same tree?

15, 10, 23, 25, 20, 35, 42, 39, 30

The following numbers are inserted into an empty Binary Search Tree in the given order: 10, 1, 3, 5, 15, 12, 16. What is the height of the Binary Search Tree (the height is the maximum level when the root is at level 0)?

3

What is the minimum height of a binary tree with 16 elements? (The root is at level zero)

4

which operation would have the same time (complexity) as counting the number of elements in a list? part 1

Adding 5 to every elements of the set

Compare the two problems: Problem 1: I want each of you to shake hands with every other person in the room. Problem 2: I want each of you to shake hands with every other person in the room five times. How do the solutions compare in complexity?

Both have the same complexity.

The definition of Big O is as follows: Time(n) <= c*f(n) whenever n >= n0. What is the role of c?

It allows us to measure runtime without being concerned with a specific constant.

If a node in a Binary Search Tree has two children, then its in-order successor has...

No children

If Node A is a proper ancestor of Node B in a Binary Search Tree, which of the following is always true?

Node A is inserted before Node B.

What is the complexity of the following code: public void doIt(int n) { if (n <= 0) return; for (int i = 0; i < n; i++) x++; doIt(n - 1); doIt(n - 1); }

O(2^n)

Given the following data, what is the complexity? N Running Time 2 5 4 10 8 15 16 20 32 25

O(log(n))

What is the complexity of the following code: public void doIt(int n) { if (n <= 0) return; x++; doIt(n/2); }

O(log(n))

Given the following data, what is the complexity? N Running Time 2 12 4 48 8 144 16 384 32 960

O(n log(n))

What is the complexity of the following code: public void doIt(int n) { if (n <= 0) return; for (int i = 0; i < n; i++) { x++; } doIt(n/2); doIt(n/2); }

O(n log(n))

Given the following data, what is the complexity? N Running Time 2 144 4 288 8 576 16 1152 32 2304

O(n)

Given the following data, what is the complexity? N Running Time 2 286 4 572 8 1144 16 2288 32 4676

O(n)

What is the complexity of the following code: for (i=0; i < n; i++) { x++; }

O(n)

What is the complexity of the following code: public void doIt(int n) { if (n <= 0) return; for (int i = 0; i < n; i++) x++; doIt(n/2); }

O(n)

What is the complexity of the following code: public void doIt(int n) { if (n <= 0) return; x++; doIt(n/2); doIt(n/2); }

O(n)

Given the following data, what is the complexity? N Running Time 2 12 4 48 8 192 16 768 32 3072

O(n^2)

What is the complexity of the following code: for (i=0; i < n; i++) { for (j=0; j< n / 2; j++) { x++; } }

O(n^2)

What is the complexity of the following code: for (i=0; i < n; i++) { for (j=0; j< n; j++) { x++; } }

O(n^2)

What is the complexity of the following code: for (i=0; i < n; i++) { for (j=i; j< n; j++) { // Loop begins at i x++; } }

O(n^2)

What is the complexity of the following code: public void doIt(int n) { if (n <= 0) return; for (int i = 0; i < n; i++) x++; doIt(n - 1); }

O(n^2)

Given the following data, what is the complexity? N Running Time 2 8 4 64 8 512 16 4,096 32 32,768

O(n^3)

What is the complexity of the following code: for (i=0; i < n; i++) { for (j=0; j< n; j++) { for (k = 0; k< n; k++) { x++; } } }

O(n^3)

For the following code, determine the a, b, and k values for the complexity formulas: public void doIt(int n) { if (n <= 0) return; x++; doIt(n/2); }

a=1, b=2, k=0

For the following code, determine the a, b, and k values for the complexity formulas: public void doIt(int n) { if (n <= 0) return; for (int i = 0; i < n; i++) { x++; } doIt(n/2); }

a=1, b=2, k=1

For the following code, determine the a, b, and k values for the complexity formulas: public void doIt(int n) { if (n <= 0) return; x++; doIt(n/2); doIt(n/2); }

a=2, b=2, k=0

For the following code, determine the a, b, and k values for the complexity formulas: public void doIt(int n) { if (n <= 0) return; for (int i = 0; i < n; i++) { x++; } doIt(n/2); doIt(n/2); }

a=2, b=2, k=1

True or false: Binary search trees always have the same shape for a particular set of data.

false

Which of the following traversals outputs the data in sorted order in a Binary Search Tree?

in order traversal

In order to get the information stored in a Binary Search Tree in descending order, one should recursively traverse it in the following order.

right subtree, node, left subtree

True or false: In a Binary Search Tree with root.element = 10, if we want to insert the element 5, we need to follow the left node

true

Suppose the numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are inserted in that order into an initially empty Binary Search Tree. The Binary Search Tree uses the usual ordering on natural numbers. What is the in-order traversal sequence of the resultant tree?

0 1 2 3 4 5 6 7 8 9

Which is the best example of an operation which would be linear time (grows with the size of the set)?

Finding the biggest element of the set

whats a problem that has solutions (algorithms) that run in Θ(n) time in the worst-case?

Finding the largest of n integers

which operation would have the same time (complexity) as counting the number of elements in a list? part 2

Seeing if the elements are in order

Which is the best example of an operation which would be constant time (independent of the size of the set)?

Selecting a random element from a set

The definition of Big O is as follows: Time(n) <= c*f(n) whenever n >= n0. What is the role of n0?

The condition doesn't need to hold when n is smaller than n0

For the following code, determine the a, b, and k values for the complexity formulas: public void doIt(int n) { if (n <= 0) return; for (int i = 0; i < n; i++) x++; doIt(n - 1); doIt(n - 1); }

The formula method is not applicable

For the following code, determine the a, b, and k values for the complexity formulas: public void doIt(int n) { if (n <= 0) return; for (int i = 0; i < n; i++) x++; doIt(n - 1); }

The formula method is not applicable


संबंधित स्टडी सेट्स

M1 - Intro to Data Struct. & Abstract Data Types

View Set

HealthTrust ENA: Patient Assessment

View Set

Chapter 11: Safe Facilities and Equipment

View Set

CH 70 Oncologic and Degenerative Neuro Disorders

View Set

EXAM FX- Simulated Test Questions

View Set