Examples: data compression, path-finding, game-playing, scheduling, bin packing

Similar documents
Sorting Algorithms. Algorithms Kyuseok Shim SoEECS, SNU.

CS 270 Algorithms. Oliver Kullmann. Growth of Functions. Divide-and- Conquer Min-Max- Problem. Tutorial. Reading from CLRS for week 2

Analysis of Algorithms. Introduction. Contents

CSI 5163 (95.573) ALGORITHM ANALYSIS AND DESIGN

This Lecture. Divide and Conquer. Merge Sort: Algorithm. Merge Sort Algorithm. MergeSort (Example) - 1. MergeSort (Example) - 2

CS161 Design and Analysis of Algorithms. Administrative

Analysis of Algorithms -Quicksort-

Design and Analysis of ALGORITHM (Topic 2)

Model of Computation and Runtime Analysis

Model of Computation and Runtime Analysis

Classification of problem & problem solving strategies. classification of time complexities (linear, logarithmic etc)

Data Structures and Algorithm. Xiaoqing Zheng

Advanced Course of Algorithm Design and Analysis

Recursive Algorithms. Recurrences. Recursive Algorithms Analysis

A recurrence equation is just a recursive function definition. It defines a function at one input in terms of its value on smaller inputs.

Divide & Conquer. Divide-and-conquer algorithms. Conventional product of polynomials. Conventional product of polynomials.

CS583 Lecture 02. Jana Kosecka. some materials here are based on E. Demaine, D. Luebke slides

CS 332: Algorithms. Linear-Time Sorting. Order statistics. Slide credit: David Luebke (Virginia)

Chapter 22 Developing Efficient Algorithms

4.3 Growth Rates of Solutions to Recurrences

CIS 121 Data Structures and Algorithms with Java Spring Code Snippets and Recurrences Monday, February 4/Tuesday, February 5

Algorithm Analysis. Chapter 3

CS / MCS 401 Homework 3 grader solutions

Matriculation number: You have 90 minutes to complete the exam of InformatikIIb. The following rules apply:

) n. ALG 1.3 Deterministic Selection and Sorting: Problem P size n. Examples: 1st lecture's mult M(n) = 3 M ( È

CSE 4095/5095 Topics in Big Data Analytics Spring 2017; Homework 1 Solutions

Design and Analysis of Algorithms

SECTION 1.5 : SUMMATION NOTATION + WORK WITH SEQUENCES

Algorithm Analysis. Algorithms that are equally correct can vary in their utilization of computational resources

Divide and Conquer. 1 Overview. 2 Multiplying Bit Strings. COMPSCI 330: Design and Analysis of Algorithms 1/19/2016 and 1/21/2016

OPTIMAL ALGORITHMS -- SUPPLEMENTAL NOTES

Lecture 3: Asymptotic Analysis + Recurrences

CS 332: Algorithms. Quicksort

Hand Out: Analysis of Algorithms. September 8, Bud Mishra. In general, there can be several algorithms to solve a problem; and one is faced

Infinite Sequences and Series

Data Structures and Algorithms

Recurrence Relations

Average-Case Analysis of QuickSort

Algorithms. Elementary Sorting. Dong Kyue Kim Hanyang University

Design and Analysis of Algorithms

Sums, products and sequences

Merge and Quick Sort

Data Structures Lecture 9

Math 475, Problem Set #12: Answers

Recursive Algorithm for Generating Partitions of an Integer. 1 Preliminary

CSE 202 Homework 1 Matthias Springer, A Yes, there does always exist a perfect matching without a strong instability.

Department of Informatics Prof. Dr. Michael Böhlen Binzmühlestrasse Zurich Phone:

( ) = p and P( i = b) = q.

CS161: Algorithm Design and Analysis Handout #10 Stanford University Wednesday, 10 February 2016

4x 2. (n+1) x 3 n+1. = lim. 4x 2 n+1 n3 n. n 4x 2 = lim = 3

