Fundamental Algorithms

Similar documents
Fundamental Algorithms

COMP 382: Reasoning about algorithms

Stoichiometry World of Chemistry: Chapter 9

How many molecules are in 0.25 moles of CH 4?

Growth of Functions (CLRS 2.3,3)

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 3. Θ Notation. Comparing Algorithms

Input Decidable Language -- Program Halts on all Input Encoding of Input -- Natural Numbers Encoded in Binary or Decimal, Not Unary

5 + 9(10) + 3(100) + 0(1000) + 2(10000) =

Limiting Reactants. and Percentage Yield. Section 3

Algorithm Analysis Recurrence Relation. Chung-Ang University, Jaesung Lee

Computational Complexity

Algorithms Design & Analysis. Analysis of Algorithm

Induction. Induction. Induction. Induction. Induction. Induction 2/22/2018

Analysis of Algorithms [Reading: CLRS 2.2, 3] Laura Toma, csci2200, Bowdoin College

Announcements. discussion tomorrow. tomorrow. 10/15 (Type I) and Wednesday, 10/17 (Type II) by 7:00pm. 1. Bring handout from Tuesday to

Reading 10 : Asymptotic Analysis

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

COMP 555 Bioalgorithms. Fall Lecture 3: Algorithms and Complexity

Stoichiometry & Chemical Reactions

EXTRA CREDIT REMINDER

COMPUTER ALGORITHMS. Athasit Surarerks.

Quiz 1 Solutions. Problem 2. Asymptotics & Recurrences [20 points] (3 parts)

What does "USB" stand for? Upstream Series Buffer Unilateral Saving Button Universal Serial Bus United Storage Banks

COMP Analysis of Algorithms & Data Structures

Module 1: Analyzing the Efficiency of Algorithms

COMP Analysis of Algorithms & Data Structures

Chemical reactions are processes in which the atoms of one or more substances are rearranged to form different chemical compounds.

CS Data Structures and Algorithm Analysis

Module 1: Analyzing the Efficiency of Algorithms

Algorithms and Theory of Computation. Lecture 2: Big-O Notation Graph Algorithms

Recursion: Introduction and Correctness

Algoritmi di Bioinformatica. Computational efficiency I

Defining Efficiency. 2: Analysis. Efficiency. Measuring efficiency. CSE 421: Intro Algorithms. Summer 2007 Larry Ruzzo

CS Non-recursive and Recursive Algorithm Analysis

Computer Algorithms CISC4080 CIS, Fordham Univ. Outline. Last class. Instructor: X. Zhang Lecture 2

Computer Algorithms CISC4080 CIS, Fordham Univ. Instructor: X. Zhang Lecture 2

data structures and algorithms lecture 2

Math 391: Midterm 1.0 Spring 2016

Mid-term Exam Answers and Final Exam Study Guide CIS 675 Summer 2010

Define Efficiency. 2: Analysis. Efficiency. Measuring efficiency. CSE 417: Algorithms and Computational Complexity. Winter 2007 Larry Ruzzo

STOICHIOMETRY. Engr. Yvonne Ligaya F. Musico 1

Mat Week 6. Fall Mat Week 6. Algorithms. Properties. Examples. Searching. Sorting. Time Complexity. Example. Properties.

Student Responsibilities Week 6. Mat Properties of Algorithms. 3.1 Algorithms. Finding the Maximum Value in a Finite Sequence Pseudocode

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation:

Lecture 3. Big-O notation, more recurrences!!

Fall 2016 Test 1 with Solutions

Introduction. An Introduction to Algorithms and Data Structures

CSC236 Week 3. Larry Zhang

Problem Set 1. MIT students: This problem set is due in lecture on Wednesday, September 19.

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms

Lecture 3: Big-O and Big-Θ

Introduction to Algorithms: Asymptotic Notation

Introduction to Algorithms and Asymptotic analysis

CSE 421: Intro Algorithms. 2: Analysis. Winter 2012 Larry Ruzzo

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 1: Introduction

Section 1.8 The Growth of Functions. We quantify the concept that g grows at least as fast as f.

Sequences and Summations ICS 6D. Prof. Sandy Irani

Running Time Evaluation

Algorithm Analysis, Asymptotic notations CISC4080 CIS, Fordham Univ. Instructor: X. Zhang

CS 344 Design and Analysis of Algorithms. Tarek El-Gaaly Course website:

Algorithm efficiency analysis

Asymptotic Analysis 1

LP03 Chapter 5. A prime number is a natural number greater that 1 that has only itself and 1 as factors. 2, 3, 5, 7, 11, 13, 17, 19, 23, 29,

Section 3.2 The Growth of Functions. We quantify the concept that g grows at least as fast as f.

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation:

Asymptotic Analysis. Slides by Carl Kingsford. Jan. 27, AD Chapter 2

Lecture 2: Asymptotic Notation CSCI Algorithms I

with the size of the input in the limit, as the size of the misused.

CSE 548: (Design and) Analysis of Algorithms

Order Notation and the Mathematics for Analysis of Algorithms

MATH 223 FINAL EXAM APRIL, 2005

Solving Recurrences. Lecture 23 CS2110 Fall 2011

Outline. 1 Introduction. 3 Quicksort. 4 Analysis. 5 References. Idea. 1 Choose an element x and reorder the array as follows:

Lecture 1 - Preliminaries

Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort

Ordered Pairs and Patterns

Binary Relations Part II

CSCE 222 Discrete Structures for Computing. Dr. Hyunyoung Lee

Big O Notation. P. Danziger

CDM. Recurrences and Fibonacci. 20-fibonacci 2017/12/15 23:16. Terminology 4. Recurrence Equations 3. Solution and Asymptotics 6.

CS173 Running Time and Big-O. Tandy Warnow

CS 4407 Algorithms Lecture 3: Iterative and Divide and Conquer Algorithms

Advanced Algorithmics (6EAP)

Review Of Topics. Review: Induction

Divide and Conquer CPE 349. Theresa Migler-VonDollen

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

Data Structures and Algorithms Chapter 2

CSC Design and Analysis of Algorithms. Lecture 1

282 Math Preview. Chris Brown. September 8, Why This? 2. 2 Logarithms Basic Identities Basic Consequences...

Asymptotic Notation. such that t(n) cf(n) for all n n 0. for some positive real constant c and integer threshold n 0

CS 61B Asymptotic Analysis Fall 2017

Recurrences COMP 215

Markov chains. Randomness and Computation. Markov chains. Markov processes

Hoare Logic and Model Checking

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Given a sequence a 1, a 2,...of numbers, the finite sum a 1 + a 2 + +a n,wheren is an nonnegative integer, can be written

Data Structures and Algorithms CSE 465

Big , and Definition Definition

12 Sequences and Recurrences

Transcription:

Fundamental Algorithms Chapter 1: Introduction Michael Bader Winter 2011/12 Chapter 1: Introduction, Winter 2011/12 1

Part I Overview Chapter 1: Introduction, Winter 2011/12 2

Organizational Stuff 2 SWS / 3 credits Master CSE compulsory Master BiomedComp elective Lecture only But practice necessary (as usual) Offer of tutorial sheets Maybe review of one exercise at beginning of next lecture Slides, tutorial sheets and announcements on website Chapter 1: Introduction, Winter 2011/12 3

Contents Introduction of fundamental algorithms and their analysis Aim: get common basis for other lectures Topics Fundamentals (Analysis, Complexity Measures) Basic discipline: sorting (Selecting) Searching (hashing, search trees,... ) Arithmetic problems (e.g. parallel matrix and vector operations) Graph problems Chapter 1: Introduction, Winter 2011/12 4

Part II Algorithms Chapter 1: Introduction, Winter 2011/12 5

What is an Algorithm? Some Definitions Definition (found on numerous websites) An algorithm is a set of rules that specify the order and kind of arithmetic operations that are used on a specified set of data. Definition (Wikipedia) An algorithm is an effective method for solving a problem using a finite sequence of instructions. Definition (Donald Knuth) An algorithm is a finite, definite, effective procedure, with some output. Definition (Britannica.com) Systematic procedure that produces in a finite number of steps the answer to a question or the solution of a problem. Chapter 1: Introduction, Winter 2011/12 6

