0% found this document useful (0 votes)
33 views

Chapter 3

An algorithm is a precise sequence of instructions to perform a computation or solve a problem. This document defines algorithms for finding the maximum element, performing linear search, binary search, bubble sort, insertion sort, and scheduling talks in a greedy manner. It analyzes the complexity of matrix multiplication and a brute force closest pair of points algorithm. The greedy algorithm discussed schedules talks based on earliest finishing time to maximize the number of talks scheduled without conflicts.

Uploaded by

SULTAN Sksa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Chapter 3

An algorithm is a precise sequence of instructions to perform a computation or solve a problem. This document defines algorithms for finding the maximum element, performing linear search, binary search, bubble sort, insertion sort, and scheduling talks in a greedy manner. It analyzes the complexity of matrix multiplication and a brute force closest pair of points algorithm. The greedy algorithm discussed schedules talks based on earliest finishing time to maximize the number of talks scheduled without conflicts.

Uploaded by

SULTAN Sksa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Chapter 3

algorithms

DEFINITION 1
An algorithm is a finite sequence of precise instructions for performing a
computation or for solving a problem.

The term algorithm is a corruption of the name al-Khowarizmi

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
return max{max is the largest element}

ALGORITHM 2 The Linear Search Algorithm.


procedure linear search(x: integer, a1, a2 , . . . , an : distinct integers)
i := 1
while (i ≤ n and x not= ai )
i := i + 1
if i ≤ n then location := i
else location := 0
return location{location is the subscript of the term that equals x, or is 0 if x is not
found}

+
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}

ALGORITHM 4 The Bubble Sort.


procedure bubblesort(a1 , . . . , 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 , . . . , an is in increasing order}
ALGORITHM 5 The Insertion Sort.
procedure insertion sort(a1 , a2 , . . . , an )

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

{a1 , . . . , an is in increasing order}

ALGORITHM 6 Greedy Algorithm for Scheduling Talks.

procedure schedule(s1 ≤ s2 ≤ · · · ≤ sn: start times of talks,


e1 ≤ e2 ≤ · · · ≤ en: ending times of talks)
sort talks by finish time and reorder so that e1 ≤ e2 ≤ . . . ≤ en
S := ∅
for j := 1 to n
if talk j is compatible with S then
S := S ∪ {talk j }
return S{S is the set of talks scheduled}
other criterion.
We now consider these possible criteria. Suppose we add the talk that starts earliest
among the talks compatible with those already selected. We can construct a counter
example to see that the resulting algorithm does not always produce an optimal
schedule.

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.

12. Describe an algorithm that uses only assignment state-


ments that replaces the triple (x, y, z) with (y, z, x).
What is the minimum number of assignment statements
needed?

9. A palindrome is a string that reads the same forward


and backward. Describe an algorithm for determining
whether a string of n characters is a palindrome.

Complexity of Algorithms

Complexity of Matrix Multiplication

ALGORITHM 1 Matrix Multiplication.


procedure matrix multiplication(A, B: matrices)
for i := 1 to m
for j := 1 to n
cij := 0
for q := 1 to k
cij := cij + aiq bqj
return C {C = [cij ] is the product of A and B}

How many additions of integers and multiplications of integers are used by


Algorithm 1 to
multiply two n × n matrices with integer entries?
Solution: There are n2 entries in the product of A and B. To find each entry requires a
total
of n multiplications and n − 1 additions. Hence, a total of n3 multiplications and n2(n
− 1)
additions are used.

ALGORITHM 2 Brute-Force Algorithm for Closest Pair of Points.


procedure closest-pair((x1 , y1 ), (x2 , y2 ), . . . , (xn , yn ): pairs of real numbers)
min= ∞
for i := 2 to n
for j := 1 to i − 1
if (xj − xi )2 + (yj − yi )2 < min then
min := (xj − xi )2 + (yj − yi )2
closest pair := ((xi , yi ), (xj , yj ))
return closest pair

You might also like