Sunteți pe pagina 1din 16

CHAPTER 1

The Role of Algorithms in


Computing
October 2010

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 well-


specified computational problem. The statement of the
problem specifies in general terms the desired input/output
relationship.

The algorithm describes a specific computational


procedure for achieving that input/output relationship.

‫التمس لغيرك‬
‫ فأنت ال‬،‫األعذار‬
.‫تعلم الحقيقة كاملة‬

‫ أو بالبريد االلكتروني‬SMS ‫ديناران هدية عنـد التنبيه على كـل خطـأ بمذكرات الموقع برسالة‬
Circuits, Numerical Methods, Dynamics, Strength, Statics :‫عامة‬ ‫مواد‬
C++, Java, MATLAB, Data Structures, Algorithms, Digital Logic, Concepts ‫مواد كمبيوتر‬:
Mechanical Design I/II, Structural Analysis I/II, Fluid Mechanics, System Dynamics ‫مواد تصميم‬:
eng-hs.com, eng-hs.net ً ‫ شرح ومسائل محلولة مجانا‬info@eng-hs.com 260 4444 9 ‫حمادة شعبان‬.‫م‬
October 2010

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) ‹a′1, a′2, …, a′n› of the input
sequence such that a′1≤ a′2 ≤ … ≤ a′n.

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 ‫هلل كانت‬
‫األول مقروء‬ .‫الكبر‬
.‫بالعكس‬

‫ أو بالبريد االلكتروني‬SMS ‫ديناران هدية عنـد التنبيه على كـل خطـأ بمذكرات الموقع برسالة‬
Circuits, Numerical Methods, Dynamics, Strength, Statics :‫عامة‬ ‫مواد‬
C++, Java, MATLAB, Data Structures, Algorithms, Digital Logic, Concepts ‫مواد كمبيوتر‬:
Mechanical Design I/II, Structural Analysis I/II, Fluid Mechanics, System Dynamics ‫مواد تصميم‬:
eng-hs.com, eng-hs.net ً ‫ شرح ومسائل محلولة مجانا‬info@eng-hs.com 260 4444 9 ‫حمادة شعبان‬.‫م‬
October 2010

In general, an instance of a problem consists of the input


(satisfying whatever constraints are imposed in the problem
statement) needed to compute a solution to the problem.

Sorting is a fundamental operation in computer science,


and as a result a large number of good sorting algorithms have
been developed. Which algorithm is best for a given application
depends on:
- The number of items to be sorted.
- The extent to which the items are already sorted.
- Possible restrictions on the item values.
- The kind of storage device to be used: main memory,
disks, or tapes.

An algorithm is said to be correct if, for every input


instance, it halts with the correct output.
We say that a correct algorithm solves
the given computational problem.
،‫تعلم كيف تنصت‬
An incorrect algorithm might not ‫فاآلخرين يحبون‬
.‫دوما من يسمعهم‬
halt at all on some input instances,

‫ أو بالبريد االلكتروني‬SMS ‫ديناران هدية عنـد التنبيه على كـل خطـأ بمذكرات الموقع برسالة‬
Circuits, Numerical Methods, Dynamics, Strength, Statics :‫عامة‬ ‫مواد‬
C++, Java, MATLAB, Data Structures, Algorithms, Digital Logic, Concepts ‫مواد كمبيوتر‬:
Mechanical Design I/II, Structural Analysis I/II, Fluid Mechanics, System Dynamics ‫مواد تصميم‬:
eng-hs.com, eng-hs.net ً ‫ شرح ومسائل محلولة مجانا‬info@eng-hs.com 260 4444 9 ‫حمادة شعبان‬.‫م‬
October 2010

or it might halt with an answer


other than the desired one.

Contrary to what one might expect, incorrect algorithms


can sometimes be useful, if their error rate can be controlled.
Ordinarily, however, we shall be concerned only with correct
algorithms.

An algorithm can be specified in English, as a computer


program, or even as a hardware design.
The only requirement is that the specification must provide a
precise description of the computational procedure to be
followed.

‫ ما أكثر‬:‫األستاذ‬
‫الجزع عند‬ ‫شيء يعجبك في‬
،‫المصيبة‬ ‫المدرسة؟‬
‫مصيبة أخرى‬ !‫ الجرس‬:‫التلميذ‬

‫ أو بالبريد االلكتروني‬SMS ‫ديناران هدية عنـد التنبيه على كـل خطـأ بمذكرات الموقع برسالة‬
Circuits, Numerical Methods, Dynamics, Strength, Statics :‫عامة‬ ‫مواد‬
C++, Java, MATLAB, Data Structures, Algorithms, Digital Logic, Concepts ‫مواد كمبيوتر‬:
Mechanical Design I/II, Structural Analysis I/II, Fluid Mechanics, System Dynamics ‫مواد تصميم‬:
eng-hs.com, eng-hs.net ً ‫ شرح ومسائل محلولة مجانا‬info@eng-hs.com 260 4444 9 ‫حمادة شعبان‬.‫م‬
October 2010

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.

