Top Banner
Cao Hoang Tru CSE Faculty - HCMUT 1 10 September 2008 Chapter 1: Introduction • Pseudocode Abstract data type Algorithm efficiency
49
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

110 September 2008

Chapter 1: Introduction• Pseudocode• Abstract data type• Algorithm efficiency

Page 2: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

210 September 2008

Pseudocode• What is an algorithm?

Page 3: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

310 September 2008

Pseudocode• What is an algorithm?

– The logical steps to solve a problem.

Page 4: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

410 September 2008

Pseudocode• What is a program?

– Program = Data structures + Algorithms (Niklaus Wirth)

Page 5: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

510 September 2008

Pseudocode• The most common tool to define algorithms.

• English-like representation of the code required for an algorithm.

Page 6: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

610 September 2008

Pseudocode• Pseudocode = English + Code

relaxed syntax being instructions using easy to read basic control structures

(sequential, conditional, iterative)

Page 7: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

710 September 2008

Pseudocode

Algorithm Header

Algorithm Body

Page 8: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

810 September 2008

Pseudocode• Algorithm Header:

– Name– Parameters and their types– Purpose

• what the algorithm does– Precondition

• precursor requirements for the parameters– Postcondition

• taken action and status of the parameters– Return condition

• returned value

Page 9: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

910 September 2008

Pseudocode• Algorithm Body:

– Statements– Statement numbers

• decimal notation to express levels– Variables

• important data– Algorithm analysis

• comments to explain salient points– Statement constructs

• sequence, selection, iteration

Page 10: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

1010 September 2008

ExampleAlgorithm average

Pre nothingPost numbers read and their average printed1 i = 02 loop (all data not read)

1 i = i + 12 read number3 sum = sum + number

3 average = sum / i4 print average5 returnEnd average

Page 11: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

1110 September 2008

Algorithm Design • Divide-and-conquer• Top-down design• Abstraction of instructions • Step-wise refinement

Page 12: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

1210 September 2008

Abstract Data Type• What is a data type?

– Class of data objects that have the same properties

Page 13: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

1310 September 2008

Abstract Data Type• Development of programming concepts:

– GOTO programming• control flow is like spaghetti on a plate

– Modular programming• programs organized into subprograms

– Structured programming• structured control statements (sequence, selection, iteration)

– Object-oriented programming• encapsulation of data and operations

Page 14: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

1410 September 2008

Abstract Data Type• ADT = Data structures + Operations

Page 15: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

1510 September 2008

Abstract Data Type

Implementation of data and operations

Interface

User knows what a data type can do.

How it is done is hidden.

Page 16: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

1610 September 2008

Abstract Data Type

data structure

function A

function B

internalfunction

data

data

Page 17: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

1710 September 2008

Example: Variable Access• Rectangle: r

– length: x– width: y

• Rectangle: r– length: x (hidden)– width: y (hidden)– get_length()– get_width()

Page 18: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

1810 September 2008

Example: List• Interface:

– Data: • sequence of components of a particular data type

– Operations: • accessing• insertion• deletion

• Implementation:– Array, or– Linked list

Page 19: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

1910 September 2008

Algorithm Efficiency• How fast an algorithm is?

• How much memory does it cost?

• Computational complexity: measure of the difficulty degree (time or space) of an algorithm.

Page 20: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

2010 September 2008

Algorithm Efficiency• General format:

f(n)n is the size of a problem (the key number that determines the size of input data)

Page 21: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

2110 September 2008

Linear Loops1 i = 1 1 i = 12 loop (i <= 1000) 2 loop (i <= 1000)

1 application code 1 application code2 i = i + 1 2 i = i + 2

The number of times the body The number of times the bodyof the loop is replicated is of the loop is replicated is1000 500

Page 22: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

2210 September 2008

Linear Loops

n

timef(n) = n.T

f(n) = (n/2).T

Page 23: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

2310 September 2008

Logarithmic LoopsMultiply loops1 i = 12 loop (i <= 1000)

1 application code2 i = i × 2

The number of times the body of the loop is replicated islog2n

Page 24: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

2410 September 2008

Logarithmic LoopsMultiply loops Divide loops1 i = 1 1 i = 10002 loop (i <= 1000) 2 loop (i >= 1)

1 application code 1 application code2 i = i × 2 2 i = i / 2

The number of times the body of the loop is replicated islog2n

Page 25: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

2510 September 2008

Logarithmic Loops

n

time

f(n) = (log2n).T

Page 26: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

2610 September 2008

Nested Loops

Iterations = Outer loop iterations × Inner loop iterations

Page 27: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

2710 September 2008

Linear Logarithmic Loops1 i = 12 loop (i <= 10)

1 j = 12 loop (j <= 10)

1 application code2 j = j × 2

3 i = i + 1

The number of times the body of the loop is replicated is nlog2n

Page 28: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

2810 September 2008

Linear Logarithmic Loops

n

time f(n) = (nlog2n).T

Page 29: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

2910 September 2008

Quadratic Loops1 i = 12 loop (i <= 10)

1 j = 12 loop (j <= 10)

1 application code2 j = j + 1

3 i = i + 1

The number of times the body of the loop is replicated is n2

Page 30: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

3010 September 2008

Dependent Quadratic Loops1 i = 12 loop (i <= 10)

1 j = 12 loop (j <= i)

1 application code2 j = j + 1

3 i = i + 1

The number of times the body of the loop is replicated is 1 + 2 + … + n = n(n + 1)/2