CSI 2101 Discrete Structures Winter Homework Assignment #4 (100 points, weight 5%) Due: Thursday, April 5, at 1:00pm (in lecture)

Algorithms and Data Structures Lecture IV

Introduction to Computational Molecular Biology. Gibbs Sampling

Fundamental Algorithms

An Introduction to Randomized Algorithms

Ma 530 Infinite Series I

Sequences, Mathematical Induction, and Recursion. CSE 2353 Discrete Computational Structures Spring 2018

CSE 5311 Notes 1: Mathematical Preliminaries

Ch3. Asymptotic Notation

Skip Lists. Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 S 3 S S 1

ITEC 360 Data Structures and Analysis of Algorithms Spring for n 1

1 Hash tables. 1.1 Implementation

CS:3330 (Prof. Pemmaraju ): Assignment #1 Solutions. (b) For n = 3, we will have 3 men and 3 women with preferences as follows: m 1 : w 3 > w 1 > w 2

COMP26120: Introducing Complexity Analysis (2018/19) Lucas Cordeiro

3.2 Properties of Division 3.3 Zeros of Polynomials 3.4 Complex and Rational Zeros of Polynomials

Once we have a sequence of numbers, the next thing to do is to sum them up. Given a sequence (a n ) n=1

Math 113 Exam 3 Practice

CHAPTER 5. Theory and Solution Using Matrix Techniques

Chapter 6. Advanced Counting Techniques

Introduction to Algorithms 6.046J/18.401J LECTURE 3 Divide and conquer Binary search Powering a number Fibonacci numbers Matrix multiplication

Test One (Answer Key)

CHAPTER 10 INFINITE SEQUENCES AND SERIES

Bertrand s Postulate

Divide and Conquer. 1 Overview. 2 Insertion Sort. COMPSCI 330: Design and Analysis of Algorithms 1/19/2016 and 1/21/2016

Zeros of Polynomials

+ au n+1 + bu n = 0.)

THE SOLUTION OF NONLINEAR EQUATIONS f( x ) = 0.

The Growth of Functions. Theoretical Supplement

62. Power series Definition 16. (Power series) Given a sequence {c n }, the series. c n x n = c 0 + c 1 x + c 2 x 2 + c 3 x 3 +

Math 155 (Lecture 3)

Discrete Mathematics for CS Spring 2007 Luca Trevisan Lecture 22

2. ALGORITHM ANALYSIS

2.4 Sequences, Sequences of Sets

Randomized Algorithms I, Spring 2018, Department of Computer Science, University of Helsinki Homework 1: Solutions (Discussed January 25, 2018)

Trial division, Pollard s p 1, Pollard s ρ, and Fermat s method. Christopher Koch 1. April 8, 2014

10.2 Infinite Series Contemporary Calculus 1

Quantum Computing Lecture 7. Quantum Factoring

ECE-S352 Introduction to Digital Signal Processing Lecture 3A Direct Solution of Difference Equations

Math 25 Solutions to practice problems

FIR Filters. Lecture #7 Chapter 5. BME 310 Biomedical Computing - J.Schesser

In algebra one spends much time finding common denominators and thus simplifying rational expressions. For example:

NUMERICAL METHODS FOR SOLVING EQUATIONS

COMP285 Midterm Exam Department of Mathematics

2.4 - Sequences and Series

MATH 2300 review problems for Exam 2

CSE Introduction to Parallel Processing. Chapter 3. Parallel Algorithm Complexity

CS322: Network Analysis. Problem Set 2 - Fall 2009

Lecture 11: Pseudorandom functions

Lecture 9: Hierarchy Theorems

Northwest High School s Algebra 2/Honors Algebra 2 Summer Review Packet

Transcription:

