hw1
DESCRIPTION
HW1TRANSCRIPT
-
dsa homework #1due date : march 24, 2015
Name : Jui-Hui ChungID : B02202008
1 More from the class
(1) The Art of Computer Programming is a comprehensive monograph written byDonald Knuth that covers many kinds of programming algorithms and their analysis.It is the bible of all fundamental algorithms and the work that taught many of todayssoftware developers most of what they know about computer programming. As Bill Gateshas said : "If you think youre a really good programmer... read [Knuths] Art of ComputerProgramming... You should definitely send me a rsum if you can read the whole thing."
(2) I will prove the correctness of an algorithm by loop invariant in three steps stated in thebook Introduction to Algorithms by mit Press.
Initialization : Prior to the first iteration when i = 0, the counter has not encounteredany character in the given string, and was set to zero, which shows that the loop invariantholds prior to the first iteration of the loop.Maintenance : The counter increase in increment of 1 value after each iteration whenit encounter a cahracter in the given string.Termination : The condition causing for termination is when the loop reach out of thelength of string. Because each loop iteration increases i by 1, we must have i = str.lengthat that time. Hence, the algorithm is correct.
2 Searching for Greatest Common Divisor
(1) The mathematical definition of gcd is that for any integer k > gcd(a, b), (a mod k) 6= 0or (b mod k) 6= 0.
Initialization : Before the first iteration, when i = min(a, b). Any integer j > i willhave min(a, b) mod j 6= 0. Thus I may or may not be the gcd.Maintenance : In each loop, the if statement test the condition whether i is the commondivisor. The body of the loop works in the decrement of 1. The first iteration is wheni =min(a, b) ; if it is the common divisor, it returns the value, which is the greatestcommon divisor. If it is not a common divisor, i decrease by 1 and repeat the abovestatement for the next iteration.Termination : The loop terminates when it reaches 1 (or when it has returned the gcd).Then the value 1 will be the gcd.
(2) The minimum number of iterations is 1, and the situation occurs when (a mod b) = 0.
1
-
3 Filtering for Greatest Common Divisor
(1) It is trivial that gcd is distributive gcd(ma,mb) = mgcd(a, b). Let m = 1/l and mutiplythe equation by l gives gcd(a, b) = l gcd(a/l, b/l) if a/l and b/l are positive integers.
(2) If the pseudo code of the algorithm is true if a correct value is given to , I choose to be1, since it returns after the previous loop termination and that the loop starts with i 2.
(3) It is the first iteration, and should be tested with the smallest possible value, which4 = 2.
4 Binary Algorithm for GCD
(1)
initial n = 14 m = 56 ans = 1
i n = 7 m = 21 ans = 2
ii n = 7 m = 14 ans = 2
iii n = 7 m = 0 ans = 2
return n ans = 14
(2) Inside the while statement, there are three conditions regarding the even number, whichif satisfied the value will be halved. And regardless of parity, on exit m m n andm is the bigger value. So in every iteration there must be decrease in either or both thevalue ; the while will only run for a finite number before termination.
(3) Let gcd(a, b) = d N, we have a = da1, b = db1 with gcd(a1, b1) = 1. Then ab = d(a1b1) and b = db1. Since gcd(a1, b1) = 1 then gcd(a1b1, b1) = 1 (if gcd(a1b1, b1) = m > 1,then m|b1 and m|(a1 b1) + b1 or m|a1, a contradiction to gcd(a1, b1) = 1). Thereforegcd(d(a1 b1), db1) = d (distributive) or gcd(a b, b) = d = gcd(a, b).
5 Euclids Algorithm for GCD
(1)
initial n = 14 m = 56
return n = 14
(2) Let n = n/3,m = m/3, temp = temp/3. Sice m mod n equals 3m mod 3n, the stepsrequired for the algorithm stay the same T = T .
6 Comparison of GCD
(1)
2
-
(2) GCD-By-Reverse-Search averages 1245, GCD-By-Filter averages 483,GCD-By-Filter-Faster averages 481,GCD-By-Binary averages 17 andGCD-By-Euclidaverages 5 iterations. Each algorithm is faster than the previouse one. GCD-By-Euclidis arguably the quickest and GCD-By-Reverse-Search the slowest. AlthoughGCD-By-Filter-Faster is suppose to be quicker thanGCD-By-Filter, they somewhatshare the same complexity.
3