Example Algorithm: Chocolate Chip Cookies Ingredients 1 cup butter, softened 1 cup white sugar 1 cup packed brown sugar 2 eggs 2 teaspoons vanilla extract 3 cups all-purpose flour 1 teaspoon baking soda 2 teaspoons hot water 1/2 teaspoon salt 2 cups semisweet chocolate chips 1 cup chopped walnuts Directions 1. Preheat oven to 350 degrees F (175 degrees C). 2. Cream together the butter, white sugar, and brown sugar until smooth. Beat in the eggs one at a time, then stir in the vanilla. Dissolve baking soda in hot water. Add to batter along with salt. Stir in flour, chocolate chips, and nuts. Drop by large spoonfuls onto ungreased pans. 3. Bake for about 10 minutes in the preheated oven, or until edges are nicely browned. Chapter 1: Introduction, Winter 2011/12 7

Essential properties of an algorithm an algorithm is finite (w.r.t.: set of instructions, use of resources, time of computation) instructions are precise and computable instructions have a specified logical order, however, we can discriminate between deterministic algorithms (every step has a well-defined successor) non-deterministic algorithms (randomized algorithms, but also parallel algorithms!) produce a result Chapter 1: Introduction, Winter 2011/12 8

Basic Questions About Algorithms For each algorithm, we should answer the following basic questions: does it terminate? is it correct? is the result of the algorithm determined? how much resources will it use in terms of memory? (and memory bandwidth?) operations? run-time?...? Chapter 1: Introduction, Winter 2011/12 9

Example: Fibonacci Numbers Definition The sequence f j, j N, of the Fibonacci numbers is defined recursively as: f 0 := 1 f 1 := 1 f j := f j 1 + f j 2 for j 2 Origin: simple model of a rabbit population starts with one pair of rabbits (male and female) every month, each pair of rabbits gives birth to a new pair but: new-born rabbits need one month to become mature (compare lecture in Scientific Computing) Chapter 1: Introduction, Winter 2011/12 10

A Recursive Algorithm for the Fibonacci Numbers Fibo ( n : Integer ) : Integer { i f n=0 then return 1; i f n=1 then return 1; i f n>1 then return Fibo ( n 1) + Fibo ( n 2); } How many arithmetic operations does it take to compute f j? Definition T Fibo (n) shall be the number of arithmetic operations (here: additions) that the algorithm Fibo will perform with n as input parameter. Chapter 1: Introduction, Winter 2011/12 11

Number of Additions by Fibo We observe that: T Fibo (0) = T Fibo (1) = 0 (both cases do not require any additions) If the parameter n is larger than 1, then we have to: perform all additions of calling Fibo(n 1) and Fibo(n 2) and add the two results thus: No better: T Fibo (n) = T Fibo (n 1) + T Fibo (n 2) + 1 T Fibo (n) = T Fibo (n 1) + T Fibo (n 2) + 3 because: we forgot to compute n 1 and n 2 We obtain a so-called recurrence equation Chapter 1: Introduction, Winter 2011/12 12

Number of Additions by Fibo (2) Solving the recurrence: (in this example) first observation: recurrence looks a lot like Fibonacci recurrence, itself draw a table of n vs. additions assumption: T Fibo (n) = 3f n 3 Proof: by induction over n Estimate of the number of operations: algebraic formulation of the Fibonacci numbers: (( ) n ( ) n ) f n = 1 5 + 1 5 1 5 2 2 exponential growth of number of operations example: T Fibo (100) 10 21 (requires more than 30,000 years, if we process one addition per nanosecond) Chapter 1: Introduction, Winter 2011/12 13

Why is Fibo so Slow? Examine recursive calls: Fibo(4) Fibo(2) Fibo(3) Fibo(0) Fibo(1) Fibo(1) Fibo(2) Fibo(0) Fibo(1) Obviously, lots of numbers f j are computed multiple times! Chapter 1: Introduction, Winter 2011/12 14

An Iterative Algorithm for the Fibonacci Numbers F i b I t ( n : Integer ) : Integer { i f n < 2 then return 1; else { l a s t 2 := 1; l a s t 1 := 1: f o r i from 2 to n do { f := l a s t 2 + l a s t 1 ; l a s t 2 := l a s t 1 ; l a s t 1 := f ; } return f ; } } Idea: keep the last two values f i 2 and f i 1 in last2 and last1 Chapter 1: Introduction, Winter 2011/12 15

Is This Correct? Only loop critical Basic idea: use so-called loop invariant to prove properties about loop Statement of conditions that are valid for each loop execution Here, e.g. before the loop body is executed: last1 and last2 contain f i 1 and f i 2, respectively For loop invariants, we need to prove: Initialization: It is true prior to first execution of loop (body) Maintenance: If it is true before iteration of loop, it remains true before next iteration Termination: When loop terminates, invariant gives us useful property, helping to prove correctness (Note: compare scheme of proof by induction) Chapter 1: Introduction, Winter 2011/12 16