‫أنت أكبر‬
‫ تكلم عن‬:‫األستاذ‬ ‫وأفضل مما‬
‫أهمية طاعة‬ ‫تعتقده عن‬
.‫الوالدين‬ ..‫نفسك‬
‫ طاعة الوالدين‬:‫التلميذ‬
!‫أهم من طاعة أبي وأمي‬

‫ أو بالبريد االلكتروني‬SMS ‫ديناران هدية عنـد التنبيه على كـل خطـأ بمذكرات الموقع برسالة‬
Circuits, Numerical Methods, Dynamics, Strength, Statics :‫عامة‬ ‫مواد‬
C++, Java, MATLAB, Data Structures, Algorithms, Digital Logic, Concepts ‫مواد كمبيوتر‬:
Mechanical Design I/II, Structural Analysis I/II, Fluid Mechanics, System Dynamics ‫مواد تصميم‬:
eng-hs.com, eng-hs.net ً ‫ شرح ومسائل محلولة مجانا‬info@eng-hs.com 260 4444 9 ‫حمادة شعبان‬.‫م‬
October 2010

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
will be accessed in an unpredictable fashion.
3- Iterating through an array has good locality of reference.
Limitations:
1- It is not efficient for insertion and deletion of elements
(O(n)).
2- It can not grow or shrink dynamically.
‫ أنا أريد‬:‫البنت ألبيها‬
‫شاب درس مصائب‬
‫إذا كان لديك‬ .‫الحياة وصعوباتها‬
‫ فال يمكن‬،‫مشكلة‬ ‫ إذا البد أن‬:‫األب‬
‫حلها إذا أنكرت‬ ‫يكون قد تزوج من‬
‫وجودها‬ !‫قبل‬

‫ أو بالبريد االلكتروني‬SMS ‫ديناران هدية عنـد التنبيه على كـل خطـأ بمذكرات الموقع برسالة‬
Circuits, Numerical Methods, Dynamics, Strength, Statics :‫عامة‬ ‫مواد‬
C++, Java, MATLAB, Data Structures, Algorithms, Digital Logic, Concepts ‫مواد كمبيوتر‬:
Mechanical Design I/II, Structural Analysis I/II, Fluid Mechanics, System Dynamics ‫مواد تصميم‬:
eng-hs.com, eng-hs.net ً ‫ شرح ومسائل محلولة مجانا‬info@eng-hs.com 260 4444 9 ‫حمادة شعبان‬.‫م‬
October 2010

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.
If we have an algorithm of time complexity O(1) needed,
we need to use only this algorithm. This is the best
solution.
2- Soft real time systems: These are not critical time
requirements we can use on desired algorithms with
desired running time. There’s no need for best solution.
Approximately best solution can also be used.

‫ما عدد المثلثات الغير‬


‫إن لم تكن األفضل‬ ‫ والتي‬،‫متشابهة‬
‫الآن فثق أن األمر‬ ‫قياسات زواياها‬
‫مسألة وقت‬ ‫بالتقدير الستيني أعداد‬
‫صحيحة؟‬

‫ أو بالبريد االلكتروني‬SMS ‫ديناران هدية عنـد التنبيه على كـل خطـأ بمذكرات الموقع برسالة‬
Circuits, Numerical Methods, Dynamics, Strength, Statics :‫عامة‬ ‫مواد‬
C++, Java, MATLAB, Data Structures, Algorithms, Digital Logic, Concepts ‫مواد كمبيوتر‬:
Mechanical Design I/II, Structural Analysis I/II, Fluid Mechanics, System Dynamics ‫مواد تصميم‬:
eng-hs.com, eng-hs.net ً ‫ شرح ومسائل محلولة مجانا‬info@eng-hs.com 260 4444 9 ‫حمادة شعبان‬.‫م‬
October 2010

1.2 Algorithms as a technology

Suppose computers were infinitely fast and computer


memory was free. Would you have any reason to study
algorithms? The answer is yes, because you would still like to
demonstrate that your solution method terminates and does so
with the correct answer.

If computers were infinitely fast, any correct method for


solving a problem would do. You would probably want your
implementation to be within the bounds of good software
engineering practice (i.e., well designed and documented),
but you would most often use whichever method was the
easiest to implement.

Of course, computers may be fast, but they are not


infinitely fast. And memory may be cheap, but it is not free.
Computing time is therefore a bounded resource, and so is
space in memory. These resources should be used wisely, and

