L01 - What is an algorithm? An algorithm is a sequence of...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
CS0250, Set 1 What is an algorithm? An algorithm is a sequence of  precise and concise  instructions that  guide you (or a computer) to solve some  specific  problem. Examples: cooking recipe, VCR trouble shooting instructions,  furniture assembly directions. Note:  Unlike programs, algorithms are free from any grammar rules  (of a programming language). For programs:   form  more important than  content ;  i.e., although  your program has the right ideas to solve the problem, it is still  wrong if it has syntax error. For algorithm:  content  more important than  form ; i.e., it is  acceptable as long as you can guide me to find the solution  correctly; I don’t care what languages (or in whatever ways)  you use.  It is still okay even if there are some trivial details  missing.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS0250, Set 1 An Example: Euclid’s Algorithm First appeared in Euclid, Elements (c. 300BC), Book 7. It solves the following specific problem: input:  two positive integers a, b      output: the greatest common divisor (gcd) of a, b. Example input:  100, 92      output:  gcd(100, 92) = 4 The algorithm (mutual subtraction) Replace the larger number by the difference of the two numbers until both  are equal; then the answer is this common value.
Background image of page 2
CS0250, Set 1 Example      {18, 42}   {18, 24}   {18, 6}   {12, 6}   {6,6} Answer: gcd(18, 42) = 6. A C++ implementation int gcd(int a, int b) { while (a != b) { if (a > b) a = a – b; else b = b – a; } return a; } You can describe an algorithm in whatever form you like, as long as the description is concise and precise. In fact, in assignments, quiz, and exam, you can describe your algorithm in whatever ways as long as you can describe to us clearly that it works.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS0250, Set 1 Analysis of algorithms After designing an algorithm, we analyze it.  More precisely, we  do the followings: Proof of correctness:   whether it is correct, i.e., it returns the  correct answer for any possible input. Time complexity analysis:  find out how fast your algorithm  runs – in the best case,  – in the worst case, – on average. Space complexity analysis:  decide how much memory space  your algorithm requires. Look for improvement (or proof of optimality):  decide whether  your algorithm is best possible; can we improve it such that it  runs faster or uses less memory space.
Background image of page 4
Proof of Correctness (for Euclid) Prove that given any a, b, the Euclid’s algorithm always returns the greatest  common divisor of a, b. Fact 1: If d divides integers x and y; then d divides x+y and x-y.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/05/2008 for the course CS CSIS0250 taught by Professor Dr.hing-fungting during the Summer '08 term at HKU.

Page1 / 28

L01 - What is an algorithm? An algorithm is a sequence of...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online