Discrete Mathematics & Mathematical Reasoning Algorithms Colin - - PowerPoint PPT Presentation

discrete mathematics mathematical reasoning algorithms
SMART_READER_LITE
LIVE PREVIEW

Discrete Mathematics & Mathematical Reasoning Algorithms Colin - - PowerPoint PPT Presentation

Discrete Mathematics & Mathematical Reasoning Algorithms Colin Stirling Informatics Some slides based on ones by Myrto Arapinis Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 1 / 22 Algorithms Definition An algorithm


slide-1
SLIDE 1

Discrete Mathematics & Mathematical Reasoning Algorithms

Colin Stirling

Informatics

Some slides based on ones by Myrto Arapinis

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 1 / 22

slide-2
SLIDE 2

Algorithms

Definition

An algorithm is a finite sequence of precise instructions for performing a computation or for solving a problem

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 2 / 22

slide-3
SLIDE 3

Algorithms

Definition

An algorithm is a finite sequence of precise instructions for performing a computation or for solving a problem

Euclidian algorithm

algorithm gcd(x,y) if y = 0 then return(x) else return(gcd(y,x mod y))

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 2 / 22

slide-4
SLIDE 4

Properties of an algorithm

Input it has input values from specified sets

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 3 / 22

slide-5
SLIDE 5

Properties of an algorithm

Input it has input values from specified sets Output from the input values, it produces the output values from specified sets which are the solution

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 3 / 22

slide-6
SLIDE 6

Properties of an algorithm

Input it has input values from specified sets Output from the input values, it produces the output values from specified sets which are the solution Correctness it should produce the correct output values for each set of input values

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 3 / 22

slide-7
SLIDE 7

Properties of an algorithm

Input it has input values from specified sets Output from the input values, it produces the output values from specified sets which are the solution Correctness it should produce the correct output values for each set of input values Finiteness it should produce the output after a finite number of steps for any input

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 3 / 22

slide-8
SLIDE 8

Properties of an algorithm

Input it has input values from specified sets Output from the input values, it produces the output values from specified sets which are the solution Correctness it should produce the correct output values for each set of input values Finiteness it should produce the output after a finite number of steps for any input Effectiveness it must be possible to perform each step correctly and in a finite amount of time

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 3 / 22

slide-9
SLIDE 9

Properties of an algorithm

Input it has input values from specified sets Output from the input values, it produces the output values from specified sets which are the solution Correctness it should produce the correct output values for each set of input values Finiteness it should produce the output after a finite number of steps for any input Effectiveness it must be possible to perform each step correctly and in a finite amount of time Generality it should work for all problems of the desired form

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 3 / 22

slide-10
SLIDE 10

Euclidian algorithm (proof of correctness)

Lemma

If a = bq + r, where a, b, q, and r are positive integers, then gcd(a, b) = gcd(b, r)

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 4 / 22

slide-11
SLIDE 11

Euclidian algorithm (proof of correctness)

Lemma

If a = bq + r, where a, b, q, and r are positive integers, then gcd(a, b) = gcd(b, r)

Proof.

(⇒) Suppose that d divides both a and b. Then d also divides a − bq = r. Hence, any common divisor of a and b must also be a common divisor of b and r (⇐) Suppose that d divides both b and r. Then d also divides bq + r = a. Hence, any common divisor of b and r must also be a common divisor of a and b. Therefore, gcd(a, b) = gcd(b, r)

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 4 / 22

slide-12
SLIDE 12

Description of algorithms in pseudocode

Intermediate step between English prose and formal coding in a programming language

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 5 / 22

slide-13
SLIDE 13

Description of algorithms in pseudocode

Intermediate step between English prose and formal coding in a programming language Focus on the fundamental operation of the program, instead of peculiarities of a given programming language

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 5 / 22

slide-14
SLIDE 14

Description of algorithms in pseudocode

Intermediate step between English prose and formal coding in a programming language Focus on the fundamental operation of the program, instead of peculiarities of a given programming language Analyze the time required to solve a problem using an algorithm, independent of the actual programming language

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 5 / 22

