Introduction to Computer Science Lecture 5: Algorithms

Similar documents
Big O 2/14/13. Administrative. Does it terminate? David Kauchak cs302 Spring 2013

Lecture 2: Asymptotic Notation CSCI Algorithms I

3.1 Asymptotic notation

i=1 i B[i] B[i] + A[i, j]; c n for j n downto i + 1 do c n i=1 (n i) C[i] C[i] + A[i, j]; c n

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

Data Structures and Algorithms Chapter 2

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

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

The Time Complexity of an Algorithm

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Computational Complexity

The Time Complexity of an Algorithm

CS473 - Algorithms I

Growth of Functions (CLRS 2.3,3)

data structures and algorithms lecture 2

Algorithms, Design and Analysis. Order of growth. Table 2.1. Big-oh. Asymptotic growth rate. Types of formulas for basic operation count

COMP 9024, Class notes, 11s2, Class 1

Module 1: Analyzing the Efficiency of Algorithms

Module 1: Analyzing the Efficiency of Algorithms

Algorithms and Their Complexity

Lecture 2. More Algorithm Analysis, Math and MCSS By: Sarah Buchanan

Cpt S 223. School of EECS, WSU

CIS 121. Analysis of Algorithms & Computational Complexity. Slides based on materials provided by Mary Wootters (Stanford University)

Analysis of Algorithms

CS 4407 Algorithms Lecture 2: Growth Functions

Data Structures and Algorithms CSE 465

Analysis of Algorithms

Analysis of Algorithms

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

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

Review Of Topics. Review: Induction

Introduction to Algorithms and Asymptotic analysis

Asymptotic Analysis of Algorithms. Chapter 4

CS 577 Introduction to Algorithms: Strassen s Algorithm and the Master Theorem

Grade 11/12 Math Circles Fall Nov. 5 Recurrences, Part 2

Notes for Recitation 14

COMPUTER ALGORITHMS. Athasit Surarerks.

CSCE 222 Discrete Structures for Computing

2. ALGORITHM ANALYSIS

CS Non-recursive and Recursive Algorithm Analysis

Asymptotic Analysis 1

CSC Design and Analysis of Algorithms. Lecture 1

COMP Analysis of Algorithms & Data Structures

EECS 477: Introduction to algorithms. Lecture 5

COMP Analysis of Algorithms & Data Structures

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 2

Time complexity analysis

P, NP, NP-Complete, and NPhard

CSC236 Week 4. Larry Zhang

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

Data Structures and Algorithms Running time and growth functions January 18, 2018

Big-O Notation and Complexity Analysis

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

Analysis of Algorithms

More Asymptotic Analysis Spring 2018 Discussion 8: March 6, 2018

Sorting. Chapter 11. CSE 2011 Prof. J. Elder Last Updated: :11 AM

Omega notation. Transitivity etc.

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

Divide and Conquer CPE 349. Theresa Migler-VonDollen

Ch01. Analysis of Algorithms

Data Structures and Algorithms. Asymptotic notation

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

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc.

ECOM Discrete Mathematics

Big O (Asymptotic Upper Bound)

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

Recommended readings: Description of Quicksort in my notes, Ch7 of your CLRS text.

Introduction to Algorithms 6.046J/18.401J

Introduction to Algorithms: Asymptotic Notation

Algorithms Design & Analysis. Analysis of Algorithm

LECTURE NOTES ON DESIGN AND ANALYSIS OF ALGORITHMS

Homework 1 Solutions

Math 391: Midterm 1.0 Spring 2016

2.2 Asymptotic Order of Growth. definitions and notation (2.2) examples (2.4) properties (2.2)

Homework 1 Submission

CS173 Running Time and Big-O. Tandy Warnow

CSED233: Data Structures (2017F) Lecture4: Analysis of Algorithms

CIS 121 Data Structures and Algorithms with Java Spring Big-Oh Notation Monday, January 22/Tuesday, January 23

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

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

Asymptotic Algorithm Analysis & Sorting

3. Algorithms. What matters? How fast do we solve the problem? How much computer resource do we need?

Md Momin Al Aziz. Analysis of Algorithms. Asymptotic Notations 3 COMP Computer Science University of Manitoba

