14 views

Uploaded by Eko Pradipta Mustofa Toha

- Javascript
- The MagPi Issue 14 En
- HN DAA 15CS43 LectureNotes 1
- VBA
- MS EXCEL
- Commodore Pet 2001 Quick Reference
- MATLAB LOOPS
- Style Rules Checklist
- Wk 1b Flowchart Selection
- All In One
- PD ControlFlow Repetition IA UB
- Ee6301 Digital Logic Circuits
- Problem Set 2 Sol
- P12 - Iterations
- Automatic Transformation of Series Expressions Into Loops
- Programming Group Assignment W8-14 May2015
- h
- Essentials_C_v1.pdf
- PL_SQL.docx
- c quick guide.docx

You are on page 1of 35

analysis of algorithms, we start with the algorithm of

insertion sort.

¾ We describe algorithms as programs written in a pseudocode

that is similar in many aspects to C or Java.

¾ In pseudocode, we employ whatever expressive method is

most clear and concise to specify a given algorithm. Issues of

implementation are often ignored in order to convey the

essence of the algorithm.

¾ Insertion sort is an efficient algorithm for sorting a small

number of elements. The input numbers are sorted in place:

the numbers are rearranged within the input array, with at

most a constant number of them stored outside the array at

any time.

2.1

Pseudocode for Insertion Sort

¾ INSERTION-SORT(A)

1 FOR j := 2 TO length[A]

2 DO key := A[j];

3 //Insert A[j] into the sorted sequence A[1 .. j-1]

4 i := j – 1;

5 WHILE i > 0 and A[i] > key

6 DO A[i+1] := A[i];

7 i := i – 1

END

8 A[i+1] := key

END

2.2

Loop Invariants and

the Correctness of Algorithms

¾ In the insertion sort algorithm, the elements

A[1..j-1] are the elements originally in positions 1

through j-1, but now in sorted order. We state

these properties of A[1..j-1] formally as a loop

invariant:

lines 1 - 8, the subarray A[1..j-1] consists of the

elements originally in A[1..j-1] but in sorted order.

an algorithm is correct.

2.3

¾ We must show three things about a loop invariant:

1. Initialization: It is true prior to the first iteration

of the loop.

2. Maintenance: If it is true before an iteration of the

loop, it remains true before the next iteration.

3. Termination: When the loop terminates, the invariant

gives us a useful property that helps show that the

algorithm is correct.

2.4

Showing the correctness of insertion sort by using a

loop invariant

¾ Initialization: We have to show that the loop invariant holds before

the first loop iteration, when j = 2. The subarray A[1..j-1],

therefore, consists of just the single element A[1], which is in fact

the original element in A[1]. Moreover, this subarray is sorted,

trivially.

¾ Maintenance: We have to show that each iteration maintains the

loop invariant. The body of the FOR loop works by moving A[j-1],

A[j-2], and so on by one position to the right until the proper

position for A[j] is found (lines 4-7), at which point the value of

A[j] is inserted (line 8).

¾ Termination: We examine what happens when the loop terminates.

The FOR loop ends when j exceeds n (the length of A), i.e., when j =

n + 1. Substituting n+1 for j in the statement of loop invariant, we

have that the subarray A[1..n] consists of the elements originally in

A[1..n], but in sorted order. But the array A[1..n] is the entire array.

Hence, the entire array is sorted, which means that the algorithm

is correct.

2.5

WHILE Loops

constructs, such as while loops, for loops, and

repeat-until loops.

¾ We will focus on while loops. The other loops can be

written in terms of while loops, so no expressive

programming power is lost by restricting attention

to while loops alone.

¾ The template for a WHILE loop is as follows:

WHILE E

DO S

END

2.6

¾ The predicate E is a condition or boolean

expression. It is called the guard of the loop.

¾ If the loop is executed in a state in which E is

false, then the loop terminates immediately

without changing the state.

¾ If the loop is executed in a state s in which E is

true, then the loop executes the sequence of

statements S to reach a state s' from which the

loop is again executed. Execution of a loop thus

passes through a sequence of intermediate states.

¾ In general it is possible that a loop never

terminates execution, because the guard is true in

every state that is reached. It will therefore be

necessary to prove that the loops always

terminate when they are required to.

2.7

false

E

true

2.8

Example

¾ The "Russian Multiplication" algorithm multiplies two numbers

a and b by producing two columns of numbers, headed by a

and b.

¾ The first column is produced by repeated halving with

rounding down (integer division), and the second column is

produced by repeated doubling.

