Questions for Computer Algorithms
Total Page:16
File Type:pdf, Size:1020Kb
www.YoYoBrain.com - Accelerators for Memory and Learning Questions for computer algorithms Category: Default - (246 questions) Define: symmetric key encryption also known as secret-key encryption, is a cryptography technique that uses a single secret key to both encrypt and decrypt data. Define: cipher text encrypted text Define: asymmetric encryption also known as public-key encryption, relies on key pairs. In a key pair, there is one public key and one private key. Define: hash is a checksum that is unique to a specific file or piece of data. Can be used to verify that a file has not been modified after the hash was generated. Algorithms: describe insertion sort algorithm Start with empty hand and cards face down on the table. Remove one card at a time from the table and insert it into the correct position in left hand. To find the correct position for a card, we compare it with each of the cards already in the hand, from right to left. Algorithms: loop invariant A loop is a statement which allows code to be repeatedly executed;an invariant of a loop is a property that holds before (and after) each repetition Algorithms: selection sort algorithm Sorting n numbers stored in array A by first finding the smallest element of A and exchanging it with A[1], then the second smallest and exchanging it with A[2], etc. Algorithms: merge sort Recursive formula1.) Divide the n-element sequence into 2 sub sequences on n/2 elements each2.) Conquer - sort the 2 sub sequences using merge sort3.) Combine - merge the 2 sorted sub sequences to produce sorted answer. Combine by putting smallest of either array into merged array Algorithms: performance of insertion sort Theta( n^2 ) Algorithms: performance of merge sort Theta( n lg n ) Algorithms: binary search data is sorterdLook at midpoint then you know if it is located higher or lower. Then find mid point of remaining array until element found. Algorithms: asymptotic efficiency of when we are concerned with how the algorithms running time of an algorithm increases with the size of the input in the limit (as input goes to infinity) Algorithms: difference between Theta( n^2 ), Theta has asymptotic upper and lower and O( n^2 ) and Omega( n^2) boundsO has only upper boundOmega has only lower bound Algorithms: Special notation to indicate equality of remaindersIf (a mod n ) = ( b mod n ) Algorithms: we say that a function f(n) is polylogarithmically bounded if f(n) = ______ Algorithms:n! = o( ____ )n! = omega( ____ ) n ^ n2 ^ n Algorithms: little oh notationo( n^2 ) indicates an upper bound that is not asymptotically tight Algorithms: little omega notation omega( denotes the lower bound that is not n^2 ) asymptotically tight Algorithms: master method for solving for some constant epsilon > 0, then recurrences of the form T(n) = a T( n/b ) + f(n)case 1 ( - epsilon ) Algorithms: master method for solving recurrences of the form T(n) = a T( n/b ) + f(n)case 2 ( = ) Algorithms: master method for solving for some constant epsilon > 0 and if a f(n/b) recurrences of the form T(n) = a T( n/b ) + <= c f(n) for some constant c < 1 and at f(n)case 3 ( + epsilon) sufficiently large n, then Algorithms: describe coupon collector's a person trying to collect each of b different problem coupons must acquire approximately ( b ln b ) randomly obtained coupons in order to succeed Algorithms: binary heap data structure an array object that can be viewed as a nearly complete binary tree.Each node of the tree corresponds to an element of the array that stores the value in the node.The tree is completely filled on all levels except possibly the lowest, which is filled from left up to a point. Algorithms: Binary heap structure1.) 1.) return [ i / 2 ]2.) return 2i3.) return 2i + 1 Parent(i)2.) Left(i)3.) Right(i) Algorithms: max-heap versus min-heap 1. max-heap property A[ Parent(i) ] >= A[i]2. min heap property A[ Parent(i) ] <= A[i] Algorithms: d-ary heap like a binary heap, but non-leaf nodes have d children instead of 2 Algorithms: m x n Young tableau a m x n matrix such that the entries of each row are in sorted order from left to right and the entries of each column are in sorted order from top to bottom Algorithms: heap-sort algorithm 1.) Build a max-heap data structure in O(n) time2.) Root node is greatest value, so grab for end of array, then run max-heapify to recreate the heap and grab next rootruns in O( n ln n ) time Algorithms: good data structure to maintain a max-heap data structure max-priority queue Algorithms: meaning of floor operator of x Algorithms: meaning of ceiling operator on x Algorithms: selection sort algorithm Go through entire array and find smallest element. Swap with first element.Then repeat with element 2 through n Algorithms: performance of selection sort Theta( n^2 ) Algorithms: quicksort's worst case running Theta( n^2 )Theta( n lg(n) ) time and average-case running time Algorithms: quicksort algorithm choose any array element that is in slots p through r. Call this pivot element. Rearrange the elements so that all elements smaller than pivot are to the left and larger elements to the right. This is called partitioning. Then repeat recursively for each partition. Algorithms: In the worst case, any Omega( n ln(n) ) comparison sorting algorithm for n elements requires ___ comparison between pairs of elements Algorithms: partition method for quicksort rearranges the sub array A[p ... r ] in placex algorithm <- A[r]i <- p - 1for j <- p to r-1..do if A[j] <= x.....then i -< i + 1.......exchange A[i] <- A[j]exchange A[i+1] <- A[r]return i + 1 Algorithms: randomized quicksort algorithm instead of always picking right most element as pivot, pick random element and swap with right most before partition Algorithms: basic idea behind counting sort Assumes each of the n input elements is an integer in the range of 0 to k, for some integer k.Determine, for each input element of x, the number of elements less than x. This info can be used to place element x directly into its position in output array. Algorithms: performance of counting sort O( n ) Algorithms: radix sort sorts based on least significant digit first up to most significant.Digit 1 is lowest order digit and d is highestfor i <- 1 to d... do a stable sort to sort array A on digit i Algorithms: performance of bucket sort runs in linear time when the input is drawn from uniform distribution Algorithms: bucket sort algorithm Divide the interval [0, 1) into n equal sized subintervals, or buckets, and then distribute the n input numbers into buckets. Then do insertion sort on buckets. Algorithms: the ith order statistic of a set of n the ith smallest element elements is ______ Algorithms: define selection problem Input: a set A of n (distinct) numbers and a number i, with 1 <= i <= nOutput: the element x is a member of A that is larger than exactly i-1 other elements of A Algorithms: asymptotic running time for Theta( n ) selection problem Algorithm: queue implements a first-in, first-out (FIFO) storage policy Algorithms: stack implements a last-in, first-out (LIFO) storage policy Algorithms: stack - insert is often called PUSHPOP _____ and delete is often called _____ Algorithms: queue - insert is often called EnqueueDequeue ____ and delete operation is called _____ Algorithms: linked list data structure with a linear order maintained by pointers in each object Algorithms: scheme for using binary trees to left-child, right-sibling representations represent trees with arbitrary numbers of children Algorithms: direct-address table Used when universe U of keys is reasonably small small. Denoted by T[0, ... m-1], in which each position, or slot, corresponds to a key in the universe U. Slot k points to an element in the set with key K. Algorithms: hash table With hashing, element is stored in slot h(K); that is, we use a hash function h to compute the slot from key K. Algorithms: how is collision handled (2 keys used a chained linked list for keys that collide hash to same spot) in hash table Algorithms: load factor for hashing table for a hash table T with m slots that stores n elements, it is n/m, average number of elements stored in a chain Algorithms: search performance of a hash worst case Theta(n) which corresponds to all table elements stored in one keyexpected search time Theta( 1 + alpha) with alpha = load factor Algorithms: division method for hash table h(k) = k mod n hash function Algorithms: hash function multiplication h(k) = lower bound ( m ( k A mod 1 ) )multiply method for hash table the key by a constant A in range of 0 < A < 1 and extract the fractional part of k * Athen multiply by m and take the floor of the result Algorithms: universal hashing to select the hash function at random from carefully designed class of functions at the beginning of execution. Guarantees that no single input will always evoke worst-case behavior Algorithms: open addressing in hash table all elements are stored in hash table itself Algorithms: how does insertion work in hash we successively exam, or probe, the hash table with open addressing table we find an empty slot into which to put the key Algorithms: linear programming for hash h( k, i ) = ( h'(k) + i ) mod m function in hash table with open addressing Algorithms: problem with linear probing in primary clustering = long runs of occupied open addressing hash tables slots build up, increasing average search time Algorithms: quadratic probing in open use a hash function of the formh(k, i) = ( h'(k) addressing hash table + c1*i + c2*i^2 ) mod m Algorithms: double hashing in open address hash function h(k,i) = ( h1(k) + i h2(k) ) mod hash tables mwhere h1, h2 are auxiliary hash functions Algorithms: perfect hashing use 2 levels hashing scheme with universal hashing at each level.