Introduction to Algorithms

COMP 555 Bioalgorithms. Fall Lecture 3: Algorithms and Complexity

Central Algorithmic Techniques. Iterative Algorithms

Running Time Evaluation

IS 709/809: Computational Methods in IS Research Fall Exam Review

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

CSCE 222 Discrete Structures for Computing. Review for Exam 2. Dr. Hyunyoung Lee !!!

Answer the following questions: Q1: ( 15 points) : A) Choose the correct answer of the following questions: نموذج اإلجابة

In-Class Soln 1. CS 361, Lecture 4. Today s Outline. In-Class Soln 2

Intro to Theory of Computation

Principles of Algorithm Analysis

Foundations II: Data Structures and Algorithms

Data Structure and Algorithm Homework #1 Due: 2:20pm, Tuesday, March 12, 2013 TA === Homework submission instructions ===

Ch 01. Analysis of Algorithms

Analysis of Algorithms - Using Asymptotic Bounds -

Agenda. We ve discussed

Divide and Conquer. Andreas Klappenecker. [based on slides by Prof. Welch]

Transcription:

Introduction to Computer Science Lecture 5: Algorithms Tian-Li Yu Taiwan Evolutionary Intelligence Laboratory (TEIL) Department of Electrical Engineering National Taiwan University tianliyu@cc.ee.ntu.edu.tw Slides made by Tian-Li Yu, Jie-Wei Wu, and Chu-Yu Hsu Tian-Li Yu Algorithms 1 / 41

Algorithms Definitions Algorithm: ordered set of unambiguous, executable steps that defines a terminating process. Program: formal representation of an algorithm. Process: activity of executing a program. Primitives, programming languages. Abstraction Tian-Li Yu Algorithms 2 / 41

Algorithms Folding a Bird Refer to figure 5.2 in Computer Science An Overview 11th Edition. Tian-Li Yu Algorithms 3 / 41

Algorithms Origami Primitives Refer to figure 5.3 in Computer Science An Overview 11th Edition. Tian-Li Yu Algorithms 4 / 41

Algorithms Algorithm Representation Lamp doesn t work Flowchart Popular in 50s and 60s Overwhelming for complex algorithms Lamp plugged in? No Plug in lamp Pseudocode A loosen version of formal programming languages Bulb burned out? No Yes Replace bulb Buy new lamp Tian-Li Yu Algorithms 5 / 41

Algorithms Pseudocode Primitives Assignment name expression Conditional selection if (condition) then (activity) Repeated execution while (condition) do (activity) procedure Greetings Count 3 while (Count > 0) do (print the message Hello and Count Count 1) Procedure procedure name Tian-Li Yu Algorithms 6 / 41

Problem Solving Pólya s Problem Solving Steps How to Solve It by George Pólya, 1945. 1 Understand the problem. 2 Devise a plan for solving the problem. 3 Carry out the plan. 4 Evaluate the solution for accuracy and its potential as a tool for solving other problems. Tian-Li Yu Algorithms 7 / 41

Problem Solving Problem Solving Top-down Stepwise refinement Problem decomposition Bottom-up Both methods often complement each other Usually, planning top-down implementation bottom-up Tian-Li Yu Algorithms 8 / 41

Problem Solving Iterations Iterations Loop control Initialize: Establish an initial state that will be modified toward the termination condition Test: Modify: Compare the current state to the termination condition and terminate the repetition if equal Change the state in such a way that it moves toward the termination condition Tian-Li Yu Algorithms 9 / 41

Problem Solving Iterations Loops Pre-test (while... ) Post-test (do... while, repeat... until) Test condition Condition false Activity Activity Condition true Condition false Test condition Condition true Tian-Li Yu Algorithms 10 / 41

Insertion Sort Initial list: Fred Alex Diana Byron Problem Solving Insertion Sort Sorted Fred Alex Diana Byron Fred Diana Byron Alex Fred Diana Byron Alex Alex Fred Diana Byron Sorted Alex Fred Diana Byron Alex Fred Byron Diana Alex Fred Byron Diana Alex Diana Fred Byron Sorted Alex Diana Fred Byron Alex Diana Fred Byron Alex Diana Fred Byron Alex Byron Diana Fred Sorted Alex Byron Diana Fred Alex Byron Diana Fred Alex Byron Diana Fred Alex Byron Diana Fred Tian-Li Yu Sorted list: Alex Fred Algorithms 11 / 41

