Docsity

Algorithm Design and Analysis: Pseudocode, Sorting, and Dynamic Programming - Prof. Dulle, Study Guides, Projects, Research of Computer Applications

Partial preview of the text

Download Algorithm Design and Analysis: Pseudocode, Sorting, and Dynamic Programming - Prof. Dulle and more Study Guides, Projects, Research Computer Applications in PDF only on Docsity!

Analysis of Algorithm

Chapter one

Quick Review of Basic concepts

Outline

Introduction

Complexity measures

model of computation

Algorithm Design Paradigm

  • (^) Divide and Conquer
  • (^) Recursion, Greedy method
  • (^) Dynamic programming

Introduction:Pseudocode

  • (^) High-level description of an algorithm
  • (^) More structured than English prose
  • (^) Less detailed than a program
  • (^) Preferred notation for describing

algorithms

  • (^) Hides program design issues Example: find max element of an array

Introduction:Pseudocode Details

Control flow if … then … [else …] while … do … repeat … until … for … do … Indentation replaces braces Method declaration Algorithm method ( arg [, arg …]) Input … Output … Method/Function call var.method ( arg [, arg …]) Return value return expression Expressions ¬Assignment (like in C++) =Equality testing (like in C++) n 2 Superscripts and other mathematical formatting allowed

Introduction: What is Analysis

of Algorithm?

  • (^) The theoretical study of design and analysis of computer algorithms.
  • (^) Basic goals for an algorithm
    • (^) always correct
    • (^) always terminates
    • (^) performance
      • (^) Performance often draws the line between what is possible and what is impossible.

Introduction: Analysis of Algorithm(Time and

space)

  • (^) To analyze an algorithm means:
  • (^) developing a formula for predicting how fast an algorithm is, based on the size of the input (time complexity), and/or
  • (^) developing a formula for predicting how much memory an algorithm requires, based on the size of the input (space complexity)
  • (^) Usually time is our biggest concern
  • (^) Most algorithms require a fixed amount of space

Introduction: Algorithm Design

and Analysis(Analysis)

  • (^) Idea: characterize performance in terms of key operation(s)
  • (^) Sorting:
    • (^) count number of times two values compared
    • (^) count number of times two values swapped
  • (^) Search:
    • (^) count number of times value being searched for is compared to values in array
  • (^) Recursive function:
    • (^) count number of recursive calls

Introduction: Algorithm Design

and Analysis(general steps)

  • (^) Design an algorithm
  • (^) Prove the algorithm is correct
  • (^) Analyze the algorithm

Complexity measures: what is

complexity of an Algorithm?

  • (^) Complexity of an algorithm is a measure of the amount of time and/or space required by an algorithm for an input of a given size (n).
  • (^) What effects run time of an algorithm?
    • (^) Computer used, the hardware platform
    • (^) Efficiency of compiler
    • (^) Competence of implementer (programming skills)
    • (^) Complexity of underlying algorithm
    • (^) Size of the input

Complexity measures: what is

complexity of an

Algorithm(contd.)

  • (^) Some algorithms perform differently on various inputs of similar size.
  • (^) It is sometimes helpful to consider the Worst-Case, Best-Case and Average-Case efficiencies of algorithms.
  • (^) For example, say we want to search an array A of size n for a given value K.
  • (^) Worst-Case: we must search every item (n comparisons)
  • (^) Best-Case: K is the first item that we check, so only one comparison

Amortized Analysis:The Time Complexity of an

Algorithm

  • (^) Specifies how the running time depends on the size of the input
  • (^) purpose
    • (^) To estimate how long a program will run.
    • (^) To estimate the largest input that can reasonably be given to the program.
    • (^) To compare the efficiency of different algorithms.
    • (^) To help focus on the parts of code that are executed the largest number of times.
    • (^) To choose an algorithm for an application.

Purpose(example)

  • (^) Suppose a machine that performs a million floating-point operations per second ( 6 FLOPS), then how long an algorithm will run for an input of size n=50?
  1. If the algorithm requires n 2 such operations: 0.0025 second
  2. If the algorithm requires 2 n such operations: over 35 years!

Complexity Measures(Types of Algorithm

analysis)

  • (^) Best-case: what is the fastest an algorithm can run for a

problem of size N?

  • (^) Average-case: on average how fast does an algorithm run for

a problem of size N?

  • (^) Worst-case: what is the longest an algorithm can run for a

problem of size N?

Best-case, average-case and worst-case(contd.)

  • (^) For some algorithms, efficiency depends on form of input:
  • (^) Worst case: C worst( n ) – maximum over inputs of size^ n
  • (^) Best case: C best( n ) – minimum over inputs of size^ n
  • (^) Average case: C avg( n ) – “average” over inputs of size^ n
  • (^) Number of times the basic operation will be executed on typical input
  • (^) NOT the average of worst and best case