b m 202 algorithms n.
Skip this Video
Loading SlideShow in 5 Seconds..
BİM 202 ALGORITHMS PowerPoint Presentation
Download Presentation


138 Vues Download Presentation
Télécharger la présentation


- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript


  2. References - Books IntroductiontoAlgorithms Cormen, Leiserson and Rivest and Stein Data Structures & Algorithm Analysis in C++ Mark AllenWeiss RecommendedReadings: Algoritmalar Prof. Dr. VasifVagifogluNabiyev Veri Yapıları ve Algoritmalar Dr. RifatÇölkesen

  3. Grading Guidelines Two Exams: 20% midterm, 60% final. %20 projects & homeworks & quizzes& attitudetothelesson. Minimum 70%attendancetolessonrequiredtoclear the course.

  4. Topics IntroductiontoAlgorithms AlgorithmAnalysis AlgorithmTypes SortingAlgorithms SearchingAlgorithms GraphAlgorithms TreeAlgorithms NP-CompleteProblems

  5. WARNING: This lecture contains mathematical content that may be shocking to some students.

  6. Good, better, best. Neverlet it rest. ‘Tillyourgood is betterandyourbetter is best.Saint Jerome

  7. The Computer as a Tool • Much like the microscope does not define biology or the test tube does not define chemistry, the computer doesn't define Computer Science. • The computer is a tool by which Computer Scientists accomplish their goals – to solve problems.

  8. What is Computer Science? • NOT about coding or hardware or software! • Computer Science is about PROBLEM SOLVING • Computer Science is about DEVELOPING ALGORITHMS to solve complex problems

  9. Problem Solving: Main Steps • Problem definition • Algorithm design / Algorithm specification • Algorithm analysis • Implementation • Testing • [Maintenance]

  10. 1. Problem Definition • What is the task to be accomplished? • Calculate the average of the grades for a given student • Understand the talks given out by politicians and translate them in Chinese • What are the time / space / speed / performance requirements ?

  11. 2. Algorithm Design / Specifications • Algorithm: Finite set of instructions that, if followed, accomplishes a particular task. • Describe: in natural language / pseudo-code / diagrams / etc. • Criteria to follow: • Input: Zero or more quantities (externally produced) • Output: One or more quantities • Definiteness: Clarity, precision of each instruction • Finiteness: The algorithm has to stop after a finite (may be very large) number of steps • Effectiveness: Each instruction has to be basic enough and feasible • Understand speech • Translate to Chinese

  12. Algorithm Questions • Computer Scientists ask themselves four critical questions when they evaluate algorithms … • Does the algorithm solve the stated problem? • Is the algorithm well-defined? • Does the algorithm produce an output? • Does the algorithm end in a reasonable length of time?

  13. Developing an Algorithm • Identify the Inputs • Identify the Processes • Identify the Outputs • Develop a HIPO Chart • Identify modules

  14. 2.1. Identify the Inputs • What data do I need? • How will I get the data? • In what format will the data be?

  15. 2.2. Identify the Processes • How can I manipulate data to produce meaningful results? • Data vs. Information

  16. 2.3. Identify the Outputs • What outputs do I need to return to the user? • What format should the outputs take?

  17. 2.4. Develop a HIPO Chart Hierarchy of Inputs Processes & Outputs

  18. 2.5. Identify Relevant Modules • How can I break larger problems into smaller, more manageable pieces? • What inputs do the modules need? • What processes need to happen in the modules? • What outputs are produced by the modules?

  19. Summary • The goal of Computer Science is to develop sound algorithms for solving complex problems. • An algorithm is a well-developed, detailed approach for solving a problem.

  20. Summary To develop an algorithm: • Identify the inputs • Identify the processes • Identify the outputs • Develop a HIPO chart • Identify relevant modules

  21. 3. Algorithm Analysis • Space complexity • How much space is required • Time complexity • How much time does it take to run the algorithm • Often, we deal with estimates!

  22. 4,5,6: Implementation, Testing, Maintainance • Implementation • Decide on the programming language to use • C, C++, Lisp, Java, Perl, Prolog, assembly, etc. , etc. • Write clean, well documented code • Test, test, test • Integrate feedback from users, fix bugs, ensure compatibility across different versions  Maintenance

  23. Space Complexity • Space complexity = The amount of memory required by an algorithm to run to completion • [Core dumps = the most often encountered cause is “memory leaks” – the amount of memory required larger than the memory available on a given system] • Some algorithms may be more efficient if data completely loaded into memory • Need to look also at system limitations • E.g. Classify 2GB of text in various categories [politics, tourism, sport, natural disasters, etc.] – can I afford to load the entire collection?

  24. Space Complexity (cont’d) • Fixed part: The size required to store certain data/variables, that is independent of the size of the problem: - e.g. name of the data collection - same size for classifying 2GB or 1MB of texts • Variable part: Space needed by variables, whose size is dependent on the size of the problem: - e.g. actual text - load 2GB of text VS. load 1MB of text

  25. Space Complexity (cont’d) • S(P) = c + S(instance characteristics) • c = constant • Example: void float sum (float* a, int n) { float s = 0; for(int i = 0; i<n; i++) { s+ = a[i]; } return s; } Space? one word for n, one for a [passed by reference!], one for i  constant space!

  26. Time Complexity • Often more important than space complexity • space available (for computer programs!) tends to be larger and larger • time is still a problem for all of us • 3-4GHz processors on the market • still … • researchers estimate that the computation of various transformations for 1 single DNA chain for one single protein on 1 TerraHZ computer would take about 1 year to run to completion • Algorithms running time is an important issue

  27. Running Time • Problem: prefix averages • Given an array X • Compute the array A such that A[i] is the average of elements X[0] … X[i], for i=0..n-1 • Sol 1 • At each step i, compute the element X[i] by traversing the array A and determining the sum of its elements, respectively the average • Sol 2 • At each step i update a sum of the elements in the array A • Compute the element X[i] as sum/I Big question: Which solution to choose?

  28. Running time Suppose the program includes an if-then statement that may execute or not:  variable running time Typically algorithms are measured by their worst case

  29. Experimental Approach • Write a program that implements the algorithm • Run the program with data sets of varying size. • Determine the actual running time using a system call to measure time (e.g. system (date) ); • Problems?

  30. Experimental Approach • It is necessary to implement and test the algorithm in order to determine its running time. • Experiments can be done only on a limited set of inputs, and may not be indicative of the running time for other inputs. • The same hardware and software should be used in order to compare two algorithms. – condition very hard to achieve!

  31. Use a Theoretical Approach • Based on high-level description of the algorithms, rather than language dependent implementations • Makes possible an evaluation of the algorithms that is independent of the hardware and software environments  Generality

  32. Algorithm Description • How to describe algorithms independent of a programming language • Pseudo-Code = a description of an algorithm that is • more structured than usual prose but • less formal than a programming language • (Or diagrams) • Example: find the maximum element of an array. Algorithm arrayMax(A, n): Input: An array A storing n integers. Output: The maximum element in A. currentMax A[0] for i 1 to n -1 do ifcurrentMax < A[i] thencurrentMax A[i] returncurrentMax

  33. Pseudo Code • Expressions: use standard mathematical symbols • use  for assignment ( ? in C/C++) • use = for the equality relationship (? in C/C++) • Method Declarations: -Algorithm name(param1, param2) • Programming Constructs: • decision structures: if ... then ... [else ..] • while-loops while ... do • repeat-loops: repeat ... until ... • for-loop: for ... do • array indexing: A[i] • Methods • calls: object method(args) • returns: return value • Use comments • Instructions have to be basic enough and feasible!

  34. Low Level Algorithm Analysis • Based on primitive operations (low-level computations independent from the programming language) • E.g.: • Make an addition = 1 operation • Calling a method or returning from a method = 1 operation • Index in an array = 1 operation • Comparison = 1 operation etc. • Method: Inspect the pseudo-code and count the number of primitive operations executed by the algorithm

  35. Example Algorithm arrayMax(A, n):Input: An array A storing n integers.Output: The maximum element in A.currentMax A[0]fori  1to n -1 doif currentMax < A[i] then currentMax  A[i]return currentMax How many operations ?

  36. Types of Algorithms

  37. Algorithm classification • Algorithms that use a similar problem-solving approach can be grouped together • We’ll talk about a classification scheme for algorithms • This classification scheme is neither exhaustive nor disjoint • The purpose is not to be able to classify an algorithm as one type or another, but to highlight the various ways in which a problem can be attacked

  38. A short list of categories • Algorithm types we will consider include: • Simple recursive algorithms • Backtracking algorithms • Divide and conquer algorithms • Dynamic programming algorithms • Greedy algorithms • Branch and bound algorithms • Brute force algorithms • Randomized algorithms

  39. Simple recursive algorithms I • A simple recursive algorithm: • Solves the base cases directly • Recurs with a simpler subproblem • Does some extra work to convert the solution to the simpler subproblem into a solution to the given problem • I call these “simple” because several of the other algorithm types are inherently recursive

  40. Example recursive algorithms • To count the number of elements in a list: • If the list is empty, return zero; otherwise, • Step past the first element, and count the remaining elements in the list • Add one to the result • To test if a value occurs in a list: • If the list is empty, return false; otherwise, • If the first thing in the list is the given value, return true; otherwise • Step past the first element, and test whether the value occurs in the remainder of the list

  41. Recursive Algorithms long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); } Compute 5!

  42. Recursive Algorithms long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); }

  43. Recursive Algorithms long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); }

  44. Recursive Algorithms long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); }

  45. Recursive Algorithms long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); }

  46. Recursive Algorithms long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); }

  47. Recursive Algorithms long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); }

  48. Recursive Algorithms long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); }

  49. Recursive Algorithms long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); }

  50. Recursive Algorithms long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); }