Documente Academic
Documente Profesional
Documente Cultură
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.
التمس لغيرك
فأنت ال،األعذار
.تعلم الحقيقة كاملة
أو بالبريد االلكتروني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:
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.
أو بالبريد االلكتروني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
أو بالبريد االلكتروني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
ما أكثر:األستاذ
الجزع عند شيء يعجبك في
،المصيبة المدرسة؟
مصيبة أخرى ! الجرس:التلميذ
أو بالبريد االلكتروني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
أو بالبريد االلكتروني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
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
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.
أو بالبريد االلكتروني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
Continue
To sort one million numbers:
2. 106 instructions
2000 seconds,
9
10 instructions / second
50.106 lg106 instructions
100 seconds.
107 instructions / second
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 حمادة شعبان.م