Algorithms Solutions CH 01
Algorithms Solutions CH 01
July 2013
1.1 Algorithms
Definition
An algorithm is any well-defined computational procedure that
takes some value, or set of values, as input and produces some
value, or set of values, as output.
An algorithm is thus a sequence of computational steps that
transform the input into the output.
We can also view an algorithm as a tool for solving a wellspecified computational problem.
The statement of the problem specifies in general terms the
desired input/output relationship.
The algorithm
describes a
specific computational
9 4444 260
Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy
eng-hs.com, eng-hs.net
July 2013
Example
One might need to sort a sequence of numbers into
nondecreasing order.
Here is how we formally define the
sorting problem:
Input: A sequence of n numbers a1, a2, , an.
Output: A permutation (reordering) a1, a2, , an of the input
sequence such that a1 a2 an.
Given the input sequence 31, 41, 59, 26, 41, 58.
A sorting algorithm returns as output the sequence
26, 31, 41, 41, 58, 59.
Such an input sequence is called an instance of the sorting
problem.
3
2
1
.
9 4444 260
Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy
eng-hs.com, eng-hs.net
July 2013
9 4444 260
Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy
eng-hs.com, eng-hs.net
July 2013
Exercise 1.1-1
Give a real-word example in which one of the following
computational problems appears: sorting.
Solution
Real world examples:
1- In web service if we need to see the mails by date we use
sorting.
2- A simple example is when we are working with a PC, on
the desktop we can arrange the icons by name, type, size.
These are all using sorting.
..
:
.
:
!
9 4444 260
Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy
eng-hs.com, eng-hs.net
July 2013
Exercise 1.1-3
Select a data structure that you have seen previously, and
discuss its strengths and limitations.
Solution
Data Structure : Arrays.
Strengths:
1- Arrays permit efficient ( constant time , O(1) ).
2- Most appropriate for sorting a fixed amount of data which
we need to access in random fashion.
3- Iterating through an array has an easier way of reference.
Limitations:
1- It is not efficient for insertion and deletion of elements
(O(n)).
2- It can not grow or shrink dynamically.
:
.
:
!
9 4444 260
Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy
eng-hs.com, eng-hs.net
July 2013
Exercise 1.1-5
Come up with a real-word problem in which only the best
solution will do. Then come up with one in which a solution
that is approximately the best is good enough.
Solution
1- Hard real time systems: These are typically found
interacting at a low level with physical hardware in
embedded
systems.
In
which
time
is
critical.
9 4444 260
Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy
eng-hs.com, eng-hs.net
July 2013
9 4444 260
Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy
eng-hs.com, eng-hs.net
July 2013
Efficiency
Algorithms devised to solve
the same problem often differ
dramatically in their efficiency.
These differences can be much
more significant than differences
Example
Two algorithms for sorting:
First: insertion sort, takes time roughly equal to c1n2 to
sort n items, where c1 is a constant that does not depend on n.
That is, it takes time roughly proportional to n2.
Second: merge sort, takes time roughly equal to c2n lg n,
where lg n stands for log2 n and c2 is another constant that also
does not depend on n.
Note: Insertion sort usually has a smaller constant factor
than merge sort, so that c1 < c2. The constant factors can be far
less significant in the running time than the dependence on the
input size n.
9 4444 260
Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy
eng-hs.com, eng-hs.net
July 2013
Continue
Where merge sort has a factor of lg n in its running time,
insertion sort has a factor
9 4444 260
Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy
eng-hs.com, eng-hs.net
July 2013
Continue
To sort one million numbers:
2. 106 instructions
109 instructions / second
2000 seconds,
100 seconds.
9 4444 260
Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy
eng-hs.com, eng-hs.net
July 2013
Exercise 1.2-2
Suppose we are comparing implementations of insertion sort
and merge sort on the same machine.
For inputs of size n, merge sort runs in 64n lg n steps, while
insertion sort runs in 8n2 steps. For which values on n does
insertion sort beat merge sort ?
Solution
Insertion sort beats merge sort when
8n2 < 64 n lg n
n < 8 lg n
2n/8 < n
This is true for 2 n 43 (found by using a calculator).
Rewrite merge sort to use insertion sort for
input size 43 or less in order to improve
the running time.
9 4444 260
Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy
eng-hs.com, eng-hs.net
July 2013
Exercise 1.2-3
What is the smallest value of n such that an algorithm whose
running time is 100n2 runs faster than an algorithm whose
running time is 2n on the same machine?
Solution
The smallest value of n is 15. Where algorithm having running
time 100n2 runs faster than algorithm having running time 2n
on the same machine.
If
2n
100(n2)
n=0
n=1
100
n=2
400
n = 13
8,192
16,900
n = 14
16,384
19,600
n = 15
32,768
22,500
9 4444 260
Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy
eng-hs.com, eng-hs.net