data structures and algorithms introduction to algorithms

Download Data Structures and Algorithms  Introduction to Algorithms

Post on 23-Feb-2016

95 views

Category:

Documents

1 download

Embed Size (px)

DESCRIPTION

Data Structures and Algorithms Introduction to Algorithms. M. B. Fayek CUFE 2006. Agenda. Introduction to Algorithms Algorithm Design Basics Time Complexity of an Algorithm Analysis using Asymptotic Notation Basic Efficiency Classes. 1. Introduction to Algorithms. Why study Algorithms? - PowerPoint PPT Presentation

TRANSCRIPT

  • Data Structures and Algorithms Introduction to AlgorithmsM. B. FayekCUFE 2006

  • AgendaIntroduction to AlgorithmsAlgorithm Design BasicsTime Complexity of an AlgorithmAnalysis using Asymptotic NotationBasic Efficiency Classes

  • 1. Introduction to AlgorithmsWhy study Algorithms?Heart of computer Promote analytical skillsDonald Knuth: A person does not understand something until after teaching it to someone else.Actually: A person does not understand something until after he teaches it to a COMPUTER!

  • 1. Introduction to Algorithms What is an algorithm?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.

  • 1. Introduction to Algorithms Algorithms are all around us in everyday life. In the recipe of a cook book. In assembling a toy. In setting the table. In preparing a cup of tea. In calling your friend on the phone.. There are countless examples!

  • AgendaIntroduction to AlgorithmsAlgorithm Design BasicsTime Complexity of an AlgorithmAnalysis using Asymptotic NotationBasic Efficiency Classes

  • 2. Algorithm Design BasicsGuidelines for Algorithm Designing and Analysis:Understand the Problem (requirement analysis) Select Data structure Write Pseudo CodeAnalyze PerformanceImplement using suitable programming languageTest to resolve syntax and logic errors

  • 2. Algorithm Design BasicsGuidelines for Algorithm Designing and Analysis:Understand the Problem (requirement analysis) Gather dataAsk usersCarefully review any written requirements

  • 2. Algorithm Design BasicsGuidelines for Algorithm Designing and Analysis:Select Data structure: To verify the appropriateness of the selected data structure:Judge how well your data structure responds to user requirements (updates, questions)Modify design as necessary

  • 2. Algorithm Design BasicsGuidelines for Algorithm Designing and Analysis:Write Pseudo CodeUse pseudo code or flow chartlevel of details of the pseudo code may vary

  • 2. Algorithm Design BasicsGuidelines for Algorithm Designing and Analysis:Analyze PerformanceDetermine the feasibility of solution w.r.t. memory requirements, performance constraints etc.Manually review and validate pseudo codeAnalyze the complexity of the algorithm using the big O notation to determine the complexity w.r.t. to time and storage. Other criteria include: Clarity, Maintainability, Portability

  • 2. Algorithm Design BasicsGuidelines for Algorithm Designing and Analysis:Implement using suitable programming language: Is rather direct provided a good pseudo code is written, however erroneous mapping may occur!

  • 2. Algorithm Design BasicsGuidelines for Algorithm Designing and Analysis:Test to resolve syntax and logic errors.Testing is divided into 2 main parts: Trying to break the function of the program by entering unexpected data.Debugging: It is concerned with finding out what is what caused the program to function in incorrectly.

  • AgendaIntroduction to AlgorithmsAlgorithm Design BasicsTime Complexity of an AlgorithmAnalysis using Asymptotic NotationBasic Efficiency Classes

  • 3. Time Complexity of an AlgorithmTime complexity is a main issue in evaluating an algorithm. It reflects how the algorithm responds to the increase in data size (n) it handles, by measuring the corresponding increase in number of instructions to be performed. Time complexity is meant to classify algorithms into categories.

  • 3. Time Complexity of an AlgorithmSteps to determine time complexity:Identify basic operations used for evaluating complexity.Usually, loops and nested loops are the significant parts of a program. One iteration of the loop is considered as a unit. It is then important to determine the order of magnitude of run time involved based on the number of iterations. Parts concerned with initializations and reporting summary results are of secondary importance.

  • Example 1Example1: count number of different chars in a file:// Input: string of characters // Output: array of 256 integers containing the count of the 256 charsFor all 256 char doassign zero to counterend forwhile there are more chars doget next charincrement count for this charend while

  • Example 1

    Consider basic operation BOP = 1 Inc + 1 check for N= 500, integral BOP 500bookkeeping 50 %For N =10000, BOP 10000bookkeeping = 7 %Total = 257 assign + 256 Inc + 258 check (bookkeeping) + N Inc + N+1 check (integral)

    Time grows linearly with n !

  • Example 2Summing each of the rows of an N x N two-dimensional array A, storing row sums in a one-dimensional array Sum and the overall in GrandTotal. // Input: 2-dimensional array A // Output: 1- dimensional array Sum that contains the sum of 1st, 2nd, row in 1st, 2nd, element and GrandTotal that contains the total sum

  • Example 2First Algorithm : GrandTotal = 0For k = 1 to N doSum [K] = 0For J = 1 to N doSum [K] = Sum [K] + A [K,J]GrandTotal = GrandTotal + A [K,J]EndForEndFor

    complexity = n2 !

  • Example 2Second Algorithm : GrandTotal = 0For k = 1 to N doSum [K] = 0For J = 1 to N doSum [K] = Sum [K] + A [K,J]EndForGrandTotal = GrandTotal + A [K,J]EndFor

    complexity = n2 complexity = n

  • Example 2Total complexity= n2 + n Hence Total Complexity mainly depends on n2 Note however that:In the first approach the basic operation includes 2 additionsIn the second approach each basic operation includes 1 additionHence, BOTH approaches are of order n2, however, the second is better! . Why?

  • AgendaIntroduction to AlgorithmsAlgorithm Design BasicsTime Complexity of an AlgorithmAnalysis using Asymptotic NotationBasic Efficiency Classes

  • 4. Analysis using Asymptotic NotationBig Oh:If

    Then there exists c such that

  • 4. Analysis using Asymptotic Notation

    Find Big Oh

    by finding f(n) such that

  • 4. Analysis using Asymptotic NotationA Useful Property:If and

    Then

  • AgendaIntroduction to AlgorithmsAlgorithm Design BasicsTime Complexity of an AlgorithmAnalysis using Asymptotic NotationBasic Efficiency Classes

  • 5. Basic Efficiency Classes

    Common complexities (ordered in ascending order) are:log nnn log nn2n32n

    Complexity

  • 5. Basic Efficiency Classes Values of important asymptotic functions

  • Example 1Find the number of binary digits in the binary representation of a number // input: A is a positive number// output: number of bin digits that represent Acount 1 docount
  • Example 2: Element Uniqueness Element Uniqueness Problem: //Check whether all elements of array are unique // input: array A[0, .. n-1] // output true or falsefor i
  • Example 3: Fibonacci NumbersIt is a series described by: Fi = Fi-1+ Fi-2 and F(0) =0, F(1) =1// Input: A nonnegative integer n// Output: The nth Fibonacci numberF[0]
  • ExercisesWrite an implementation of the fibonacci example without using and additional array!Check if a number is prime or notFind all prime numbers in a specified rangeFind the GCD of 2 numbers

  • Website athttp://elearning.eng.cu.edu.eg

Recommended

View more >