Algorithms
Which of the following is always true when adding an element to a heap? a) the new element will always be a leaf b) the new element will always be a root c) the new element will always be an internal node d) the new element will always have 2 children e) none of the above is always true
E when adding an element to a heap, it can end up anywhere in the tree
In the quick union implementation, suppose we set id[p] to id[root(q)] instead of setting id[root(p)]. Would the resulting algorithm be correct?
False
Bubble Sort works by separating a list into two lists, recursively sorting the two lists using bubble sort, and then combining the sorted sublists into a sorted list.
False, bubble sort is not a recursive sort
Bubble sort works by separating a list into two lists, recursively sorting the two lists using bubble, sort and then combining the sorted sub lists into a sorted list.
False, bubble sort is not a recursive sort the sort described is Merge sort.
To represent constant time complexity we use O(c)?
False, to represent constant time complexity we use O(1) because an algorithms growth rate is constant.
Which algorithm has the worst case complexity of O(n^2)?
Insertion Sort Selection Sort Quick Sort Bubble Sort
What is the minimum number of items that must be exchanged during a remove the maximum operation in a heap of size N with no duplicate keys?
2
Which of the following statements are true? a) a heap is a complete binary tree b) a binary tree is complete if every level of the tree is full except that the last level may not be full and all the leaves on the last level are placed left-most c) each node is greater than or equal to any of its children d) a heap is full binary tree
A a heap is a COMPLETE binary tree B a binary tree is complete if every level of the tree is full except that the last level may not be full and all the leaves on the last level are placed left most C each node is greater thn or equal to any of its children
Explain how an element is added to a heap
An element is added to a heap as a leaf, then it is moved up the tree until the tree has the heap property again
Give a reason that shows why this intuitive implementation of union() for quick-find is not correct: public void union(int p, int q) { if (connected(p,q)) return; for(int i = 0; i < id.length; i++) if (id[i] == id[p]) id[i] = id[q]; count --; }
The value of id[p] changes to id[q] in the for loop
Suppose we would like to swap the elements at index i and index j in an array. Does the following code work? array[i] = array[j] array[j] = array[i]
This fails because the first step overwrites the element stored in array[i] A temp variable is required to properly swap the two elements in the array.
The selection sort algorithm sorts a list of values by repeatedly putting a particular value into its final, sorted position.
True
In the weighted quick-union implementation, suppose we set id[root(p)] to q instead of id[root(q)]. Would the resulting algorithm be correct?
True! However, it would increase the height of the tree, so the performance guarantee would be invalid
Suppose that we use Insertion sort on a randomly ordered array where items have only one of three key values. Is the running time linear, quadratic, or something in between?
Quadratic
Suppose that we use insertion sort on a randomly ordered array where items have only one of three key values. Is the running time linear, quadratic, or something else?
Quadratic
Too slow for large problems
Quick Find it uses quadratic time
The ________________ algorithm sorts values by repeatedly comparing neighboring elements in the list and swapping their position if they are not in order relative to each other.
Bubble Sort
The _______________________ algorithm sorts values by repeatedly comparing neighboring elements in the list and swapping their position if they are not in order relative to each other
Bubble Sort
Write a recursive method to compute the sum of integers form 1 to some positive value, say n. Time Complexity?
public int sum(int n){ int result; if (n==1) result =1; else result = n + sum(n-1) return result; } O(n)
Which method runs fastest for an array with all key identical, Selection Sort or Insertion Sort?
Insertion Sort runs in linear time when all keys are equal
Which method runs fastest for an array with all keys identical, Selection sort or Insertion sort?
Insertion sort runs in linear time when all keys are equal
Worst Case Analysis is very useful becuase
It can be shown that the algorithm will never be slower than the worst case
Prove that sink based heap construction uses at most 2N comparisions and at most N exchanges
It suffices to prove that sink based heap construction uses fewer than N exchanges because the number of comparisons is at most twice the number of exchanges. For simplicity assume that a binary heap is perfect and has height h
Worst Case
Longest execution time
Method for finding Fibonacci Number Time Complexity?
public static long fib(long index){ if(index == 0) // base case return 0; else if(index == 1) // base case return 1; else // reduction and recursive calls return fib(index-1)+fib(index-2) O(2^n)
O(n log n)
Mergesort and Heapsort are sorting algorithms that perform ____________________ comparisons
About how many comparisons will Quick.sort( ) make when sorting an array of N items that are all equal?
N lg N compares Each partition will divide the array in half, plus or minus one.
Is the array {1,2,4,5,9,3} a heap?
No for a node at position i, its left child is at position 2i+l and is right child is at position 2i+2 and is parrent is (i-2)/2
Linearithmic Time
O(n log n) time: Arise in divide in conquer algorithms
Linear Time
O(n) running time is at most a constant factor times the size of the input
Quadratic Time
O(n^2)
Suppose that your application will have a huge number of find the maximum operations, but a relatively small number of insert and remove the maximum operations. Which priority queue implementation do you think will be most effective: heap, unordered array, ordered array?
Ordered array. Find maximum is constant time
Which data structure is appropriate to store patients in an emergency room? Stack Queue Priority Queue Linked List
Priority Queue
Best Case
Shortest execution time
Suppose we have algorithms that solve a particular problem that have the following complexities: O(1) O(log2n) O(n^2) O(n^3) O(2^n) Which one is the most efficient?
The algorithm with constant time complexity O(1) is the most EFFICIENT
Growth Function
The of an algorithm shows the relationship between the size of the problem and the value we hope to optimize
Suppose that your application will have a huge number of inserted operations, but only a few remove the maximum operations. which priority queue implementation do you think would be the most effective: heap unordered array ordered array
Unordered array. Insert is constant time
Criticize the following idea: to implement find the maximum in constant time why not keep track of the maximum value inserted so far, then return that value for find the maximum?
We will need to update the maximum value from scratch after a remove the maximum operation
Is the array {64.42.59.32.39.44} a heap?
Yes
Is an array that is sorted in decreasing order a max oriented heap
Yes!
Does the abstract in place merge produce proper output if and only if the two input subarrays are in sorted order?
Yes! If the subarrays are in sorted order, then the in place merge produces proper output If one subarray is not in sorted order, then its entries will appear in the output in the same order that they appear in the input.
What is an Algorithm?
a set of well-defined steps for performing a task or solving a problem
Binary Search
an efficient algorithm for finding an item from a sorted list of items. It works by repeatedly dividing in half the portion of the list that could contain the item, until you've narrowed down the possible locations to just one.
Recursive Counting #bits
if n = 1 return 1 else return BinRec([n/2])+1