slide-15
SLIDE 15

Maximum

Describe an algorithm for finding the maximum value in a finite sequence of integers

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 6 / 22

slide-16
SLIDE 16

Maximum

Describe an algorithm for finding the maximum value in a finite sequence of integers Input finite sequence of integers a1, . . . , an

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 6 / 22

slide-17
SLIDE 17

Maximum

Describe an algorithm for finding the maximum value in a finite sequence of integers Input finite sequence of integers a1, . . . , an Output ak, k ∈ {1, . . . , n}, where for all j ∈ {1, . . . , n}, aj ≤ ak

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 6 / 22

slide-18
SLIDE 18

Maximum

Describe an algorithm for finding the maximum value in a finite sequence of integers Input finite sequence of integers a1, . . . , an Output ak, k ∈ {1, . . . , n}, where for all j ∈ {1, . . . , n}, aj ≤ ak procedure maximum(a1,...,an) max:= a1 for i:=2 to n if max < ai then max:= ai return max

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 6 / 22

slide-19
SLIDE 19

Linear search

Describe an algorithm for locating an item in a sequence of integers Input integer x and finite sequence of integers a1, . . . , an

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 7 / 22

slide-20
SLIDE 20

Linear search

Describe an algorithm for locating an item in a sequence of integers Input integer x and finite sequence of integers a1, . . . , an Output integer i ∈ {0, . . . , n} where ai = x or i = 0 if x = aj for all aj

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 7 / 22

slide-21
SLIDE 21

Linear search

Describe an algorithm for locating an item in a sequence of integers Input integer x and finite sequence of integers a1, . . . , an Output integer i ∈ {0, . . . , n} where ai = x or i = 0 if x = aj for all aj procedure linear_search(x, a1, . . . , an) i:= 1 while i≤n and x= ai i:=i+1 if i≤n then location:=i else location:=0 return location

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 7 / 22

slide-22
SLIDE 22

Binary search

Describe an algorithm for locating an item in an ordered sequence of integers

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 8 / 22

slide-23
SLIDE 23

Binary search

Describe an algorithm for locating an item in an ordered sequence of integers Input integer x and finite ordered sequence of integers a1, . . . , an

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 8 / 22

slide-24
SLIDE 24

Binary search

Describe an algorithm for locating an item in an ordered sequence of integers Input integer x and finite ordered sequence of integers a1, . . . , an Output integer i ∈ {0, . . . , n} where ai = x or i = 0 if x = aj for all aj

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 8 / 22

slide-25
SLIDE 25

Binary search

Describe an algorithm for locating an item in an ordered sequence of integers Input integer x and finite ordered sequence of integers a1, . . . , an Output integer i ∈ {0, . . . , n} where ai = x or i = 0 if x = aj for all aj The algorithm begins by comparing x with the middle element

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 8 / 22

slide-26
SLIDE 26

Binary search

Describe an algorithm for locating an item in an ordered sequence of integers Input integer x and finite ordered sequence of integers a1, . . . , an Output integer i ∈ {0, . . . , n} where ai = x or i = 0 if x = aj for all aj The algorithm begins by comparing x with the middle element If the middle element is strictly smaller than x, then the search proceeds with the upper half of the list

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 8 / 22

slide-27
SLIDE 27

Binary search

Describe an algorithm for locating an item in an ordered sequence of integers Input integer x and finite ordered sequence of integers a1, . . . , an Output integer i ∈ {0, . . . , n} where ai = x or i = 0 if x = aj for all aj The algorithm begins by comparing x with the middle element If the middle element is strictly smaller than x, then the search proceeds with the upper half of the list Otherwise the search proceeds with the lower half of the list (including the middle element)

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 8 / 22

slide-28
SLIDE 28

Binary search