Algorithms - Basic Cocepts Algorithms so what is a algorithm, ayway? The dictioary defiitio: A algorithm is a well-defied computatioal procedure that takes iput ad produces output. This class will deal with how to desig algorithms ad uderstad their complexity i terms of rutime, space, ad correctess. Examples: data compressio, path-fidig, game-playig, schedulig, bi packig Example algorithm: Isertio Sort Isertio sort is how people ofte sort a had of cards. Startig from the left, go towards the right ad make sure that everythig o the left had side is correctly sorted. For example: Origial: 5 3 8 10 7 Pass 1: 3 5 8 10 7 Pass : 3 5 8 10 7 Pass 3: 3 5 8 10 7 Pass 4: 3 5 8 10 7 Pass 5: 3 5 7 8 10 Here is the pseudocode for the Isertio sort algorithm. Typically we will use pseudocode ad ot actual implemetatio code. Isertio-Sort(A) ; Assume our arrays start at idex 1 ad ot at 0 For j to legth(a) Do keya[j] i j-1 while i>0 ad A[i] > key do A[i+1] A[i] i i-1 A[i+1]key Ru through algorithm o our iput, 5 3 8 10 7 J= A=[5 3 8 10 7] Key=3 I=1 A[1]>3 so A[]=A[1] A[1]=3

J=3 J=4 A=[3 5 8 10 7] Key=8 I= A[] < 8 A[3]=8 A=[3 5 8 10 7] Key= I=3 A[3]> so A[4]=A[3] A[]> so A[3]=A[] A[1]> so A[]=A[1] A[1]= J=5 A=[ 3 5 8 10 7] The fial two passes are left as a exercise for you to verify. It looks like this algorithm works to sort the iput. But how do we aalyze it? There are also certaily may other ways to sort the data (ad we ll look at lots of them!) What makes oe algorithm better tha aother? Let s make a assumptio that we will use throughout most of this class. RAM model. This is a uiprocessor, radom access machie with o parallelism (PRAM). Ru time ad space will geerally deped o the size of the iput. Iput Size : Defiitio of depeds o the problem. It may be bits, bytes, but is usually the umber of items i the iput. For sortig, =# of items to sort. For a graph, could be the umber of odes or the umber of liks. Ruig time: defied as the umber of steps that are executed i the program. Let s cout up the rutime for this algorithm. This ca be a little tricky sice the loop does t always ru the same umber of times, but varies depedig o the iput. Let t j = the # of times the ier while loop is examied for the curret value of j. This icludes checkig for the exit coditio of the loop.

Code Cost Times For j to legth(a) C1 Do keya[j] C -1 i j-1 C3-1 j while i>0 ad A[i] > key C4 t j do j j A[i+1] A[i] C5 ( t 1) i i-1 C6 ( t 1) A[i+1]key C7-1 The statemet for the outer j loop will ru times (oce for each elemet, oce to check the exit coditio). The ier statemets ru -1 times. Sice we ve defied t j as the umber of times the ier loop is executed for a value of j, to get the total times it is ru we just add up the sum or all values of j. Our total rutime is the: C1()+C(-1)+C3(-1)+C4( t j )+C5( t j 1)+C6( j j j j t j j 1)+C7(-1) Doig a little math: C1()+C()-C+C3()-C3+C4( t j )+(C5+C6)( t j 1)+C7()-C7 j j (C1+C+C3+C7)() (C+C3+C7) + (C4)( t j j ) + (C5+C6)( ( 1) j j t ) What s the best case? If the iput is already sorted. I this case, j, t j 1. That is the ier loop oly gets executed oce sice we ll immediately fid the right spot for the last item. Rutime for this case: (C1+C+C3+C7)() (C+C3+C7) + (C4)( 1) + (C5+C6)(0) (C1+C+C3+C7)() (C+C3+C7) + (C4)(-1) (C1+C+C3+C4+C7)()-(C+C3+C7+C4) = (CONSTANT1) CONSTANT j

