Evaluating algorithms. - PowerPoint PPT Presentation

1 / 47
About This Presentation
Title:

Evaluating algorithms.

Description:

Brute Force algorithm ... The Brute Force algorithm has worse case time ... But unlike the brute force algorithm, after a full match of the pattern with the ... – PowerPoint PPT presentation

Number of Views:71
Avg rating:3.0/5.0
Slides: 48
Provided by: tbe33
Category:

less

Transcript and Presenter's Notes

Title: Evaluating algorithms.


1
  • Evaluating algorithms.

Thomas Berry Room 706A Byrom Street Liverpool
John Moores University
1
2
Introduction
  • A lot of research is done into the theoretical
    construction of algorithms.
  • Very few of these algorithms ever get put into
    practice.
  • Theoretical evaluations of algorithms are
    sometimes inexact.
  • Choice of an algorithm.
  • theoretical Versus practical evaluation.

2
3
Contemporary Issue
  • Although this problem has existed for some time
    more theoreticians are evaluating their work
    practically.
  • This is due to pressure from the practical
    industry for guaranteed efficient algorithms to
    solve their problem.

3
4
Lecture Structure
  • Introduce big oh notation.
  • Give some examples of big oh
  • Introduce string matching algorithms and their
    big oh notation evaluations.
  • Further analysis of string matching algorithms.

4
5
Analyzing Algorithms
  • What do we want from an algorithm?
  • 1. correctness.
  • 2. simplicity, clarity.
  • 3. amount of space.
  • 4. amount of work done.

5
6
Input size
  • The amount of work performed depends on the size
    of the input
  • Example
  • for i 1 to n do
  • sumsumAi
  • The input size here is n. Let T(n) represent the
    time complexity of an algorithm on any input size
    n.

6
7
Input size
  • Even if the input is n, the number of operations
    performed may depend on a particular input.
  • We describe the performance of an algorithm by
    stating its worst case time complexity.
  • Which is the maximum number of operations
    performed by the algorithm on any input n.

7
8
Example
  • Suppose we have an array L of distinct entries
    and wish to find the location of x in L. The
    following algorithm compares x to each entry in
    turn until a match is found or L is exhausted. If
    x is in the list, the algorithm returns the index
    of the array entry containing x, and index equal
    to 0 otherwise.

8
9
Example
  • index1
  • while (index ??n) and (Lindex ??x) do
  • begin
  • indexindex 1
  • end
  • if index gt n then index 0
  • writeln(index)

9
10
Example
  • Clearly, the worst case time complexity T(n) is
    equal to cn where c is a constant.
  • The worst case in this example is when x is the
    last entry in the array L or is not present in L.
  • In either of the cases x is compared to all n
    entries.

10
11
Big Oh Notation
  • Suppose one algorithm takes 2n basic operations
    and another takes 3n. Or 2c1n and 3c2n in total.
    Where c1 and c2 are constants. Which runs the
    quickest?
  • We really dont know.
  • One algorithm may have greater overheads.
  • If algorithms differ by a constant factor we say
    that are in the same complexity class.

11
12
Big Oh Notation
  • Suppose one algorithm does 1/2(n2) basic
    operations and another does 5n. Which algorithm
    is faster?
  • Well if n ??10 the first algorithm performs the
    fewer number of basic operations. When n gt 10
    then the second algorithm performs the fewer
    number of comparisons.

12
13
Big Oh Notation
  • Whatever the coefficients of n2 and n in the
    expression the second will always be faster than
    the first. For all n greater than some value, say
    n0.
  • For this reason we usually express the time
    complexity of an algorithm using big Oh
    notation, which is designed to allow us to hide
    constant factors.

13
14
Big Oh Notation
  • Let f(n) be some function defined on the non
    negative integers n. We say that T(n) is O(f(n))
    if there exists an integer n0 and a constant cgt0
    such that for all integers n?n0,we have
    T(n)??cf(n).

14
15
Example
  • Suppose we have an algorithm whose time
    complexity T(n)6n3. We can say that T(n)
    O(n). Due to
  • T(n)6n3
  • ??6n3n
  • ??9n
  • We can let c9 and n0 1 in the definition on
    the previous slide.