Describe an algorithm for locating an item in an ordered sequence of integers Input integer x and finite ordered sequence of integers a1, . . . , an Output integer i ∈ {0, . . . , n} where ai = x or i = 0 if x = aj for all aj The algorithm begins by comparing x with the middle element If the middle element is strictly smaller than x, then the search proceeds with the upper half of the list Otherwise the search proceeds with the lower half of the list (including the middle element) Repeat this process until we have a list of size 1

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 8 / 22

slide-29
SLIDE 29

Binary search

Describe an algorithm for locating an item in an ordered sequence of integers Input integer x and finite ordered sequence of integers a1, . . . , an Output integer i ∈ {0, . . . , n} where ai = x or i = 0 if x = aj for all aj The algorithm begins by comparing x with the middle element If the middle element is strictly smaller than x, then the search proceeds with the upper half of the list Otherwise the search proceeds with the lower half of the list (including the middle element) Repeat this process until we have a list of size 1 If x is equal to the single element in the list, then its position is returned

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 8 / 22

slide-30
SLIDE 30

Binary search

Describe an algorithm for locating an item in an ordered sequence of integers Input integer x and finite ordered sequence of integers a1, . . . , an Output integer i ∈ {0, . . . , n} where ai = x or i = 0 if x = aj for all aj The algorithm begins by comparing x with the middle element If the middle element is strictly smaller than x, then the search proceeds with the upper half of the list Otherwise the search proceeds with the lower half of the list (including the middle element) Repeat this process until we have a list of size 1 If x is equal to the single element in the list, then its position is returned Otherwise 0 is returned to indicate that the element was not found

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 8 / 22

slide-31
SLIDE 31

Binary search

procedure binary_search(x, a1, . . . , an) i:= 1 j:= n while i<j m:=⌊(i + j)/2⌋ if x > am then i:= m+1 else j:= m if x = ai then location:= i else location:= 0 return location

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 9 / 22

slide-32
SLIDE 32

Big-O notation for function growth

Definition

Let f, g : N → R or f, g : R → R. Then f is O(g) if there is a constant k and a positive constant c such that ∀x > k (|f(x)| ≤ c|g(x)|)

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 10 / 22

slide-33
SLIDE 33

Big-O notation for function growth

Definition

Let f, g : N → R or f, g : R → R. Then f is O(g) if there is a constant k and a positive constant c such that ∀x > k (|f(x)| ≤ c|g(x)|) c and k are witnesses to the relationship between f and g

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 10 / 22

slide-34
SLIDE 34

Big-O notation for function growth

Definition

Let f, g : N → R or f, g : R → R. Then f is O(g) if there is a constant k and a positive constant c such that ∀x > k (|f(x)| ≤ c|g(x)|) c and k are witnesses to the relationship between f and g O(g) is the set of all functions f that satisfy the condition above: it would be formally correct to write f ∈ O(g)

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 10 / 22

slide-35
SLIDE 35

Big-O notation for function growth

Definition

Let f, g : N → R or f, g : R → R. Then f is O(g) if there is a constant k and a positive constant c such that ∀x > k (|f(x)| ≤ c|g(x)|) c and k are witnesses to the relationship between f and g O(g) is the set of all functions f that satisfy the condition above: it would be formally correct to write f ∈ O(g) Often the condition is: ∀x > k (f(x) ≤ cg(x))

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 10 / 22

slide-36
SLIDE 36

Examples

f(x) = x2 + 2x + 1

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 11 / 22

slide-37
SLIDE 37

Examples

f(x) = x2 + 2x + 1 Show f(x) is O(g) where g(x) = x2

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 11 / 22

slide-38
SLIDE 38

Examples

f(x) = x2 + 2x + 1 Show f(x) is O(g) where g(x) = x2 Show f(x) is also O(g) where g(x) = x3

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 11 / 22

slide-39
SLIDE 39

Examples

f(x) = x2 + 2x + 1 Show f(x) is O(g) where g(x) = x2 Show f(x) is also O(g) where g(x) = x3 Show f(x) is not O(h) where h(x) = x

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 11 / 22

slide-40
SLIDE 40

Examples

f(x) = anxn + an−1xn−1 + . . . + a1x + a0 is O(xn)

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 12 / 22

