BİM122 Lecture Note 6
BİM122 Lecture Note 6
Copyright © McGraw-Hill Education. All rights reserved. No reproduction or distribution without the prior written consent of McGraw-Hill Education.
Chapter Summary
Algorithms
Example Algorithms
Algorithmic Paradigms
Growth of Functions
Big-O and other Notation
Complexity of Algorithms
Section 3.1
Section Summary
Properties of Algorithms
Algorithms for Searching and Sorting
Problems and Algorithms
In many domains there are key general problems that
ask for output with specific properties when given
valid input.
The first step is to precisely state the problem, using
the appropriate structures to specify the input and the
desired output.
We then solve the general problem by specifying the
steps of a procedure that takes a valid input and
produces the desired output. This procedure is called
an algorithm.
Algorithms Abu Ja’far Mohammed Ibin Musa Al-Khowarizmi
( )
Definition: An algorithm is a finite set of precise
instructions for performing a computation or for solving a
problem.
Example: Describe an algorithm for finding the maximum
value in a finite sequence of integers.
Solution: Perform the following steps:
1. Set the temporary maximum equal to the first integer in the
sequence.
2. Compare the next integer in the sequence to the temporary
maximum.
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. If not, stop.
4. When the algorithm terminates, the temporary maximum is the
largest integer in the sequence.
Specifying Algorithms
Algorithms can be specified in different ways. Their steps can be
described in English or in pseudocode.
Pseudocode is an intermediate step between an English language
description of the steps and a coding of these steps using a
programming language.
The form of pseudocode we use is specified in Appendix . It uses
some of the structures found in popular languages such as C++ and
Java.
Programmers can use the description of an algorithm in pseudocode to
construct a program in a particular language.
Pseudocode helps us analyze the time required to solve a problem
using an algorithm, independent of the actual programming language
used to implement algorithm.
Properties of Algorithms
Input: An algorithm has input values from a specified set.
Output: From the input values, the algorithm produces the
output values from a specified set. The output values are
the solution.
Correctness: An algorithm should produce the correct
output values for each set of input values.
Finiteness: An algorithm should produce the output after a
finite number of steps for any input.
Effectiveness: It must be possible to perform each step of
the algorithm correctly and in a finite amount of time.
Generality: The algorithm should work for all problems of
the desired form.
Finding the Maximum Element in a
Finite Sequence
The algorithm in pseudocode:
1. th
2. th
th
3. th
th th
4. th
th
th
5.
Binary Search Video
https://www.youtube.com/watch?v=iP897Z5Nerk
https://www.youtube.com/watch?v=j5uXyPJ0Pew
Sorting
To sort the elements of a list is to put them in increasing order
(numerical order, alphabetic, and so on).
Sorting is an important problem because:
A nontrivial percentage of all computing resources are devoted to
sorting different kinds of lists, especially applications involving large
databases of information that need to be presented in a particular
order (e.g., by customer, part number etc.).
An amazing number of fundamentally different algorithms have been
invented for sorting. Their relative advantages and disadvantages have
been studied extensively.
Sorting algorithms are useful to illustrate the basic notions of
computer science.
A variety of sorting algorithms are studied in this book; binary,
insertion, bubble, selection, merge, quick, and tournament.
In Section , we’ll study the amount of time required to sort a list
using the sorting algorithms covered in this section.
Bubble Sort
Bubble sort makes multiple passes through a list.
Every pair of elements that are found to be out of
order are interchanged.
nd
Bubble Sort Video
https://www.youtube.com/watch?v=lyZQPjUT5
B4
https://www.youtube.com/watch?v=Jdtq5uKz-
w4
Insertion Sort
Insertion sort begins with the nd element. It compares the nd element
with the st and puts it before the first if it is not larger.
procedure insertion sort
•Next the 3rd element is put into (a1,…,an:
the correct position among the real numbers with n ≥ )
first 3 elements. for j := to n
•In each subsequent pass, the n+ st
i :=
element is put into its correct while aj > ai
position among the first n+
i := i +
elements.
•Linear search is used to find the
m := aj
correct position. for k := to j i
aj-k := aj-k-1
ai := m
{Now a1,…,an is in increasing order}
Insertion Sort
Example: Show all the steps of insertion sort with the
input:
first two positions are interchanged)
element remains in its position)
fourth is placed at beginning)
element remains in its position)
Insertion Sort Video
https://www.youtube.com/watch?v=ROalU379l3U
https://www.youtube.com/watch?v=i-SKeOcBwko
Greedy Algorithms
Optimization problems minimize or maximize some parameter over all possible
inputs.
Among the many optimization problems we will study are:
Finding a route between two cities with the smallest total mileage.
Determining how to encode messages using the fewest possible bits.
Finding the fiber links between network nodes using the least amount of fiber.
Optimization problems can often be solved using a greedy algorithm, which
makes the “best” choice at each step. Making the “best choice” at each step does
not necessarily produce an optimal solution to the overall problem, but in
many instances, it does.
After specifying what the “best choice” at each step is, we try to prove that this
approach always produces an optimal solution, or find a counterexample to
show that it does not.
The greedy approach to solving problems is an example of an algorithmic
paradigm, which is a general approach for designing an algorithm. We return to
algorithmic paradigms in Section
Greedy Algorithms: Making Change
Example: Design a greedy algorithm for making change
(in U.S. money) of n cents with the following coins:
quarters ( cents), dimes ( cents), nickels ( cents), and
pennies ( cent) , using the least total number of coins.
Idea: At each step choose the coin with the largest possible
value that does not exceed the amount of change left.
1. If n = cents, first choose quarter leaving
Greedy Change-Making Algorithm
Solution: Greedy change-making algorithm for n cents. The
algorithm works with any coin denominations c1, c2, …,cr .
procedure change(c1, c2, …, cr: values of coins, where c1> c2> … > cr ;
n: a positive integer)
for i :=
di := di counts the coins of denomination ci]
while n ci
di := di + ci
n n - ci
di counts the coins ci]
Talk
Start: 9:45 AM
End :9:45 AM
End: 10:00 AM Talk
End: 11:00 AM
31
References
Discrete Mathematics and Its Applications, Seventh Edition, Kenneth Rosen
32