Data Structures 1 Final Exam

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

A max-heap is stored in an array (using the textbook's approach). What is the index of the parent of the node in location 17?

8

Consider the function shown below public static int f(int n) { if ( n < 0 ) return 2; else return f(n-1) + f(n-3); } What value is returned by the call f(2)?

8

Recall the Playground example from class (algs13). This class has lots of code in it; however from a client perspective - as a datatype, which of the following best describes this class.

A linked list of doubles

Consider the problem: Find the maximum value in array of size N. If we wanted to solve the problem using recursion, which of the following would best describe the 'smaller problem'?

An array of size N-1

35

Suppose we insert the value 35 into the heap shown below. Which one of the following would be the parent of the node containing 14?

26

Suppose we perform the delMax function on the heap shown below. Which one of the following would be the parent of the node containing 14?

Dynamic Connectivity Problem

Take values from a file, perform unions and after a number of unions, who is connected to what?

Code to iterate through linked list

(Node t=first; T!=null; t=t.next)

0

The graph below represent the current state for the QuickFindUF algorithm. What is id[0]?

6

The graph below represents the current state for the QuickUnionUF algorithm. What is stored in id[2]?

0

The graph below represents the current state for the QuickUnionUF algorithm. What value is returned by find(1)?

0

The graph below represents the current state for the QuickUnionUF algorithm. Suppose we perform: Union(1,8) What would be the value of id[5]?

If you have two powers, N^p and N^q, which is tilde ?

The larger between p and q

Consider the example from section 1.3 below: StackOfStrings s = new StackOfStrings(); while (!StdIn.isEmpty()) { String item = StdIn.readString(); if (!item.equals("-")) s.push(item); else if (s.isEmpty()) StdOut.println("BAD INPUT"); else StdOut.print(s.pop() + " "); } Suppose the input is " one two three - four five - - six - seven "What would be the second word in the output?

five

Quick-find maintains the invariant that p and q are connected if and only if

id[p] = id[q]

Swim code

swim(int k){ while(k<1 && less (k/2k){ exch(k/2,k); k=k/2; } }

Priority Queue does what?

Prioritizes what the first one out is. (MIFO- most important one is first one out)

Code for quick union find()

Public int find(int p){ while(id[p]!=p) p=id[p]; return p; }

Consider the three variations of UnionFind we have considered. For a random input pair (p,q), which one would most likely perform the find operation fastest?

QuickFind

What is the output of the code fragment below? int n = 4; for (int i = 1; i <= n; i++) { for (int j = 1; j < i; j++) StdOut.print('*'); StdOut.println(); } What is the value printed by the above code?

* ** ***

This question refers to binary search used in the rank method in class BinarySearch (p. 47). Assume integer keys with sorted keys array: int[] keys = {10,20,30,40,50,60,70}; So initially in rank lo = 0 and hi = 6. Suppose that key = 30. What would be the value of the variable lo after two iterations of the while loop?

2

A max-heap containing 13 nodes is stored in an array (using the textbook approach). In what location would you find the maximum value?

1

What is the tilde approximation for the function (1+1/N)/(1+2/N) ?

1

Quick Find order of growth for find

1 (constant)

Best to worst case for Order of growth

1, logN, N, NlogN, N^2, N^3, 2^N

Suppose we apply the selection sort algorithm (book version) to the array below. 11 9 18 12 8 15 22 14 17 6 What value is stored in location 9 at the end of step 1? [ array indexes start at 0; steps are numbered starting from 0- at the end of step 0, one value has been correctly positioned.]

11

A max-heap containing 23 nodes is stored in an array (using the textbook's approach). Which location would contain a left child of the node in location 6?

12

Suppose we apply the insertion sort algorithm (book version) to the array below. 14 9 7 12 8 15 22 14 17 6 What value will be in position 2 at the end of step 2? [ arrays indexes start at 0; at the end of step i, the value that was in position i has moved left so that is correctly ordered among elements [0-i]

14

Suppose we apply the selection sort algorithm (book version) to the array below. 11 9 18 12 8 15 22 14 17 6 What value is stored in location 2 at the end of step 1? [ array indexes start at 0; steps are numbered starting from 0- at the end of step 0, one value has been correctly positioned.]

18

Suppose we apply the selection sort algorithm (book version) to the array below. 11 9 18 12 8 15 22 14 17 6 At the end of which step number, will the value 9 be guaranteed to be correctly positioned? [ arrays indexes start at 0; steps are numbered starting from 0 - at the end of step 0, one value has been correctly positioned.]

2

This question refers to binary search used in the rank method in class BinarySearch (p. 47). Assume integer keys with sorted keys array: int[] keys = {10,20,30,40,50,60,70}; So initially in rank lo = 0 and hi = 6. Suppose that key = 30. What would be the value of the variable high after two iterations of the while loop?

2

4

Consider the Weighted QuickUnion graph shown. What would be stored in sz[5]?

Suppose we want to count the number of array comparisons done by binary search (page 47) in the worst case for an array of size N. Which of the following is the best ~function for this number?

2 log N

Left child of binary heap node k

2k

Right child of binary heap node k

2k+1

Suppose we want to count the number of array comparisons done by binary search (page 47) in the worst case for an array of size N. Which of the following is the best ~function for this number?

2logN

A max-heap contains 23 nodes. How high is the corresponding complete binary tree?

4

This question refers to binary search used in the rank method in class BinarySearch (p. 47). Assume integer keys with sorted keys array: int[] keys = {10,20,30,40,50,60,70}; So initially in rank lo = 0 and hi = 6. Suppose that key = 50. What would be the value of the variable high after two iterations of the while loop?

4

Consider the code segment below: Node x = new Node(4); Node a = new Node(2); Node c = new Node(5); Node b = new Node(3); x.next = b; a.next = x; b.next = c; If we print the list starting at x, the result would be:

4,3,5

Consider the code segment below: Node x = new Node(4); Node a = new Node(2); Node c = new Node(5); Node b = new Node(3); x.next = b; a.next = x; b.next = c; If we print the list starting at x, what will the result be?

4,3,5

What is unique to Weighted Union Find?

Array stores Ids indirectly Maintains extra array for size

True or False: The implementation of the find method in the QuickUnionUF class is the same as that in the QuickFindUF class.

False

True or false: NlogN grows faster than N^2?

False

The (amortized) complexity of resizing arrays is O(1), provided the array size is doubled when it is resized.

False, always size of the array, N. Fixed and linear

True or False: The (amortized) complexity of the push method for a stack with resizing arrays is O(1), provided the array size is doubled when it is resized.

False, always the size of the array, N. Fixed and linear.

True or False: A bag is a collection based on the FIFO policy.

False, bags are not ordered

Which of the following is correct regarding a worst case input for Selection Sort? a) the data is reverse sorted b) the data is already sorted c) there is no worst case - they are all the same. d) none of these

