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
1
Chapter 3
3.1 Algorithms
– Searching Algorithms
– Greedy Algorithms
– The Halting Problem
2
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.
3
• We perform the following steps
1. 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.)
2. 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.
3. Repeat the previous step if there are more integers
in the sequence.
4. Stop when there are no integers left in the
sequence. The temporary maximum at this point is
the largest integer in the sequence.
4
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
for i: =2 to n
if max < ai then max := ai
{max is the largest element}
5
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.
6
Searching Algorithms
Search Problem: Locating an element in an
(ordered) list.
• Linear search
• Binary search (ordered list)
7
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
If i ≤ n then location := i
Else location := 0
{location is the subscript of the term that equals x , or is
0 if x is not found}
8
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
Example 3:
end
to search for 19 in the list
If x = ai then location := i
1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22
else location :=0
{location is the subscript of the term equal to x, or 0 if x is not
found}
9
Sorting
• Sort:
– Sorting is putting elements into a list in which the
elements are in increasing order.
• E.g.
1) 7,2,1,4,5,9 -> 1,2,4,5,7,9
2) d,h,c,a,f -> a,c,d,f,h.
•
•
Bubble sort
Insertion sort
10
Bubble Sort
• ALGORITHM 4: The Bubble Sort
procedure bubble sort (a1, a2, …,an: real numbers with n ≥2)
for i := 1 to n-1
for j := 1 to n- i
if aj > aj+1 then interchange aj and aj+1
{a1, a2, …,an is in increasing order}
• Example 4: Use the sort to put 3, 2, 4, 1, 5 into
increasing order.
11
Bubble Sort
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
Example 5:
i := i + 1
Use the insertion sort to put the
elements of the list 3, 2, 4, 1, 5
m := aj
into increasing order.
for k :=0 to j-i-1
aj-k := a j-k-1
ai := m
end {a1, a2, …,an are sorted}
13
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.
14
• 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)
for i := 1 to r
while n ≥ ci
begin
add a coin with value ci to the change
n := n – ci
end
15
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.
16