26 views

Uploaded by nizar143

Algoritham

- abyazi-sani2016
- Net Optimization
- Designing Algorithms (3)
- Distrib Alg Attiya.ps
- Theory of algorithms
- 140338.pdf
- Summary an Introduction to Algorithm Design
- l5
- Sample Midterm
- Data Structures
- Fundamentals of Computation
- chapter1 Asymptotic analysis.pdf
- Introduction to Algorithms
- 2 - 4 - Quick-Union Improvements (1302)
- dsa ppt
- gudlecture11
- Evolución de la Complejidad
- Data Structure - CS 221- Course Outline
- Noise margin criteria for digital logic
- MCWASP 2015 Wwarr v9

You are on page 1of 26

Algorithm Analysis

Slides by Christopher M. Bourke Instructor: Berthe Y. Choueiry

Spring 2006

1/1

Introduction

Algorithm Analysis CSE235

How can we say that one algorithm performs better than another? Quantify the resources required to execute: Time Memory I/O circuits, power, etc Time is not merely CPU clock cycles, we want to study algorithms independent or implementations, platforms, and hardware. We need an objective point of reference. For that, we measure time by the number of operations as a function of an algorithms input size.

2/1

Input Size

Algorithm Analysis CSE235

For a given problem, we characterize the input size, n, appropriately: Sorting The number of items to be sorted Graphs The number of vertices and/or edges Numerical The number of bits needed to represent a number The choice of an input size greatly depends on the elementary operation; the most relevant or important operation of an algorithm. Comparisons Additions Multiplications

3/1

Orders of Growth

Algorithm Analysis CSE235

Small input sizes can usually be computed instantaneously, thus we are most interested in how an algorithm performs as n . Indeed, for small values of n, most such functions will be very similar in running time. Only for suciently large n do dierences in running time become apparent. As n the dierences become more and more stark.

4/1

Intractability

Algorithm Analysis CSE235

Problems that we can solve (today) only with exponential or super-exponential time algorithms are said to be (likely) intractable. That is, though they may be solved in a reasonable amount of time for small n, for large n, there is (likely) no hope of ecient execution. It may take millions or billions of years. Tractable problems are problems that have ecient (read: polynomial) algorithms to solve them. Polynomial order of magnitude usually means there exists a polynomial p(n) = nk for some constant k that always bounds the order of growth. More on asymptotics in the next slide set. Intractable problems may need to be solved using approximation or randomized algorithms.

5/1

Algorithm Analysis CSE235

Some algorithms perform dierently on various inputs of similar size. It is sometimes helpful to consider the Worst-Case, Best-Case, and Average-Case eciencies of algorithms. For example, say we want to search an array A of size n for a given value K . Worst-Case: K A then we must search every item (n comparisons) Best-Case: K is the rst item that we check, so only one comparison

6/1

Average-Case

Algorithm Analysis CSE235

