#1 designandanalysis of algo


Published on

CS202 Design and Analysis of Algorithm

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

#1 designandanalysis of algo

  1. 1. Design And Analysis of Algorithms<br />Topic #1: Algorithm Basics<br />Prepared by: BrijidaCharizma A. Navarro<br />
  2. 2. Introduction<br />As future computer scientists, you will usually more concerned with writing complex program algorithm.<br />
  3. 3. Algorithms<br />Step by step method of solving some problems<br />Solution that can be executed by a computer<br />Derived from the name of the nith-century Persian mathematician al-Khowarizmi.<br />
  4. 4. Algorithm<br />A complete, step-by-step procedure for solving a specific problem.<br />
  5. 5. Pakiisipnga…..<br />Does the computer understand an algorithm?<br />
  6. 6. Properties of Algorithms<br />Input<br />Output<br />Precision<br />
  7. 7. Properties of Algorithms<br />Determinism<br />Finiteness<br /> Correctness<br />Generality <br />
  8. 8. Example<br />Make an algorithm that finds the maximum of three numbers a, b and c.<br />
  9. 9. Algorithm: Finding the maximum in a list of 3 numbers<br />a = 1, b = 5,<br />c = 3<br />x = a<br />If b > x then x = b<br />If c > x, then x=c.<br />
  10. 10. Two Main Tasks in the Study of Algorithms<br />Algorithm Design<br />Analysis of Algorithms<br />
  11. 11. Algorithm Design<br />To solve problems<br />An art more than a science<br />
  12. 12. Analysis of Algorithms<br />Will provide insights into designing new algorithms<br />
  13. 13. in analysis of algorithms, we ask ourselves the following questions:<br />Correctness.Given an algorithm for a problem, does it solve the problem?<br />Termination. Does the algorithm always stop after a finite number of steps?<br />Time Analysis. How many instructions does the algorithm execute?<br />Space Analysis. How much memory does the algorithm need to execute?<br />
  14. 14. Exercise<br />Write an algorithm that finds the smallest element among a, b and c<br />
  15. 15. Solution<br />x = a<br />If b < x, then x = b.<br />If c < x, then x = c.<br />
  16. 16. 2. Exercise<br />Which properties of an algorithm – input, output, precision, determinism, finiteness, correctness, generality – if any, are lacking in the following. Explain. The input is a set of S of integers and an integer m. The output is all subsets of S that sum to m.<br />
  17. 17. Here’s the algorithm:<br />List all subsets of S and their sums.<br />Proceed through the subsets listed in step 1 and output each whose sum is m.<br />
  18. 18. Answer:<br />If the set S is an infinite set, the algorithm will not terminate, so it lacks finiteness and output properties.<br />Line 1 is not precisely stated since how to list the subsets of S and their sums is not specified; thus the algorithm lacks the precision property.<br />The order of the subsets listed in line 1 depends on the method used to generate them, so the algorithm lacks the determinism property.<br />Since line 2 depends on the order of the subsets generated in line 1, the determinism property is lacking here as well.<br />
  19. 19. Pseudocode for Algorithms<br />Made of ordinary language<br />Precision, structure and universality<br />Closely resembles the actual code of computer languages such as C++ and Java<br />Any version of pseudocode is acceptable as long as the instructions are unambiguous.<br />
  20. 20. Algorithm Structure<br />Algorithm: Finding the Maximum of Three Numbers. <br />This algorithm finds the largest of the numbers a, b, and c<br />Input Parameters: a, b, c<br />Output Parameter: x<br />max(a,b,c){<br /> x = a<br /> if (b > x) // b is larger than x, update x<br /> x = b<br /> if (c > x) // c is larger than x, update x<br /> x = c<br />}<br />
  21. 21. Title<br />Brief description of the algorithm<br />The input and output parameters<br />Function containing the instructions of the algorithm<br />
  22. 22. Do this…………<br />Design an algorithm that will find the largest number in an array s<br />s[1],s[2],…,s[n]<br />Note: an array is a group of data of the same type<br />Use while<br />