Top Banner

of 13

CSC331 Week 2 Topic D

Jun 04, 2018

Download

Documents

frankjamison
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/13/2019 CSC331 Week 2 Topic D

    1/13

    CSC331 Discrete Structuresand Logic

    October 2012

    Topic D

    Algorithms

  • 8/13/2019 CSC331 Week 2 Topic D

    2/13

    Section 4.1 Introduction

    Analgorithm is a finite set of instructions with

    the following characteristics:

    Precision: steps are precisely stated

    Uniqueness: Results of each step of execution are

    uniquely defined. They depend only on inputs and

    results of preceding steps

    Finiteness: the algorithm stops after finitely many

    steps

  • 8/13/2019 CSC331 Week 2 Topic D

    3/13

    More characteristics of algorithms

    Input: the algorithm receives input

    Output: the algorithm produces output

    Generality: the algorithm applies to various sets of

    inputs

  • 8/13/2019 CSC331 Week 2 Topic D

    4/13

    Example: a simple algorithm

    Algorithm to find the largest of three

    numbers a, b, c:

    Assignment operator

    s := k means copy the value of k into s

    1. x:= a

    2. If b > x then x:= b

    3. If c > x then x:= cA traceis a check of the algorithm for

    specific values of a, b and c

  • 8/13/2019 CSC331 Week 2 Topic D

    5/13

    Section 4.2 Examples of Algorithms

    Algorithm 4.2.1Text Search:

  • 8/13/2019 CSC331 Week 2 Topic D

    6/13

    Traces of algorithms

    In simple words, a trace is a test run of an

    algorithm for some specific inputs.

    We will do an example of trace for the Text

    Search algorithm together.

  • 8/13/2019 CSC331 Week 2 Topic D

    7/13

    Section 4.3 Analysis of algorithms

    Complexity: the amount of time and/or

    space needed to execute the algorithm.

    Complexity depends on many factors: data

    representation type, kind of computer,

    computer language used, etc.

  • 8/13/2019 CSC331 Week 2 Topic D

    8/13

    Types of complexity

    Best-case time = minimum time needed to

    execute the algorithm for inputs of size n Worst-case time = maximum time needed

    to execute the algorithm for inputs of size n

    Average-case time = average time needed

  • 8/13/2019 CSC331 Week 2 Topic D

    9/13

    Order of an algorithm

    Let f and g be functions with domain Z+= {1, 2, 3,}

    f(n) = O(g(n)): f(n) is of order at most g(n)

    if there exists a positive constant C1such that |f(n)| < C1|g(n)|

    for all but finitely many n

    f(n) = (g(n)): f(n) is of order at least g(n)

    if there exists a positive constant C2such that |f(n)| > C2|g(n)|

    for all but finitely many n

    f(n) = (g(n)): f(n) is or order g(n) if it is O(g(n)) and

    (g(n)).

  • 8/13/2019 CSC331 Week 2 Topic D

    10/13

  • 8/13/2019 CSC331 Week 2 Topic D

    11/13

  • 8/13/2019 CSC331 Week 2 Topic D

    12/13

    Section 4.4 Recursive algorithms

    A recursive procedureis a procedure that invokes

    itself

    Example: given a positive integer n, factorial of nisdefined as the product of n by all numbers less than n

    and greater than 0. Notation: n! = n(n-1)(n-2)3.2.1

    Observe that n! = n(n-1)! = n(n-1)(n-2)!, etc.

    A recursive algorithmis an algorithm that containsa recursive procedure

  • 8/13/2019 CSC331 Week 2 Topic D

    13/13

    Fibonacci sequence

    Leonardo Fibonacci (Pisa, Italy, ca. 1170-1250)

    Fibonacci sequence f1, f2, defined recursively

    as follows:

    f1= 1

    f2= 2

    fn= fn-1+ fn-2 for n > 3

    First terms of the sequence are: 1, 2, 3, 5, 8, 13,21, 34, 55, 89, 144, 233, 377, 610, 987, 1597,