Since any worth-while algorithm will be used quite extensively, the average running-time is arguably the best measure of its performance (if the worst-case is not frequently encountered). For searching an array, and assuming that p is the probability of a successful search, we have: Cavg (n) = = = p + 2p + . . . + ip + . . . np + n(1 p) n p (1 + 2 + . . . + i + . . . + n) + n(1 p) n p (n(n + 1) + n(1 p) n 2

7/1

If p = 1 (search succeeds), Cavg (n) = n+1 2 .5n. If p = 0 (search fails), Cavg (n) = n. A more intuitive interpretation is that the algorithm must examine, on average, half of all elements in A.

Average-Case

Algorithm Analysis CSE235

Average-Case analysis of algorithms is important in a practical sense. Often, Cavg and Cworst have the same order of magnitude and thus, from a theoretical point of view, are no dierent from each other. Practical implementations, however, require a real-world examination.

8/1

Algorithm Analysis CSE235

After developing pseudo-code for an algorithm, we wish to analyze its eciency as a function of the size of the input, n in terms of how many times the elementary operation is performed. Here is a general strategy:

1 2 3

Decide on a parameter(s) for the input, n. Identify the basic operation. Evaluate if the elementary operation depends only on n (otherwise evaluate best, worst, and average-case separately. Set up a summation corresponding to the number of elementary operations Simplify the equation to get as simple of a function f (n) as possible.

9/1

Analysis Examples

Example I

Algorithm Analysis CSE235

Algorithm (UniqueElements)

Input : Integer array A of size n Output : true if all elements a A are distinct for i = 1, . . . , n 1 do for j = i + 1, . . . , n do if ai = aj then return false end end

1 2 3 4 5 6

10 / 1

Analysis Example

Example I - Analysis

Algorithm Analysis CSE235

For this algorithm, what is The elementary operation? Input Size? Does the elementary operation depend only on n?

11 / 1

Analysis Example

Example I - Analysis

Algorithm Analysis CSE235

For this algorithm, what is The elementary operation? Input Size? Does the elementary operation depend only on n? The outer for-loop is run n 2 times. More formally, it contributes

n1 i=1

12 / 1

Analysis Example

Example I - Analysis

Algorithm Analysis CSE235

n j =i+1

13 / 1

Analysis Example

Example I - Analysis

Algorithm Analysis CSE235

n j =i+1

We observe that the elementary operation is executed once in each iteration, thus we have

n1 n

Cworst (n) =

i=1 j =i+1

1=

n(n 1) 2

14 / 1

Analysis Example

Example II

Algorithm Analysis CSE235

The parity of a bit string determines whether or not the number of 1s appearing in it is even or odd. It is used as a simple form of error correction over communication networks.

Algorithm (Parity)

Input : An integer n in binary (b[]) Output : 0 if the parity of n is even, 1 otherwise parity 0 while n > 0 do if b[0] = 1 then parity parity + 1 mod 2 right-shift(n) end

1 2 3 4 5 6

15 / 1

Analysis Example

Example II - Analysis

Algorithm Analysis CSE235

For this algorithm, what is The elementary operation? Input Size? Does the elementary operation depend only on n?

16 / 1

Analysis Example

Example II - Analysis

Algorithm Analysis CSE235

For this algorithm, what is The elementary operation? Input Size? Does the elementary operation depend only on n? The while-loop will be executed as many times as there are 1-bits in its binary representation. In the worst case, well have a bit string of all ones.

17 / 1

Analysis Example

Example II - Analysis

Algorithm Analysis CSE235

For this algorithm, what is The elementary operation? Input Size? Does the elementary operation depend only on n? The while-loop will be executed as many times as there are 1-bits in its binary representation. In the worst case, well have a bit string of all ones. The number of bits required to represent an integer n is log n so the running time is simply log n.

18 / 1

Analysis Example

Example III

Algorithm Analysis CSE235

Input : Integers n, m, p such that n > m > p Output : Some function f (n, m, p) x=1 for i = 0, . . . , 10 do for j = 0, . . . , n do for k = m/2, . . . , m do x=xp end end

1 2 3 4 5 6 7

8 end 9 return x

19 / 1

Analysis Example

Example III - Analysis

Algorithm Analysis CSE235

20 / 1

Analysis Example

Example III - Analysis

Algorithm Analysis CSE235

21 / 1

Analysis Example

Example III - Analysis

Algorithm Analysis CSE235

Outer Loop: executed 11 times. 2nd Loop: executed n + 1 times. Inner Loop: executed about

m 2

times.

22 / 1

Analysis Example

Example III - Analysis

Algorithm Analysis CSE235

Outer Loop: executed 11 times. 2nd Loop: executed n + 1 times. Inner Loop: executed about Thus we have C (n, m, p) = 11(n + 1)(m/2)

m 2

times.

23 / 1

Analysis Example

Example III - Analysis

Algorithm Analysis CSE235

Outer Loop: executed 11 times. 2nd Loop: executed n + 1 times. Inner Loop: executed about Thus we have C (n, m, p) = 11(n + 1)(m/2) But, do we really need to consider p?

m 2

times.

24 / 1

Summation Tools I

Algorithm Analysis CSE235

Section 3.2 (p229) has more summation rules. You can always use Maple to evaluate and simplify complex expressions (but know how to do them by hand!). To invoke maple, on cse you can use the command-line interface by typing maple. Under unix (gnome or KDE) or via any xwindows interface, you can use the graphical version via xmaple. Will be demonstrated during recitation.

25 / 1

Summation Tools II

Algorithm Analysis CSE235

Example

> simplify(sum(i, i=0..n)); 1 2 1 n + n 2 2 > Sum(Sum(j, j=i..n), i=0..n);

n n

i=0 j =1

26 / 1

- abyazi-sani2016Uploaded byAlex Trindade
- Net OptimizationUploaded byKei
- Designing Algorithms (3)Uploaded byLulu Tojeen
- Distrib Alg Attiya.psUploaded byManoj Kumar G
- Theory of algorithmsUploaded byiv727
- 140338.pdfUploaded bysubramanyam62
- Summary an Introduction to Algorithm DesignUploaded byFarahKhan
- l5Uploaded byJenny Jack
- Sample MidtermUploaded byKamySingh
- Data StructuresUploaded byDiman Ionut
- Fundamentals of ComputationUploaded byvanbuyten
- chapter1 Asymptotic analysis.pdfUploaded byzeeshan_frnd
- Introduction to AlgorithmsUploaded byXeeshan Ali
- 2 - 4 - Quick-Union Improvements (1302)Uploaded byeshwaar1
- dsa pptUploaded byVaibhav Gandhi
- gudlecture11Uploaded bysagargupta012
- Evolución de la ComplejidadUploaded byCarlos Panameño
- Data Structure - CS 221- Course OutlineUploaded byzachaboi
- Noise margin criteria for digital logicUploaded byTushar Gupta
- MCWASP 2015 Wwarr v9Uploaded bykb183627
- documentaryUploaded byromarking
- On Quantum Effects in a Theory of Biological EvolutionUploaded byBrian Hines
- R161213052017.pdfUploaded byNaga Ravi Krishna T
- algorithmandflowchart-100620081843-phpapp02Uploaded byFaizan Habib
- Approximation Algorithms and Hardness of the K-route Cut ProblemUploaded bydojomaster9846
- np completeUploaded bymahesh0291
- Data StructureUploaded byakram
- Problems in Task Scheduling in Multiprocessor SystemUploaded byEditor IJTSRD
- Belotti-Et-Al-12-TR.pdfUploaded byJorge Ignacio Cisneros Saldana
- An Analysis of Lamport Clocks Using CulmUploaded byAuthor

- Beep Codes SGUploaded byhairul28
- Outlook 2003 - JournalUploaded bynizar143
- Information System DataUploaded bynizar143
- Linux AdministrationUploaded bynizar143
- J-87-11 Paper - IIIUploaded byabinayamalathy
- LJ 3050 3052 3055_TroubleshootingUploaded byfd106
- Engg Master Eligible Qualifications 2010-11Uploaded bynizar143

- 4-Application-Worksheet-2f4u132.docUploaded byahmed mahamed
- Perceptions of Luxury Products as Status SymbolUploaded byDr. Firoze Khan
- Distributed Control System dcsUploaded bysendutdut
- The Planetary Quarantine Program Origins and Achievements, 1956 - 1973Uploaded byBob Andrepont
- Africanus Journal Vol 8 No 1eUploaded byGordon-Conwell Theological Seminary
- Journal of Structural Engineering Volume Issue 2016 [Doi 10.1061%2F%28ASCE%29ST.1943-541X.0001704] Kanvinde, Amit -- Predicting Fracture in Civil Engineering Steel Structures- State of the ArtUploaded byGuillermo Ramirez
- Theme ActivitiesUploaded bypriyaspv
- PhastUploaded byAhmad Muzammil
- Osteomyelitis Oral SurgeryUploaded bySmile Guy
- 3 3 4 aenergycodes docxUploaded byapi-355856202
- 5. of the Abrogation of the Old Covenant.Uploaded byitisme_angela
- Nagatomo_logic of Diamond-SūtraUploaded byMigellango
- A Comparison Between the Firefly Algorithm and Particle Swarm OptimizationUploaded bysufikasih
- en-feasibility-study-biogas-2007Uploaded bykhladun
- Manager as MotivatorUploaded byRahul Pratap Singh Kaurav
- Scientific Abbreviations and SymbolsUploaded byPrasad Mande
- Catalogue LED 2 MCSCUploaded bySantiago Hernández Salerni
- Unmanned Aircraft PilotUploaded byedwardsilva
- The Importance of Frames of Reference.pdfUploaded byubunturider
- augustan poetry.pdfUploaded byমৃন্ময় ঘোষ
- Handling of Absolute Photometry Data in EULUMDAT FilesUploaded bymoezzat13
- SML-Summary OTRS Customer and Agent GuideUploaded byVin
- Megadrive-lci en Rev b LowresUploaded byVladimir Danilov
- DADM-Correlation and RegressionUploaded byDipesh Karki
- Clive BarkerUploaded byaakashtank
- emersonanddogenUploaded byapi-254406702
- TE Catalogue 2014Uploaded byRaheel Walani
- "Prayers to Oshun".pdfUploaded byFelipe Aranda
- ANZ Royal Commission Document 2019Uploaded byDavid Johnston
- Project Management Bo Tonnquist ENGUploaded byIgor Brodin