Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Introduction to design and analysis algorithm. 2.

Similar presentations


Presentation on theme: "1 Introduction to design and analysis algorithm. 2."— Presentation transcript:

1 1 Introduction to design and analysis algorithm

2 2

3 3

4 4

5 5 Overview Algorithm Definition Why study Algorithm Problem

6 6 Algorithm An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate input in a finite amount of time. Algorithm Input Output

7 7 Algorithm 1: How to bake a Banana Cake Ingredients Ingredients 2 1/2 cups all-purpose flour1 tablespoon baking soda1 pinch salt1/2 cup unsalted butter1 cup white sugar3/4 cup light brown sugar2 eggs4 ripe bananas, mashed2/3 cup buttermilk1/2 cup chopped walnuts Directions Directions 1Preheat oven to 350 degrees F (175 degrees C). Grease and flour 2 - 8 inch round pans. In a small bowl, whisk together flour, soda and salt; set aside. 2In a large bowl, cream butter, white sugar and brown sugar until light and fluffy. Beat in eggs, one at a time. Mix in the bananas. Add flour mixture alternately with the buttermilk to the creamed mixture. Stir in chopped walnuts. Pour batter into the prepared pans. 3Bake in the preheated oven for 30 minutes. Remove from oven, and place on a damp tea towel to cool. Input Algorithm

8 What is a Computer Algorithm? A computer algorithm is  a detailed step-by-step method for  solving a problem  by using a computer.

9 9 What is Algorithm? Algorithm is Not an ANSWER to a problem Algorithm is Not an ANSWER to a problem It is rather precisely defined PROCEDURES for getting answers. It is rather precisely defined PROCEDURES for getting answers. Thus specific algorithm design techniques can be interpreted as problem-solving strategie. Thus specific algorithm design techniques can be interpreted as problem-solving strategie.

10 10 Notion of Algorithm Programming

11 Example for writing algorithn computing the greatest common divisor. the definition of the greatest common divisor of m and n as the largest integer that divides both numbers evenly there are several algorithms for computing the greatest common divisor. Let us look at the other two methods for this problem 11

12 1- Euclid’s algorithm Euclid’s algorithm( finding the greatest common diviso) is based on applying repeatedly the equality gcd(m, n) = gcd(n, m mod n) where m mod n is the remainder of the division of m by n, until m mod n is equal to 0. Since gcd(m, 0) = m (why?), the last value of m is also the greatest common divisor of the initial m and n. For example, gcd(60, 24) can be computed as follows: gcd(60, 24) = gcd(24, 12) = gcd(12, 0) = 12. 12

13 Here is a more structured description of this algorithm Euclid’s algorithm for computing gcd(m, n) Step 1 If n = 0, return the value of m as the answer and stop; otherwise, proceed to Step 2. Step 2 Divide m by n and assign the value of the remainder to r. Step 3 Assign the value of n to m and the value of r to n. Go to Step 1. Alternatively, we can express the same algorithm in pseudocode: 13

14 Pseudocode for algorithm euclid(m, n) ALGORITHM Euclid(m, n) //Computes gcd(m, n) by Euclid’s algorithm //Input: Two nonnegative, not-both-zero integers m and n //Output: Greatest common divisor of m and n while n = 0 do r ← m mod n m ← n n ← r return m 14

15 2- Consecutive integer checking algorithm Consecutive integer checking algorithm for computing gcd(m, n) Step 1 Assign the value of min{m, n} to t. Step 2 Divide m by t. If the remainder of this division is 0, go to Step 3; otherwise, go to Step 4. Step 3 Divide n by t. If the remainder of this division is 0, return the value of t as the answer and stop; otherwise, proceed to Step 4. Step 4 Decrease the value of t by 1. Go to Step 2. Note that unlike Euclid’s algorithm, this algorithm, in the form presented,does not work correctly when one of its input numbers is zero 15

16 16 1.2 Fundamentals of Algorithmic Problem Solving Algorithm = Procedural Solutions to Problem NOT an answer, BUT rather specific instructions of getting answers. Therefore, requires steps in designing and analyzing an algorithm