‫ أو بالبريد االلكتروني‬SMS ‫ديناران هدية عنـد التنبيه على كـل خطـأ بمذكرات الموقع برسالة‬
Circuits, Numerical Methods, Dynamics, Strength, Statics :‫عامة‬ ‫مواد‬
C++, Java, MATLAB, Data Structures, Algorithms, Digital Logic, Concepts ‫مواد كمبيوتر‬:
Mechanical Design I/II, Structural Analysis I/II, Fluid Mechanics, System Dynamics ‫مواد تصميم‬:
eng-hs.com, eng-hs.net ً ‫ شرح ومسائل محلولة مجانا‬info@eng-hs.com 260 4444 9 ‫حمادة شعبان‬.‫م‬
October 2010

algorithms that are efficient in terms of time or space will help


you do so.

Efficiency
Algorithms devised to solve
the same problem often differ
‫لديك نقاط قوة‬
dramatically in their efficiency. ‫يمكن أن تسرع‬
These differences can be much ‫بك للوصول إلى‬
‫ تعرف‬،‫القمة‬
more significant than differences .‫عليها‬
due to hardware and software.

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 log 2 n and c2 is another constant that also
does not depend on n.

‫ أو بالبريد االلكتروني‬SMS ‫ديناران هدية عنـد التنبيه على كـل خطـأ بمذكرات الموقع برسالة‬
Circuits, Numerical Methods, Dynamics, Strength, Statics :‫عامة‬ ‫مواد‬
C++, Java, MATLAB, Data Structures, Algorithms, Digital Logic, Concepts ‫مواد كمبيوتر‬:
Mechanical Design I/II, Structural Analysis I/II, Fluid Mechanics, System Dynamics ‫مواد تصميم‬:
eng-hs.com, eng-hs.net ً ‫ شرح ومسائل محلولة مجانا‬info@eng-hs.com 260 4444 9 ‫حمادة شعبان‬.‫م‬
October 2010

Note: Insertion sort usually has a smaller constant factor


than merge sort, so that c1 < c2. We shall see that the constant
factors can be far less significant in the running time than the

Continue
dependence on the input size n. Where merge sort has a factor
of lg n in its running time, insertion sort has a factor of n, which
is much larger. Although insertion sort is usually faster than
merge sort for small input sizes, once the input size n becomes
large enough, merge sort’s advantage of lg n vs. n will more
than compensate for the difference in constant factors.
No matter how much smaller c1 is than c2, there will always
be a crossover point beyond which merge sort is faster.

Assume that a faster computer (computer A) is running


insertion sort against a slower computer (computer B) running
merge sort.

A: executes one billion instructions per second with an


Insertion code that requires 2n2 instructions to sort n numbers.
(c1 = 2).

‫ أو بالبريد االلكتروني‬SMS ‫ديناران هدية عنـد التنبيه على كـل خطـأ بمذكرات الموقع برسالة‬
Circuits, Numerical Methods, Dynamics, Strength, Statics :‫عامة‬ ‫مواد‬
C++, Java, MATLAB, Data Structures, Algorithms, Digital Logic, Concepts ‫مواد كمبيوتر‬:
Mechanical Design I/II, Structural Analysis I/II, Fluid Mechanics, System Dynamics ‫مواد تصميم‬:
eng-hs.com, eng-hs.net ً ‫ شرح ومسائل محلولة مجانا‬info@eng-hs.com 260 4444 9 ‫حمادة شعبان‬.‫م‬
October 2010

B: executes only ten million instructions per second with a


Merge sort code using a high-level language with
an inefficient compiler, which takes 50n lg n
‫طريق األلف‬
instructions to sort n numbers (c2 =50). ‫ميل يبدأ‬
.‫بخطــــــوة‬

Continue
To sort one million numbers:
 
2. 106 instructions
 2000 seconds,
9
10 instructions / second
50.106 lg106 instructions
 100 seconds.
107 instructions / second

We notice that computer B runs 20 times faster than computer


A.
The advantage of merge sort is even more pronounced
when we sort ten million numbers: Insertion sort takes
approximately 2.3 days, merge sort takes under 20 minutes.

In general, as the problem size increases, so


does the relative advantage of merge sort.
،‫األمس شيك تم سحبه‬
‫والغد شيك مؤجل قد ال‬
‫ أما الحاضر‬،‫يتم صرفه‬
‫لماذا يحرم‬ ‫فهو السيولة الوحيدة‬
‫الشرع جواز‬ .‫المتوفرة لديك‬
‫المرء من أخت‬
‫ أو بالبريد االلكتروني‬SMS ‫أرملته؟مذكرات الموقع برسالة‬
‫ديناران هدية عنـد التنبيه على كـل خطـأ ب‬
Circuits, Numerical Methods, Dynamics, Strength, Statics :‫عامة‬ ‫مواد‬
C++, Java, MATLAB, Data Structures, Algorithms, Digital Logic, Concepts ‫مواد كمبيوتر‬:
Mechanical Design I/II, Structural Analysis I/II, Fluid Mechanics, System Dynamics ‫مواد تصميم‬:
eng-hs.com, eng-hs.net ً ‫ شرح ومسائل محلولة مجانا‬info@eng-hs.com 260 4444 9 ‫حمادة شعبان‬.‫م‬
October 2010

