Instruction count for statements - PowerPoint PPT Presentation

About This Presentation
Title:

Instruction count for statements

Description:

Count = number of times that barometer instruction is executed. Search algorithms: ... Use loop condition of while as barometer operation. Count6(7) = n s since ... – PowerPoint PPT presentation

Number of Views:14
Avg rating:3.0/5.0
Slides: 28
Provided by: wat88
Category:

less

Transcript and Presenter's Notes

Title: Instruction count for statements


1
Topics
  • Instruction count for statements
  • Methods
  • Examples

2
Deriving Instruction Counts
  • Given a (non-recursive) algorithm expressed in
    pseudo code we explain how to
  • Assign counts to high level statements
  • Describe methods for deriving an instruction
    count
  • Compute counts for several examples

3
Counts for High Level Statements
  • Assignment
  • loop condition
  • for loop
  • for loop body
  • for loop control
  • while loop
  • while loop control
  • while loop body
  • if
  • Note
  • The counts we use are not accurate
  • The goal is to derive a correct growth function

4
Assignment Statement
  • 1. A BC-D/F
  • Count1 1
  • In reality? At least 4
  • Note When numbers can be very large, algorithms
    that deal with large numbers will be used and the
    count will depend also on the number of digits
    needed to store the large numbers.

5
Loop condition
  • (i lt n)(!found)
  • Count1 1
  • Note if loop condition invokes functions count
    of function must be used

6
for loop body
  • for (i0 i lt n i)
  • Ai i
  • Count2 1
  • Count1(2)

i 0
i lt n
false
true
Ai i
i i 1
7
for loop body
  • for (i0 i lt n i)
  • Ai i
  • Count1(2)
  • n
  • last time condition is checked and evaluates to
    true is when i n -1

i 0
i lt n
false
true
Ai i
i i 1
8
for loop control
i 0
  • for (i0 i lt n i)
  • ltbodygt
  • Count number
  • times loop
  • condition is
  • executed
  • (assuming loop condition has a count of 1)

i lt n
false
true
Body of the loop
i i 1
9
for loop control
i 0
  • for (i0 i lt n i)
  • ltbodygt
  • Count1 number times loop condition i lt n is
    executed
  • n 1
  • Note last time condition is checked i n and (n
    lt n) evaluates to false

i lt n
false
true
Body of the loop
i i 1
10
while loop control
Line 1
i 0
  • i 0
  • while (i lt n)
  • Ai i
  • i i 1
  • Count number
  • times loop condition
  • is executed (assuming loop condition has a count
    of 1)

Line 2
i lt n
false
true
Ai i
Line 3
i i 1
Line 4
11
while loop control
Line 1
i 0
  • i 0
  • while (i lt n)
  • Ai i
  • i i 1
  • Count2 number times loop condition
  • (i lt n) is executed
  • n 1

Line 2
i lt n
false
true
Ai i
Line 3
i i 1
Line 4
12
If statement
  • Line 1 if (i 0)
  • Line 2 statement
  • else
  • Line 3 statement
  • For worst case analysis
  • Countif 1 maxcount2, count3

13
Method 1 Sum Line Counts
  • Derive a count for each line of code taking into
    account all loop nesting.
  • Compute total by adding line counts.

14
Method 2 Barometer Operation
  • A barometer instruction is selected
  • Count number of times that barometer
    instruction is executed.
  • Search algorithms
  • barometer instruction (x Lj?).
  • Sort algorithms
  • barometer instruction (Li Lj?).
  • Should be chosen with care.

15
Example 1
  • 1. for (i0 iltn i )
  • 2. Ai i
  • Method 1
  • count1 n1
  • count1(2) n1 n
  • _________________
  • Total (n1)n 2n1

16
Example 1 continued
  • Method 2
  • 1. for (i0 iltn i )
  • 2. Ai i 1
  • Barometer operation in body of loop
  • count1() n

17
Example 2 What is count1(2) now?
Line 1
i 1
  • 1.(for i1iltni3i)
  • 2. Ai i

Line 1
i lt n
no
yes
Ai i
Line 2
i 3 i
Line 1
18
Example 2 What is count1(2) now?
Line 1
i 1
  • 1.(for i1iltn i3i)
  • 2. Ai i
  • For simplicity,
  • n 3k for some positive integer k.
  • Body of the loop executed for
  • i 1(30), 31, 32,,3k.
  • So count1(2)k1
  • Since k log3n, it is executed log3n 1 times.

Line 1
i lt n
no
yes
Ai i
Line 2
i 3 i
Line 1
19
Example 3Sequential Search
  • 1. location0
  • while (locationltn-1
  • Llocation! x)
  • 4. location
  • 5. return location
  • Barometer operation (Llocation! x?)
  • Best case analysis
  • x L0 and the count is 1
  • Worst case analysis
  • x Ln-1 or x not in the list.
  • Count is n.

20
Example 4
1. x 0 2. for (i0 iltn i) 3. for
(j0, jltm j) 4. x x 1
  • Barometer is in body of loop.
  • count2(3()))

21
Flowchart for example 4
x 0
Line 1
i 0
Line 2
i lt n
Line 2
false
Line 3
j 0
i i 1
j lt m
Line 3
false
j j 1
x x 1
Line 4
22
Example 5
  • x0
  • for (i0 iltn i)
  • for (j0, jltn2 j)
  • x x 1
  • The body of the loop of line 2 is executed n
    times.
  • The body of the loop of line 3 is executed n2
    times.
  • Count2(3()) nn21

23
Example 6
  • Line 1 for (i0 iltn i)
  • Line 2 for (j0, jlti j)
  • Line 3. x x 1
  • Barometer
  • Count1(2())

24
Pigeonhole sort
Input Array T containing n keys in ranges 1..
s Main idea1) Maintain count of number of keys
in T of value i in an auxiliary array U2) Use
counts to overwrite array in place
7
1
2
2
1
4
3
Input T
2
1
1
2
3
4
Aux U
2
3
1
1
Output T
1
1
2
2
3
4
2
25
Pigeonhole Sort
  • Pigeonhole-Sort( T, s)
  • for i ? 1 to s
    //initialize U
  • Ui ? 0
  • for j ? 1 to lengthT
  • UT j ? UT j 1 //Count keys
  • q ? 1
  • for j ? 1 to s //rewrite T
  • while Ujgt0
  • Tq j
  • U j ? U j -1
  • q ? q1

26
Example 8
  • q ? 1
  • for j ? 1 to s //rewrite T
  • while Uj gt 0
  • Tq j
  • U j ? U j -1
  • q ? q1
  • Barometer operation in line 9
  • n and not n s
  • Why is this bad?

27
Example 8
  • Use loop condition of while as barometer
    operation
  • Count6(7)
  • n s since
  • Is this algorithm efficient?
Write a Comment
User Comments (0)
About PowerShow.com