15
16
Big Oh notation examples
  • Big Oh Informal name
  • O(1) constant
  • O(log n) logarithmic
  • O(n) linear
  • O(n log n) n log n
  • O(n2) quadratic
  • O(n3) cubic
  • O(2n) exponential

16
17
Comparison of time complexities
17
18
Improvements with more processor power
18
19
General Rules
  • If T1(n) O(f(n)) and T2(n) O(g(n)) then
  • Rule 1 T1(n)???T2(n) O(f(n)???g(n))
  • Rule 2 T1(n)??T2(n) max(O(f(n)?g(n)))
  • Simple input and output statements such as read
    and write in Pascal take O(1) time.

19
20
Big Oh Notation Examples
  • Simple example
  • n5
  • loop
  • get(m)
  • nn-1
  • until(m0 or n0)
  • Worst case 5 iterations.

20
21
Big Oh Notation Examples
  • As mentioned we are not concerned with the number
    of steps for a fixed case but wish to estimate
    the run-time in terms of the input size.
  • get(n)
  • loop
  • get(m)
  • nn-1
  • until(m0 or n0)
  • Worst case n iterations and so has Big Oh of
    O(n).

21
22
Big Oh Notation Examples
  • Another example is
  • for i in 1..n loop
  • for j in 1..n loop
  • total total matrix(i,j)
  • end loop
  • end loop
  • So iteration worst case is nn1 n2 which in
    Big Oh Notation gives O(n2). As the input size
    increases the run time increases by a power of 2.
  • Note constants are dropped when calculating Big
    Oh.

22
23
String Matching in Text
  • String matching is used in word processing
    packages for tasks such as search, replace, spell
    checking, etc..
  • String matching is the process of finding a
    pattern P of length m in a text T of length n
    where ngtm starting from the top left corner.

23
24
The Brute Force algorithm
  • The comparisons are performed from left to right.
  • On a mismatch or full match the pattern is
    shifted one place to the right.
  • The worst case for string matching is searching
    for P am in T an.

24
25
Brute Force algorithm
  • After a mismatch or a match, the pattern is
    shifted by one position to the right.
  • The search continues until the text is exhausted.
    By this we mean that the last character of the
    pattern shifts past the last character of the
    text.

25
26
Brute Force Example
26
27
Brute Force Example
27
28
Brute Force Example
28
29
Brute Force Example
29
30
String Matching cont.....
  • The Brute Force algorithm has worse case time
    complexity of O(nm).
  • All comparison based string matching algorithms
    have either one of two Big Oh notations
  • O(nm)
  • O(nm).

30
31
KMP Algorithm
  • Searches from left to right with worse case time
    complexity O(nm).
  • Shifts the pattern to the right by one place
    unless a partial match is found.
  • Uses a shift table to calculate the shift if a
    partial match is found.

31
32
KMP_NEXT
  • The value of kmp_next is the position were the
    searching process continues. If the value of
    kmp_next -1 then we start at position 0.
  • This is due to the fact that the characters to
    the right of this position have already been
    compared and we know that will match the text
    after the shift.

32
33
KMP shift table
33
34
KMP Shift Table
34
35
KMP Shift Table
35
36
Another Example
36
37
KMP Example
37
38
KMP Example
38
39
KMP example
40
KMP example
41
KMP example
42
KMP example
43
KMP example
44
KMP example
45
Points to note
  • When using the KMP with English text there are
    very few words that have a repeated substring.
    Some may repeat the first letter like awake.
  • So for the task of string matching in English
    text the KMP performs as well as the Brute Force
    algorithm

39
46
KMP Time Complexity
  • As before the worst case input is a text and
    pattern composed of as. But unlike the brute
    force algorithm, after a full match of the
    pattern with the text. The pattern is shifted by
    m-1 positions to the right and the last character
    of the pattern is compared first. After the next
    full match the process is repeated. So therefore
    we have an O(nm) algorithm.

40
47
Next Week
  • Horspool algorithm.
  • Berry Ravindran algorithm.

41
Write a Comment
User Comments (0)
About PowerShow.com