slide-41
SLIDE 41

Examples

f(x) = anxn + an−1xn−1 + . . . + a1x + a0 is O(xn) f(x) = 1 + 2 + . . . + x is O(x2)

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 12 / 22

slide-42
SLIDE 42

Examples

f(x) = anxn + an−1xn−1 + . . . + a1x + a0 is O(xn) f(x) = 1 + 2 + . . . + x is O(x2) log(n) is O(n)

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 12 / 22

slide-43
SLIDE 43

Examples

f(x) = anxn + an−1xn−1 + . . . + a1x + a0 is O(xn) f(x) = 1 + 2 + . . . + x is O(x2) log(n) is O(n) n! = 1 × 2 × · · · × n is O(nn)

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 12 / 22

slide-44
SLIDE 44

Examples

f(x) = anxn + an−1xn−1 + . . . + a1x + a0 is O(xn) f(x) = 1 + 2 + . . . + x is O(x2) log(n) is O(n) n! = 1 × 2 × · · · × n is O(nn) log(n!) is O(n log(n))

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 12 / 22

slide-45
SLIDE 45

Big-Omega notation for function growth

Definition

Let f, g : N → R or f, g : R → R. Then f is Ω(g) if there if there is a constant k and a positive constant c such that ∀x > k (|f(x)| ≥ c|g(x)|)

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 13 / 22

slide-46
SLIDE 46

Big-Omega notation for function growth

Definition

Let f, g : N → R or f, g : R → R. Then f is Ω(g) if there if there is a constant k and a positive constant c such that ∀x > k (|f(x)| ≥ c|g(x)|) c and k are witnesses to the relationship between f and g

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 13 / 22

slide-47
SLIDE 47

Big-Omega notation for function growth

Definition

Let f, g : N → R or f, g : R → R. Then f is Ω(g) if there if there is a constant k and a positive constant c such that ∀x > k (|f(x)| ≥ c|g(x)|) c and k are witnesses to the relationship between f and g Big-O gives an upper bound on the growth of a function, while Big-Omega gives a lower bound

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 13 / 22

slide-48
SLIDE 48

Big-Omega notation for function growth

Definition

Let f, g : N → R or f, g : R → R. Then f is Ω(g) if there if there is a constant k and a positive constant c such that ∀x > k (|f(x)| ≥ c|g(x)|) c and k are witnesses to the relationship between f and g Big-O gives an upper bound on the growth of a function, while Big-Omega gives a lower bound Often the condition is: ∀x > k (f(x) ≥ cg(x))

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 13 / 22

slide-49
SLIDE 49

Big-Omega notation for function growth

Definition

Let f, g : N → R or f, g : R → R. Then f is Ω(g) if there if there is a constant k and a positive constant c such that ∀x > k (|f(x)| ≥ c|g(x)|) c and k are witnesses to the relationship between f and g Big-O gives an upper bound on the growth of a function, while Big-Omega gives a lower bound Often the condition is: ∀x > k (f(x) ≥ cg(x)) f is Ω(g) if and only if g is O(f)

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 13 / 22

slide-50
SLIDE 50

Big-Theta notation for function growth

Definition

Let f, g : N → R or f, g : R → R. Then f is Θ(g) iff f is O(g) and Ω(g)

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 14 / 22

slide-51
SLIDE 51

Big-Theta notation for function growth

Definition

Let f, g : N → R or f, g : R → R. Then f is Θ(g) iff f is O(g) and Ω(g) f and g are of the same order

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 14 / 22

slide-52
SLIDE 52

Big-Theta notation for function growth

Definition

Let f, g : N → R or f, g : R → R. Then f is Θ(g) iff f is O(g) and Ω(g) f and g are of the same order f is Θ(g) iff there exists constants c1, c2 and k such that for all x > k(c1|g(x)| ≤ |f(x)| ≤ c2|g(x)|)

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 14 / 22

slide-53
SLIDE 53

Big-Theta notation for function growth

