ALG #1

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

Computational problem solving: Estimating problem solving time: Suppose there are three algorithms to solve a problem- a O(n) algorithm (A1), a O(nlogn) algorithm (A2) and a O(n2 ) algorithm (A3) where log is to the base 2. Using the techniques and assumptions in slide set L2- Buffet(SelectionProblem).ppt, determine how long in seconds it will take for each algorithm to solve a problem of size 200 million. You must show your work to get credit, i.e., a correct answer without showing how it was arrived at will receive zero credit.

Assumed Machines Time = 2 x 107 steps per second O(n): 2 x 108 / 2 x 107 = 10 seconds O(nlogn): 2 x 108 (ln(2 x 108 )) / 2 x 107 = 275.8 seconds O(n2 ): (2 x 108 ) 2 / 2 x 107 = 2 x 109 seconds or 63.4 years

Function g (n: nonnegative integer) if n ≤ 1 then return(n) else return(5*g(n-1) - 6*g(n-2)) Prove by induction that algorithm g is correct, if it is intended to compute the function 3n -2 n for all n ≥ 0.

Base Case Proof: If n = 0 in the function, 0 is returned If n = 1 in the function, 1 is returned. 31 - 2 1 = 1 Inductive Hypothesis: We shall propose that g(n) is true for any value that n > 1. Inductive Step: Show g(n) = 3n - 2 n . g(n) = 5*g(n) - 6*g(n-1). = 5*(3n -2 n ) - 6*(3 n-1 -2 n-1 ) = 5*(3n ) - 5*(2 n ) - 2*(3 n ) - 3*(2 n ) = 3*(3n ) - 2*(2 n ) = 3 n+1 - 2 n+1 Proves that g(n) is true for any value that n > 1.

Computational problem solving: Developing strategies: An array A contains n−1 unique integers in the range [0,n−1]; that is, there is one number from this range that is not in A. Describe a strategy (not an algorithm) for finding that number. You are allowed to use only a constant number of additional spaces beside the array A itself.

I would find the sum of all the integers in the range [0,n-1]. Then I would find the sum off all the actual integers in array A. From there I would subtract the sums and the missing integer from array A would be the answer.

Explain a correct and efficient strategy to check what the maximum difference is between any pair of numbers in an array containing n numbers. Your description should be such that the strategy is clear, but at the same time, the description should be at the level of a strategy, not an algorithm. Then state the total number of number pairs any algorithm using the strategy "compute the difference between every number pair in the array and select that pair with the largest difference" will have to consider as a function of n.

I would iterate over the array using the most efficient sorting method I could find. During this iteration, I would find the maximum and minimum of the array. Then I would subtract the maximum and minimum to find the difference. The algorithm would have to consider 2 numbers as a function of n.

Suppose you are asked to develop a mobile application to provide turn by turn directions on a smartphone to an AU parking lot in which there are at least five empty parking spots nearest to a campus building that a user selects. Assume that you can use the Google Map API for two functions (only) ─ display campus map on the phone so user can select a campus building, and produce turn-byturn directions from a source location to a destination location ─ where any location in the map is specified as a pair (latitude, longitude). Also assume that there is an application called AUparking that you can query to determine the # of vacant spots in any parking lot specified as a pair (latitude, longitude). Specify the problem to a level of detail that would allow you to develop solution strategies and corresponding algorithms: State the problem specification in terms of (1) inputs, (2) data representation and (3) desired outputs; no need to discuss solution strategies.

Inputs: current location, desired campus building Data Representation: From all inputs, the important data that would need to be derived from them would be the latitude and longitude. Each would be stored in a float and could be separated into individual variables or stored a 2-dimensional array [long, latitude]. The exported data after used by the interfaces given to us would be varied. The number of spots would be stored as integers. The directions would take us from coordinate to coordinate which would be used as floats again. If there are verbal directions, they would be stored as strings. Desired Outputs: set of turning directions to available spots on auburn campus, data which allows ability for user to determine which spot is closest to campus building