¾ All rows in which the first entry is even are removed.

¾ The sum of the second column then gives the product of a

and b.

2.9

Russian multiplication of 57 and 43

57 43

28 86

removed

14 172

7 344

3 688

1 1376

2451

2.10

¾ The algorithm can be described with a loop, which keeps track of

the running total as execution proceeds. When the loop finishes,

then the running sum total will be the product of the two

variables a and b.

x := a;

y := b;

total := 0;

WHILE x > 0

DO IF x mod 2 = 1 THEN total := total + y END;

x := x/2;

y := y*2

END

2.11

¾ The loop in the previous slide is supposed to implement the Russian

Multiplication algorithm, but no argument has yet been given concerning

its correctness.

¾ Correctness must be with respect to a requirement, expressed as a

postcondition P. For the Russian Multiplication loop, the postcondition

is that total = a*b.

¾ Since a loop execution passes through many intermediate states, the

relationship between the initial state from which it is executed, and

the final state it reaches, must take the intermediate states into

account.

¾ The relationship between one intermediate state and the next is

described by the body S of the loop.

¾ The key link between successive states is captured by a loop invariant.

2.12

¾ A loop invariant is a condition (predicate) which holds of all

of the states that the execution of the loop passes through,

before and after each execution of the loop body S. It

therefore provides a link between the initial and final

states, connected through all of the intermediate states.

2.13

I

I

I ∧ ¬E

E

I∧E

S

P

I

2.14

Example

¾ Consider again the Russian Multiplication.

¾ It requires that total = a*b in the final state.

¾ In the initial state: x = a & y = b & total = 0.

¾ Proving that the algorithm is correct requires the

identification of some predicate I on some or all of the

variables, which is true for the initial state, is preserved by

the body S of the loop, and which implies the postcondition

total = a*b when the guard x > 0 is false.

¾ One suitable invariant is the following:

total + x * y = a * b

¾ At any particular stage, the invariant describes what has

been achieved so far, as encapsulated in the value total.

2.15

¾ The proof is as follows:

9 Initialization: Initially, total = 0 and x*y is indeed a*b. So the

invariant is true when the loop begins.

9 Maintenance: On a single pass through the loop, there are two

possibilities:

1. If x is even, then total remains as it was, x is halved, and y is

doubled. In this case (x/2)*(y*2) is the same as x*y, so the

invariant remains true.

2. If x is odd, then y is added to total, x is then halved with

rounding down, and y is doubled. In this case, (total + y) +

x/2*(y*2) = total + x*y, so the invariant again remains true.

Thus the invariant is preserved by every iteration of the loop.

9 Termination: Finally, on termination total + x*y = a*b and also

the negation of the guard holds: x = 0. It follows that total =

a*b, which is the required postcondition.

2.16

An execution of the Russian Multiplication loop

x y total INVARIANT

57 43 0 0 + 57*43 = 57*43

28 86 43 43 + 28*86 = 57*43

2.17

Finding an Invariant

I ∧ ¬E ⇒ P. This says that the invariant, together

with the negation of the guard, must imply the

postcondition.

¾ This requirement can be used to guide the

development of a loop which is required to

establish a particular postcondition P.

2.18

¾ One technique is to obtain I by weakening P, so I

holds for more states than P does. The loop should

then terminate when the particular instance of I

corresponds to the situation where P also holds:

this will influence the choice of guard.

2.19

Constructing I by Weakening P

¾ The postcondition P can be weakened by replacing a

constant N by a variable i, so that P = I when i is equal

to N.

¾ In this case, the guard of the loop should be i ≠ N.

¾ When the loop terminates, the guard is false, and we will

have I ∧ i=N, which indeed implies P.

2.20

Example

¾ We want to develop a loop to sum the elements of an array

aa [1..N].

¾ The postcondition P is

sum = Σj.(j ∈ 1..N | aa[j]).

¾ Replacing the constant N by a variable i results in the invariant

I:

sum = Σj.(j ∈ 1..i | aa[j])

The type of i is natural number.

¾ The guard E of the loop is:

i ≠ N.

¾ It follows by construction that I ∧ ¬E ⇒ P.

2.21

¾ The suitable loop is:

sum := 0;

i := 0;

WHILE i ≠ N

DO i := i + 1; sum := sum + aa[i]

END

2.22

Exercise

¾ The postcondition in the previous example can also be

weakened to obtain the following invariant I:

sum = Σj.(j ∈ i..N | aa(j))

¾ Develop a complete loop with this invariant.

2.23

