Chapter 1

1 / 45

# Chapter 1 - PowerPoint PPT Presentation

##### Chapter 1
An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Chapter 1 Introduction

2. Outline • Introduction • Pseudo-code • the abstract data type • algorithm efficiency analysis

3. Reasons for using data structure • Efficiency • make algorithms more efficient. • Abstraction • provide more understandable way to look at data. • Reusability • tend to be modular and context-free. • Data Structure + Basic Operation = Abstract Data Type (ADT)

4. Algorithms • Well-defined procedures for solving problems • Reasons for using formal algorithms: • Efficiency • Eg. Sort algorithms. • Abstraction • Eg. Single-pair shortest-paths problems. • Reusability • always reuse again.

5. Approaches in Algorithm Design • Randomized algorithms • Eg. Quicksort • Divide-and-conquer algorithms • Eg. Merge sort. • Dynamic-programming solutions. • Each sub problem is not independent.. • Greedy algorithms • Huffman coding • Approximation algorithms • Travelling-salesman problem

6. Software Engineering • Modularity • black boxes, data hiding, encapsulation. • Readability • document the program. • Simplicity • intelligent solutions are often the simplest ones. • Consistency • establish coding conventions.

7. 1-1 Pseudocode • Thai-English-like representation of the code required for an algorithm • English part and structured code part • The code part consists of basic algorithmic constructs • sequence • selection • iteration

8. Example 1 algorithm sample (ref pageNumber <integer>) This algorithm reads a file and prints a report Pre pageNumber must be initialized Post Report printed. pageNumber contain number of pages in report. Return Number of lines printed.

9. Algorithm 1-1 Example 1-1 pseudocode 1 Open file 2 Lines =0 3 Loop (not end of file) 1 Read file 2 If (full page) 1 Form feed 2 Add 1 to pageNumber 3 Write page heading 3 Write report line 4 lines = line +1 4 Close file 5 Return lines end sample

10. Sample (pageNumber) { Open file; Lines =0; Loop (not end of file) { Read file; If (full page) { Form feed; Add 1 to pageNumber; Write page heading; } Write report line; lines = line +1; } Close file; Return lines; } Algorithm 1-1 Example 1-1 pseudocode

11. Example 2 Algorithm search (ref list <array>, val argument <integer>, ref location <index>) Search array for specific item and return index location. Pre list contains data array to be searched argument contains data to be located in list Post location contains index of element matching argument argument -or- undetermined if not found Return <boolean> true if found, false if not found

12. Variables • Intelligent data names • Rules for naming variables 1. Do not use single character names e.g. i, j, etc. 2. Do not use generic names e.g. sum, total, count, row, etc. 3. Abbreviations e.g. numstu = numberStudents stdCnt = studentCount

13. Algorithm 1-2 Print deviation from mean for series of numbers Algorithm deviation Pre noting Post numbers reads and deviation, average, and total 1 i=0 2 loop (all data not read) 1 i=i+1 2 read number into array[i] 3 sum=sum+1 3 average=sum+1 4 print (average) 5 j=0 6 Loop (j<i) 1 j=j+1 2 devFromAve=array[j] – average 3 print (array[j], devFromAve) 7 return end deviation

14. Algorithm 1-2 Print deviation from mean for series of numbers Algorithm Deviation() { n=0; sum=0.0; sum2=0.0; do { read number into x; If (x != 9999.99) { n=n+1; array[n] = x; sum=sum+ array[n]; } } While (x != 9999.99); average=sum/n; print (average); for( i=1 to n) { sum2 = sum2+ (array[i] – average)*(array[i] – average); } Std-deviation = sqrt(sum2/(n-1)); print (Std-deviation ); }

15. 1-2 The Abstract Data Type • Spaghetti code • nonstructured linear prgs • Modular programming • Prgs organize in functions • Object-oriented programming • Functions are developed around an object

16. Atomic & Composite data • Atomic data • A set of values • A set of operations on values • Example: integer values: …, -2,-1,0,1,2,... Operations: *,+,-,%,.,+ +,- - • Composite data • Data can be broken out into subfields that have meaning

17. Abstract Data Type • The concept of abstraction means: 1. We know what a data type can do 2. How it is done is hidden • Example: consider the concept of a list • 3 data structures are supported • Array, linked list, file

18. Figure 1-1

19. Abstract Data Type 1. Declaration of Data 2. Declaration of Operations

20. Figure 1-2

21. Figure 1-3

