Chapter 3
Chapter 3
algorithms
DEFINITION 1
An algorithm is a finite sequence of precise instructions for performing a
computation or for solving a problem.
+
algoritm of summation
algorithm of duble summation
algorithm of the scalar product
algorithm of product matrix vector
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
m := (i + j )/2
if x > am then i := m + 1
else j := m
if x = ai then location := i
else location := 0
return location{location is the subscript i of the term ai equal to x, or 0 if x is not
found}
for j := 2 to n
i := 1
while aj > ai
i := i + 1
m := aj
for k := 0 to j − i − 1
aj −k := aj −k−1
ai := m
For instance, suppose that we have three talks: Talk 1 starts at 8 a.m. and ends at 12
noon, Talk 2 starts at 9 a.m. and ends at 10 a.m., and Talk 3 starts at 11 a.m. and ends
at 12 noon. We first select the Talk 1 because it starts earliest. But once we have
selected Talk 1 we cannot select either Talk 2 or Talk 3 because both overlap Talk 1.
Hence, this greedy algorithm selects only one talk. This is not optimal because we
could schedule Talk 2 and Talk 3, which do not overlap.
Now suppose we add the talk that is shortest among the talks that do not overlap any
of those already selected. Again we can construct a counterexample to show that this
greedy algorithm does not always produce an optimal schedule. So, suppose that we
have three talks: Talk 1 starts at 8 a.m. and ends at 9:15 a.m., Talk 2 starts at 9 a.m.
and ends at 10 a.m., and Talk 3 starts at 9:45 a.m. and ends at 11 a.m. We select Talk
2 because it is shortest, requiring one hour. Once we select Talk 2, we cannot select
either Talk 1 or Talk 3 because neither is compatible with Talk 2. Hence, this greedy
algorithm selects only one talk. However, it is possible to select two talks, Talk 1 and
Talk 3, which are compatible.
However, it can be shown that we schedule the most talks possible if in each step we
select the talk with the earliest ending time among the talks compatible with those
already selected.
We will prove this in Chapter 5 using the method of mathematical induction. The first
step we will make is to sort the talks according to increasing finish time. After this
sorting, we relabel the talks so that e1 ≤ e2 ≤ . . . ≤ en . The resulting greedy
algorithm is given as Algorithm 6.
▲
Exercises
1. List all the steps used byAlgorithm 1 to find the maximum
of the list 1, 8, 12, 9, 11, 2, 14, 5, 10, 4.
2. Determine which characteristics of an algorithm de-
scribed in the text (after Algorithm 1) the following pro-
cedures have and which they lack.
a) procedure double(n: positive integer)
while n > 0
n := 2n
b) procedure divide(n: positive integer)
while n ≥ 0
m := 1/n
n := n − 1
c) procedure sum(n: positive integer)
sum := 0
while i < 10
sum := sum + i
d) procedure choose(a, b: integers)
x := either a or b
3. Devise an algorithm that finds the sum of all the integers
in a list.
Complexity of Algorithms