17 17 Algorithm Design & Analysis Process

18 18 1.3 Important Problem Types Sorting Sorting Searching Searching String processing String processing Graph problems Graph problems Combinatorial problems Combinatorial problems Geometric problems Geometric problems Numerical problems Numerical problems

19 19 Sorting The sorting problem asks us to rearrange the items of a given list in ascending order. The sorting problem asks us to rearrange the items of a given list in ascending order.

20 20 Searching The searching problem deals with finding a given value, called a search key, in a given set (or a multiset, which permits several elements to have the same value). The searching problem deals with finding a given value, called a search key, in a given set (or a multiset, which permits several elements to have the same value).

21 21 String Processing A string is a sequence of characters from an alphabet. A string is a sequence of characters from an alphabet. String of particular interest: String of particular interest: 1. Text string – comprises letters, numbers, and special characters 2. Bit string – comprises zeros and ones 3. Gene sequence Mainly string matching problem: searching for a given word in a text Mainly string matching problem: searching for a given word in a text

22 22 Graph Problems A graph can be thought of as a collection of points called vertices, some of which are connected by line segments called edges. A graph can be thought of as a collection of points called vertices, some of which are connected by line segments called edges. Used for modeling a wide variety of real-life applications. Used for modeling a wide variety of real-life applications. Basic graph algorithms include: Basic graph algorithms include: 1. Graph traversal algorithms - How can one visit all the points in a network? 2. Shortest-path algorithms - What is the best Introduction route between two cities?

23 23 Combinatorial Problems combinatorial problems: problems that ask (explicitly or implicitly) to find a combinatorial object— such as a combination, or a subset—that satisfies certain constraints and has some desired property (e.g., maximizes a value or minimizes a cost). combinatorial problems: problems that ask (explicitly or implicitly) to find a combinatorial object— such as a combination, or a subset—that satisfies certain constraints and has some desired property (e.g., maximizes a value or minimizes a cost). Are the most difficult problems in computing: Are the most difficult problems in computing: Combinatorial grows extremely fast with problem size Combinatorial grows extremely fast with problem size

24 24 Geometric Problems Geometric algorithms deal with geometric objects such as points, lines, and polygons. Geometric algorithms deal with geometric objects such as points, lines, and polygons.

25 25 Introduction to algorithm analysis

26 26 Analyzing an Algorithm 1. Efficiency Time efficiency indicates how fast the algorithm runs. space efficiency indicates how much extra memory the algorithm needs. 2. Simplicity 3. Generality Design an algorithm for a problem posed in more general terms. Design an algorithm that can handle a range of inputs that is natural for the problem at hand.

27 27 Analyzing Algorithms Analyzing algorithms involves Analyzing algorithms involves  thinking about how their resource requirement will scale with increasing input size. Resource means Resource means  time  and space

28 28 Algorithm efficiency when we want to classify the efficiency of an algorithm, we must first identify the costs to be measured  memory used?  execution time? to classify an algorithm's efficiency, first identify the steps that are to be measured # of steps

29 Problem Solving Using Computers Problem: Strategy: Algorithm:  Input:  Output:  Step: Analysis:  Correctness:  Time & Space:  Optimality: Implementation: Verification:

30 Importance of Analyze Algorithm Need to recognize limitations of various algorithms for solving a problem Need to understand relationship between problem size and running time  When is a running program not good enough? Need to learn how to analyze an algorithm's running time without coding it Need to learn techniques for writing more efficient code Need to recognize bottlenecks in code as well as which parts of code are easiest to optimize 30

31 Which algorithm is better? The algorithms are correct, but which is the best? Measure the running time (number of operations needed). Measure the amount of memory used. Note that the running time of the algorithms increase as the size of the input increases. 31

32 32 End of Chapter 1 Thank You!


Download ppt "1 Introduction to design and analysis algorithm. 2."

Similar presentations


Ads by Google