22. 1-4 Algorithm Efficiency • “Algorithmics” : the systematic study of the fundamental techniques used to design and analyze efficient algorithms • The algorithm’s efficiency is a function of the number of elements to be processed • The general format: f(n) = efficiency

23. 1. Linear Loops 1 i =1 2 loop (i <=1000) 1 application code 2 i = i +1 f(n) = n

24. Linear Loops (2) 1 i =1 2 loop (i<=1000) 1 application code 2 i=i+2 f(n) = n

25. 2. Logarithmic Loops Multiply Loops Divide Loops 1 i = 1 2 loop (i <1000) 1 application code 2 i=i * 2 1 i =1000 2 loop (i >= 1) 1 application code 2 i=i/2 Multiply Divide

26. Multiply Divide Iteration value of i Iteration value of i 1 1 1 1000 2 2 2 500 3 4 3 250 4 8 4 125 5 16 5 62 6 32 6 31 7 64 7 15 8 128 8 7 9 256 9 3 10 512 10 1 (exit) 1024 (exit) 0 Table 1-2 Analysis of multiply / divide loops f(n) = log2n

27. 3. Nested Loops • Iterations = outer loop iterations * inner loop iterations 1. Linear Logarithmic 2. Dependent Quadratic 3. Quadratic

28. 3.1 Linear Logarithmic 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

29. 3.1 Linear Logarithmic • 10 * [log210] • general form • f(n) = [nlog2n]

30. 3.2 Dependent Quadratic 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

31. 3.2 Dependent Quadratic (2) • 1 + 2 + 3 + … + 9 + 10 = 55 • average of the inner loop = 55/2 • generalized to • Multiply the outer loop

32. 3.3 Quadratic 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 f(n) = n2

33. Big-O Notation • A dominant factor in the equation that determines the “order of magnitude” of the result • This factor is the big-O, On-the-Order-Of • An expressed as O(n), on-the-order-of n

34. Big-O Notation (2) • The big-O notation can be derived from f(n) using the following steps: 1. In each term, set the coefficient of the term to one 2. Keep the largest term in the function and discard the others.

35. Example 4 • remove all coefficients = n2 + n • remove the smaller factors = n2 • big-O notation O(f(n)) = O(n2)

36. Example 5 • eliminate all coefficients • the largest term is the first one • big-O notation O(f(n)) = O(nk)

37. Big-O Notation (3) • Terms are ranked from lowest to highest as shown below: 1. log n 2. n 3. n log n 4. n2 , n3 , n4 , …, nk 5. 2n 6. n!

38. Figure 1-4

39. Efficiency Big-O Iteration Est. timea logarithm O(log n) 14 microseconds linear O(n) 10,000 0.1 seconds linear logarithm O(n(logn)) 140,000 2 seconds quadratic O(n2) 10,000^2 15-20mins. polynomial O(nk) 10,000^k hours exponential O(cn) 2^10,000 intractable factorial O(n!) 10,000! intractable Standard Measures of Efficiency a. Assumes instruction speed of one microsecond (1/million) and 10 instructions in loop

40. Figure 1-5 Example 6: Add Metrices

41. Algorithm 1-3 Add two matrices algorithm addMatrix (val matrix1 <matrix>, val matrix2 <matrix>, val size <integer>, ref matrix3 <matrix>) Add matrix 1 to matrix 2 and place results in matrix 3 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

42. Figure 1-6 Example 7: Multiply Matrices

43. Example 7: Multiply Matrices (2) Matrix [r, c] = matrix1[r, 1] * matrix2[1, c] + matrix1[r, 2] * matrix2[2, c] + matrix1[r, 3] * matrix2[3, c] … + matrix1[r, n] * matrix2[n, c]

44. Algorithm 1-4 Multiply two matrices algorithm addMatrix (val matrix1 <matrix>, val matrix2 <matrix>, val size <integer>, ref matrix3 <matrix>) Multiply matrix 1 by matrix 2 and place product in matrix 3 Pre matrix1 and matrix2 have data size is number of columns and rows in matrix Post matrices multiplied--result in matrix3 1 r = 1 2 loop (r <= size) 1 c = 1 2 loop (c <= size) 1 matrix3[r, c] = 0 2 m = 1 3 loop (m <= size) 1 matrix3[r, c] = matrix3[r, c] + matrix1[r, m] x matrix2[m, c] 2 m = m + 1 4 c = c + 1 3 r = r + 1 3 return end addMatrix