## Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

• Pseudocode • Abstract data type • Algorithm efficiency

1

Pseudocode

• What is an algorithm?

2

Pseudocode

• What is an algorithm?

– The logical steps to solve a problem.

3

Pseudocode

• What is a program?

– Program = Data structures + Algorithms (Niklaus Wirth)

4

Pseudocode

• The most common tool to define algorithms. • English-like representation of the code required for an algorithm.

5

Pseudocode

• Pseudocode = English + Code

relaxed syntax that of the is easy to read

iterative)

**extended version basic control structures
**

(sequential, conditional,

6

Pseudocode

Algorithm Header

Algorithm Body

7

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

8

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

9

Example

• Algorithm average Pre nothing Post numbers read and their average printed

1 2 i=0 loop (all data not read) 1 i=i+1 2 read number 3 sum = sum + number average = sum / i print average return

10

3 4 5

End average

**Abstract Data Type
**

• What is a data type?

– Class of data objects that have the same properties

11

**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

12

**Abstract Data Type
**

• ADT = Data structures + Operations

13

**Abstract Data Type
**

Interfac e

Implementation of data and operations

User knows what a data type can do. How it is done is hidden.

14

Abstract Data Type

data structure internal functio n

d ata

function A

data

function B

15

**Example: Variable Access
**

• Rectangle: r

– length: x – width: y

• Rectangle: r

– – – – length: x (hidden) width: y (hidden) get_length() get_width()

16

Example: List

• Interface:

– Data:

• sequence of components of a particular data type

– Operations:

• accessing • insertion • deletion

• Implementation:

– Array, or – Linked list

17

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.

18

Algorithm Efficiency

• General format: f(n)

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

19

Linear Loops

1 i=1 2 loop (i <= 1000) 1 application code 2 i=i+1 1 i=1 2 loop (i <= 1000) 1 application code 2 i=i+2

The number of times the body The number of times the body of the loop is replicated is of the loop is replicated is 1000 500

20

Linear Loops

t ime f(n) = n.T

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

n

21

Logarithmic Loops

Multiply loops

1 i=1 2 loop (i <= 1000) 1 application code 2 i=i×2

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

22

Logarithmic Loops

Multiply loops

1 i=1 2 loop (i <= 1000) 1 application code 2 i=i×1

Divide loops

1 i = 1000 2 loop (i >= 1) 1 application code 2 i=i/2

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

23

Logarithmic Loops

time

f(n) = (log2n).T n

24

Nested Loops

Iterations = Outer loop iterations × Inner loop iterations

25

**Linear Logarithmic Loops
**

1 i=1 2 loop (i <= 10) 1 j=1 2 loop (j <= 10) 1 application code 2 j=j×2 3 i=i+1

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

26

**Linear Logarithmic Loops
**

t ime f(n) = (nlog2n).T

n

27

Quadratic Loops

1 i=1 2 loop (i <= 10) 1 j=1 2 loop (j <= 10) 1 application code 2 j=j+1 3 i=i+1

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

28

**Dependent Quadratic Loops
**

1 i=1 2 loop (i <= 10) 1 j=1 2 loop (j <= i) 1 application code 2 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

29

Quadratic Loops

t ime f(n) = n2.T

n

30

Asymtotic 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 substabtially change the function’s magnitude are eliminated.

31

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).

32

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!

33

**Standard Measures of Efficiency
**

Efficiency

logarithmic linear linear logarithmic quadratic polynomial exponential factorial

Big-O

O(log2n) O(n) O(nlog2n) O(n2) O(nk) O(2n) O(n!)

Iterations

14 10,000 140,000 10,0002 10,000k 210,000 10,000!

Est. Time

microsecond s seconds .1 2 seconds 15-20 min. hours intractable intractable

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

34

**Standard Measures of Efficiency
**

O (n) n2 nlog2n n

log2n n

35

**Big-O Analysis Examples
**

Algorithm addMatrix (val matrix1 <matrix>, val matrix2 <matrix>, val size <integer>, ref matrix3 <matrix>) Add matrix1 to matrix2 and place results in matrix3 Pre matrix1 and matrix2 have data size is number of columns and rows in matrix Post matrices added - result in matrix3 1 r=1 2 loop (r <= size) 1 c=1 2 loop (c <= size) 1 matrix3[r, c] = matrix1[r, c] + matrix2[r, c] 2 c=c+1 3 r=r+1 3 return End addMatrix

36

**Big-O Analysis Examples
**

Algorithm addMatrix (val matrix1 <matrix>, val matrix2 <matrix>, val size <integer>, ref matrix3 <matrix>) Add matrix1 to matrix2 and place results in matrix3 Pre matrix1 and matrix2 have data size is number of columns and rows in matrix Post matrices added - result in matrix3 1 r=1 2 loop (r <= size) 1 c=1 2 loop (c <= size) 1 matrix3[r, c] = matrix1[r, c] + matrix2[r, c] 2 c = c + 1 Nested linear loop: f(size) = O(size2) 3 r=r+1 3 return End addMatrix

37

**Time Costing Operations
**

• The most time consuming: data movement to/from memory/storage. • Operations under consideration:

– Comparions – Arithmetic operations – Assignments

38

Recurrence Equation