Constructing I by Weakening P

(cont.)

Deleting a conjunct

¾ If a postcondition consists of a number of conjuncts, then

it can be weakened by deleting one (or several) of its

conjuncts.

¾ The resulting predicate will be true in more states than the

postcondition, and might be suitable as a loop invariant.

¾ The loop guard in this case will be the negation of the

deleted conjunct. So the negation of the guard and the

remaining conjuncts together imply the postcondition.

2.24

Example

¾ The integer square root r of a natural number n is the

greatest integer whose square is no more than n. So we

have

P = r2 <= n & n < (r + 1)2

¾ Deleting the second conjunct leaves r2 <= n. This will do

as an invariant of a loop to achieve the postcondition P.

It is true when r = 0, so an initial state for the loop can

easily be established. The loop guard will be the

negation of the deleted conjunct: E = (r + 1)2 <= n.

¾ The loop body simply increments r .

2.25

¾ Thus the complete loop to compute integer square root

is:

r := 0;

WHILE (r + 1)2 <= n

DO r := r + 1

END

2.26

Random-Access Machine (RAM)

¾ Analyzing an algorithm means predicting the

resources (computing time, memory, communication

bandwidth, etc) that the algorithm requires.

¾ Most often it is computing time that we want to

measure.

¾ Before we can analyze an algorithm, we must have a

model of the implementation technology.

¾ We will use a generic one-processor RAM (random-

access machine) model of computation and

implement our algorithms as computer programs on

that machine.

2.27

The RAM model:

¾ contains instructions commonly found in real

computers:

arithmetic (add, subtract, multiply, divide,

remainder, floor, ceiling),

data movement (load, store, copy), and

control (conditional and unconditional branch,

subroutine call and return).

Each such instruction takes a constant amount of

time.

¾ supports the data types: integer and floating point.

2.28

Running Time

primitive operations or steps executed.

¾ Running time depends on

input size (e.g. 8 elements vs. 8000)

input itself (e.g. already sorted or not)

¾ The mathematical expression for the running time

of INSERTION-SORT can be determined as

follows.

tj be the number of times the while loop test in

line 5 is executed for that value of j.

2.29

INSERTION-SORT (A) cost times

1 FOR j := 2 TO length[A] c1 n

4 i := j – 1 c4 n-1

∑ t

j=2 j

6 DO A[i+1] := A[i]; c6

∑ (t −1)

n

j =2 j

7 i := i – 1 END c7

∑ (t −1)

n

j =2 j

8 A[i+1] := key END c8 n-1

2.30

¾ The running time of INSERTION-SORT is:

n n

T(n) = c1n + c2 (n −1) + c4 (n −1) + c5 ∑t j + c6 ∑(t j −1)

j =2 j =2

n

+ c7 ∑(t j −1) + c8 (n −1).

j =2

2.31

¾ For inputs of a given size, an algorithm's running time may

depend on which input of that size is given. For example, in

INSERTION-SORT, the best case occurs if the array is

already sorted. In this case, tj = 1 for j = 2,3, ...,n.

¾ The best-case running time is

T(n) = c1n+c2(n−1)+c4(n−1)+c5(n−1)+c8(n−1)

= (c1 +c2 +c4 +c5 +c8)n−(c2 +c4 +c5 +c8).

2.32

¾ The worst case occurs if the array is in reverse sorted order.

In this case, each element A[j] must be compared with each

element in the entire sorted subarray A[1 .. j-1], and so tj = j

for j = 2, 3, ..., n.

n n ( n + 1)

∑ j = −1

j=2 2

n n ( n − 1)

∑ ( j − 1) =

j=2 2

¾ The worst case running time is a quadratic function of n :

c5 c6 c7 2 c5 c6 c7

T(n) = ( + + )n + (c1 + c2 + c4 + − − + c8 )n

2 2 2 2 2 2

− (c2 + c4 + c5 + c8 ).

2.33

Kinds of Algorithm Analysis

T(n) = max time on any input of size n.

¾ (sometimes) -- Average case

T(n) = average time over all inputs of size n

(assumes statistical distribution of inputs)

¾ (never) -- Best case

Useless, because we can cheat with slow algorithm

that works fast on some input.

2.34

¾ We usually concentrate on finding the worst-case

running time. Why?

Three reasons:

1. The worst-case running time is an upper bound on

the running time for any input. Knowing it gives us

a guarantee that the algorithm will never take any

longer.

2. For some algorithms, the worst-case occurs fairly

often. In some searching applications, searches for

absent info may be frequent.

