Créer une présentation
Télécharger la présentation

Télécharger la présentation
## BİM 202 ALGORITHMS

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -

**BİM 202 ALGORITHMS**Öğr. Gör. ZEYNEP TURGUT**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**Grading Guidelines**Two Exams: 20% midterm, 60% final. %20 projects & homeworks & quizzes& attitudetothelesson. Minimum 70%attendancetolessonrequiredtoclear the course.**Topics**IntroductiontoAlgorithms AlgorithmAnalysis AlgorithmTypes SortingAlgorithms SearchingAlgorithms GraphAlgorithms TreeAlgorithms NP-CompleteProblems**WARNING: This lecture contains mathematical content that may**be shocking to some students.**Good, better, best. Neverlet it rest. ‘Tillyourgood is**betterandyourbetter is best.Saint Jerome**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.**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**Problem Solving: Main Steps**• Problem definition • Algorithm design / Algorithm specification • Algorithm analysis • Implementation • Testing • [Maintenance]**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 ?**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**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?**Developing an Algorithm**• Identify the Inputs • Identify the Processes • Identify the Outputs • Develop a HIPO Chart • Identify modules**2.1. Identify the Inputs**• What data do I need? • How will I get the data? • In what format will the data be?**2.2. Identify the Processes**• How can I manipulate data to produce meaningful results? • Data vs. Information**2.3. Identify the Outputs**• What outputs do I need to return to the user? • What format should the outputs take?**2.4. Develop a HIPO Chart**Hierarchy of Inputs Processes & Outputs**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?**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.**Summary**To develop an algorithm: • Identify the inputs • Identify the processes • Identify the outputs • Develop a HIPO chart • Identify relevant modules**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!**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**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?**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**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!**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**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?**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**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?**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!**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**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**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!**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**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 ?**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**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**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**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**Recursive Algorithms**long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); } Compute 5!**Recursive Algorithms**long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); }**Recursive Algorithms**long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); }**Recursive Algorithms**long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); }**Recursive Algorithms**long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); }**Recursive Algorithms**long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); }**Recursive Algorithms**long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); }**Recursive Algorithms**long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); }**Recursive Algorithms**long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); }**Recursive Algorithms**long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); }