Problem Solving Insertion Sort Pseudocode for Insertion Sort procedure InsertionSort (List) 1 N 2 2 while (the value of N does not exceed the length of List) do 3 (Select the N-th entry in List as the pivot entry 4 Move the pivot to a temporary location leaving a hole in List 5 while (there is a name above the hole and that name is greater than the pivot) do 6 (move the name above the hole down into the hole leaving a hole above the name) 7 Move the pivot entry into the hole in List 8 N N + 1 9 ) Tian-Li Yu Algorithms 12 / 41

Problem Solving Recursion Binary Search Original list First sublist Second sublist Alice Bob David Elaine Fred George Harry Irene John Kelly Larry Mary Nancy Oliver Irene John Kelly Larry Mary Nancy Oliver Irene John Kelly Tian-Li Yu Algorithms 13 / 41

Problem Solving Recursion Pseudocode for Binary Search procedure BinarySearch (List, TargetValue) 1 if (List empty) then 2 (Report that the search failed.) 3 else ( 4 Select the middle entry in List to be the TestEntry 5 Execute the block of instructions below that is associated with the appropriate case. 6 case 1: TagetValue = TestEntry 7 (Report that the search succeeded.) 8 case 2: TagetValue < TestEntry 9 (Search the portion of List preceding TestEntry for TargetValue, and report the result of that search.) 10 case 3: TagetValue > TestEntry 11 (Search the portion of List succeeding TestEntry for TargetValue, and report the result of that search.) 12 ) end if Tian-Li Yu Algorithms 14 / 41

Problem Solving Recursion Recursive Problem Solving (contd.) Factorial int factorial (int x) { if (x==0) return 1; return x factorial(x 1); } Do not abuse Calling functions takes a long time Avoid tail recursions int factorial (int x) { int product = 1; for (int i=1; i<=x; ++i) product = i; return product; } int Fibonacci (int x) { if (x==0) return 0; if (x==1) return 1; return Fibonacci(x 2) + Fibonacci(x 1); } Tian-Li Yu Algorithms 15 / 41

Dynamic Programming Divide and Conquer vs. Dynamic Programming Divide and conquer (D&C): Subproblems Top-down Binary search, merge sort,... Dynamic programming (DP): Subprograms share subsubproblems Bottom-up Shortest path, matrix-chain multiplication,... Tian-Li Yu Algorithms 16 / 41

Dynamic Programming Shortest Path Shortest Path Shortest AE = min i {A,B,C,D,E} (Shortest Ai + Shortest ie ) A 3 A:3 B 5 C 3 1 2 5 D 6 E Tian-Li Yu Algorithms 17 / 41

Dynamic Programming Shortest Path Shortest Path (contd.) Shortest AE = min i {A,B,C,D,E} (Shortest Ai + Shortest ie ) A 3 A:3 B:8 B 5 C 3 1 2 5 D B:4 6 E Tian-Li Yu Algorithms 18 / 41

Dynamic Programming Shortest Path Shortest Path (contd.) Shortest AE = min i {A,B,C,D,E} (Shortest Ai + Shortest ie ) A 3 A:3 D:7 B 5 C 3 1 2 5 D B:4 6 E D:10 Tian-Li Yu Algorithms 19 / 41

Dynamic Programming Shortest Path Shortest Path (contd.) Shortest AE = min i {A,B,C,D,E} (Shortest Ai + Shortest ie ) A 3 A:3 D:7 B 5 C 3 1 2 5 D B:4 6 E C:9 Tian-Li Yu Algorithms 20 / 41

Dynamic Programming Shortest Path Shortest Path (contd.) Shortest AE = min i {A,B,C,D,E} (Shortest Ai + Shortest ie ) A 3 A:3 D:7 B 5 C 3 1 2 5 D B:4 6 E C:9 Tian-Li Yu Algorithms 21 / 41