Definition

Let f, g : N → R or f, g : R → R. Then f is Θ(g) iff f is O(g) and Ω(g) f and g are of the same order f is Θ(g) iff there exists constants c1, c2 and k such that for all x > k(c1|g(x)| ≤ |f(x)| ≤ c2|g(x)|) f(x) = anxn + an−1xn−1 + . . . + a1x + a0 is Θ(xn) if an = 0

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 14 / 22

slide-54
SLIDE 54

Big-Theta notation for function growth

Definition

Let f, g : N → R or f, g : R → R. Then f is Θ(g) iff f is O(g) and Ω(g) f and g are of the same order f is Θ(g) iff there exists constants c1, c2 and k such that for all x > k(c1|g(x)| ≤ |f(x)| ≤ c2|g(x)|) f(x) = anxn + an−1xn−1 + . . . + a1x + a0 is Θ(xn) if an = 0 f(x) = 1 + 2 + . . . + x is Θ(x2)

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 14 / 22

slide-55
SLIDE 55

Complexity of algorithms

Given an algorithm, how efficient is it for solving the problem relative to input size?

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 15 / 22

slide-56
SLIDE 56

Complexity of algorithms

Given an algorithm, how efficient is it for solving the problem relative to input size? How much time does it take or how much computer memory does it need

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 15 / 22

slide-57
SLIDE 57

Complexity of algorithms

Given an algorithm, how efficient is it for solving the problem relative to input size? How much time does it take or how much computer memory does it need We measure time complexity in terms of the number of basic

  • perations executed and use big-O and big-Theta notation to

estimate it

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 15 / 22

slide-58
SLIDE 58

Complexity of algorithms

Given an algorithm, how efficient is it for solving the problem relative to input size? How much time does it take or how much computer memory does it need We measure time complexity in terms of the number of basic

  • perations executed and use big-O and big-Theta notation to

estimate it Focus on worst-case time complexity. Derive an upper bound on the number of operations it uses to solve a problem with input of particular size (as opposed to the average-case complexity)

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 15 / 22

slide-59
SLIDE 59

Complexity of algorithms

Given an algorithm, how efficient is it for solving the problem relative to input size? How much time does it take or how much computer memory does it need We measure time complexity in terms of the number of basic

  • perations executed and use big-O and big-Theta notation to

estimate it Focus on worst-case time complexity. Derive an upper bound on the number of operations it uses to solve a problem with input of particular size (as opposed to the average-case complexity) Compute an f(n) as worst case for input size n

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 15 / 22

slide-60
SLIDE 60

Complexity of algorithms

Given an algorithm, how efficient is it for solving the problem relative to input size? How much time does it take or how much computer memory does it need We measure time complexity in terms of the number of basic

  • perations executed and use big-O and big-Theta notation to

estimate it Focus on worst-case time complexity. Derive an upper bound on the number of operations it uses to solve a problem with input of particular size (as opposed to the average-case complexity) Compute an f(n) as worst case for input size n Compare efficiency of different algorithms for the same problem

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 15 / 22

slide-61
SLIDE 61

Growth

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 16 / 22

slide-62
SLIDE 62

Linear search

procedure linear_search(x, a1, . . . , an) i:= 1 while i≤n and x= ai i:=i+1 if i≤n then location:=i else location:=0 return location

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 17 / 22

slide-63
SLIDE 63

Worst-Case complexity of linear search

Count the number of comparisons

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 18 / 22

slide-64
SLIDE 64

Worst-Case complexity of linear search

Count the number of comparisons at each step two comparisons are made i ≤ n and x = ai

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 18 / 22

slide-65
SLIDE 65

Worst-Case complexity of linear search

Count the number of comparisons at each step two comparisons are made i ≤ n and x = ai to end the loop, one comparison i ≤ n is made

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 18 / 22

slide-66
SLIDE 66

Worst-Case complexity of linear search

Count the number of comparisons at each step two comparisons are made i ≤ n and x = ai to end the loop, one comparison i ≤ n is made after the loop, one more i ≤ n comparison is made

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 18 / 22

