CMSC 203 Section 0401 Discrete Structures Fall 2004

  • Slides: 16
Download presentation
CMSC 203, Section 0401 Discrete Structures Fall 2004 Matt Gaston mgasto 1@cs. umbc. edu

CMSC 203, Section 0401 Discrete Structures Fall 2004 Matt Gaston mgasto 1@cs. umbc. edu http: //www. csee. umbc. edu/~mgasto 1/203 UMBC CMSC 203, Section 0401 -- Fall 2004 1

Algorithms Ch. 2. 1 -2. 3 UMBC CMSC 203, Section 0401 -- Fall 2004

Algorithms Ch. 2. 1 -2. 3 UMBC CMSC 203, Section 0401 -- Fall 2004 2

Definition • Book definition: § Algorithm: a finite set of precise instructions for performing

Definition • Book definition: § Algorithm: a finite set of precise instructions for performing a computation or for solving a problem • Better definition (? ): § Algorithm: An algorithm is a finite set of unambiguous, executable instructions that directs a terminating activity. UMBC CMSC 203, Section 0401 -- Fall 2004 3

Algorithm Features • • Input Output Definiteness Correctness Finiteness Effectiveness Generality UMBC CMSC 203,

Algorithm Features • • Input Output Definiteness Correctness Finiteness Effectiveness Generality UMBC CMSC 203, Section 0401 -- Fall 2004 4

Linear Search procedure linear search (x: integer, a 1, a 2, . . .

Linear Search procedure linear search (x: integer, a 1, a 2, . . . , an: distinct integers) i : = 1 while (i n and x ai) i : = i + 1 if i n then location : = i else location : = 0 10 13 17 1 4 18 3 5 11 9 8 16 2 7 6 14 15 19 20 18 12 How many steps? Worse case? On average? UMBC CMSC 203, Section 0401 -- Fall 2004 5

Binary Search procedure binary search (x: integer, a 1, a 2, . . .

Binary Search procedure binary search (x: integer, a 1, a 2, . . . , an: increasing integers) i : = 1 j : = n while (i j) begin m : = (i + j) / 2 if x am then i : = m + 1 else j : = m end if x = ai then location : = I else location : = 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 UMBC CMSC 203, Section 0401 -- Fall 2004 6

Growth of Functions • Big-O Notation: Let f and g be functions from the

Growth of Functions • Big-O Notation: Let f and g be functions from the set of integers to the set of integers or the set of real numbers to the set of real numbers. We say that f(x) is O(g(x)) if there are constants C and k such that | f(x) | C | g(x) | whenever x > k. § C and k are called “witnesses UMBC CMSC 203, Section 0401 -- Fall 2004 7

Pictorial: Big-O Notation C g(x) f(x) g(x) k UMBC CMSC 203, Section 0401 --

Pictorial: Big-O Notation C g(x) f(x) g(x) k UMBC CMSC 203, Section 0401 -- Fall 2004 8

Example • 7 x 2 = O(x 3) UMBC CMSC 203, Section 0401 --

Example • 7 x 2 = O(x 3) UMBC CMSC 203, Section 0401 -- Fall 2004 9

Combinations of Functions • f(x) = anxn + an-1 xn-1 +. . . +

Combinations of Functions • f(x) = anxn + an-1 xn-1 +. . . + a 1 x + a 0 § f(x) = O(xn) • (f 1 + f 2)(x) = O(max( g 1(x), g 2(x) )) • (f 1 f 2)(x) = O(g 1(x)g 2(x)) UMBC CMSC 203, Section 0401 -- Fall 2004 10

Big-Omega Notation Let f and g be functions from the set of integers to

Big-Omega Notation Let f and g be functions from the set of integers to the set of integers or the set of real numbers to the set of real numbers. We say that f(x) is (g(x)) if there are constants C and k such that | f(x) | C | g(x) | whenever x > k. UMBC CMSC 203, Section 0401 -- Fall 2004 11

Big-Theta Notation Let f and g be functions from the set of integers to

Big-Theta Notation Let f and g be functions from the set of integers to the set of integers or the set of real numbers to the set of real numbers. We say that f(x) is (g(x)) if f(x) is O(g(x)) and f(x) is (g(x)). § f(x) is of order g(x) UMBC CMSC 203, Section 0401 -- Fall 2004 12

Bubble Sort procedure bubble sort (a 1, a 2, . . . , an)

Bubble Sort procedure bubble sort (a 1, a 2, . . . , an) for i : = 1 to n – 1 for j : = 1 to n – i if aj > aj+1 then interchange aj and aj+1 {a 1, a 2, . . . , an is in increasing order } UMBC CMSC 203, Section 0401 -- Fall 2004 13

Insertion Sort procedure insertion sort (a 1, a 2, . . . , an

Insertion Sort procedure insertion sort (a 1, a 2, . . . , an : real numbers with n 2) for j : = 2 to n begin i : = 1 while aj > ai i : = i + 1 m : = aj for k : = 0 to j - i – 1 aj-k : = aj-k-1 ai : = m end {a 1, a 2, . . . , an are sorted } UMBC CMSC 203, Section 0401 -- Fall 2004 14

Make Change! UMBC CMSC 203, Section 0401 -- Fall 2004 15

Make Change! UMBC CMSC 203, Section 0401 -- Fall 2004 15

Understanding Complexity • n!, 2 n, n 2, n log(n), n, log(n), 1 •

Understanding Complexity • n!, 2 n, n 2, n log(n), n, log(n), 1 • • Tractable/Intractable Solvable/Unsolvable – Halting Problem Class P, Class NP NP-Complete UMBC CMSC 203, Section 0401 -- Fall 2004 16