Dynamic Programming Matrix-Chain Multiplication Matrix-Chain Multiplication Matrices: A : p q; B : q r Then C = A B is a p r matrix. C i,j = q k=1 A i,k B k,j Time complexity: pqr scalar multiplications The matrix-chain multiplication problem Given a chain < A 1, A 2,..., A n > of n matrices, which A i is of dimension p i 1 p i, parenthesize properly to minimize # of scalar multiplications. Tian-Li Yu Algorithms 22 / 41

Dynamic Programming Matrix-Chain Multiplication Matrix-Chain Multiplication (p q) (q r) (p r) (pqr) scalar multiplications A 1, A 2, A 3 : (10 100), (100 5), (5 50) (A 1 A 2 )A 3 (10 100 5) + (10 5 50) = 7500 A 1 (A 2 A 3 ) (100 1000 50) + (1000 50 50) = 75000 4 matrices: ((A 1 A 2 )A 3 )A 4 A 1 (A 2 A 3 )A 4 (A 1 A 2 )(A 3 A 4 ) A 1 (A 2 (A 3 A 4 )) Tian-Li Yu Algorithms 23 / 41

Dynamic Programming Matrix-Chain Multiplication The Minimal # of Multiplications m[i, j]: minimal # of multiplications to compute matrix A i,j = A i A i+1...a j, where 1 i j n. { 0, i = j m[i, j] = min k (m[i, k] + m[k + 1, j] + p i 1 p k p j ), i j Tian-Li Yu Algorithms 24 / 41

Dynamic Programming Matrix-Chain Multiplication Bottom-Up DP A 1 : 7 3 A 2 : 3 1 A 3 : 1 2 A 4 : 2 4 p 0 = 7 p 1 = 3 p 2 = 1 p 3 = 2 p 4 = 4 m[i, i] = 0 m[1, 2] = 0 + 0 + 7 3 1 = 21 m[2, 3] = 6 m[3, 4] = 8 m[1, 3] = 35 min {21 + 0 + 7 1 2, 0 + 6 + 7 3 2} m[2, 4] = 20 min {6 + 0 + 3 2 4, 0 + 8 + 3 1 4} Tian-Li Yu Algorithms 25 / 41

Dynamic Programming Matrix-Chain Multiplication Bottom-Up DP (contd.) A 1 : 7 3 A 2 : 3 1 A 3 : 1 2 A 4 : 2 4 p 0 = 7 p 1 = 3 p 2 = 1 p 3 = 2 p 4 = 4 m[1, 4] = min{ m[1, 1] + m[2, 4] + 7 3 4, m[1, 2] + m[3, 4] + 7 1 4, m[1, 3] + m[4, 4] + 7 2 4} = 57 Ans: (A 1 A 2 )(A 3 A 4 ) Tian-Li Yu Algorithms 26 / 41

Dynamic Programming Matrix-Chain Multiplication Table Filling A 1 : 7 3 A 2 : 3 1 A 3 : 1 2 A 4 : 2 4 p 0 = 7 p 1 = 3 p 2 = 1 p 3 = 2 p 4 = 4 57 35 20 21 6 8 0 0 0 0 A 1 A 2 A 3 A 4 Tian-Li Yu Algorithms 27 / 41

Top-down vs. Botton-up Top-Down Manner (Binary Search) Full list First half list Second half list First quarter Second quarter Tian-Li Yu Algorithms 28 / 41

Top-down vs. Botton-up Bottom-up Manner (Shortest Path) Four adjacent nodes Three adjacent nodes Two adjacent nodes Tian-Li Yu Algorithms 29 / 41

Analyzing Algorithms Efficiency Algorithm Efficiency Number of instructions executed Execution time What about on different machines? O, Ω, Θ notations Pronunciations: big-o, big-omega, big-theta Tian-Li Yu Algorithms 30 / 41

Analyzing Algorithms Efficiency Asymptotic Analysis Exact analysis is often difficult and tedious. Asymptotic analysis emphasizes the behavior of the algorithm when n tends to infinity. Asymptotic Upper bound Lower bound Tight bound T(n) asymptotic upper bond asymptotic lower bond n Tian-Li Yu Algorithms 31 / 41