The algorithm of Q.11 can also be proven correct using the Loop Invariant method. The proof will first show that it will correctly compute F0 & F1 by virtue of lines 1 and 2, and then show that it will correctly compute Fn, n>1, using the LI technique on the for loop. For this latter part of the correctness proof, complete the Loop Invariant below by filing in the blanks. Then complete the three parts of the rest of the proof.

Loop Invariant: Before any execution of the for loop of line 5 in which the loop variable i=k, 2≤k≤n, the variable last will contain F0 and the variable current will contain F1. Initialization: When initialized the value of 1 or F0 is stored in last and 1 or F1 is stored in current. Maintenance will then begin. Maintenance: The values stored in last and current (1 also known as F0 and F1) are then added together into temp. Current is then moved into last and temp is moved into current. The current value is now initialized to represent F2 and the last value represents F1. This pattern of upscaling the Fibonacci sequence will continue until the for loop reaches termination. Termination: The loop terminates when it reaches a value greater than n. N is number in the Fibonacci sequence we are looking for. The correct n value is returned, and the loop knows when to correctly terminate.

What is the approximate time complexity (running time) of the above algorithm (you can use Big-Oh notation)

O(n3 ) there are 3 nested for loops

Is the algorithm below correct or incorrect? Prove it! It is supposed to count the number of all identical integers that appear consecutively in a file of integers. E.g., if f contains 1 2 3 3 3 4 3 5 6 6 7 8 8 8 8 then the correct answer is 9

Proof by Counterexample: I believe this coded algorithm is incorrect. If the input 1 2 2 3 is input, the correct answer should be 1. Incorrectly, the answer 0 is returned. This is because i = 1 and j = 2 will be compared and then i = 2 and j = 3 will be compared. Only pairs of 2 are compared in this algorithm because it uses read next.

Explain what the following algorithm outputs and simulate its operation on a valid input instance (e.g., an array of n elements - you can choose n to be 10)

The following algorithm simulated shows the largest series of values in the array. Using an input of n being 8 with the values [1, 2, 3, 4, 0, 1, 1, 2]. The sum of 10 would be returned. Because 1 + 2 + 3 + 4 is the greatest series of the array. After looping through the nested loops and adding all of them into the sum. Once we hit number 0 then the sum would not be lower than the stored max. This would allow the correct series to be returned.

Bill has an algorithm, find2D, to find an element x in an n×n array A. The algorithm find2D iterates over the rows of A and calls the algorithm arrayFind (see below) on each one, until x is found or it has searched all rows of A. What is the worst-case running time of find2D in terms of n? Is this a lineartime algorithm? Why or why not?

The worst-case running time of find2D in terms of n is O(n2 ) because arrayFind has a worst-case time complexity of O(n) and find2D iterates over an nxn array. Therefore, this is not a linear-time algorithm because linear time algorithms have the time complexity of O(n).

How does the following algorithm improve the time complexity of the algorithm? What is its time complexity?

This algorithm improves the time complexity because there are only 2 nested loops and one of them is independent. The time complexity is O(n2 ).

Computational problem solving: Developing strategies: Given a string, S, of n digits in the range from 0 to 9, describe an efficient strategy for converting S into the integer it represents.

To solve this problem, I would iterate over the string and then convert each character into the integer it represents. If this solves the problem, then leave it like this. If I need a singular whole number to represent the string then after converting in the previous iteration, I would use a variable called count and sum each integer represented into it, to get the total number.

Draw your algorithm's recursion tree on input string "i<33270!"- remember to show inputs and outputs of each recursive execution including the execution of any base cases.

i<33270! Is input as A, p=0 q=8. !<33270i is returned and then recursively called p=1 q=7.. !03327 q = 4.


Ensembles d'études connexes

Ch. 11: Statistical Inference: One-Sample Confidence Interval

View Set

skills final (back of the book questions and online)

View Set

BCOM Chapter 7 Email and other traditional tools for business

View Set

Chapter 24 Lymphatic System Questions

View Set

Chapter 31- Environmental Emergencies

View Set

Knowledge Check Questions Agile Scrum Master

View Set