Fundamental Algorithms

Similar documents
Fundamental Algorithms

COMP 382: Reasoning about algorithms

Stoichiometry World of Chemistry: Chapter 9

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

How many molecules are in 0.25 moles of CH 4?

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

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

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

Reading 10 : Asymptotic Analysis

COMPUTER ALGORITHMS. Athasit Surarerks.

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

COMP Analysis of Algorithms & Data Structures

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

COMP Analysis of Algorithms & Data Structures

COMP 555 Bioalgorithms. Fall Lecture 3: Algorithms and Complexity

Stoichiometry & Chemical Reactions

EXTRA CREDIT REMINDER

Recursion: Introduction and Correctness

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

Module 1: Analyzing the Efficiency of Algorithms

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

Module 1: Analyzing the Efficiency of Algorithms

Math 391: Midterm 1.0 Spring 2016

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

CS Data Structures and Algorithm Analysis

Algoritmi di Bioinformatica. Computational efficiency I

Sequences and Summations ICS 6D. Prof. Sandy Irani

CS Non-recursive and Recursive Algorithm Analysis

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

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

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

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

STOICHIOMETRY. Engr. Yvonne Ligaya F. Musico 1

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

CSE 548: (Design and) Analysis of Algorithms

Fall 2016 Test 1 with Solutions

Introduction. An Introduction to Algorithms and Data Structures

Lecture 2: Asymptotic Notation CSCI Algorithms I

MATH 223 FINAL EXAM APRIL, 2005

CSCE 222 Discrete Structures for Computing. Dr. Hyunyoung Lee

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

CSC236 Week 3. Larry Zhang

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

Searching Algorithms. CSE21 Winter 2017, Day 3 (B00), Day 2 (A00) January 13, 2017

Lecture 3: Big-O and Big-Θ

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

Introduction to Algorithms and Asymptotic analysis

Introduction to Algorithms: Asymptotic Notation

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

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

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

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

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

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

Running Time Evaluation

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

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

Lecture 1 - Preliminaries

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

CSC Design and Analysis of Algorithms. Lecture 1

Algorithm efficiency analysis

3 Finite continued fractions

Binary Relations Part II

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,

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

Data Structures and Algorithms CSE 465

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Big , and Definition Definition

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

12 Sequences and Recurrences

Fundamental Algorithms

6th Grade Practice Exam

Order Notation and the Mathematics for Analysis of Algorithms

Solving Recurrences. Lecture 23 CS2110 Fall 2011

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

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

Ordered Pairs and Patterns

CDM. Recurrences and Fibonacci

Problem Set 1 Solutions

Solutions. Problem 1: Suppose a polynomial in n of degree d has the form

Big O Notation. P. Danziger

CS173 Running Time and Big-O. Tandy Warnow

3.3 Accumulation Sequences

Algorithms Exam TIN093 /DIT602

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

Advanced Algorithmics (6EAP)

Review Of Topics. Review: Induction

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

Divide and Conquer CPE 349. Theresa Migler-VonDollen

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

Transcription:

Fundamental Algorithms Chapter 1: Introduction Jan Křetínský Winter 2016/17 Chapter 1: Introduction, Winter 2016/17 1

Part I Overview Chapter 1: Introduction, Winter 2016/17 2

Organization Extent: 2 SWS / 3 credits Master CSE compulsory Master BiomedComp elective Master Informatics bridge courses Lecture only But practice necessary (as usual) Offer of tutorial sheets Maybe review of one exercise at beginning of next lecture Solution examples on the website Slides, tutorial sheets and announcements on the website Chapter 1: Introduction, Winter 2016/17 3

Contents Topics: Fundamentals (Analysis, Complexity Measures) Sorting Parallel Algorithms Searching (hashing, search trees,... ) Arithmetic problems (e.g. parallel matrix and vector operations) Graph problems Techniques: (more important!) Analysis of fundamental algorithms not all algorithms will be explained in detail ( do it yourself! ) Aim: get common basis for other lectures Chapter 1: Introduction, Winter 2016/17 4

Color Code for Headers Blue Headers: for all slides with regular topics Green Headers: summarized details: will be explained in the lecture, but usually not as an explicit slide; green slides will only appear in the handout versions Orange Headers: advanced topics or outlook Black Headers: repeat fundamental concepts that are probably already known, but are important throughout the lecture Chapter 1: Introduction, Winter 2016/17 5

Part II Algorithms Chapter 1: Introduction, Winter 2016/17 6

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 expressed as a finite list of well-defined instructions for calculating a function. 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 2016/17 7

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 2016/17 8

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, e.g.) how about parallel algorithms? ( logical order can be parallel as well as non-deterministic) produce a result Chapter 1: Introduction, Winter 2016/17 9

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 2016/17 10

Example: Fibonnacci 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 2016/17 11

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 2016/17 12

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 2016/17 13

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 observation/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+1 ( ) n+1 f n = 1 5 1 + 5 2 1 5 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 2016/17 14

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 2016/17 15

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 l a s t 1 ; / / where l a s t 1 = f } } Idea: keep the last two values f i 2 and f i 1 in last2 and last1 Chapter 1: Introduction, Winter 2016/17 16

Is This Correct? Only the loop is critical for correctness Basic idea for a correctness proof: 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 a useful property that helps to prove correctness (Note: compare scheme of proof by induction) Chapter 1: Introduction, Winter 2016/17 17

Correctness Invariant {last1 = f i 1 ; last2 = f i 2 } Initialization Before first execution of the loop body, we have i = 2 last1 = 1 = f 1 last2 = 1 = f 0 Chapter 1: Introduction, Winter 2016/17 18

Correctness (2) Maintenance: Proof of invariant: Consider function body {value of variables in brackets} {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 body, 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 2016/17 19

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 we return last1, which holds the value f n q.e.d. Chapter 1: Introduction, Winter 2016/17 20

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 2016/17 21

Part IV Asymptotic Behaviour of Functions Chapter 1: Introduction, Winter 2016/17 22

Asymptotic Behaviour of Functions Definition (Asymptotic upper bound) g is called an asymptotic upper bound of f, or f O(g), if f (n) c > 0 n 0 n n 0 : f (n) c g(n) 0 lim n g(n) < Definition (Asymptotic lower bound) g is called an asymptotic lower bound of f, or f Ω(g), if f (n) c > 0 n 0 n n 0 : f (n) c g(n) 0 < lim n 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)

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 f (n) c > 0 n 0 n n 0 : f (n) c g(n) lim n g(n) = Remark on Notation: c > 0 n 0 n n 0 : f (n) c g(n) reads as: For all c > 0 there exists an n 0 such that for all n n 0 we have f (n) c g(n) Chapter 1: Introduction, Winter 2016/17 24

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 2016/17 25

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 2016/17 26