3. The "average case" is often roughly as bad as the

worst case. For example, suppose that we randomly

choose n numbers and apply insertion sort. The

average-case running time is just like the worst-

case running time, i.e., a quadratic function of n.

2.35

- JavascriptUploaded bySanthosh Soundararajan
- The MagPi Issue 14 EnUploaded byNebojsa Mihajlovic
- HN DAA 15CS43 LectureNotes 1Uploaded byharivinodn
- VBAUploaded bySmpnsatubontang Kaltim
- MS EXCELUploaded byFurqan Ahmad
- Commodore Pet 2001 Quick ReferenceUploaded byCubemanPDX
- MATLAB LOOPSUploaded byvenki249
- Style Rules ChecklistUploaded byKinger.one
- Wk 1b Flowchart SelectionUploaded byRizza Mae Rafer
- All In OneUploaded bySávio Daniel Sávio Daniel
- PD ControlFlow Repetition IA UBUploaded byYAHYA
- Ee6301 Digital Logic CircuitsUploaded bySahil Ashnani
- Problem Set 2 SolUploaded bysristisagar
- P12 - IterationsUploaded bysullivan583
- Automatic Transformation of Series Expressions Into LoopsUploaded byergodic
- Programming Group Assignment W8-14 May2015Uploaded byAh Ying
- hUploaded byDulanga Fernando
- Essentials_C_v1.pdfUploaded byaditya raj
- PL_SQL.docxUploaded byvivek patel
- c quick guide.docxUploaded bypramod_rp2007
- lcdesigner_syntaxcheckingUploaded bybonkersbeckton
- CheatSheet-Python-1-Keywords-1.pdfUploaded byNeeraj
- [Developer Shed Network] Server Side - PHP - PHP 101 (Part 3) - Chocolate Fudge and Time MachinesUploaded byeminem_b
- cpp-study notesUploaded byashfaqmb
- Assignment for Class 12 StudentUploaded byAnuj Tahlan
- OPTIM Report Bancora VenkateswaranUploaded bySwaminath Venkateswaran
- ScsUploaded byAfeefa
- Code EfficiencyUploaded byAdewale Quadri
- Data Type in Phpphp notesUploaded bygaurav singh
- CH01Uploaded byTanvir Ahmed

- 2018 smd catalog.pdfUploaded byammar2 ranjbar
- 221_ch03.pptUploaded byCesar
- ALM11.00_PC_UserAdminGdUploaded byAbhinav Varakantham
- Datasheet-VIO-L208-rev1_0 (1)Uploaded byJuan Carlos May
- uclinux_introduction.pdfUploaded byΘΑΝΑΣΗΣ ΒΑΚΟΥΦΤΣΗΣ
- NetWorker Module for MicrosoftUploaded bybawler
- configuracion de red de trupunch 5000Uploaded byGerardo Villarreal Mejorado
- sedra42021_ch14Uploaded byCarlos Enrique Falcon Zapata
- L03-CISCRISCUploaded bysam_almasry
- Identity and Access Management Solutions Using WebSphere Portal V5.1, Tivoli Identity Manager V4.5.1, And Tivoli Access Manager V5.1 Sg246692Uploaded bybupbechanh
- Ccs c ManualUploaded byAlex Ivan PPerro Valencia
- What is MHEGUploaded byWebdog Dogweb
- tonospag381Uploaded byWalter Grasselli
- Hardware_Answers_to_Review_QuestionsUploaded byepriyaa
- 10 Plc ScadaUploaded bymtdesta
- applications of dfa.pdfUploaded byabhi231594
- Quiz for Chapter 4 With SolutionsUploaded byorlandolanchipa
- ECT REVIEWERUploaded byDannahcaye Api Palces
- ScriptWorX32 Intro 4Uploaded byZeljo Sampion
- GraphUploaded byselavilization
- 47eea5901e73e8ae6aca4d6319d89128.Underground Cable Fault Detection Using ArduinoUploaded byHarshal Patil
- Samuel French ResumeUploaded bysamuelfrench
- Introduction to DSPUploaded bySiony Bello
- PulpUploaded byfacebook
- Oracle Application Server Performance GuideUploaded byAnkur Arora
- 03-Chapter 3 Alarm HandlingUploaded byAhmed Badr
- Potential Thesis TopicsUploaded byJerome Salazar Caballero
- Dell Equallogic Parts ListingUploaded byDave Lowery
- gmlc-rm002_-en-pUploaded bysicramh
- CH 1 EXAMUploaded byhnoor6