Analyzing Algorithms Efficiency Big-O O(g(n)) = {f(n) c > 0, n 0 > 0 s.t. n n 0, 0 f(n) cg(n)} Asymptotic upper bound If f(n) is a member of the set of O(g(n)), we write f(n) = O(g(n)). cg(n) f(n) Examples 100n = O(n 2 ) n 100 = O(2 n ) 2n + 100 = O(n) n0 f(n) = O(g(n)) n Tian-Li Yu Algorithms 32 / 41

Analyzing Algorithms Efficiency Big-Omega Ω(g(n)) = {f(n) c > 0, n 0 > 0 s.t. n n 0, 0 cg(n) f(n)} Asymptotic lower bound If f(n) is a member of the set of Ω(g(n)), we write f(n) = Ω(g(n)). Examples 0.01n 2 = Ω(n) 2 n = Ω(n 100 ) 2n + 100 = Ω(n) n0 f(n) = Ω(g(n)) f(n) cg(n) n Tian-Li Yu Algorithms 33 / 41

Analyzing Algorithms Efficiency Big-Theta Θ(g(n)) = {f(n) c 1, c 2, n 0 > 0 s.t. n n 0, 0 c 1 g(n) f(n) c 2 g(n)} Asymptotic tight bound If f(n) is a member of the set of Θ(g(n)), we write f(n) = Θ(g(n)). Examples 0.01n 2 = Θ(n 2 ) 2n + 100 = Θ(n) n + log n = Θ(n) n0 f(n) = Θ(g(n)) cg(n) 2 f(n) cg(n) 1 n Theorem f(n) = Θ(g(n)) iff f(n) = O(g(n)) and f(n) = Ω(g(n)) Tian-Li Yu Algorithms 34 / 41

Analyzing Algorithms Efficiency Efficiency Analysis Best, worst, average cases Comparisons made for each pivot Initial list Elaine David Barbara Alfred 1st pivot 1 Elaine David Barbara Alfred 2nd pivot 3 2 David Elaine Barbara Alfred 3rd pivot 6 5 4 David Elaine Barbara Alfred 4th pivot 10 9 8 7 Barbara David Elaine Alfred Sorted list Alfred Barbara Elaine David Worst case for insertion sort Worst: (n 2 n)/2, best: (n 1), average: Θ(n 2 ) Tian-Li Yu Algorithms 35 / 41

Analyzing Algorithms Correctness Software Verification Cut Traveler s gold chain Cut Cut Cut Tian-Li Yu Algorithms 36 / 41

Analyzing Algorithms Correctness Assertion for While Precondition Initialize Loop invariant Test condition Body Condition true Condition false Loop invariant and terminate condition Precondition Loop invariant Termination condition Modifiy Tian-Li Yu Algorithms 37 / 41

Analyzing Algorithms Correctness Correct or Not? Count 0 Remainder Dividend repeat (Remainder Remainder Divisor Count Count + 1) until (Remainder < Divisor) Quotient Count Problematic Remainder > 0? Preconditions: Dividend > 0 Divisor > 0 Count = 0 Remainder = Dividend Loop invariants: Dividend > 0 Divisor > 0 Dividend = Count Divisor + Remainder Termination condition: Remainder < Divisor Tian-Li Yu Algorithms 38 / 41

Analyzing Algorithms Correctness Verification of Insertion Sort Loop invariant of the outer loop Each time the test for termination is performed, the names preceding the N-th entry form a sorted list Termination condition The value of N is greater than the length of the list. If the loop terminates, the list is sorted Tian-Li Yu Algorithms 39 / 41

Analyzing Algorithms Correctness Final Words for Software Verification In general, not easy. Need a formal PL with better properties. Tian-Li Yu Algorithms 40 / 41

Analyzing Algorithms Correctness License Page File Licensing Source/ author 7 George Plya ca 1973.,Author: Thane Plambeck from Palo Alto, California, Original ca 1973, scanned March 14, 2007 Source: http://en.wikipedia.org/wiki/file: George_P%C3%B3lya_ca_1973.jpg, Date:2013/05/14, Licensed under the terms of the cc-by-2.0. Tian-Li Yu Algorithms 41 / 41