1 / 16

Chapter 3

Chapter 3. 3.1 Algorithms 3.2 The Growth of Functions 3.3 Complexity of Algorithms 3.4 The Integers and Division 3.5 Primes and Greatest Common Divisors 3.6 Integers and Algorithms 3.7 Applications of Number Theory 3.8 Matrices. Chapter 3. 3.1 Algorithms Searching Algorithms

kent
Download Presentation

Chapter 3

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3 • 3.1 Algorithms • 3.2 The Growth of Functions • 3.3 Complexity of Algorithms • 3.4 The Integers and Division • 3.5 Primes and Greatest Common Divisors • 3.6 Integers and Algorithms • 3.7 Applications of Number Theory • 3.8 Matrices

  2. Chapter 3 • 3.1 Algorithms • Searching Algorithms • Greedy Algorithms • The Halting Problem

  3. Algorithm • Definition 1: An algorithm is a finite set of precise instructions for performing a computation or for solving a problem. • Example 1: Describe an algorithm for finding the maximum (largest) value in a finite sequence of integers.

  4. We perform the following steps • Set the temporary maximum equal to the first integer in the sequence. (the temporary maximum will be the largest integer examined at any stage of the procedure.) • Compare the next integer in the sequence to the temporary maximum, and if it is larger than the temporary maximum, set the temporary maximum equal to this integer. • Repeat the previous step if there are more integers in the sequence. • Stop when there are no integers left in the sequence. The temporary maximum at this point is the largest integer in the sequence.

  5. Pseudocode • Pseudocode provides an intermediate step between an English language description of an algorithm and an implementation of this algorithm in a programming language. • Algorithm 1: Finding the maximum element in a finite sequence. procedure max(a1, a2, . . . ,an: integers) max := a1 fori: =2 to n if max < aithen max := ai {max is the largest element}

  6. Property of Algorithm • Input. • Output. • Definiteness. The steps of an algorithm must be defined precisely. • Correctness. • Finiteness. • Effectiveness. • Generality. The procedure should be applicable for all problems of the desired form, not just for a particular set of input values.

  7. Searching Algorithms Search Problem: Locating an element in an (ordered) list. • Linear search • Binary search (ordered list)

  8. The linear search • Algorithm 2 : the linear search algorithm procedure linear search (x: integer, a1, a2, …,an: distinct integers) i :=1; while ( i ≤n and x ≠ ai) i:= i + 1 Ifi≤ nthenlocation := i Else location := 0 {location is the subscript of the term that equals x , or is 0 if x is not found}

  9. The binary search • Algorithm 3: the binary search algorithm Procedure binary search (x: integer, a1, a2, …,an: increasing integers) i :=1 { i is left endpoint of search interval} j :=n { j is right endpoint of search interval} While i < j begin m := (i+j)/2 if x > am then i := m+1 else j := m end If x = ai then location := i else location :=0 {location is the subscript of the term equal to x, or 0 if x is not found} Example 3: to search for 19 in the list 1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22

  10. Sorting • Sort: • Sorting is putting elements into a list in which the elements are in increasing order. • E.g. • 7,2,1,4,5,9 -> 1,2,4,5,7,9 • d,h,c,a,f -> a,c,d,f,h. • Bubble sort • Insertion sort

  11. Bubble Sort • ALGORITHM 4: The Bubble Sort procedurebubble sort (a1, a2, …,an: real numbers with n ≥2) fori := 1 to n-1 for j := 1 to n- i if aj > aj+1 then interchange aj and aj+1 {a1, a2, …,anis in increasing order} • Example 4: Use the sort to put 3, 2, 4, 1, 5 into increasing order.

  12. Bubble Sort

  13. Insertion Sort • Algorithm 5: The Insertion Sort procedure insertion sort (a1, a2, …,an: real numbers with n ≥2) for j := 2 to n begin i:= 1 while aj> ai i := i + 1 m := aj for k :=0 to j-i-1 aj-k := a j-k-1 ai := m end {a1, a2, …,anare sorted} Example 5: Use the insertion sort to put the elements of the list 3, 2, 4, 1, 5 into increasing order.

  14. Greedy Algorithm • Optimization Problem: find the best solution. • Algorithms that make what seems to be the best choice at each step are called greedy algorithms.

  15. Example 6: Consider the problem of making n cents change with quarters, dimes, nickels, and pennies, and using the least total number of coins. • Algorithm 6: Greedy Change-Marking Algorithm procedure change (c1, c2, …, cr: values of denominations of coins, where c1> c2> … > cr; n: a positive integer) fori := 1 to r while n ≥ ci begin add a coin with value ci to the change n := n – ci end

  16. The Halting Problem • There is a problem that cannot be solved using any procedure. • That is, there are unsolvable problems. • Halting Problem FIGURE 2 Showing that the Halting Problem is Unsolvable.

More Related