• An equation or inequality that describes a function in terms of its value on smaller input.

39

Recurrence Equation

• Example: binary search.

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

4

7

8

10 14 21 22 36 62 77 81 91

40

Recurrence Equation

• Example: binary search.

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

4

7

8

10 14 21 22 36 62 77 81 91

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

41

**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.

42

**Best, Average, Worst Cases
**

• Example: sequential search.

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

4

8

7

10 21 14 22 36 62 91 77 81

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

43

**Best, Average, Worst Cases
**

• Example: sequential search.

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

4

8

7

10 21 14 22 36 62 91 77 81

Average case: f(n) =

∑i.pi

**pi: probability for the target being at a[i] pi = 1/n ⇒ f(n) = (∑i)/n = O(n)
**

44

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).

45

P and NP Problems

Travelling 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 1 D 15 B 5 C 5 5

46

10 E

P and NP Problems

Travelling Salesman Problem:

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

1 D 15 B 5 C 5 5 A 10 E

47

P and NP Problems

• NP-complete: NP and every other problem in NP is polynomially reducible to it. • Open question: P = NP?

P NP NP-complete

48

- Ans
- Algorithm basics
- notes
- Complexity Theory
- CIP_Manufacturing_service_models_2014.pdf
- 12-Soft-Arch
- International Journal of Computational Engineering Research(IJCER)
- 2005nashequilibria-24
- MCS-031 Solved 2014-15
- A Proof that Hitting Set is NP-Complete
- Data Structure by Aakash 11 (5)
- Microsoft PowerPoint - Chap1
- Algorithms and Complexity
- Flowcharts and Pseudocode
- apostila-otimização-MIT
- Deconstructing Systems Using ROUN
- Concrete Computing Customization of Traceability Tractability
- Geovariances MPS
- Ply
- Algorithms for Media-Artists
- Lesson 1-Introduction to Algorithmic Thinking
- Data Structure
- A Case for Spreadsheets
- Algae+John
- introduction to computing
- 01 Algorithms-FlowCharts (1)
- 1 Algorithm and Flowchart
- lfr5thsemster-130624152721-phpapp02
- Appendix
- Bscs II Year

- UT Dallas Syllabus for opre7313.001.08f taught by Milind Dawande (milind)
- Scheduling Resources In a Hetero-Gene Cloud Using Genetic Algorithm
- OCR for Gujarati Numeral using Neural Network
- A Survey on Gesture Recognition
- UT Dallas Syllabus for cs2305.002 05f taught by Timothy Farage (tfarage)
- tmpDF60.tmp
- Voice Recognition System using Template Matching
- Analysis & Design Algorithm MCQ'S
- Appraisal of PSO Algorithm over Genetic Algorithm in WSN Using NS2
- UT Dallas Syllabus for cs6363.002.08s taught by Balaji Raghavachari (rbk)
- The Optimizing Multiple Travelling Salesman Problem Using Genetic Algorithm
- Comparative Analysis of Optimization Algorithms Based on Hybrid Soft Computing Algorithm
- Simulation of Single and Multilayer of Artificial Neural Network using Verilog
- Introduction to Multi-Objective Clustering Ensemble
- tmp8BC6
- Hashing Algorithm
- Public Cloud Partition Using Load Status Evaluation and Cloud Division Rules
- A review on Development of novel algorithm by combining Wavelet based Enhanced Canny edge Detection and Adaptive Filtering Method for Human Emotion Recognition
- Content-Based Image Retrieval Using Features Extracted From Block Truncation Coding
- tmpAA72
- Principles of parallel algorithm models and their objectives
- UT Dallas Syllabus for se3345.502.07f taught by Ivor Page (ivor)
- UT Dallas Syllabus for cs3345.501 05s taught by Greg Ozbirn (ozbirn)
- As 2805.5.1-1992 Electronic Funds Transfer - Requirements for Interfaces Ciphers - Data Encipherment Algorith
- UT Dallas Syllabus for opre7313.001.11s taught by Milind Dawande (milind)
- Comparison of different Sub-Band Adaptive Noise Canceller with LMS and RLS
- tmp904.tmp
- UT Dallas Syllabus for cs3333.001.11s taught by Jeyakesavan Veerasamy (veerasam)
- A Survey of Modern Data Classification Techniques
- tmpC0CF.tmp

- Qui hoạch động
- tÌm HiỂu vỀ Analytic Hierachy Process ThÔng
- GrammarParser
- Chapter1 - Overview
- 02_ToChucCongViec
- Vấn đề NP-đầy đủ
- Control Structures
- Introduction
- 01_Ghichep
- Approximation Algorithms
- Giải thuật quay lui Giải thuật nhánh-và-cận
- Chiến lược biến thể để trị
- ADT_OOP
- FuncProg
- Lex
- Search Tree
- Hashing
- Graph
- Recursion
- Chapter 8
- Sorting
- Linked List
- Binary Tree
- Chiến lược giảm để trị
- Heap Sort
- divide and conquer
- Multiway Tree
- definition about analysis and design Algorithm
- Hardware Basic

Sign up to vote on this title

UsefulNot usefulClose Dialog## Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

Close Dialog## This title now requires a credit

Use one of your book credits to continue reading from where you left off, or restart the preview.

Loading