Exercise 1
Use Top-Down Design to “design” a set of instructions to write an algorithm for “travel arrangement”. For example, at a high level of abstraction, the algorithm for “travel arrangement” is: book a hotel buy a plane ticket rent a car Using the principle of stepwise refinement, write more detailed pseudocode for each of these three steps at a lower level of abstraction.
Exercise 2
Asymptotic Complexity (3 pts) Determine the Big-O notation for the following growth functions: 1. a(n) = 4n 3 + 2n 2 2. b(n) = log(n) + n 2 1000 + n 3. c(n) = 10 log(n) + 3n log(n) + 2 log log(n) 4. (Bonus question: 1 pt) d(n) = n! 100 + n 43 + 4n 5. (Bonus question: 1 pt) e(n) = 2n + n 7
Exercise 3
Consider the following loop.
i ← 0 while(i<n)
helperFunction(...)
i ←i+1
done
1. What is the worst case time complexity (Big-O) of this loop if helperFunction(. . . ) is just a single operation?
2. What is the worst case time complexity (Big-O) of this loop if helperFuncton(. . . ) is an O(n log(n)) algorithm?
3. (Bonus question: 2 pts) What is the worst case time complexity (Big-O) of this loop if helperFuncton(. . . ) is an O(n 2 ) algorithm?
4. (Bonus question: 5 pts) What is the worst case time complexity (Big-O) of this loop if helperFuncton(. . . ) is an O(i) algorithm? (Note that i is the loop counter, so helperFuncton(. . . ) does more work as i increases) Justify your answers and show all your work.
Exercise 4
The following code fragment is a Monte Carlo method for approximating Pi. (for more background, see the link http://mathfaculty.fullerton.edu/mathews/n2003/montecarlopimod.html). Assume that random() is a function that returns a random floating point value in the range from 0 to 1 with uniform density (all values are equally likely); for this question, assume that calling random() is a primitive operation, and requires only one “step.”
i ← 0
count ← 0
while(i<n)
x ← random()
y ← random()
if (x^2 + y^2 <= 1)
count ← count+1 i ← i + 1 done pi ← 4*count/n
1. Use the statement counting approach from the lecture notes to determine the number of operations on each line of the program.
2. Determine the number of primitive operations that will be done in the Worst case as a function of n.
3. Determine the number of primitive operations that will be done in the Best case as a function of n.
4. What is the worst case time complexity (Big-O) of this code fragment? Justify your answers and
show all your work.
Solution:
Note: Exercise 1 and 2 are solved as per the Chegg guidelines, please repost others.
Exercise 1:
The detailed pseudocode is given below:
Pseudocode:
Exercise 2:
1. a(n) = 4n 3 + 2n 2 2. b(n) = log(n) + n 2 1000 + n 3. c(n) = 10 log(n) + 3n log(n) + 2 log log(n) 4. (Bonus question: 1 pt) d(n) = n! 100 + n 43 + 4n 5. (Bonus question: 1 pt) e(n) = 2n + n 7
Let's have a look at Big-O first.
1. a(n) = 4n^3 + 2n^2
4n^3 + 2n^2<= c*n^3
which means
a(n)= O(n^3)
2. b(n) = log(n) + n^2 1000 + n^3
b(n)= O(n^3)
3. c(n) = 10 log(n) + 3n log(n) + 2 log log(n)
c(n)= O(n log n)
4. d(n) = n! 100 + n^43 + 4n^5
d(n)= O(n^n)
please let me know if I have missed or mistaken on taking the notations in exercise 2.
Please give it a thumbs up if this helped you, also provide your valuable feedback.
Exercise 1 Use Top-Down Design to “design” a set of instructions to write an algorithm for...
(a) Design an algorithm that reveals some secret integer number from the set {1, 2, ... , n} by guessing random numbers within the given range until the secret number has been guessed. Numbers should be replaced after being guessed such that it is possible to guess 2 and then 2 again, assuming 2 is in the given range. The algorithm should return both the secret number as well as the number of guesses taken. (b) If possible, calculate the...
Which big-O expression best characterizes the worst case time complexity of the following code? public static int foo(int N) ( int count = 0; int i1; while (i <N) C for (int j = 1; j < N; j=j+2) { count++ i=i+2; return count; A. O(log log N) B. O(log N2) C. O(N log N) D. O(N2)
1. Exercise 3.7 of the textbook. An algorithm prints the following pattern: * * * * * * * * * * * * * * * A. What are the basic operations performed by the algorithm that you would count towards its running time? B. Count the number of these basic operations for the specific output shown above. C. The number of lines printed in the preceding pattern is 5. Assume that the algorithm can extend this pattern for...
(V). Given the following algorithm, answer relevant questions. Algorithm 1 An algorithm 1: procedure WHATISTHIS(21,22,...,n: a list of n integers) for i = 2 to n do c= j=i-1 while (j > 0) do if ra; then break end if 4j+1 = a; j= j-1 end while j+1 = 1 end for 14: return 0.02. 1, 15: end procedure Answer the following questions: (1) Run the algorithm with input (41, 02, 03, 04) = (3, 0, 1,6). Record the values...
Solve ques no. 2 a, b, c, d . Algorithm 1 Sort a list al,..., an for i=1 to n-1 do for j=1 to n-i do if aj > aj+1 then interchange a; and a;+1 end if end for end for (b) Algorithm 1 describes a sorting algorithm called bubble sort for a list al,...,an of at least two numbers. Prove that the algorithm is complete, correct and terminates. (2) Complexity of Algorithms (Learning Target C2) (a) What is the...
Question 2 Consider the following algorithm Fun that takes array A and key Kas Fun(AO,...,n - 1], K) count = 0 for i = 0 ton - 1 do for j = i +1 to n - 1 do if A[i] + A[j] == K then count = count +1 end if end for end for return count What is the best case time complexity of the above algorithm?! (log(n)) O(1) (n) (na) Previous o H H 9
1) A Java program that implements an insertion sort algorithm. (InsertionSort.java): Must include the proper comments in the code. 2) A report in pdf. It contains: a) the pseudocode of the insertion sort algorithm and assertions between lines of the algorithm. b) As insertion sort has a nested-loop of two layers, you need to put one assertion in each loop. c) a proof of the partial correctness of the algorithm using mathematical induction c.1) prove the correctness of the two...
1. Design and write a Divide& Conquer algorithm that, given an array A of n distinct integers which is already sorted into ascending order, will find if there is some i such that Ali] in worst-case 0(log n) time.
(d) Consider an algorithm A, whose runtime is dependent on some "size" variable n of the input. Explain the difference between the two statements below, and give an explicit example of an algorithm for which one statement is true but the other is false. 1. The worst case time complexity of A is n2. 2. A is O(n). (e) Give an example of an algorithm (with a clear input type) which has a Big-Oh (0) and Big-Omega (12) bound on...
Exercise 7.3.5: Worst-case time complexity - mystery algorithm. The algorithm below makes some changes to an input sequence of numbers. MysteryAlgorithm Input: a1, a2....,an n, the length of the sequence. p, a number Output: ?? i != 1 j:=n While (i < j) While (i <j and a < p) i:= i + 1 End-while While (i <j and a 2 p) j:=j-1 End-while If (i < j), swap a, and a End-while Return( aj, a2,...,an) (a) Describe in English...