Page 31: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

3110 September 2008

Quadratic Loops

n

time f(n) = n2.T

Page 32: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

3210 September 2008

Asymptotic Complexity• Algorithm efficiency is considered with only

big problem sizes.

• We are not concerned with an exact measurement of an algorithm's efficiency.

• Terms that do not substantially change the function’s magnitude are eliminated.

Page 33: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

3310 September 2008

Big-O Notation• f(n) = c.n ⇒ f(n) = O(n).

• f(n) = n(n + 1)/2 = n2/2 + n/2 ⇒ f(n) = O(n2).

Page 34: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

3410 September 2008

Big-O Notation• Set the coefficient of the term to one.

• Keep the largest term and discard the others.log2n n nlog2n n2 n3 ... nk ... 2n n!

Page 35: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

3510 September 2008

Standard Measures of Efficiency

intractable10,000!O(n!)factorial

intractable210,000O(2n)exponential

hours10,000kO(nk)polynomial

15-20 min. 10,0002O(n2)quadratic

2 seconds140,000O(nlog2n)linear logarithmic

.1 seconds10,000O(n)linear

microseconds14O(log2n)logarithmic

Est. TimeIterationsBig-OEfficiency

Assume instruction speed of 1 microsecond and 10 instructions in loop.n = 10,000

Page 36: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

3610 September 2008

Standard Measures of Efficiency

n

O(n)

log2n

nlog2nn2 n

Page 37: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

3710 September 2008

Big-O Analysis ExamplesAlgorithm addMatrix (val matrix1 <matrix>, val matrix2 <matrix>,

val size <integer>, ref matrix3 <matrix>)Add matrix1 to matrix2 and place results in matrix3Pre matrix1 and matrix2 have data

size is number of columns and rows in matrixPost matrices added - result in matrix31 r = 12 loop (r <= size)

1 c = 12 loop (c <= size)

1 matrix3[r, c] = matrix1[r, c] + matrix2[r, c] 2 c = c + 1

3 r = r + 13 returnEnd addMatrix

Page 38: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

3810 September 2008

Big-O Analysis ExamplesAlgorithm addMatrix (val matrix1 <matrix>, val matrix2 <matrix>,

val size <integer>, ref matrix3 <matrix>)Add matrix1 to matrix2 and place results in matrix3Pre matrix1 and matrix2 have data

size is number of columns and rows in matrixPost matrices added - result in matrix31 r = 12 loop (r <= size)

1 c = 12 loop (c <= size)

1 matrix3[r, c] = matrix1[r, c] + matrix2[r, c] 2 c = c + 1

3 r = r + 13 returnEnd addMatrix

Nested linear loop: f(size) = O(size2)

Page 39: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

3910 September 2008

Time Costing Operations• The most time consuming: data movement

to/from memory/storage.

• Operations under consideration:– Comparisons– Arithmetic operations– Assignments

Page 40: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

4010 September 2008

Recurrence Equation• An equation or inequality that describes a

function in terms of its value on smaller input.

Page 41: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

4110 September 2008

Recurrence Equation• Example: binary search.

918177623622211410874

a[12]a[11]a[10]a[9]a[8]a[7]a[6]a[5]a[4]a[3]a[2]a[1]

Page 42: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

4210 September 2008

Recurrence Equation• Example: binary search.

918177623622211410874

a[12]a[11]a[10]a[9]a[8]a[7]a[6]a[5]a[4]a[3]a[2]a[1]

f(n) = 1 + f(n/2) ⇒ f(n) = O(log2n)

Page 43: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

4310 September 2008

Best, Average, Worst Cases• Best case: when the number of steps is

smallest.

• Worst case: when the number of steps is largest.

• Average case: in between.

Page 44: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

4410 September 2008

Best, Average, Worst Cases• Example: sequential search.

817791623622142110784

a[12]a[11]a[10]a[9]a[8]a[7]a[6]a[5]a[4]a[3]a[2]a[1]

Best case: f(n) = O(1)Worst case: f(n) = O(n)

Page 45: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

4510 September 2008

Best, Average, Worst Cases• Example: sequential search.

817791623622142110784

a[12]a[11]a[10]a[9]a[8]a[7]a[6]a[5]a[4]a[3]a[2]a[1]

Average case: f(n) = ∑i.pi

pi: probability for the target being at a[i]

pi = 1/n ⇒ f(n) = (∑i)/n = O(n)

Page 46: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

4610 September 2008

P and NP Problems• P: Polynomial (can be solved in polynomial

time on a deterministic machine).

• NP: Nondeterministic Polynomial (can be solved in polynomial time on a non-deterministic machine).

Page 47: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

4710 September 2008

P and NP ProblemsTravelling Salesman Problem:A salesman has a list of cities, each of which he must visit exactly once. There are direct roads between each pair of cities on the list. Find the route the salesman should follow for the shortest possible round trip that both starts and finishes at any one of the cities.

A

B

C

D E

1 10

5 5515

Page 48: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

4810 September 2008

P and NP ProblemsTravelling Salesman Problem:

Deterministic machine: f(n) = n(n-1)(n-2) … 1 = O(n!)

⇒ NP problem

A

B

C

D E

1 10

5 5515

Page 49: Chapter 1 - Introduction

Cao Hoang TruCSE Faculty - HCMUT

4910 September 2008

P and NP Problems• NP-complete: NP and every other problem

in NP is polynomially reducible to it.

• Open question: P = NP?

NP

P

NP-complete