Exercise 1.2-1
Give an example of an application that requires algorithmic
content at the application level, and discuss the function of the
algorithms involved.

Solution
Simple web-based applications require a degree of algorithmic
content on their own.
For example, Consider a web based service that determines
how to travel from one location to another, its implementation
would rely on fast hardware.
A graphical user interface, wide-area networking.
And also possible on object orientations.
However it would also require algorithms for certain
operations, such as finding routes (probably using shortest path
algorithm), rendering maps, and interpolating
‫ أتعرفون‬:‫األستاذ‬
addresses. ‫من هو المنافق؟‬
‫ من يدخل‬:‫التلميذ‬
‫ أو بالبريد االلكتروني‬SMS ‫المدرسة هدية عنـد التنبيه على كـل خطـأ بمذكرات الموقع برسالة‬
‫ديناران‬ ‫باب‬
Circuits, Numerical Methods, Dynamics, Strength, Statics :‫مبتسما! مواد عامة‬
C++, Java, MATLAB, Data Structures, Algorithms, Digital Logic, Concepts ‫مواد كمبيوتر‬:
Mechanical Design I/II, Structural Analysis I/II, Fluid Mechanics, System Dynamics ‫مواد تصميم‬:
eng-hs.com, eng-hs.net ً ‫ شرح ومسائل محلولة مجانا‬info@eng-hs.com 260 4444 9 ‫حمادة شعبان‬.‫م‬
October 2010

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 < 64n 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. ‫ فستحصل‬،‫التعاسة‬
‫حتما على القدر‬
‫الكافي حسب قوة‬
‫ أو بالبريد االلكتروني‬SMS ‫ديناران هدية عنـد التنبيه على كـل خطـأ بمذكرات الموقع برسالة‬
.‫اعتقادك‬
Circuits, Numerical Methods, Dynamics, Strength, Statics :‫عامة‬ ‫مواد‬
C++, Java, MATLAB, Data Structures, Algorithms, Digital Logic, Concepts ‫مواد كمبيوتر‬:
Mechanical Design I/II, Structural Analysis I/II, Fluid Mechanics, System Dynamics ‫مواد تصميم‬:
eng-hs.com, eng-hs.net ً ‫ شرح ومسائل محلولة مجانا‬info@eng-hs.com 260 4444 9 ‫حمادة شعبان‬.‫م‬
October 2010

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 2 n on
the same machine.
If
2n 100(n2)
n=0 1 0 (where ‘0’ not considered)
n=1 2 100
n=2 4 400
. . . ‫ليكن احترامك‬
‫لذاتك أفضل من‬
‫احترام اآلخرين‬
‫ أو بالبريد االلكتروني‬SMS ‫ديناران هدية عنـد التنبيه على كـل خطـأ بمذكرات الموقع برسالة‬
Circuits, Numerical Methods, Dynamics, Strength, Statics :‫لكمواد عامة‬
C++, Java, MATLAB, Data Structures, Algorithms, Digital Logic, Concepts ‫مواد كمبيوتر‬:
Mechanical Design I/II, Structural Analysis I/II, Fluid Mechanics, System Dynamics ‫مواد تصميم‬:
eng-hs.com, eng-hs.net ً ‫ شرح ومسائل محلولة مجانا‬info@eng-hs.com 260 4444 9 ‫حمادة شعبان‬.‫م‬
October 2010

. . .
n = 13 8192 16900
n = 14 16384 19600
n = 15 32,768 22500
Thus n = 15 is the smallest value at which 100 n 2 runs faster
than 2n.

‫ أو بالبريد االلكتروني‬SMS ‫ديناران هدية عنـد التنبيه على كـل خطـأ بمذكرات الموقع برسالة‬
Circuits, Numerical Methods, Dynamics, Strength, Statics :‫عامة‬ ‫مواد‬
C++, Java, MATLAB, Data Structures, Algorithms, Digital Logic, Concepts ‫مواد كمبيوتر‬:
Mechanical Design I/II, Structural Analysis I/II, Fluid Mechanics, System Dynamics ‫مواد تصميم‬:
eng-hs.com, eng-hs.net ً ‫ شرح ومسائل محلولة مجانا‬info@eng-hs.com 260 4444 9 ‫حمادة شعبان‬.‫م‬

S-ar putea să vă placă și