160 likes | 302 Views
CMSC 203, Section 0401 Discrete Structures Fall 2004 Matt Gaston mgasto1@cs.umbc.edu http://www.csee.umbc.edu/~mgasto1/203. Algorithms Ch. 2.1-2.3. Definition. Book definition: Algorithm : a finite set of precise instructions for performing a computation or for solving a problem
E N D
CMSC 203, Section 0401 Discrete Structures Fall 2004 Matt Gaston mgasto1@cs.umbc.edu http://www.csee.umbc.edu/~mgasto1/203
Algorithms Ch. 2.1-2.3
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.
Algorithm Features • Input • Output • Definiteness • Correctness • Finiteness • Effectiveness • Generality
Linear Search procedurelinear search (x:integer, a1, a2, . . . , an: distinct integers) i := 1 while (i n and x ai) i := i + 1 ifi n thenlocation := i elselocation := 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?
Binary Search procedurebinary search (x:integer, a1, a2, . . . , an: increasing integers) i := 1 j := n while (i j) begin m := (i + j) / 2 ifx amtheni := m + 1 elsej := m end if x = aithen 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
Growth of Functions • Big-O Notation: • C and k are called “witnesses 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.
Pictorial: Big-O Notation C g(x) f(x) g(x) k
Example • 7x2 = O(x3)
Combinations of Functions • f(x) = anxn + an-1xn-1 + . . . + a1x + a0 • f(x) = O(xn) • (f1 + f2)(x) = O(max( g1(x), g2(x) )) • (f1f2)(x) = O(g1(x)g2(x))
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.
Big-Theta Notation • f(x) is of orderg(x) 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)).
Bubble Sort procedurebubble sort (a1, a2, . . . , an) for i := 1 ton – 1 for j := 1 ton – i ifaj > aj+1then interchange aj and aj+1 {a1, a2, . . . , an is in increasing order }
Insertion Sort procedureinsertion sort (a1, a2, . . . , an : real numbers with n 2) for j := 2 ton begin i := 1 whileaj > ai i := i + 1 m := aj for k := 0 toj - i – 1 aj-k:= aj-k-1 ai:= m end {a1, a2, . . . , an are sorted }
Understanding Complexity • n!, 2n, n2, n log(n), n, log(n), 1 • Tractable/Intractable • Solvable/Unsolvable – Halting Problem • Class P, Class NP • NP-Complete