C

sz[0]

Consider the Weighted QuickUnion graph below. Suppose we perform Union(1,3). What sz element would change?

4

Consider the Weighted QuickUnion graph below. What value is returned by find(9)?

id[7]

Consider the Weighted QuickUnion graph shown. Suppose we perform Union(1,7). What id element would change?

5

Consider the Weighted QuickUnion graph shown. What would be stored in id[6]?

Application for Union/Find

Dynamic Connectivity Problem

What is the tilde approximation for the function (N^3)/4)-((N^2)/2)+18

N^3/4

The Playground class we have been using contains only one instance variable, first. Yet an instance of Playground can 'store' a linked list of 1000's of nodes. What best explains this?

First is a Node reference and every Node contains a reference to another Node.

how to find( ) in quick Union

Follow ids

How many times can you double something before maxing out?

LogN

Quick Find order of growth for union

N

Quick Union Order of growth find

N

Weighted quick union order of growth union

N

Quick-Union Order of growth for Union

N!

Insertion Sort order of growth worst case

N^2

Order of Growth for the number of comparisons done by Insertion sort in average case for array of size N

N^2

Selection sort order of growth worst case

N^2

Insertion sort worst case for compares and exchanges

N^2 /2compares and 1/2N^2 exchanges (Combined tilde of N^2/2)

3-sum fast order of growth

N^2 log N

Selection sort tilde compares and exchanges

N^2/2 compares and N exchanges

What is the rate of growth for ThreeSumFast algorithm?

N^2LogN

3-sum fast Order of Growth

N^2logN

What technique used to speed up the ThreeSum algorithm (1.4)?

Use binary search on a sorted array

What describes the java interface?

a collection of method headers

Recall the Playground example from class (algs13). This class has lots of code in it; however from a client perspective - as a datatype, what best describes this class?

a linked list of doubles

Consider the java program segment below: int oc = 0; int num = StdIn.readInt(); for (int i = 1; i < num; i++) { int val = StdIn.readInt(); if ( val % 2 != 0) oc++; else StdOut.println(val); } StdOut.println( " the tally is " + oc); Which one of the input sets below would result in exactly 4 lines of output? a) 5 6 4 2 3 9 8 7 5 1 b) 5 3 6 8 9 7 8 9 c) 6 9 8 5 3 4 13 12 d) none of these e) 4 2 4 5 6 7 8 9 4

a) 5 6 4 2 3 9 8 7 5 1

The Playground class we have been using contains only one instance variable, first. Yet an instance of Playground can 'store' a linked list of 1000's of nodes. Which of the following best explains this? a)The client can create as many instances of Playground as they want. b)First is a Node reference and every Node contains a reference to another Node. c)The client can create as many instances of Node as they want. d)The client can create an array of Nodes inside the playground instance. e)None of these is a good explanation

b

Iterator class methods

boolean hasNext(); Item next();

Which one of the functions below best represents the (worst case) order of growth of the binary search algorithm for a sorted array of size N? a)constant time b)N c)log N d)N log N

c

What single method is defined by the Comparable interface?

compareTo

Quick union: items are connected if and only if

find ( ) leads to the same root

Parent of binary heap node k

k/2

Average Case Binary Search

lgN

Binary search tilde

lgN

Weighted quick union order of growth

lgN

Weighted quick union order of growth worst case

lgN

Worst Case Binary Search Order of Growth

lgN

insert heap order of growth worst case

logN

remove max heap order of growth worst case

logN

Binary Search Code

public class Binary Search public static int rank(int key, int[]a) { int lo=0; int hi=a.length-1; while (lo<=hi){ int mid=lo+(hi-lo)/2 if (key<a[mid]) hi=mid-1; else if(key>a[mid] lo=mid+1; else return mid; } return -1;

Code for quick union union();

public void union (int p, int q) { int idp = find(p) int idq = find(q) if(idp==idq) return; id[idp]=idq; N--;


Ensembles d'études connexes

Thigh: tensor fasscia latae, fascia latae, and iliotibial band

View Set

General Psychology (PY21051) - Chapter 6,7, and 8 Exam

View Set

Chapter 2 Reconstruction Study Guide

View Set