2. Trace the following algorithm and determine the return value: Name : Algorithm AB Input : None Output: An integer 1 a = 0 2 b = 20 3 while a < b : 4 a = a + 1 5 b = b - 2 6 end while 7 return a - b
1 a=0
2 b=20
3 while a<b
4 a=a+1
5 b=b-2
6 end while
7 return a-b
Trace:
Initially
a=0
b=20
1st iteration
while(a<b) (0<20 which is true )
a=a+1 =0+1=1
b=b-2=20-2=18
2nd iteration
while(a<b) (1<18 which is true )
a=a+1 =1+1=2
b=b-2=18-2=16
3rd iteration
while(a<b) (2<16 which is true )
a=a+1 =2+1=3
b=b-2=16-2=14
4th iteration
while(a<b) (3<14 which is true )
a=a+1 =3+1=4
b=b-2=14-2=12
5th iteration
while(a<b) (4<12 which is true )
a=a+1 =4+1=5
b=b-2=12-2=10
6th iteration
while(a<b) (5<10 which is true )
a=a+1 =5+1=6
b=b-2=10-2=8
7th iteration
while(a<b) (6<8 which is true )
a=a+1 =6+1=7
b=b-2=8-2=6
8th iteration
while(a<b) (7<6 which is false loop fails)
end while loop
return a-b (7-6=1)
there fore output =1
2. Trace the following algorithm and determine the return value: Name : Algorithm AB Input :...
Given algorithm- procedure factorial (n: nonnegative integer) if n = 0 then return 1 else return n*factorial(n-1) {output is n!} Trace the above algorithm when it is given n = 7 as input. That is, show all steps used by above algorithm to find 7!
Please write and draw the recursive trace, and please explain,
thank you!
Problem 1. [26 pts] Given the Fibonacci numbers, defined as: Fo 0, F1-1, F k Fk2 write or draw the recursive trace of the calculation of the 5th Fibonacci number (Fs 5) with the following Algorithm (which is based on linear recursion) Algorithm Fibonacci Linear (k) Input: a positive integer value k Output: a pair of Fibonacci numbers (F., F..) if k < 2 then R-1 return (R,...
Consider the following algorithm: ocedure Algorithm (b: integer, n: positive integer,i datinct integem) proc answer :", 0 nand 6 while (j print(j, z, b, answer) if jSn then answer:-j return answer (8 points] Assume that this algorithm receives as input the numbers b-17 andn9nd the corresponding sequence or iaie i 2 3 4 516 7 8 corresponding sequence of integers 19 Fill out the table below: i 는, ↓answer (b) [I point] Assume that the algorithm receives the same input...
32. (6 points) Trace the Text Search Algorithm for the input t"0101001' and p "001 Input: p( indexed from 1 to m),m, t (indexed from 1 to n),n Output: i text search(p,m,t,n) f For i= 1 to n-m+1( while (ti+/-1-= pj){ jj+1 If (j> m) Return i return 0
32. (6 points) Trace the Text Search Algorithm for the input t"0101001' and p "001 Input: p( indexed from 1 to m),m, t (indexed from 1 to n),n Output: i text...
2. Prove that abSearch returns ?1 if str does not contain “ab”
as a substring. You may omit the base case for this proof (done in
problem 1). Hint: you will need to think carefully about the
different cases of the if statement in lines 15–23 to show that
abSearch always returns ?1 when str doesn’t contain “ab.”
Answer the following questions about the algorithm below, which searches an input string for the substring "ab" and returns where "ab" first...
Data Structures – Test D (Java)
Trace Dijkstra’s algorithm starting from for the graph
represented in the handout.
From the algorithm’s output, determine the shortest path from
to ?
We were unable to transcribe this imageWe were unable to transcribe this imageWe were unable to transcribe this imageTest B To AM 7 6 4 3 2 0 1 11 5 0 From 2 7 7 5 1 2 1 1 7 3 5 4 6 1 5 4 2 6...
1) 2) 3) integer A, B; input (A): while (A> 0) 5) A=2*A; 6) i (A < 20 or A> 30) 7) 8) 9) 10 lse B-A 2, 12) B-A 2 13) 14) output (A, B): 15) input (A): 16) end;
ALGORITHM RecS(n) // Input: A nonnegative integer n ifn=0 return 0 else return RecS(n+ n n n Determine what this algorithm computes. You must justify your answer. made by this algorithm and solve it. You must justify your answer. same thing using for/while loop(s) developed in (3). You must justify your answer. 1) 2) Set up the initial condition and recurrence relation for the number of multiplications 3) Write the pseudocode for the non-recursive version of this algorithm, i.e., compute...
Given the following algorithm:
Algorithnm Input: a1, a2,...,an, a sequence of numbers n, the length of the sequence x, a number Output: ?? i:- 1 While (x2 # a, and i < n) i+1 End-while If (x- - a) Return(i) Return(-1) 3, -1, 2,9, 36,-7, 6,4 a) What is the correct output of the Algorithm with the following input: a1, a2,..an b) What is the asymptotic worst-case time complexity of the Algorithm?
Algorithnm Input: a1, a2,...,an, a sequence of numbers...
Consider the following problem: Input: a list of n-1 integers and these integers are in the range of 1 to n. There are no duplicates in list. One of the integers from 1 to n is missing in the list. Output: find the missing integer Let the input array be [2, 4, 1, 6, 3, 7, 8]. Elements in this list are in the range of 1 to 8. There are no duplicates, and 5 is missing. Your algorithm needs...