slide-67
SLIDE 67

Worst-Case complexity of linear search

Count the number of comparisons at each step two comparisons are made i ≤ n and x = ai to end the loop, one comparison i ≤ n is made after the loop, one more i ≤ n comparison is made If x = ai , 2i + 1 comparisons are used

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 18 / 22

slide-68
SLIDE 68

Worst-Case complexity of linear search

Count the number of comparisons at each step two comparisons are made i ≤ n and x = ai to end the loop, one comparison i ≤ n is made after the loop, one more i ≤ n comparison is made If x = ai , 2i + 1 comparisons are used If x is not in the list, 2n + 2 comparisons are made which is the worst case

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 18 / 22

slide-69
SLIDE 69

Worst-Case complexity of linear search

Count the number of comparisons at each step two comparisons are made i ≤ n and x = ai to end the loop, one comparison i ≤ n is made after the loop, one more i ≤ n comparison is made If x = ai , 2i + 1 comparisons are used If x is not in the list, 2n + 2 comparisons are made which is the worst case This means that the complexity is Θ(n)

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 18 / 22

slide-70
SLIDE 70

Binary search

procedure binary_search(x, a1, . . . , an) i:= 1 j:= n while i<j m:=⌊(i + j)/2⌋ if x > am then i:= m+1 else j:= m if x = ai then location:= i else location:= 0 return location

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 19 / 22

slide-71
SLIDE 71

Worst-Case complexity of binary search

Assume (for simplicity) n = 2k; so k = log2n

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 20 / 22

slide-72
SLIDE 72

Worst-Case complexity of binary search

Assume (for simplicity) n = 2k; so k = log2n Two comparisons are made at each stage i < j and x > am

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 20 / 22

slide-73
SLIDE 73

Worst-Case complexity of binary search

Assume (for simplicity) n = 2k; so k = log2n Two comparisons are made at each stage i < j and x > am At the first iteration the size of the list is 2k; after the first iteration it is 2k−1. Then 2k−2 and so on until the size of the list is 21 = 2

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 20 / 22

slide-74
SLIDE 74

Worst-Case complexity of binary search

Assume (for simplicity) n = 2k; so k = log2n Two comparisons are made at each stage i < j and x > am At the first iteration the size of the list is 2k; after the first iteration it is 2k−1. Then 2k−2 and so on until the size of the list is 21 = 2 At the last step, a comparison tells us that the size of the list is 20 = 1 and the element is compared with the single remaining element

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 20 / 22

slide-75
SLIDE 75

Worst-Case complexity of binary search

Assume (for simplicity) n = 2k; so k = log2n Two comparisons are made at each stage i < j and x > am At the first iteration the size of the list is 2k; after the first iteration it is 2k−1. Then 2k−2 and so on until the size of the list is 21 = 2 At the last step, a comparison tells us that the size of the list is 20 = 1 and the element is compared with the single remaining element Hence, at most 2k + 2 = 2log2n + 2 comparisons are made

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 20 / 22

slide-76
SLIDE 76

Worst-Case complexity of binary search

Assume (for simplicity) n = 2k; so k = log2n Two comparisons are made at each stage i < j and x > am At the first iteration the size of the list is 2k; after the first iteration it is 2k−1. Then 2k−2 and so on until the size of the list is 21 = 2 At the last step, a comparison tells us that the size of the list is 20 = 1 and the element is compared with the single remaining element Hence, at most 2k + 2 = 2log2n + 2 comparisons are made This means that complexity is Θ(log n)

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 20 / 22

slide-77
SLIDE 77

Computer time

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 21 / 22

slide-78
SLIDE 78

Further topics

An algorithm is polynomial time if for some k it is Θ(nk)

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 22 / 22

slide-79
SLIDE 79

Further topics

An algorithm is polynomial time if for some k it is Θ(nk) Tractable problem: there is a polynomial time algorithm that solves

  • it. (Class P is tractable problems)

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 22 / 22