This is a liear fuctio of ; y=ax+b. The rutime will icrease liearly as the size of the iput icreases. rutime What s the worst case? Lets say that the iput cospires to provide the worst rutime. This would happe if the array was i reverse order, because the we have to compare with every other umber i the ier loop. 5 4 3 1 j= t j = compares j=3 t j = 3 compares j=4 t j = 4 compares So we kow that t j = j t j j = j j t j j 1 = 1 1 = j - 1 j j 1 = 1 - (-1) = = ( 1) Plug these ito our formula: (C1+C+C3+C7)() (C+C3+C7) + (C4)( (C1+C+C3+C7)() (C+C3+C7) + (C4)( t j j ) + (C5+C6)( t j j 1) 1 1) + (C5+C6)( ( 1) ) Of the form (Costat1)( ) + (Costat)() + Costat3

iput. This is a quadratic; a +b+c; the rutime will icrease based o the square of the rutime Average case is also quadratic. How much space does this take? Sort is doe i place, so just a few variables. This requires costat space. Fuctio Growth -otatio : Gives asymptotically tight boud o a fuctios growth Defiitio: ( ) ( ):,, : ( ) ( ) ( ) g f c c 0 c g f c g for all 1 0 1 0 c g() f() c 1 g() 0 F()=3 is () sice we ca fid g()=, c1=1, c=4. Isertio sort is ot ( ) sice it is liear i the best case. However, average ad worst case isertio sort is ( ).

O-otatio: Big-O otatio. This gives a asymptotic upper boud. Defiitio: O g( ) f ( ): c, : 0 f ( ) c g( ) for all 1 0 1 0 c1g() f() 0 Isertio sort is O( ). Tight upper boud i the worst case, loose i the best case. Looser: O( 3 ), O( ). Use little-o otatio if you kow that it is a loose boud. o( 3 ) if f=. - Omega otatio. Omega provides a asymptotic lower boud. Defiitio: g( ) f ( ): c, : 0 c g( ) f ( ) for all 0 1 0 1 f() c1g() 0 (1) Costat time, this is a trivial lower boud for most cases. For isertio sort, () : liear time is the best possible

Ruig Time Compariso: =10 =100 =1000 =10000 lg() 5 7 9 10 100 1000 10000 (lg()) 0 500 7000 90000 100 10000 1,000,000 100,000,000 104 1.3x10 30 huge very huge expoetial quadratic liear log For compariso: estimate of the umber of atoms i the uiverse is about 10 80 (i.e. about 70 ). Oe of the tools we will use for aalysis ad solutios is Divide ad Coquer Recursive approach to solve problems. The idea is to break the problem up ito subproblems with the same solutio, but smaller size. Solve these problems recursively, the combie the sub-solutios to solve the origial problem. Divide ito sub-problems Coquer Sub-problems recursively Combie Sub-problem solutios to origial problem Let s look at Merge Sort: Divide elemets ito two subsequeces to be sorted of size / Coquer sort subsequeces recursively with merge sort Combie merge sorted subsequeces ito big sorted aswer Need termiatio criteria for recursio Quit if sequece to sort is legth 1

Pseudocode: Merge_Sort(A,p,r) If p<r the p r q Merge_Sort(A,p,q) Merge_Sort(A,q+1,r) Merge(A,p,q,r) ; Merges A[p..q] with A[q+q..r] ito A[p..q] Call with Merge_Sort(A,1,legth(A)) q gets the media, merge left, right A[1 5 10] becomes Merge_Sort(A,1,5), Merge_Sort(A,6,10) How do we merge? A1: 1 5 8 30 31 50 A: 10 11 1 15 Combied: 1 5 8 10 11 1 15 30 31 50 Easy to do; just start at the frot of each array, compare the poiters, ad put the smallest ito a ew array ad the icremet the poiter that was the smallest. We ca t do this merge i-place though (usig the origial array storage locatio oly) so we eed to make a copy of the merged array ad the copy it back to the origial array. If is the umber of elemets to merge, the it takes () time to merge. Example: A= 1 5 3 7 11 8 4 MS(A,1,8) A=[1, 5, 3, 7, 11, 8,, 4] MS(A,1,4) MS(A,5,8) MS(A,1,) MS(A,3,4) MS(A,5,6) MS(A,7,8) MS(A,1,1) MS(A,,) MS(A,3,3) MS(A,4,4) MS(A,5,5) MS(A,6,6) MS(A,7,7) MS(A,8,8) Merge(A[1..1], A[..]) Merge(A[3..4], A[4..4]) Merge(A[5..5], A[6..6]) Merge(A[7..7], A[8..8]) A = [1, 5, 3, 7, 11, 8,, 4] Merge(A[1..], A[3..4]) A = [1, 5, 3, 7, 11, 8,, 4] A = [1, 5, 3, 7, 8, 11,, 4] Merge(A[5..6], A[7..8]) A = [1, 5, 3, 7, 8, 11,, 4] A = [1, 3, 5, 7, 11, 8,, 4] A = [1, 3, 5, 7,, 4, 8, 11] Merge(A[1..4], A[5..8]) A=[1,, 3, 4, 5, 7, 8, 11]