Correctness Invariant {last1 = f i 1 ; last2 = f i 2 } Initialization Before first iteration of loop, we have i = 2 last1 = 1 = f 1 last2 = 1 = f 0 Chapter 1: Introduction, Winter 2011/12 17

Correctness (2) Maintenance: Proof of invariant: Consider function body {last1 = f i 1 ; last2 = f i 2 } f := l a s t 2 + l a s t 1 ; {last1 = f i 1 ; last2 = f i 2 ; f = f i } l a s t 2 := l a s t 1 ; {last1 = f i 1 ; last2 = f i 1 ; f = f i } l a s t 1 := f ; {last1 = f i ; last2 = f i 1 ; f = f i } At end of (before beginning of next) loop iteration, we have implicitly i := i + 1; {last1 = f i 1 ; last2 = f i 2 } thus, invariant still holds at next loop entry Chapter 1: Introduction, Winter 2011/12 18

Correctness (3) Termination At loop termination, i exceeds n; thus i = n + 1 (Note: think in while-loops where increment is done explicitly) If loop invariant holds, then last1 and last2 contain f i 1 = f n and f i 2 = f n 1, respectively f equals last1, hence f n q.e.d. Chapter 1: Introduction, Winter 2011/12 19

Does FibIt Require Fewer Operations? We observe that: T FibIt (1) = T Fibo (1) = 0 (no additions, if input parameter n < 2) If n 2: the for loop will be executed n 1 times in the loop body, there is always exactly one addition per loop iteration Therefore: T FibIt (n) = { 0 for n 1 n 1 for n 2 the operation count of FibIt increases linearly with n. Question: will f 10 9 be computed in 1 second? Chapter 1: Introduction, Winter 2011/12 20

Part IV Asymptotic Behaviour of Functions Chapter 1: Introduction, Winter 2011/12 21

Asymptotic Behaviour of Functions Definition (Asymptotic upper bound) g is called an asymptotic upper bound of f, or f O(g), if c > 0 n 0 n n 0 : f (n) c g(n) Definition (Asymptotic lower bound) g is called an asymptotic lower bound of f, or f Ω(g), if c > 0 n 0 n n 0 : f (n) c g(n) Definition (Asymptotically tight bound) g is called an asymptotically tight bound of f, or f Θ(g), if f O(g) and f Ω(g) Chapter 1: Introduction, Winter 2011/12 22

Asymptotic Behaviour of Functions (2) Definition (Asymptotically smaller) f is called asymptotically smaller than g, or f o(g), if f (n) c > 0 n 0 n n 0 : f (n) c g(n) lim n g(n) = 0 Definition (Asymptotically larger) f is called asymptotically larger than g, or f ω(g), if c > 0 n 0 n n 0 : f (n) c g(n) lim n g(n) f (n) = 0 Chapter 1: Introduction, Winter 2011/12 23

Properties of the Asymptotics Relations O, Ω, Θ, o, and ω define relations: all of the relations are transitive, e.g.: f O(g) and g O(h) f O(h) O, Ω, and Θ are reflexive: f O(f ) f Ω(f ) f Θ(f ) only Θ is symmetric: f Θ(g) g Θ(f ) and there is a transpose symmetry: f O(g) g Ω(f ) f o(g) g ω(f ) Chapter 1: Introduction, Winter 2011/12 24

Example: Asymptotics of the Fibonacci Numbers Famous inequality 2 n 2 f n 2 n f n O(2 n ) (with c = 1, proof by induction): (Base case) for n = 0: f 0 = 1 2 0 = 1 (Base case) for n = 1: f 1 = 1 2 1 = 2 (Inductive case) from n 1 and n 2 to n (n 2): f n = f n 1 + f n 2 2 n 1 + 2 n 2 = 3 2 n 2 2 n f n Ω(2 n/2 ) (proof by induction over k = n/2 only for even n): (Base case) for k = 0 n = 0: f 0 = 1 2 0 = 1 (Ind. case) induction step: from n = 2k 2 to n = 2k (n 2): f 2k = f 2k 1 + f 2k 2 2f 2k 2 = 2f 2(k 1) 2 2 k 1 = 2 k Chapter 1: Introduction, Winter 2011/12 25