slide-80
SLIDE 80

Further topics

An algorithm is polynomial time if for some k it is Θ(nk) Tractable problem: there is a polynomial time algorithm that solves

  • it. (Class P is tractable problems)

Intractable problem: there is no polynomial time algorithm that solves it

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 22 / 22

slide-81
SLIDE 81

Further topics

An algorithm is polynomial time if for some k it is Θ(nk) Tractable problem: there is a polynomial time algorithm that solves

  • it. (Class P is tractable problems)

Intractable problem: there is no polynomial time algorithm that solves it Class NP with P ⊆ NP and which has complete problems such as satisfiability of boolean formulas

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 22 / 22

slide-82
SLIDE 82

Further topics

An algorithm is polynomial time if for some k it is Θ(nk) Tractable problem: there is a polynomial time algorithm that solves

  • it. (Class P is tractable problems)

Intractable problem: there is no polynomial time algorithm that solves it Class NP with P ⊆ NP and which has complete problems such as satisfiability of boolean formulas Open problem: NP ⊆ P ?

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 22 / 22

slide-83
SLIDE 83

Further topics

An algorithm is polynomial time if for some k it is Θ(nk) Tractable problem: there is a polynomial time algorithm that solves

  • it. (Class P is tractable problems)

Intractable problem: there is no polynomial time algorithm that solves it Class NP with P ⊆ NP and which has complete problems such as satisfiability of boolean formulas Open problem: NP ⊆ P ? If there is a polynomial time algorithm for any NP complete problem then P = NP

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 22 / 22

slide-84
SLIDE 84

Further topics

An algorithm is polynomial time if for some k it is Θ(nk) Tractable problem: there is a polynomial time algorithm that solves

  • it. (Class P is tractable problems)

Intractable problem: there is no polynomial time algorithm that solves it Class NP with P ⊆ NP and which has complete problems such as satisfiability of boolean formulas Open problem: NP ⊆ P ? If there is a polynomial time algorithm for any NP complete problem then P = NP There are quick algorithms for testing whether a large integer is prime O((log n)6)

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 22 / 22

slide-85
SLIDE 85

Further topics

An algorithm is polynomial time if for some k it is Θ(nk) Tractable problem: there is a polynomial time algorithm that solves

  • it. (Class P is tractable problems)

Intractable problem: there is no polynomial time algorithm that solves it Class NP with P ⊆ NP and which has complete problems such as satisfiability of boolean formulas Open problem: NP ⊆ P ? If there is a polynomial time algorithm for any NP complete problem then P = NP There are quick algorithms for testing whether a large integer is prime O((log n)6) How hard is it to factorise integers?

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 22 / 22

slide-86
SLIDE 86

Further topics

An algorithm is polynomial time if for some k it is Θ(nk) Tractable problem: there is a polynomial time algorithm that solves

  • it. (Class P is tractable problems)

Intractable problem: there is no polynomial time algorithm that solves it Class NP with P ⊆ NP and which has complete problems such as satisfiability of boolean formulas Open problem: NP ⊆ P ? If there is a polynomial time algorithm for any NP complete problem then P = NP There are quick algorithms for testing whether a large integer is prime O((log n)6) How hard is it to factorise integers? We don’t know if it belongs to P (it is in NP)

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 22 / 22

slide-87
SLIDE 87

Further topics

An algorithm is polynomial time if for some k it is Θ(nk) Tractable problem: there is a polynomial time algorithm that solves

  • it. (Class P is tractable problems)

Intractable problem: there is no polynomial time algorithm that solves it Class NP with P ⊆ NP and which has complete problems such as satisfiability of boolean formulas Open problem: NP ⊆ P ? If there is a polynomial time algorithm for any NP complete problem then P = NP There are quick algorithms for testing whether a large integer is prime O((log n)6) How hard is it to factorise integers? We don’t know if it belongs to P (it is in NP) It is very unlikely to be NP complete

Colin Stirling (Informatics) Discrete Mathematics (Chap 3) Today 22 / 22