How much work is doe? It s the cost to split + the cost to merge. Let s defie T() to be the rutime for a problem of size(). This is called a recurrece relatio, sice it is recursively defied. ( 1), 1 Recurrece: T( ) T ( ), 1 The T(/) comes from the divide, ad the () comes from the merge. Later we will show that Merge Sort is (lg) i rutime by illustratig a umber of techiques to solve these recurrece relatios. Math Review x - floor of x, roud dow x - ceilig of x, roud up lg(x) = log x l x = log e x a b log b a log c (ab) = log c (a) + log c (b) logc a logb a logc b log b (1/a) = -log b a log b a = 1 / (log a b) log b log b a a iterated log : log(log ) = log () This grows very slowly! Almost the same as costat time. 1 k 1 3 4... ( 1) ( ) k 1 1 1 1 1 1... l( ) O( 1) (l ) Harmoic series k 1 k 3 ak ak 1 a a0 k 0 Telescopig There are some other summatios we will use, but we ll discuss them as we get to that part of the course.

Mote Carlo Methods Throughout this class we ll focus primarily o aalytic methods to characterize algorithms. However, there is aother class of statistical/probabilistic methods that are commoly referred to as Mote Carlo algorithms. From http://mathworld.wolfram.com/motecarlomethod.html : Ay method which solves a problem by geeratig suitable radom umbers ad observig that fractio of the umbers obeyig some property or properties. The method is useful for obtaiig umerical solutios to problems which are too complicated to solve aalytically. It was amed by S. Ulam Eric Weisstei's World of Biography, who i 1946 became the first mathematicia to digify this approach with a ame, i hoor of a relative havig a propesity to gamble (Hoffma 1998, p. 39). Numerical methods that are kow as Mote Carlo methods ca be loosely described as statistical simulatio methods, where statistical simulatio is defied i quite geeral terms to be ay method that utilizes sequeces of radom umbers to perform the simulatio. Mote Carlo methods have bee used for ceturies, but oly i the past several decades has the techique gaied the status of a full-fledged umerical method capable of addressig the most complex applicatios. The ame Mote Carlo was coied durig the Mahatta Project of World War II, because of the similarity of statistical simulatio to games of chace, ad because the capital of Moaco was a ceter for gamblig ad similar pursuits. Mote Carlo is ow used routiely i may diverse fields, but has gaied some of the greatest popularity i computer sciece ad physics. For example, cosider a complex physical system (solar system, uclear reactio, quatum chromodyamics, traffic patters, etc.) that has bee modeled through a series of pdf s (probability desity fuctios). The behavior ad iteractio of the model with the iput is ofte too complex to determie determiistically. Istead, the system is simulated by ruig may trials usig radom iputs selected from a proper rage. The resultig output helps us lear about the outputs of the system ad the complexity of the system. As aother example, the merge sort example we previously covered could be aalyzed usig a mote carlo method by radomly selectig iputs, ruig the algorithm, ad measurig the rutime (or the umber of comparisos). By averagig out the average rutime, we could determie that merge sort is a (lg) algorithm.