SlideShare une entreprise Scribd logo
1  sur  207
Télécharger pour lire hors ligne
ALGORITHMS AND
DATA STRUCTURE
‫المعطيات‬ ‫وبنى‬ ‫الخوارزميات‬ ‫محور‬
By Eng. Joud Khattab
Content
■ OOP. (‫التوجه‬ ‫غرضية‬ ‫)البرمجة‬
■ Complexity. (‫)التعقيد‬
■ Recursive Functions. ( ‫التوابع‬‫العودية‬ )
■ Data Structure: (‫المعطيات‬ ‫)بنى‬
– Sequential: Linked List, Stacks, Queue.
– Non Sequential: Tree, Graph.
■ Sort & Search Algorithms. (‫والترتيب‬ ‫البحث‬ ‫)خوارزميات‬
■ Advance Algorithms Methods (‫متقدمة‬ ‫)خوارزميات‬
– Greedy, Backtracking, Dynamic, Divide & Conquer.
■ Hashing Function. (‫التقطيع‬ ‫)توابع‬
■ MCQ. (‫)اختبارات‬
2
Definitions (‫)تعاريف‬
■‫مسألة‬ ‫حل‬ ‫خوارزمية‬:‫م‬ ‫منتهية‬ ‫متتالية‬ ‫شكل‬ ‫على‬ ‫الحل‬ ‫لطريقة‬ ‫صوري‬ ‫توصيف‬ ‫هي‬‫ن‬
‫محدد‬ ‫تسلسل‬ ‫حسب‬ ‫تنفذ‬ ،‫البسيطة‬ ‫العمليات‬.
■‫البرنامج‬:‫يقبله‬ ‫التي‬ ‫البرمجة‬ ‫لغات‬ ‫بإحدى‬ ‫معينة‬ ‫مسألة‬ ‫حل‬ ‫لخوارزمية‬ ‫توصيف‬ ‫هو‬‫ا‬
‫الحاسوب‬.
■‫البرمجة‬ ‫لغة‬:‫بكتاب‬ ‫تسمح‬ ‫التي‬ ‫المعرفة‬ ‫والدالالت‬ ‫والقواعد‬ ‫المفردات‬ ‫من‬ ‫مجموعة‬ ‫هي‬‫ة‬
‫الحاسوب‬ ‫على‬ ‫تنفيذه‬ ‫يمكن‬ ‫برنامج‬.
■‫المترجم‬:‫برنامج‬ ‫إلى‬ ‫ويحوله‬ ،‫معينة‬ ‫برمجة‬ ‫بلغة‬ ‫المكتوب‬ ‫البرنامج‬ ‫يفهم‬ ‫برنامج‬ ‫هو‬‫مكافئ‬
‫بالمعالج‬ ‫الخاصة‬ ‫المجمع‬ ‫بلغة‬ ‫مكتوب‬‫الصغري‬‫للحاسوب‬.
3
OBJECT ORIENTED
PROGRAMMING (OOP)
‫التوجه‬ ‫غرضية‬ ‫البرمجة‬
4
Question 1
■ Pure OOP can be implemented without using class in a program.
A. True
B. False
5
Question 1
■ Pure OOP can be implemented without using class in a program.
A. True
B. False
6
Question 2
■ What is the additional feature in classes that was not in struct?
A. Data members
B. Member functions
C. Static data allowed
D. Public access specifier
7
Question 2
■ What is the additional feature in classes that was not in struct?
A. Data members
B. Member functions
C. Static data allowed
D. Public access specifier
8
Question 3
■ Which of the following best defines a class?
A. Parent of an object
B. Instanceof an object
C. Blueprint of an object
D. Scope of an object
9
Question 3
■ Which of the following best defines a class?
A. Parent of an object
B. Instanceof an object
C. Blueprint of an object
D. Scope of an object
10
Question 4
■ Which Feature of OOP illustrated the code reusability?
A. Polymorphism
B. Abstraction
C. Encapsulation
D. Inheritance
11
Question 4
■ Which Feature of OOP illustrated the code reusability?
A. Polymorphism
B. Abstraction
C. Encapsulation
D. Inheritance
12
Question 6
■ Which among the following best describes encapsulation?
A. It is a way of combiningvarious data members into a single unit
B. It is a way of combiningvarious member functions into a single unit
C. It is a way of combiningvarious data members and member functions into a
single unit which can operate on any data
D. It is a way of combiningvarious data members and member functions that
operate on those data members into a single unit
13
Question 6
■ Which among the following best describes encapsulation?
A. It is a way of combiningvarious data members into a single unit
B. It is a way of combiningvarious member functions into a single unit
C. It is a way of combiningvarious data members and member functions into a
single unit which can operate on any data
D. It is a way of combiningvarious data members and member functions that
operate on those data members into a single unit
14
Question 7
■ Which of the two features match each other?
A. Inheritanceand Encapsulation
B. Encapsulation and Polymorphism
C. Encapsulation and Abstraction
D. Abstraction and Polymorphism
15
Question 7
■ Which of the two features match each other?
A. Inheritanceand Encapsulation
B. Encapsulation and Polymorphism
C. Encapsulation and Abstraction
D. Abstraction and Polymorphism
16
Question 8
■ What do you call the languages that support classes but not polymorphism?
A. Class based language
B. ProcedureOriented language
C. Object-basedlanguage
D. If classes are supported, polymorphism will always be supported
17
Question 8
■ What do you call the languages that support classes but not polymorphism?
A. Class based language
B. ProcedureOriented language
C. Object-basedlanguage
D. If classes are supported, polymorphism will always be supported
18
Question 9
■ If data members are private, what can we do to access them from the class object?
A. Create public member functions to access those data members
B. Create private member functions to access those data members
C. Create protected member functions to access those data members
D. Private data members can never be accessed from outside the class
19
Question 9
■ If data members are private, what can we do to access them from the class object?
A. Create public member functions to access those data members
B. Create private member functions to access those data members
C. Create protected member functions to access those data members
D. Private data members can never be accessed from outside the class
20
Question 10
■ Which among the following violates the principle of encapsulation almost always?
A. Local variables
B. Global variables
C. Public variables
D. Array variables
21
Question 10
■ Which among the following violates the principle of encapsulation almost always?
A. Local variables
B. Global variables
C. Public variables
D. Array variables
22
Question 11
■ Which among the following is not a necessarycondition for constructors?
A. Its name must be same as that of class
B. It must not have any return type
C. It must contain a definition body
D. It can contains arguments
23
Question 11
■ Which among the following is not a necessarycondition for constructors?
A. Its name must be same as that of class
B. It must not have any return type
C. It must contain a definition body
D. It can contains arguments
24
Question 12
■ How many types of constructorsare available for use in general (with respect to
parameters)?
A. 2
B. 3
C. 4
D. 5
25
Question 12
■ How many types of constructorsare available for use in general (with respect to
parameters)?
A. 2
B. 3
C. 4
D. 5
26
Question 13
■ Default constructor must be defined, if parameterized constructoris defined and the
object is to be created without arguments.
A. True
B. False
27
Question 13
■ Default constructor must be defined, if parameterized constructoris defined and the
object is to be created without arguments.
A. True
B. False
28
Question 14
■ If class C inherits class B. And B has inherited class A. Then while creating the object
of class C, what will be the sequenceof constructors getting called?
A. Constructorof C then B, finally of A
B. Constructorof A then C, finally of B
C. Constructorof C then A, finally B
D. Constructorof A then B, finally C
29
Question 14
■ If class C inherits class B. And B has inherited class A. Then while creating the object
of class C, what will be the sequenceof constructors getting called?
A. Constructorof C then B, finally of A
B. Constructorof A then C, finally of B
C. Constructorof C then A, finally B
D. Constructorof A then B, finally C
30
COMPLEXITY
‫الخوارزميات‬ ‫تعقيد‬
31
Complexity (‫الخوارزميات‬ ‫)تعقيد‬
■‫اساسيين‬ ‫بعاملين‬ ‫يرتبط‬:
–‫الذاكرة‬ ‫حجم‬:
■‫يعالجها‬ ‫التي‬ ‫والمعطيات‬ ‫البرنامج‬ ‫لتخزين‬ ‫الالزمة‬.
–‫التنفيذ‬ ‫زمن‬:
■‫كافة‬ ‫تعليماتها‬ ‫إنجاز‬ ‫من‬ ‫الخوارزمية‬ ‫لتنتهي‬ ‫الالزم‬ ‫الزمن‬.
■‫تعليمة‬ ‫كل‬ ‫لتنفيذ‬ ‫الالزم‬ ‫والزمن‬ ‫التعليمات‬ ‫عدد‬ ‫بحساب‬ ‫التنفيذ‬ ‫زمن‬ ‫يقاس‬.
32
Complexity (‫الخوارزميات‬ ‫)تعقيد‬
■‫منها‬ ‫عديدة‬ ‫عوامل‬ ‫على‬ ‫حاسوب‬ ‫على‬ ‫البرنامج‬ ‫تنفيذ‬ ‫زمن‬ ‫يعتمد‬:
–‫ما‬ ‫بتجربة‬ ‫الخاصة‬ ‫المسألة‬ ‫معطيات‬.
–‫جودة‬‫الرماز‬‫التنفيذي‬ ‫الملف‬ ‫بناء‬ ‫أجل‬ ‫من‬ ‫المترجم‬ ‫يولده‬ ‫الذي‬.
–‫الحاسوب‬ ‫في‬ ‫المتوفرة‬ ‫التعليمات‬ ‫وسرعة‬ ‫طبيعة‬(‫المعالج‬ ‫في‬‫الصغري‬.)
–‫المبرمج‬ ‫يكتبه‬ ‫الذي‬ ‫البرنامج‬ ‫جودة‬.
–‫الخوارزمية‬ ‫فعالية‬.(‫المبرمج‬ ‫قدرة‬ ‫على‬ ‫تعتمد‬)
33
Complexity (‫الخوارزميات‬ ‫)تعقيد‬
■‫بعد‬ ‫تحديد‬ ‫يجب‬ ‫خوارزمية‬ ‫تنفيذ‬ ‫زمن‬ ‫لحساب‬(‫حجم‬ ‫او‬ ‫طول‬ ‫او‬)‫أجل‬ ‫من‬ ‫المسألة‬ ‫معطيات‬
‫البعد‬ ‫هذا‬ ‫بداللة‬ ‫التعقيد‬ ‫درجة‬ ‫كتابة‬.
■‫أمثلة‬:
–‫الحدود‬ ‫كثيرات‬ ‫مسائل‬:‫يكون‬‫الحدود‬ ‫كثيرات‬ ‫درجة‬ ‫هو‬ ‫البعد‬.
–‫المصفوفات‬ ‫مسائل‬:‫يكون‬‫المصفوفة‬ ‫أبعاد‬ ‫بداللة‬ ‫البعد‬.
–‫البيانات‬ ‫مسائل‬:‫يكون‬‫األسهم‬ ‫أو‬ ‫العقد‬ ‫عدد‬ ‫بداللة‬ ‫البعد‬‫مجموعها‬ ‫أو‬.
–‫الفرز‬ ‫مسائل‬:‫يكون‬‫ترتيبها‬ ‫نريد‬ ‫التي‬ ‫العناصر‬ ‫عدد‬ ‫بداللة‬ ‫البعد‬.
–‫القواعدي‬ ‫التحليل‬ ‫مسائل‬:‫يكون‬‫الكلمة‬ ‫طول‬ ‫بداللة‬ ‫البعد‬.
34
Complexity (‫الخوارزميات‬ ‫)تعقيد‬
■‫الرياضية‬ ‫القوانين‬ ‫ببعض‬ ‫تذكرة‬:
35
Complexity (‫الخوارزميات‬ ‫)تعقيد‬
■‫الرياضية‬ ‫القوانين‬ ‫ببعض‬ ‫تذكرة‬:
36
Complexity (‫الخوارزميات‬ ‫)تعقيد‬
37
Complexity (‫الخوارزميات‬ ‫)تعقيد‬
T(n)
n n n log n n2
n3
n4
n10
2n
10 .01s .03s .1s 1s 10s 10s 1s
20 .02s .09s .4s 8s 160s 2.84h 1ms
30 .03s .15s .9s s 810s 6.83d 1s
40 .04s .21s 1.6s s 2.56ms 121d 18m
50 .05s .28s s s 6.25ms 3.1y 13d
100 .1s .66s 10s 1ms 100ms 3171y 41013
y
103
1s 9.96s 1ms 1s 16.67m 3.171013
y 3210283
y
104
s 130s 100ms 16.67m 115.7d 3.171023
y
105
s 1.66ms 10s 11.57d 3171y 3.171033
y
106
ms 19.92ms 16.67m 31.71y 3.17107
y 3.171043
y
38
Complexity (‫الخوارزميات‬ ‫)تعقيد‬
Exercise
39
// a simple loop to calculate the sum of numbers in an array
int i , sum;
for (i = sum = 0; i<n ; i++)
sum += a[i];
Complexity (‫الخوارزميات‬ ‫)تعقيد‬
Exercise
40
int i = 0;
int sum = 0;
while (i < n)
{
sum = sum + a[i];
i = i + 1;
}
The body of this loop is
executed for i = 0, 1, 2, … n-1.
Hence, it is executed
((n-1) – 0) + 1 = n times
Therefore, these 2
assignments are
executed n times
Answer: Number of Assignment Operations =
2 + n + n = 2n + 2
2
assignments
Complexity (‫الخوارزميات‬ ‫)تعقيد‬
Rule
41
If a loop iterates for i = initial value to final value in step of 1
then:
Number of iterations of the loop = final value – initial value + 1
Complexity (‫الخوارزميات‬ ‫)تعقيد‬
Question 1
■‫السؤال‬:‫تصاعديا‬ ‫التالية‬ ‫التعقيدات‬ ‫رتب‬
–n
–n*n
–n*logn
–logn
■‫اإلجابة‬:
.1logn
.2n
.3n*logn
.4n*n
42
Complexity (‫الخوارزميات‬ ‫)تعقيد‬
Question 2
■‫السؤال‬:‫لقوة‬ ‫الرفع‬ ‫لخوارزمية‬ ‫تعقيد‬ ‫أصغر‬
–n
–n*n
–n*logn
–logn
■‫اإلجابة‬:
–logn
43
Complexity (‫الخوارزميات‬ ‫)تعقيد‬
Question 2
int power (int x, int y) {
int p = 1;
for (int i = 1; i<=y; i++) {
p = p*x; }
return p; }
int power2 (int x, int y) {
int res = 1;
int factor = x;
while (y>0) {
if (y%2==1)
res = res*factor;
factor = factor*factor;
y = y / 2; }
return res; }
44
Complexity (‫الخوارزميات‬ ‫)تعقيد‬
■‫التنفيذ‬ ‫زمن‬ ‫حسب‬ ‫المسائل‬ ‫تصنيف‬:
–O(1):‫التقطيع‬ ‫جداول‬ ‫في‬ ‫البحث‬ ‫خوارزميات‬ ‫مثل‬.
–Log n:‫الثنائي‬ ‫البحث‬ ‫خوارزميات‬.
–n:‫التسلسلي‬ ‫البحث‬ ‫خوارزميات‬(‫فعالة‬.)
–n * Log n:‫الجيدة‬ ‫الفرز‬ ‫خوارزميات‬.
–n*n:‫المصفوفات‬ ‫ضرب‬(‫الحجم‬ ‫متوسطة‬ ‫معطيات‬ ‫تعالج‬.)
–2 𝑛
:‫محدود‬ ‫حجم‬ ‫ذات‬ ‫معطيات‬ ‫حالة‬ ‫في‬ ‫إال‬ ‫تستخدم‬ ‫ال‬ ‫أسي‬.
45
Complexity (‫الخوارزميات‬ ‫)تعقيد‬
■‫تنفيذ‬ ‫زمن‬ ‫يكون‬ ‫التي‬ ‫هي‬ ‫معينة‬ ‫مسألة‬ ‫لحل‬ ‫الجيدة‬ ‫الخوارزميات‬ ‫إن‬ ‫نقول‬ ‫ان‬ ‫يمكن‬‫ها‬:
–‫المعطيات‬ ‫حجم‬ ‫كان‬ ‫مهما‬ ‫ثابتا‬:‫التقطيع‬ ‫جداول‬ ‫في‬ ‫البحث‬ ‫خوارزميات‬ ‫مثل‬.
–‫لوغاريتميا‬:‫الثنائي‬ ‫البحث‬ ‫خوارزميات‬ ‫مثل‬.
–‫خطيا‬:‫التسلسلي‬ ‫البحث‬ ‫خوارزمية‬ ‫مثل‬.
–‫مرتبة‬ ‫من‬n*log(n):‫الجيدة‬ ‫الفرز‬ ‫خوارزميات‬ ‫مثل‬.
46
Complexity (‫الخوارزميات‬ ‫)تعقيد‬
Question 3
void Main()
{
for (int i=1; i<=n; i++)
{
m=m+1;
p=p*2;
l=l+3;
}
}
■‫السؤال‬:
–‫الخوارزميات‬ ‫تعقيد‬ ‫احسب‬
‫التالية‬:
■‫اإلجابة‬:
47
Complexity (‫الخوارزميات‬ ‫)تعقيد‬
Question 4
void Main()
{
for (int i=1; i<=n; i++) {
for (int j=1; j<=n;
j++){
m=m+1;
p=p*2;
l=l+3; } }
}
■‫السؤال‬:
–‫الخوارزميات‬ ‫تعقيد‬ ‫احسب‬
‫التالية‬:
■‫اإلجابة‬:
48
Complexity (‫الخوارزميات‬ ‫)تعقيد‬
Question 5
void Main()
{
for (int i=1; i<=n; i++) {
for (int j=1; j<=i;
j++){
m=m+1;
p=p*2;
l=l+3; } }
}
■‫السؤال‬:
–‫الخوارزميات‬ ‫تعقيد‬ ‫احسب‬
‫التالية‬:
■‫اإلجابة‬:
49
Complexity (‫الخوارزميات‬ ‫)تعقيد‬
Question 6
void Main()
{
for (int cnt = 5 , i = 1 ; i <= n ; i++)
for (int j = 1 ; j <= n ; j= j * 2)
cnt++; }
}
■‫السؤال‬:
–‫الخوارزميات‬ ‫تعقيد‬ ‫احسب‬
‫التالية‬:
■‫اإلجابة‬:
–n * log n
50
RECURSIVE FUNCTIONS
‫الخوارزميات‬‫العودية‬
51
Recursive Functions
( ‫الخوارزميات‬‫العودية‬ )
■‫للتوقف‬ ‫شرط‬ ‫بتحقق‬ ‫االستدعاء‬ ‫وينتهي‬ ‫نفسها‬ ‫تستدعي‬ ‫خوارزمية‬ ‫هي‬.
■‫أمثلة‬:
–‫فيبوناتشي‬ ‫اعداد‬ ‫حساب‬.
–‫أكريمان‬ ‫سلسلة‬.
–‫هانوي‬ ‫أبراج‬.
–‫المنحنيات‬ ‫رسم‬.
–‫الثمانية‬ ‫الوزراء‬ ‫مسألة‬.
–‫هلبرت‬ ‫منحنيات‬.
■‫الخوارزمية‬ ‫تعقيد‬ ‫حساب‬ ‫يؤول‬‫العودية‬‫تراجعية‬ ‫معادلة‬ ‫حل‬ ‫إلى‬.
52
Recursive Functions
( ‫الخوارزميات‬‫العودية‬ )
■‫اإلجرائيات‬ ‫من‬ ‫نوعين‬ ‫بين‬ ‫التمييز‬ ‫يمكن‬‫العودية‬:
–‫ذات‬ ‫اإلجرائيات‬‫العودية‬‫المباشرة‬:
■‫إجرائية‬ ‫عن‬ ‫نقول‬P‫إنها‬‫عودية‬‫لنفسها‬ ‫صريح‬ ‫استدعاء‬ ‫تحوي‬ ‫كانت‬ ‫إذا‬ ‫مباشرة‬.
–‫ذات‬ ‫اإلجرائيات‬‫العودية‬‫المباشرة‬ ‫غير‬:
■‫إجرائية‬ ‫عن‬ ‫نقول‬P‫إنها‬‫عودية‬‫أخرى‬ ‫إجرائية‬ ‫تستدعي‬ ‫كانت‬ ‫إذا‬ ‫مباشرة‬ ‫غير‬Q
‫تستدعي‬P.
53
Recursive Functions
( ‫الخوارزميات‬‫العودية‬ )
■‫االستدعاء‬ ‫عملية‬‫العودي‬‫المطلوبة‬ ‫الذاكرة‬ ‫وحجم‬ ‫التنفيذ‬ ‫الزمن‬ ‫حيث‬ ‫من‬ ‫مكلفة‬.
■‫خوارزمية‬ ‫تحويل‬ ‫يمكن‬‫عودية‬‫مكدس‬ ‫باستخدام‬ ‫تكرارية‬ ‫إلى‬
–‫ث‬ ‫فيه‬ ‫عودي‬ ‫استدعاء‬ ‫لكل‬ ‫المحلية‬ ‫والمتحوالت‬ ‫الدخل‬ ‫معامالت‬ ‫تخزين‬ ‫يتم‬ ‫حيث‬‫م‬
‫االستدعاء‬ ‫بعد‬ ‫التعليمات‬ ‫بقية‬ ‫لتنفيذ‬ ‫المكدس‬ ‫عناصر‬ ‫حذف‬ ‫يتم‬‫العودي‬‫عل‬‫هذه‬ ‫ى‬
‫األول‬ ‫إلى‬ ‫األخير‬ ‫االستدعاء‬ ‫من‬ ‫التنفيذ‬ ‫يتم‬ ‫حيث‬ ‫العناصر‬.
54
Recursive Functions ( ‫الخوارزميات‬‫العودية‬ )
Question 1
■‫السؤال‬:‫االستدعاء‬ ‫كان‬ ‫إذا‬ ‫النتيجة‬ ‫ما‬f(33,33)
int f(int x, int y) (
if (x==1) return y;
else if (y==1) return x;
else if (x%2==0) return y+f(x/2,y);
else return x+f(x,y/2); }
■‫اإلجابة‬:
198
55
Recursive Functions ( ‫الخوارزميات‬‫العودية‬ )
Question 2
■‫السؤال‬:‫االستدعاء‬ ‫كان‬ ‫إذا‬ ‫النتيجة‬ ‫ما‬f(1024*1024)
Public int f(int n) {
int x=0;
for (int i=2; i<=n ; i*=2) {
x++; }
return x; }
■‫اإلجابة‬:
20
56
Recursive Functions ( ‫الخوارزميات‬‫العودية‬ )
Question 3
■‫السؤال‬:‫االستدعاء‬ ‫كان‬ ‫إذا‬ ‫النتيجة‬ ‫ما‬f(33)
Int f(int n) {
for (int i=1;i<=n;i++) {
i=i+1;
n=n-1; }
return n; }
■‫اإلجابة‬:
22
57
Recursive Functions ( ‫الخوارزميات‬‫العودية‬ )
Question 4
int fun(int n) {
if (n == 4)
return n;
else return 2*fun(n+1);}
int main() {
printf("%d ", fun(2));
return 0; }
1. 4
2. 8
3. 16
4. Runtime error.
58
Recursive Functions ( ‫الخوارزميات‬‫العودية‬ )
Question 4
int fun(int n) {
if (n == 4)
return n;
else return 2*fun(n+1);}
int main() {
printf("%d ", fun(2));
return 0; }
1. 4
2. 8
3. 16
4. Runtime error.
59
Recursive Functions ( ‫الخوارزميات‬‫العودية‬ )
Question 5
int fun(int x, int y) {
if (x == 0)
return y;
return fun(x - 1, x + y); }
int main() {
printf("%d ", fun(4,3));
return 0; }
1. 13
2. 12
3. 9
4. 10
60
Recursive Functions ( ‫الخوارزميات‬‫العودية‬ )
Question 5
int fun(int x, int y) {
if (x == 0)
return y;
return fun(x - 1, x + y); }
int main() {
printf("%d ", fun(4,3));
return 0; }
1. 13
2. 12
3. 9
4. 10
The rule: x(x+1)/2 + y
61
Recursive Functions ( ‫الخوارزميات‬‫العودية‬ )
Question 6
n = 25
void fun(int n) {
if (n == 0)
return;
printf("%d", n%2);
fun(n/2); }
1. 11001
2. 10011
3. 11111
4. 00000
62
Recursive Functions ( ‫الخوارزميات‬‫العودية‬ )
Question 6
n = 25
void fun(int n) {
if (n == 0)
return;
printf("%d", n%2);
fun(n/2); }
1. 11001
2. 10011
3. 11111
4. 00000
63
Recursive Functions ( ‫الخوارزميات‬‫العودية‬ )
Question 7
n = 25
void fun(int n) {
if (n == 0)
return;
fun(n/2);
printf("%d", n%2); }
1. 11001
2. 10011
3. 11111
4. 00000
64
Recursive Functions ( ‫الخوارزميات‬‫العودية‬ )
Question 7
n = 25
void fun(int n) {
if (n == 0)
return;
fun(n/2);
printf("%d", n%2); }
1. 11001
2. 10011
3. 11111
4. 00000
65
Recursive Functions ( ‫الخوارزميات‬‫العودية‬ )
Question 8
What does the following function do?
int fun(int x, int y)
{
if (y == 0) return 0;
return (x + fun(x, y-1));
}
1. x+y
2. x+x*y
3. x*y
4. x^y
66
Recursive Functions ( ‫الخوارزميات‬‫العودية‬ )
Question 8
What does the following function do?
int fun(int x, int y)
{
if (y == 0) return 0;
return (x + fun(x, y-1));
}
1. x+y
2. x+x*y
3. x*y
4. x^y
67
Recursive Functions ( ‫الخوارزميات‬‫العودية‬ )
Question 9
Predict the output of following program
int f(int n) {
if(n <= 1)
return 1;
if(n%2 == 0)
return f(n/2);
return f(n/2) + f(n/2+1); }
int main() {
printf("%d", f(11));
return 0; }
1. Stack overflow
2. 3
3. 4
4. 5
68
Recursive Functions ( ‫الخوارزميات‬‫العودية‬ )
Question 9
Predict the output of following program
int f(int n) {
if(n <= 1)
return 1;
if(n%2 == 0)
return f(n/2);
return f(n/2) + f(n/2+1); }
int main() {
printf("%d", f(11));
return 0; }
1. Stack overflow
2. 3
3. 4
4. 5
69
Recursive Functions ( ‫الخوارزميات‬‫العودية‬ )
Question 10
Consider the following: f(5)
int fun (int n)
{
int x=1, k;
if (n==1) return x;
for (k=1; k<n; ++k)
x = x + fun(k) * fun(n – k);
return x;
}
1. 0
2. 26
3. 51
4. 71
70
Recursive Functions ( ‫الخوارزميات‬‫العودية‬ )
Question 10
Consider the following: f(5)
int fun (int n)
{
int x=1, k;
if (n==1) return x;
for (k=1; k<n; ++k)
x = x + fun(k) * fun(n – k);
return x;
}
1. 0
2. 26
3. 51
4. 71
71
Recursive Functions ( ‫الخوارزميات‬‫العودية‬ )
Question 11
If get(6) function is being called. how
many times will the get() function be
invoked?
void get (int n) {
if (n < 1) return;
get(n-1);
get(n-3);
printf("%d", n); }
1. 15
2. 25
3. 35
4. 45
72
Recursive Functions ( ‫الخوارزميات‬‫العودية‬ )
Question 11
If get(6) function is being called. how
many times will the get() function be
invoked?
void get (int n) {
if (n < 1) return;
get(n-1);
get(n-3);
printf("%d", n); }
1. 15
2. 25
3. 35
4. 45
73
get(6) [25 Calls]
/ 
[17 Calls] get(5) get(3) [7 Calls]
/ 
get(4) get(2) [5 Calls]
/ 
[7 Calls] get(3) get(1) [3 Calls]
/ 
get(2) get(0)
/ 
[3 Calls] get(1) get(-1)
/ 
get(0) get(-2)
SEQUENTIAL
DATA STRUCTURE
‫التسلسلية‬ ‫المعطيات‬ ‫بنى‬
74
Linked List (‫الخطية‬ ‫)السالسل‬
■‫النمط‬ ‫نفس‬ ‫من‬ ‫عناصر‬ ‫تخزين‬ ‫من‬ ‫تمكن‬ ‫معطيات‬ ‫بنية‬ ‫هي‬.
■‫السالسل‬ ‫ميزات‬:
–‫مسبقا‬ ‫طولها‬ ‫لتحديد‬ ‫ضرورة‬ ‫ال‬.
–‫واالضافة‬ ‫الحذف‬ ‫عند‬ ‫السلسلة‬ ‫عناصر‬ ‫ازاحة‬ ‫إلى‬ ‫نحتاج‬ ‫ال‬.
■‫سيئاتها‬:
–‫عنصر‬ ‫إلى‬ ‫المباشر‬ ‫النفاذ‬ ‫يمكن‬ ‫ال‬,‫إلى‬ ‫تحتاج‬ ‫وبالتالي‬n‫االحوال‬ ‫أسوأ‬ ‫في‬ ‫مقارنة‬ ‫عملية‬
‫العنصر‬ ‫إلى‬ ‫للوصول‬.
–‫المؤشرات‬ ‫لتخزين‬ ‫إضافي‬ ‫تخزين‬ ‫حجم‬ ‫إلى‬ ‫نحتاج‬.
■‫مثل‬ ‫للسالسل‬ ‫أخرى‬ ‫أنواع‬ ‫يوجد‬:
–‫الدائرية‬ ‫السالسل‬circular linked list.
–‫االرتباط‬ ‫المضاعفة‬ ‫السالسل‬doubly linked list.
75
Linked List (‫الخطية‬ ‫)السالسل‬
76
A B C
Head
A
data pointer
node
Linked List (‫الخطية‬ ‫)السالسل‬
V.S. Ordinary and Dynamic Arrays
Ordinary Arrays
■‫الميزات‬:
–‫واستخدام‬ ‫إنشاء‬ ‫جدا‬ ‫السهل‬ ‫من‬.
–‫عنصر‬ ‫أي‬ ‫إلى‬ ‫المباشر‬ ‫الوصول‬.
■‫السيئات‬:
–‫وقت‬ ‫في‬ ‫ا‬ً‫معروف‬ ‫حجمها‬ ‫يكون‬ ‫أن‬ ‫يجب‬
‫ف‬ ‫تغييره‬ ‫يمكن‬ ‫وال‬ ،‫البرمجي‬ ‫التحويل‬‫ي‬
‫التشغيل‬ ‫وقت‬.
–‫كل‬ ‫نقل‬ ‫حذفه‬ ‫أو‬ ‫عنصر‬ ‫إدراج‬ ‫يتطلب‬
‫األخرى‬ ‫العناصر‬.
Dynamic Arrays
■‫الميزات‬:
–‫إنشائها‬ ‫جدا‬ ‫السهل‬ ‫من‬.
–‫عنصر‬ ‫أي‬ ‫إلى‬ ‫المباشر‬ ‫الوصول‬.
–‫التشغيل‬ ‫وقت‬ ‫في‬ ‫الحجم‬ ‫تحديد‬ ‫يمكن‬.
■‫السيئات‬:
–‫كل‬ ‫نقل‬ ‫حذفه‬ ‫أو‬ ‫عنصر‬ ‫إدراج‬ ‫يتطلب‬
‫األخرى‬ ‫العناصر‬.
–‫تتخطى‬ ‫أن‬ ‫المصفوفة‬ ‫على‬ ‫كان‬ ‫إذا‬
‫تشكيل‬ ‫إعادة‬ ‫فيجب‬ ، ‫األولي‬ ‫الحجم‬
‫جديد‬ ‫موقع‬ ‫إلى‬ ‫بأكملها‬ ‫المصفوفة‬.
77
Stack (‫)المكدس‬
■‫ندع‬ ‫واحدة‬ ‫جهة‬ ‫من‬ ‫تتم‬ ‫والحذف‬ ‫اإلضافة‬ ‫عمليات‬ ‫إن‬ ‫غير‬ ،‫السلسلة‬ ‫تشبه‬ ‫خطية‬ ‫بنية‬ ‫هي‬‫وها‬
‫المكدس‬ ‫قمة‬.
■‫النمط‬ ‫نفس‬ ‫من‬ ‫عناصر‬ ‫تخزين‬ ‫من‬ ‫تمكن‬ ‫معطيات‬ ‫بنية‬ ‫هي‬.
■‫مبدأ‬ ‫تتبع‬:
–First In last Out (FILO)
–Last In First Out (LIFO)
■‫عليه‬ ‫االساسية‬ ‫العمليات‬:
–‫قمته‬ ‫خالل‬ ‫من‬ ‫العناصر‬ ‫إلى‬ ‫الوصول‬ ‫يمكن‬head))
–‫القمة‬ ‫إلى‬ ‫عنصر‬ ‫إضافة‬push))
–‫القمة‬ ‫من‬ ‫عنصر‬ ‫حذف‬pop))
■‫الخطية‬ ‫السالسل‬ ‫أو‬ ‫المصفوفات‬ ‫باستخدام‬ ‫المكدس‬ ‫تمثيل‬ ‫يمكن‬.
78
Stack (‫)المكدس‬
79
Stack (‫)المكدس‬
■‫المكدسات‬ ‫تطبيقات‬:
–‫الصفحات‬ ‫سجل‬‫المزارة‬‫االنترنت‬ ‫متصفح‬ ‫في‬.
–‫النصوص‬ ‫محررات‬ ‫في‬ ‫التراجع‬ ‫عمليات‬.
–‫الرياضية‬ ‫التعابير‬ ‫في‬ ‫األقواس‬ ‫أزواج‬ ‫ربط‬.
–‫قيم‬ ‫وحساب‬ ‫الملحقة‬ ‫التعابير‬ ‫إلى‬ ‫النظامية‬ ‫الرياضية‬ ‫التعابير‬ ‫من‬ ‫التحويل‬‫هذه‬ ‫ة‬
‫التعابير‬.
–‫تابع‬ ‫جسم‬ ‫في‬ ‫تابع‬ ‫استدعاء‬.
–‫أوال‬ ‫العمق‬ ‫بطريقة‬ ‫والبيانات‬ ‫األشجار‬ ‫في‬ ‫التجول‬.
–‫المترجمات‬ ‫في‬ ‫األخطاء‬ ‫مكدس‬.
80
Stack (‫)المكدس‬
Infix (‫)نظامي‬ Prefix (‫)مصدر‬ Postfix (‫)ملحق‬
A+B +AB AB+
A+B*C +A*BC ABC*+
A*(B+C) *A+BC ABC+*
A*B+C +*ABC AB*C+
A+B*C+D-E*F -++A*BCD*EF ABC*+D+EF*-
(A+B)*(C+D-E)*F **+AB-+CDEF AB+CD+E-*F*
81
Stack (‫)المكدس‬
■ Infix to Postfix:
1. Scan the infix expression from left to right.
2. If the scanned character is an operand, output it.
3. Else,
1. If the precedence of the scanned operator is greater than the precedence of the operator in the
stack (or the stack is empty), push it.
2. Else, Pop the operator from the stack until the precedence of the scanned operator is less-equal to
the precedence of the operator residing on the top of the stack. Push the scanned operator to the
stack.
4. If the scanned character is an ‘(‘, push it to the stack.
5. If the scanned character is an ‘)’, pop and output from the stack until an ‘(‘ is encountered.
6. Repeat steps 2-6 until infix expression is scanned.
7. Pop and output from the stack until it is not empty.
82
Stack (‫)المكدس‬
■ Infix to Prefix:
1. Reversethe infix expression i.e A+B*C will become C*B+A.
■ Note while reversing each ‘(‘ will become ‘)’ and each ‘)’ becomes ‘(‘.
2. Obtain the postfix expression of the modified expression i.e CB*A+.
3. Reversethe postfix expression. Hence in our example prefix is +A*BC.
83
Stack (‫)المكدس‬
■ Postfix Calculation:
1. Create a stack to store operands (values).
2. Scan the given expression and do following for every scanned element.
1. If the elementis a number, push it into the stack.
2. If the elementis a operator, pop operands for the operator from stack. Evaluate
the operator and push the result back to the stack.
3. When the expression is ended, the number in the stack is the final answer.
84
Stack (‫)المكدس‬
■ Prefix Calculation:
1. Put a pointer P at the end of the end.
2. If character at P is an operand push it to Stack.
3. If the character at P is an operator pop two elements from the Stack. Operate
on these elements accordingto the operator, and push the result back to the
Stack.
4. Decrement P by 1 and go to Step 2 as long as there are characters left to be
scanned in the expression.
5. The Resultis stored at the top of the Stack.
85
Queue (‫)األرتال‬
■‫الرت‬ ‫ذيل‬ ‫ندعوها‬ ‫جهة‬ ‫في‬ ‫تجري‬ ‫اإلضافة‬ ‫عمليات‬ ‫أن‬ ‫غير‬ ‫السلسلة‬ ‫تشبه‬ ‫خطية‬ ‫بنية‬ ‫هي‬‫ل‬.
‫الرتل‬ ‫بداية‬ ‫نسميها‬ ‫التي‬ ‫المعاكسة‬ ‫الجهة‬ ‫في‬ ‫الحذف‬ ‫ويجري‬.
■‫النمط‬ ‫نفس‬ ‫من‬ ‫عناصر‬ ‫تخزين‬ ‫من‬ ‫تمكن‬ ‫معطيات‬ ‫بنية‬ ‫هي‬.
■‫مبدأ‬ ‫تتبع‬:
–First In First Out (FIFO)
–Last In Last Out (LILO)
■‫عليه‬ ‫االساسية‬ ‫العمليات‬:
–‫بدايته‬ ‫خالل‬ ‫من‬ ‫العناصر‬ ‫إلى‬ ‫الوصول‬ ‫يمكن‬(head)‫نهايته‬ ‫أو‬(tail.)
–‫النهاية‬ ‫إلى‬ ‫عنصر‬ ‫إضافة‬(enqueue.)
–‫البداية‬ ‫من‬ ‫عنصر‬ ‫حذف‬(dequeue.)
■‫الخطية‬ ‫السالسل‬ ‫أو‬ ‫المصفوفات‬ ‫باستخدام‬ ‫االرتال‬ ‫تمثيل‬ ‫يمكن‬.
86
Queue (‫)األرتال‬
87
Queue (‫)األرتال‬
■‫األرتال‬ ‫تطبيقات‬:
–‫الكمبيوتر‬ ‫في‬ ‫والمخرجات‬ ‫المدخالت‬ ‫مخازن‬(‫والطابعة‬ ‫المفاتيح‬ ‫لوحة‬.)
–‫في‬ ‫أوال‬ ‫بالعرض‬ ‫التجول‬ ‫خوارزميات‬ ‫مثل‬ ‫الخوارزميات‬ ‫في‬ ‫مساعدة‬ ‫معطيات‬ ‫بنية‬
‫والبيانات‬ ‫األشجار‬.
–‫اليومية‬ ‫الحياة‬ ‫أرتال‬ ‫محاكاة‬.
88
Queue (‫)األرتال‬
■‫االولويات‬ ‫رتل‬:
–‫الرتل‬ ‫إلى‬ ‫األولوية‬ ‫عامل‬ ‫يضاف‬.
–‫اوال‬ ‫األعلى‬ ‫األولوية‬ ‫ذو‬ ‫العنصر‬ ‫تخديم‬ ‫يتم‬.
–‫مثال‬:
■‫للحاالت‬ ‫األولوية‬ ‫تكون‬ ‫حيث‬ ‫الطبيب‬ ‫عيادة‬ ‫في‬ ‫االنتظار‬ ‫أرتال‬‫اإلسعافية‬.
89
NON SEQUENTIAL
DATA STRUCTURES
‫التسلسلية‬ ‫غير‬ ‫المعطيات‬ ‫بنى‬
90
Trees (‫)االشجار‬
■‫الشجرة‬ ‫نعرف‬:
–‫عقد‬ ‫نسميها‬ ‫العناصر‬ ‫من‬ ‫مجموعة‬ ‫بأنها‬nodes))،
–‫بروابط‬ ‫بعضها‬ ‫بين‬ ‫مرتبطة‬(Links)،
–‫هرميا‬ ‫تنظيما‬ ‫ومنظمة‬(Hierarchical)،
–‫حلقات‬ ‫يحتوي‬ ‫ال‬(Cycles)،‫مغلقة‬
–‫جذر‬ ‫نسميها‬ ‫ووحيدة‬ ‫مميزة‬ ‫عقدة‬ ‫توجد‬ ‫إنه‬ ‫أي‬(Root.)
■‫أمثلة‬:
–‫مجلدات‬ ‫في‬ ‫الملفات‬ ‫تنظيم‬.
–‫حسابية‬ ‫عبارة‬ ‫تمثيل‬.
91
Binary Trees (‫الثنائية‬ ‫)األشجار‬
■‫األكثر‬ ‫علي‬ ‫ابنان‬ ‫عقدة‬ ‫لكل‬ ‫فيها‬ ‫يكون‬ ‫األشجار‬ ‫من‬ ‫خاصة‬ ‫حالة‬:
–‫داخلية‬ ‫عقد‬ ‫األقل‬ ‫على‬ ‫ولد‬ ‫لها‬ ‫التي‬ ‫العقد‬ ‫نسمي‬.
–‫أوراق‬ ‫أو‬ ‫خارجية‬ ‫عقد‬ ‫ولد‬ ‫أي‬ ‫لها‬ ‫ليس‬ ‫التي‬ ‫العقد‬ ‫نسمي‬(Leaf.)
–‫طريقا‬ ‫نسمي‬(Path)‫العقد‬ ‫من‬ ‫متتالية‬ ‫كل‬.
–‫األوراق‬ ‫وإحدى‬ ‫الجذر‬ ‫بين‬ ‫يصل‬ ‫طريق‬ ‫كل‬ ‫فرعا‬ ‫نسمي‬.
92
Binary Trees (‫الثنائية‬ ‫)األشجار‬
■‫مهمة‬ ‫تعاريف‬:
–‫الشجرة‬ ‫درجة‬:‫عقدة‬ ‫ألوالد‬ ‫األعظمي‬ ‫العدد‬( .‫هي‬ ‫ثنائية‬ ‫شجرة‬ ‫درجة‬2)
–‫الشجرة‬ ‫حجم‬:‫عقدها‬ ‫عدد‬.
–‫عقدة‬ ‫ارتفاع‬:‫الجذر‬ ‫الى‬ ‫العقدة‬ ‫من‬ ‫الواصل‬ ‫الطريق‬ ‫في‬ ‫االتصاالت‬ ‫عدد‬.
–‫الشجرة‬ ‫ارتفاع‬:‫فيها‬ ‫عقدة‬ ‫ارتفاع‬ ‫أطول‬.
–‫الشجرة‬ ‫عرض‬:‫ما‬ ‫مستوى‬ ‫في‬ ‫للعقد‬ ‫األعظم‬ ‫العدد‬.
–‫التجول‬ ‫مسافة‬:‫عقدها‬ ‫ارتفاعات‬ ‫مجموع‬ ‫هو‬ ‫الشجرة‬ ‫ضمن‬.
–‫الخارجي‬ ‫التجول‬ ‫مسافة‬:‫أوراقها‬ ‫ارتفاعات‬ ‫مجموع‬.
–‫الداخلي‬ ‫التجول‬ ‫مسافة‬:‫الداخلية‬ ‫عقدها‬ ‫ارتفاعات‬ ‫مجموع‬.
93
Binary Trees (‫الثنائية‬ ‫)األشجار‬
Exercise
■‫الشجرة‬ ‫جذر‬:n1
■‫االيسر‬ ‫االبن‬:n2
■‫األيمن‬ ‫االبن‬:n4
■‫األوراق‬:n8 – n10 – n5 – n9
■‫الداخلية‬ ‫العقد‬:n1 – n2 – n3 – n4 – n6 – n7
■‫عقدة‬ ‫ارتفاع‬:
n1=0 , n2=n4=1 , n3=n5=n7=2
■‫الشجرة‬ ‫ارتفاع‬:4
■‫التجول‬ ‫مسافة‬:22
■‫الخارجي‬ ‫التجول‬ ‫مسافة‬:13
■‫الداخلي‬ ‫التجول‬ ‫مسافة‬:9
94
Binary Trees (‫الثنائية‬ ‫)األشجار‬
■‫الخاصة‬ ‫الثنائية‬ ‫األشجار‬ ‫بعض‬:
–‫الخطية‬ ‫الثنائية‬ ‫الشجرة‬:‫األ‬ ‫على‬ ‫واحد‬ ‫ولد‬ ‫منها‬ ‫عقدة‬ ‫لكل‬ ‫يكون‬ ‫ثنائية‬ ‫شجرة‬‫كثر‬.
–‫التامة‬ ‫الثنائية‬ ‫الشجرة‬:‫المستوى‬ ‫في‬ ‫واحدة‬ ‫عقدة‬ ‫تحوي‬ ‫شجرة‬ ‫كل‬0‫في‬ ‫وعقدتين‬
‫المستوى‬1‫المستوى‬ ‫في‬ ‫عقد‬ ‫وأربع‬2‫و‬2 𝑘
‫المستوى‬ ‫في‬ ‫عقدة‬k.
■(‫ارتفاعها‬ ‫تامة‬ ‫بشجرة‬ ‫العقد‬ ‫عدد‬h‫هو‬2ℎ+1
− 1)
–‫الكاملة‬ ‫الثنائية‬ ‫الشجرة‬:‫بال‬ ‫مليئة‬ ‫مستوياتها‬ ‫كل‬ ‫تكون‬ ‫ثنائية‬ ‫شجرة‬ ‫كل‬‫ماعدا‬ ‫عقد‬
‫فراغات‬ ‫على‬ ‫يحوي‬ ‫أن‬ ‫يمكن‬ ‫األخير‬ ‫المستوى‬.
95
Binary Trees (‫الثنائية‬ ‫)األشجار‬
96
Binary search trees Not a binary search tree
5
10
30
2 25 45
5
10
45
2 25 30
5
10
30
2
25
45
Binary Trees (‫الثنائية‬ ‫)األشجار‬
Question 1
■‫السؤال‬:‫عقدها‬ ‫عدد‬ ‫خطية‬ ‫شجرة‬ ‫ارتفاع‬ ‫هو‬ ‫ما‬n‫؟‬
■‫اإلجابة‬:n-1
■‫السؤال‬:‫تامة‬ ‫ثنائية‬ ‫شجرة‬ ‫أوراق‬ ‫عدد‬ ‫كم‬‫؟‬
■‫اإلجابة‬:2h‫حيث‬h‫الشجرة‬ ‫ارتفاع‬ ‫هو‬
■‫السؤال‬:‫؟‬ ‫تامة‬ ‫ثنائية‬ ‫لشجرة‬ ‫العقد‬ ‫عدد‬ ‫كم‬
■‫اإلجابة‬:
97
෍
𝒊=𝟎
𝒊=𝒉
𝟐𝒊
= 𝟐 𝒉+𝟏
− 𝟏
Binary Trees (‫الثنائية‬ ‫)األشجار‬
Question 2
■‫السؤال‬:‫ثنائية؟‬ ‫بحث‬ ‫شجرة‬ ‫ضمن‬ ‫للبحث‬ ‫حالة‬ ‫أسوأ‬ ‫ماهي‬
■‫اإلجابة‬:‫أعظمية‬ ‫التجول‬ ‫مسافة‬ ‫تكون‬ ‫حيث‬ ‫خطية‬ ‫الشجرة‬ ‫تكون‬ ‫عندما‬.
■‫السؤال‬:‫ثنائية؟‬ ‫بحث‬ ‫شجرة‬ ‫ضمن‬ ‫للبحث‬ ‫حالة‬ ‫أفضل‬ ‫ماهي‬
■‫اإلجابة‬:‫التجول‬ ‫مسافة‬ ‫تكون‬ ‫متوازنة‬ ‫او‬ ‫كاملة‬ ‫الشجرة‬ ‫تكون‬ ‫عندما‬‫أصغرية‬.
98
Binary Search Trees
(‫الثنائية‬ ‫البحث‬ ‫)أشجار‬
■‫التالية‬ ‫الخواص‬ ‫تحقق‬ ‫ثنائية‬ ‫شجرة‬ ‫هي‬:
–‫محددة‬ ‫عقدة‬ ‫أجل‬ ‫من‬n‫أصغر‬ ‫هي‬ ‫اليسارية‬ ‫الجزئية‬ ‫الشجرة‬ ‫في‬ ‫المخزنة‬ ‫القيم‬ ‫كل‬
‫العقدة‬ ‫في‬ ‫المخزنة‬ ‫القيمة‬ ‫من‬n.
–‫محددة‬ ‫عقدة‬ ‫أجل‬ ‫من‬n‫أكب‬ ‫هي‬ ‫اليمينية‬ ‫الجزئية‬ ‫الشجرة‬ ‫في‬ ‫المخزنة‬ ‫القيم‬ ‫كل‬‫ر‬
‫العقدة‬ ‫في‬ ‫المخزنة‬ ‫القيمة‬ ‫من‬n.
99
Binary Search Trees (‫الثنائية‬ ‫البحث‬ ‫)أشجار‬
Inserting Method
100
Binary Search Trees (‫الثنائية‬ ‫البحث‬ ‫)أشجار‬
Representation Methods
■‫الثنائية‬ ‫األشجار‬ ‫تمثيل‬ ‫طرق‬:
–‫المؤشرات‬ ‫طريق‬ ‫عن‬.
–‫أعمدتها‬ ‫وعدد‬ ‫العقد‬ ‫بعدد‬ ‫أسطرها‬ ‫عدد‬ ‫يكون‬ ‫حيث‬ ‫المصفوفات‬ ‫طريق‬ ‫عن‬3‫حيث‬
‫اال‬ ‫رقم‬ ‫الثالث‬ ‫وفي‬ ‫اليميني‬ ‫االبن‬ ‫رقم‬ ‫والثاني‬ ‫العقدة‬ ‫رقم‬ ‫األول‬ ‫العمود‬ ‫يحوي‬‫بن‬
‫اليسار‬.
101
Binary Search Trees (‫الثنائية‬ ‫البحث‬ ‫)أشجار‬
Question 3
■‫السؤال‬:‫تعتبر‬ ‫التالية‬ ‫الشجر‬ ‫من‬ ‫أي‬"‫ثنائية‬ ‫بحث‬ ‫شجرة‬"‫؟‬
■‫اإلجابة‬:b‫و‬c
102
Binary Search Trees (‫الثنائية‬ ‫البحث‬ ‫)أشجار‬
Question 4
■‫السؤال‬:‫المصفوفات؟؟‬ ‫طريقة‬ ‫سيئة‬ ‫ماهي‬
■‫اإلجابة‬:‫الكاملة‬ ‫أو‬ ‫التامة‬ ‫غير‬ ‫األشجار‬ ‫حالة‬ ‫في‬ ‫للذاكرة‬ ‫كبير‬ ‫هدر‬ ‫فيها‬.
103
Binary Search Trees (‫الثنائية‬ ‫البحث‬ ‫)أشجار‬
AVL (Height-balanced Trees)
■ An AVL tree (or height-balanced tree) is a binary search tree such that:
– The height of the left and right subtrees of the root differ by at most 1
– The left and right subtrees of the root are AVL trees
104
Binary Search Trees (‫الثنائية‬ ‫البحث‬ ‫)أشجار‬
DFS
■‫الشجرة‬ ‫في‬ ‫التجول‬ ‫طرق‬:
–‫أوال‬ ‫بالعمق‬ ‫التجول‬(depth first search)
105
33
-1
6
22
16
8
7
45
start
‫التجول‬ ‫في‬ ‫العقد‬ ‫زيارة‬ ‫ترتيب‬VLR:
22,6,-1,8,7,16,33,45
Binary Search Trees (‫الثنائية‬ ‫البحث‬ ‫)أشجار‬
DFS
■‫الشجرة‬ ‫في‬ ‫التجول‬ ‫طرق‬:‫أوال‬ ‫بالعمق‬ ‫التجول‬(depth first search)
–VLR‫الترتي‬ ‫ويدعى‬ ،‫اليمينية‬ ‫ثم‬ ‫اليسارية‬ ‫الجزئية‬ ‫الشجرة‬ ‫ثم‬ ‫العقدة‬ ‫يزور‬‫المصدر‬ ‫ب‬
(preorder.)
–VRL‫اليسارية‬ ‫ثم‬ ‫اليمينية‬ ‫الجزئية‬ ‫الشجرة‬ ‫ثم‬ ‫العقدة‬ ‫يزور‬.
–LVR‫الترتي‬ ‫ويدعى‬ ‫اليمينية‬ ‫ثم‬ ‫العقدة‬ ‫ثم‬ ‫اليسارية‬ ‫الجزئية‬ ‫الشجرة‬ ‫يزور‬‫المتناظر‬ ‫ب‬
(inorder.)
–RVL
–LRV‫الملحق‬ ‫الترتيب‬ ‫ويدعى‬(postorder.)
–RLV
106
Binary Search Trees (‫الثنائية‬ ‫البحث‬ ‫)أشجار‬
BFS
■‫الشجرة‬ ‫في‬ ‫التجول‬ ‫طرق‬:
–‫أوال‬ ‫بالعرض‬ ‫التجول‬(breadth first search.)
107
33
-1
6
22
16
8
7
45
‫التجول‬ ‫في‬ ‫العقد‬ ‫زيارة‬ ‫ترتيب‬VLR:
22,6,33,-1,8,45,7,16
GRAPHS
‫البيانات‬
108
Graphs (‫)البيانات‬
■‫كعقد‬ ‫العناصر‬ ‫تخزن‬ ‫معطيات‬ ‫بنية‬ ‫هي‬vertex‫العق‬ ‫بين‬ ‫العالقات‬ ‫تمثل‬ ‫وصالت‬ ‫بينها‬ ‫ويوجد‬‫د‬
edges.
■‫النظ‬ ‫الناحية‬ ‫من‬ ‫التسلسلية‬ ‫غير‬ ‫الهرمية‬ ‫للبنى‬ ‫تعميما‬ ‫البيانات‬ ‫اعتبار‬ ‫يمكن‬‫فاألشجار‬ ،‫رية‬
‫البيانات‬ ‫من‬ ‫خاصة‬ ‫حالة‬ ‫هي‬(‫حلقات‬ ‫تحوي‬ ‫ال‬ ‫بيانات‬.)
109
Graphs (‫)البيانات‬
■‫مكان‬ ‫كل‬ ‫في‬ ‫البيانات‬ ‫تتواجد‬:
–‫الحواسيب‬ ‫شبكات‬ ،‫الجوية‬ ‫الخطوط‬ ،‫الطرق‬ ‫شبكات‬.
■‫الصنعي‬ ‫الذكاء‬ ‫في‬ ‫المسائل‬ ‫حل‬ ‫منهجيات‬ ‫أهم‬ ‫من‬:
–‫بيان‬ ‫في‬ ‫بحث‬ ‫مسألة‬ ‫إلى‬ ‫المسألة‬ ‫تحويل‬.
110
Graphs (‫)البيانات‬
■‫العقدة‬ ‫درجة‬Deg(v)‫إليها‬ ‫والواردة‬ ‫العقدة‬ ‫من‬ ‫الصادرة‬ ‫األسهم‬ ‫عدد‬ ‫هي‬
–‫حيث‬Deg-(v)‫الواردة‬ ‫الدرجة‬ ‫هي‬.
–‫و‬Deg+(v)‫الصادرة‬ ‫الدرجة‬ ‫هي‬.
■‫عقدتين‬ ‫كل‬ ‫حالة‬ ‫في‬ ‫كان‬ ‫إذا‬ ‫بشدة‬ ‫متصل‬ ‫البيان‬ ‫يكون‬v,w‫من‬ ‫طريق‬ ‫يوجد‬v‫إلى‬w‫و‬
‫من‬w‫إلى‬v
■‫يحوي‬ ‫موجه‬ ‫غير‬ ‫بيان‬ ‫في‬ ‫األعظمي‬ ‫الروابط‬ ‫عدد‬n‫يساوي‬ ‫عقدة‬
𝑛(𝑛−1)
2
‫هذه‬ ‫وفي‬
‫تام‬ ‫إنه‬ ‫البيان‬ ‫عن‬ ‫نقول‬ ‫الحالة‬(Complete.)
111
Graphs (‫)البيانات‬
■‫التجاور‬ ‫مصفوفة‬ ‫باستخدام‬ ‫البيان‬ ‫تمثيل‬(adjacency matrix.)
112
The Graph
The Adjacency Matrix
Graphs (‫)البيانات‬
■‫التجاور‬ ‫قائمة‬ ‫باستخدام‬ ‫البيان‬ ‫تمثيل‬(adjacency list.)
113The Adjacency List
The Graph
Graphs (‫)البيانات‬
Question 1
■‫السؤال‬:‫التجاور؟‬ ‫قائمة‬ ‫بطريقة‬ ‫ممثلة‬ ‫لعقدة‬ ‫مجاورة‬ ‫عقد‬ ‫عن‬ ‫البحث‬ ‫كلفة‬ ‫ماهي‬
■‫الجواب‬:O(deg(v))
■‫السؤال‬:‫التج‬ ‫قائمة‬ ‫بطريقة‬ ‫ممثل‬ ‫بيان‬ ‫في‬ ‫لعقدة‬ ‫الواردة‬ ‫الروابط‬ ‫مسح‬ ‫كلفة‬ ‫ماهي‬‫اور؟‬
■‫الجواب‬:O(V+E)‫جدا‬ ‫مكلف‬
114
Graphs (‫)البيانات‬
■‫البيان‬ ‫في‬ ‫التجول‬ ‫طرق‬:
–‫أوال‬ ‫العمق‬(DFS.)
–‫أوال‬ ‫العرض‬(BFS:)‫الوصول‬ ‫ألن‬ ‫موزون‬ ‫غير‬ ‫بيان‬ ‫في‬ ‫طريق‬ ‫أقصر‬ ‫إليجاد‬ ‫طريقة‬ ‫هي‬
‫للروابط‬ ‫عدد‬ ‫أقل‬ ‫بمسح‬ ‫يتم‬ ‫للعقد‬.
115
Graphs (‫)البيانات‬
DFS V.S BFS
116
Graphs (‫)البيانات‬
DFS V.S BFS
117
Graphs (‫)البيانات‬
Dijkstra
■‫خوارزمية‬Dijkstra‫موزون‬ ‫بيان‬ ‫في‬ ‫طريق‬ ‫أقصر‬ ‫إليجاد‬.
119
Graphs (‫)البيانات‬
Dijkstra
120
1
6
2
5
4
3
8
9
7
4
7
12
3
4
5
11
8
6
9
2
1 2 3 4 5 6 7 8 9
0 ? ? ? ? ? ? ? ?
-1 -1 -1 -1 -1 -1 -1 -1 -1
Processed
distance
predecessor
Graphs (‫)البيانات‬
Topological Sort
■‫الفرز‬‫الطبولوجي‬‫األسهم‬ ‫كل‬ ‫تكون‬ ‫حيث‬ ‫ورودها‬ ‫درجات‬ ‫حسب‬ ‫العقد‬ ‫ترتيب‬ ‫على‬ ‫يعتمد‬‫او‬
‫اليمين‬ ‫هي‬ ‫واحدة‬ ‫جهة‬ ‫إلى‬ ‫تؤشر‬ ‫الوصالت‬.
■‫حلقات‬ ‫على‬ ‫يحتوي‬ ‫الذي‬ ‫البيان‬ ‫على‬ ‫تطبيقها‬ ‫يمكن‬ ‫ال‬.
■‫مثال‬:
–‫لجامعة‬ ‫السنوي‬ ‫المنهاج‬.
121
Graphs (‫)البيانات‬
topological sort
122
1
2
4
5 3
6
7
8
next 0
1 2 3 4 5 6 7 8
2 2 3 2 1 1 0 2
Node
Indegree
LIST
7
Graphs (‫)البيانات‬
minimum spanning tree
■‫االرتباط‬ ‫شجرة‬‫األصغرية‬:‫الروابط‬ ‫من‬ ‫ممكنة‬ ‫كلفة‬ ‫بأقل‬ ‫البيان‬ ‫تربط‬ ‫شجرة‬ ‫هي‬.
■‫فقط‬ ‫المترابط‬ ‫البيان‬ ‫حالة‬ ‫في‬ ‫الشجرة‬ ‫هذه‬ ‫تتواجد‬.
■Prim’s algorithm
123
a
c
e
d
b
2
45
9
6
4
5
5
a
c
e
d
b
2
45
9
6
4
5
5
SORTING ALGORITHMS
‫الترتيب‬ ‫خوارزميات‬
124
Bubble Sort Algorithm
( ‫الفرز‬ ‫خوارزمية‬‫الفقاعي‬ )
■‫الفرز‬ ‫خوارزمية‬ ‫تعقيد‬‫الفقاعي‬:
–‫األحوال‬ ‫أحسن‬:‫حيث‬ ‫مفروزة‬ ‫المصفوفة‬ ‫تكون‬ ‫عندما‬‫اليوجد‬‫اسناد‬ ‫عمليات‬.
–‫األحوال‬ ‫أسوأ‬:‫المصفوفة‬ ‫تكون‬ ‫عندما‬‫مرتية‬‫عمليات‬ ‫عدد‬ ‫يكون‬ ‫عكسي‬ ‫ترتيب‬
‫مرتبة‬ ‫من‬ ‫االسناد‬O(n*n).
–‫الوسطية‬ ‫الحالة‬:ً‫عشوائيا‬ ‫مرتبة‬ ‫المصفوفة‬ ‫تكون‬ ‫عندما‬O(n*n).
125
Selection Sort Algorithm
(‫باالختيار‬ ‫الفرز‬ ‫)خوارزمية‬
■‫الخوارزمية‬ ‫تعقيد‬:
–‫المقارنة‬ ‫عمليات‬ ‫عدد‬O(n*n)
–‫التبديل‬ ‫عمليات‬ ‫عدد‬O(n)
■‫مكل‬ ‫غير‬ ‫المقارنة‬ ‫وعملية‬ ‫مكلفة‬ ‫التبديل‬ ‫عملية‬ ‫كانت‬ ‫حالة‬ ‫وفي‬ ‫صغير‬ ‫لحجم‬ ‫جيدة‬‫فة‬.
126
Insertion Sort Algorithm
(‫باإلضافة‬ ‫الفرز‬ ‫)خوارزمية‬
■‫باإلضافة‬ ‫الفرز‬ ‫خوارزمية‬ ‫تعقيد‬:
–‫األحوال‬ ‫أحسن‬:‫اسناد‬ ‫عمليات‬ ‫يوجد‬ ‫ال‬ ‫حيث‬ ‫مفروزة‬ ‫المصفوفة‬ ‫تكون‬ ‫عندما‬O(1)
‫المقارنة‬ ‫وعمليات‬O(n).
–‫األحوال‬ ‫أسوأ‬:‫المصفوفة‬ ‫تكون‬ ‫عندما‬‫مرتية‬‫عمليات‬ ‫عدد‬ ‫يكون‬ ‫عكسي‬ ‫ترتيب‬
‫مرتبة‬ ‫من‬ ‫االسناد‬O(n*n).
–‫الوسطية‬ ‫الحالة‬:ً‫عشوائيا‬ ‫مرتبة‬ ‫المصفوفة‬ ‫تكون‬ ‫عندما‬O(n*n).
127
Merge Sort Algorithm
(‫بالدمج‬ ‫الفرز‬ ‫)خوارزمية‬
■‫تسد‬ ‫فرق‬ ‫مبدأ‬ ‫تعتمد‬.
■‫يلي‬ ‫بما‬ ‫تقوم‬ ‫عوديه‬ ‫خوارزمية‬ ‫هي‬:
–،‫جزئين‬ ‫إلى‬ ‫المصفوفة‬ ‫تقسم‬
–،‫حدا‬ ‫على‬ ‫جزء‬ ‫كل‬ ‫تفرز‬
–‫المفروزين‬ ‫الجزئيين‬ ‫تدمج‬
‫مفروزة‬ ‫واحدة‬ ‫مصفوفة‬ ‫في‬.
■‫الخوارزمية‬ ‫تعقيد‬:
–O(nlog(n))‫دوما‬
128
Quick Sort Algorithm
(‫السريع‬ ‫الفرز‬ ‫)خوارزمية‬
■‫الخوارزمية‬ ‫تعقيد‬:
–O(nlog(n))‫الوسطية‬ ‫والحالة‬ ‫األحوال‬ ‫أحسن‬ ‫في‬.
–O(n*n)‫األحوال‬ ‫أسوأ‬ ‫في‬.
129
Sorting Algorithms
130
SEARCH ALGORITHMS
‫في‬ ‫البحث‬ ‫خوارزميات‬‫مصفوقة‬
131
Search Algorithms
Binary V.S. Sequential
■‫التسلسلي‬ ‫البحث‬:‫تعقيده‬O(n)
■‫الثنائي‬ ‫البحث‬:‫تعقيده‬O(log(n))
132
ADVANCE ALGORITHMS
‫متقدمة‬ ‫خوارزميات‬
133
Backtracking Algorithms
(‫التراجعية‬ ‫)الخوارزميات‬
■‫خوارزميات‬ ‫هي‬‫عودية‬‫المشاكل‬ ‫حل‬ ‫في‬ ‫والخطأ‬ ‫التجريب‬ ‫طريقة‬ ‫تعتمد‬.
■‫الخطوات‬ ‫من‬ ‫مجموعة‬ ‫طريق‬ ‫عن‬ ‫للمسألة‬ ‫النهائي‬ ‫الحل‬ ‫ببناء‬ ‫الطريقة‬ ‫هذه‬ ‫تتلخص‬:
–،‫التالية‬ ‫للخطوة‬ ‫المتاحة‬ ‫اإلمكانيات‬ ‫نحدد‬ ‫الخطوة‬ ‫في‬
–،‫أحدها‬ ‫ننتقي‬ ‫بأن‬ ‫اإلمكانيات‬ ‫هذه‬ ‫ندرس‬ ‫ثم‬
–،‫النهائي‬ ‫الحل‬ ‫في‬ ‫التالية‬ ‫الخطوة‬ ‫أنها‬ ‫على‬ ‫نسجلها‬ ‫و‬
–‫الخطوة‬ ‫هذه‬ ‫على‬ ً‫اعتمادا‬ ‫الخوارزمية‬ ‫نتابع‬ ‫و‬.
–‫مسدو‬ ‫طريق‬ ‫إلى‬ ‫يؤدي‬ ‫أو‬ ،‫النهائي‬ ‫الحل‬ ‫إلى‬ ‫يقود‬ ‫ال‬ ‫اختيارنا‬ ‫أن‬ ‫لنا‬ ‫يظهر‬ ‫عندما‬،‫د‬
–‫الخطوة‬ ‫هذه‬ ‫عن‬ ‫نعدل‬.
■‫مرتين‬ ‫الخطأ‬ ‫في‬ ‫الوقوع‬ ‫تجنب‬ ‫في‬ ‫يفيد‬ ‫أخطاء‬ ‫سجل‬ ‫بناء‬ ‫العملية‬ ‫هذه‬ ‫تشبه‬.
134
Backtracking Algorithms
(‫التراجعية‬ ‫)الخوارزميات‬
■‫مثال‬:
–‫الشطرنج‬ ‫حصان‬ ‫جولة‬ ‫مسألة‬:
■‫باست‬ ‫وذلك‬ ‫الحصان‬ ‫فيه‬ ‫يقف‬ ‫معين‬ ‫موقع‬ ‫من‬ ‫الشطرنج‬ ‫رقعة‬ ‫لتغطية‬ ‫طريقة‬ ‫إيجاد‬‫خدام‬
‫واحدة‬ ‫مرة‬ ‫مربع‬ ‫بكل‬ ‫المرور‬ ‫وبشرط‬ ‫الشطرنج‬ ‫في‬ ‫المعروفة‬ ‫الحصان‬ ‫حركات‬.
–‫الثمانية‬ ‫الوزراء‬ ‫مسألة‬:
■‫لألخر‬ ‫مهدد‬ ‫واحد‬ ‫اي‬ ‫فيها‬ ‫يكون‬ ‫ال‬ ‫بحيث‬ ‫الشطرنج‬ ‫رقعة‬ ‫على‬ ‫وزراء‬ ‫ثمانية‬ ‫توزيع‬.
–‫األمثل‬ ‫الخيار‬ ‫مسألة‬:
■‫معاي‬ ‫وفق‬ ‫األمثل‬ ‫بالحل‬ ‫واالحتفاظ‬ ‫معينة‬ ‫لمسألة‬ ‫ايجادها‬ ‫يمكن‬ ‫التي‬ ‫الحلول‬ ‫مقارنة‬‫ير‬
‫للمسألة‬ ‫محددة‬.
135
Greedy Algorithms
(‫الشجعة‬ ‫)الخوارزميات‬
■‫هي‬‫خوارزمية‬‫على‬ ‫تستند‬ ‫التي‬‫المهني‬ ‫الحدس‬‫اإلمكاني‬ ‫اختيار‬ ‫طريقه‬ ‫عن‬ ‫يتم‬ ‫الذي‬‫ة‬
‫الحالية‬ ‫المرحلة‬ ‫في‬ ‫المرئية‬ ‫األفضل‬(‫الحالية‬ ‫المرحلة‬ ‫في‬ ‫األمثل‬ ‫الحل‬),‫األخ‬ ‫دون‬ ‫من‬‫ذ‬
‫الحل‬ ‫تكملة‬ ‫على‬ ‫الخطوة‬ ‫هذه‬ ‫تأثير‬ ‫بالحسبان‬.
■‫للمسألة‬ ‫األمثل‬ ‫الحل‬ ‫إلى‬ ‫المرحلي‬ ‫األمثل‬ ‫الحل‬ ‫يقودنا‬ ‫ال‬ ‫قد‬.
■‫خياراتها‬ ‫عن‬ ‫تتراجع‬ ‫ان‬ ‫يكن‬ ‫ال‬ ‫الجشع‬ ‫خوارزمية‬.‫بينها‬ ‫الرئيسي‬ ‫الفرق‬ ‫هو‬ ‫هذا‬
‫وبين‬‫الديناميكية‬ ‫البرمجة‬.
■‫وأشجار‬ ‫برايم‬ ‫خوارزمية‬ ‫مثل‬‫هوفمان‬.
136
Divide and Conquer
(‫تسد‬ ‫فرق‬ ‫)خوارزميات‬
■‫عمل‬‫خوارزمية‬‫مسألتين‬ ‫إلى‬ ‫عودي‬ ‫بشكل‬ ‫المسألة‬ ‫تقسيم‬ ‫طريق‬ ‫عن‬ ‫تسد‬ ‫فرق‬
‫فيه‬ ‫بما‬ ‫بسيطة‬ ‫الجزئية‬ ‫المسائل‬ ‫تصبح‬ ‫حتى‬ ،‫النوع‬ ‫نفس‬ ‫من‬ ‫أكثر‬ ‫أو‬ ‫جزئيتين‬‫الكفاية‬
‫مباشر‬ ‫بشكل‬ ‫لتحل‬.‫ا‬ ‫للمسألة‬ ً‫حال‬ ‫لتعطي‬ ‫الجزئية‬ ‫المسائل‬ ‫حلول‬ ‫تدمج‬ ‫ثم‬ ‫ومن‬‫لجزئية‬.
■‫امثلة‬:
–‫الثنائي‬ ‫البحث‬ ‫خوارزميات‬.
–‫بالدمج‬ ‫الفرز‬ ‫خوارزميات‬.
–‫السريع‬ ‫الفرز‬ ‫خوارزميات‬.
–‫هانوي‬ ‫ابراج‬ ‫خوارزمية‬.
137
Dynamic Programming
(‫الديناميكية‬ ‫)البرمجة‬
■‫المسألة‬ ‫من‬ ‫مختلفة‬ ‫أجزاء‬ ‫حل‬ ‫إلى‬ ‫بحاجة‬ ‫نحن‬ ،‫ما‬ ‫مسألة‬ ‫لحل‬(‫فرعية‬ ‫مسائل‬)‫ومن‬ ،
‫شامل‬ ‫حل‬ ‫على‬ ‫للحصول‬ ‫الفرعية‬ ‫المسائل‬ ‫حلول‬ ‫جمع‬ ‫ثم‬.
■‫الديناميكية‬ ‫البرمجة‬ ‫نهج‬:
–،‫فقط‬ ‫واحدة‬ ‫مرة‬ ‫فرعية‬ ‫مسألة‬ ‫كل‬ ‫حل‬ ‫عن‬ ‫البحث‬
–‫الحسابات‬ ‫عدد‬ ‫تقليل‬ ‫وبالتالي‬:‫حف‬ ‫يتم‬ ،‫ما‬ ‫فرعية‬ ‫مسألة‬ ‫حل‬ ‫حساب‬ ‫تم‬ ‫حالما‬،‫ظه‬
‫استرجاعه‬ ‫ببساطة‬ ‫يتم‬ ،‫نفسه‬ ‫للحل‬ ‫الحاجة‬ ‫عند‬ ‫القادمة‬ ‫المرة‬ ‫وفي‬.
■‫وتق‬ ‫الثانوية‬ ‫للمسائل‬ ‫السابقة‬ ‫الحلول‬ ‫ستدرس‬ ‫الديناميكية‬ ‫البرمجة‬ ‫خوارزميات‬‫بدمجها‬ ‫وم‬
‫حله‬ ‫المراد‬ ‫للمسائلة‬ ‫حل‬ ‫افضل‬ ‫علي‬ ‫للحصول‬.
138
HASHING FUNCTION
(‫التقطيع‬ ‫)جداول‬
139
Hashing Tables
(‫التقطيع‬ ‫)جداول‬
■‫موقعه‬ ‫إلى‬ ‫المباشر‬ ‫النفاذ‬ ‫خالل‬ ‫من‬ ‫بحث‬ ‫خوارزمية‬ ‫إلى‬ ‫الحاجة‬ ‫دون‬ ‫مباشرة‬ ‫عنصر‬ ‫لإليجاد‬ ‫طريقة‬ ‫هي‬.
■‫التالية‬ ‫التطبيقات‬ ‫برمجة‬ ‫في‬ ‫فعالة‬ ‫بنية‬ ‫هي‬:
–‫معجم‬ ‫عمليات‬ ‫إلى‬ ‫تحتاج‬ ‫التي‬.
–‫بالترتيب‬ ‫تهتم‬ ‫ال‬ ‫التي‬ ‫االمثلة‬.
■‫المفتاح‬ ‫ذو‬ ‫العنصر‬ ‫يخزن‬k‫الخانة‬ ‫في‬h(k)‫حيث‬h‫الخانة‬ ‫دليل‬ ‫لحساب‬ ‫يستخدم‬ ‫الذي‬ ‫التقطيع‬ ‫تابع‬ ‫هو‬
‫المفتاح‬ ‫من‬k.
■‫التقطيع‬ ‫تابع‬ ‫قيمة‬ ‫نفس‬ ‫لهما‬ ‫مفتاحين‬ ‫وجود‬ ‫حال‬ ‫في‬ ‫تصادم‬ ‫يحدث‬ ‫قد‬.
■‫من‬ ‫ازواج‬ ‫شكل‬ ‫على‬ ‫القيم‬ ‫تخزن‬(key, value)
■‫القيم‬ ‫من‬ ‫سلسلة‬ ‫حول‬ ‫للتجول‬ ‫جيدة‬ ‫بنية‬ ‫ليست‬ ‫التقطيع‬ ‫جداول‬( .Iteration)
■‫مناسبة‬ ‫فرضيات‬ ‫ضمن‬ ‫تقطيع‬ ‫جدول‬ ‫في‬ ‫عنصر‬ ‫عن‬ ‫والبحث‬ ‫إلضافة‬ ‫المتوقع‬ ‫الزمن‬O(1)( .‫الوسطية‬ ‫الحالة‬)
140
Hashing Tables V.S.
Other Data Structures
■ We want to implement the dictionary operations Insert(), Delete() and Search()/Find() efficiently.
■ Arrays:
– can accomplish in O(n) time
– but are not space efficient (assumes we leave empty space for keys not currently in
dictionary)
■ Binary search trees
– can accomplish in O(log n) time
– are space efficient.
■ Hash Tables:
– A generalization of an array that under some reasonable assumptions is O(1) for
Insert/Delete/Search of a key
141
Hashing Tables
(‫التقطيع‬ ‫)جداول‬
■‫المباشرة‬ ‫العنونة‬ ‫باستخدام‬ ‫المعلومات‬ ‫تخزين‬ ‫عن‬ ‫مثال‬:
■‫مفتاح‬ ‫لكل‬ ‫الجدول‬ ‫من‬ ‫موقع‬ ‫منح‬ ‫نستطيع‬ ‫عندما‬ ‫تستخدم‬(‫ت‬ ‫المتوقع‬ ‫العناصر‬ ‫عدد‬‫خزينها‬
‫للعناصر‬ ‫الكلي‬ ‫العدد‬ ‫من‬ ‫قريب‬.
142
Anas - Mazzah
0000001
6131467
6131468
6131469
6131470
9999997
9999998
9999999
Each phone number
uniquely determines an
array element
0000000
Record for phone
number 6131496
For technical or
commercial reasons,
certain numbers may not
be assigned to any
subscriber
Hashing Tables
(‫التقطيع‬ ‫)جداول‬
■ For example, if we hash keys 0…1000 into a hash table with 5 entries and use
h(key) = key mod 5 , we get the following sequenceof events:
143
0
1
2
3
4
key data
Insert 2
2 …
0
1
2
3
4
key data
Insert 21
2 …
21 …
0
1
2
3
4
key data
Insert 34
2 …
21 …
34 …
Insert 54
There is a
collisionat
array entry
#4
???
Hashing Tables
(‫التقطيع‬ ‫)جداول‬
■‫التصادم‬ ‫لحل‬ ‫الربط‬ ‫تقنية‬:
–‫خطية‬ ‫سلسلة‬ ‫في‬ ‫التقطيع‬ ‫تابع‬ ‫قيمة‬ ‫نفس‬ ‫لها‬ ‫التي‬ ‫العناصر‬ ‫جميع‬ ‫توضع‬.
(Hashing with Chaining)
–‫المتصادمة‬ ‫العناصر‬ ‫سلسلة‬ ‫بداية‬ ‫إلى‬ ‫مؤشر‬ ‫الجدول‬ ‫خانة‬ ‫تصبح‬.
144
Hashing Tables (‫التقطيع‬ ‫)جداول‬
Hashing with Chaining
145
0
1
2
3
4
other
key key data
Insert 54
2
21
54 34
CHAIN
0
1
2
3
4
Insert 101
2
21
54 34
101
Hashing Tables (‫التقطيع‬ ‫)جداول‬
Hashing with Chaining
■ What is the running time to insert/search/delete?
– Insert: It takes O(1) time to computethe hash function and insert at head of
linked list
– Search: It is proportional to max linked list length
– Delete: Same as search
146
Hashing Tables (‫التقطيع‬ ‫)جداول‬
Question 1
■‫السؤال‬:‫عنصر؟‬ ‫إضافة‬ ‫عملية‬ ‫كلفة‬ ‫ماهي‬
■‫الجواب‬:O(1)‫السلسلة‬ ‫رأس‬ ‫في‬ ‫المتصادمة‬ ‫للعناصر‬ ‫اإلضافة‬ ‫تتم‬ ‫حيث‬.
■‫السؤال‬:‫األحوال؟‬ ‫أسوأ‬ ‫في‬ ‫والحذف‬ ‫البحث‬ ‫عملية‬ ‫كلفة‬ ‫ماهي‬
■‫الجواب‬:‫االحوال‬ ‫أسوأ‬ ‫في‬ ‫وهي‬ ‫المتصادمة‬ ‫للعناصر‬ ‫الخطية‬ ‫السلسلة‬ ‫بطول‬ ‫تتعلق‬O(n)‫عند‬
‫سيء‬ ‫تقطيع‬ ‫تابع‬ ‫اختيار‬.
■‫السؤال‬:‫واالضافة؟‬ ‫للحذف‬ ‫الوسطية‬ ‫الكلفة‬ ‫ماهي‬
■‫الجواب‬:‫م‬ ‫توزع‬ ‫يحقق‬ ‫تقطيع‬ ‫تابع‬ ‫استخدام‬ ‫عند‬ ‫السلسلة‬ ‫في‬ ‫الوسطي‬ ‫العناصر‬ ‫عدد‬ ‫هو‬‫نتظم‬
‫وهو‬n/m‫حيث‬n‫و‬ ‫الكلي‬ ‫العناصر‬ ‫عدد‬m‫الجدول‬ ‫في‬ ‫الخانات‬ ‫عدد‬.
147
Hashing Function (‫التقطيع‬ ‫)توابع‬
■‫التقسيم‬ ‫طريقة‬:
–‫المفتاح‬ ‫بربط‬k‫قسمة‬ ‫باقي‬ ‫تساوي‬ ‫التي‬ ‫بالخانة‬k‫على‬m.
–h(k)=k%m
–‫قيم‬ ‫تجنب‬ ‫يجب‬m‫قوى‬ ‫من‬2‫عنها‬ ‫بعيدة‬ ‫أولية‬ ‫أعداد‬ ‫واختيار‬.
■‫الضرب‬ ‫طريقة‬:
–‫المفتاح‬ ‫نضرب‬k‫بثابت‬A‫حيث‬0<A<1‫القيمة‬ ‫هذه‬ ‫نضرب‬ ‫ثم‬ ‫الكسري‬ ‫الجزء‬ ‫ونأخذ‬
‫ب‬m‫الناتج‬ ‫العدد‬ ‫من‬ ‫الطبيعي‬ ‫الجزء‬ ‫ونأخذ‬.
–‫للعدد‬ ‫قيمة‬ ‫أي‬ ‫اختيار‬ ‫يمكن‬m.
148
MCQ
152
Question 1
■ For a binary search algorithm to work, it is necessarythat the array (list) must be?
A. sorted
B. unsorted
C. in a heap
D. popped out of stack
153
Question 1
■ For a binary search algorithm to work, it is necessarythat the array (list) must be?
A. sorted
B. unsorted
C. in a heap
D. popped out of stack
154
Question 2
■ Which one of the below mentioned is linear data structure?
A. Queue
B. Stack
C. Arrays
D. All of the above
155
Question 2
■ Which one of the below mentioned is linear data structure?
A. Queue
B. Stack
C. Arrays
D. All of the above
156
Question 3
■ Binary search tree has best case run-time complexity of Ο(log n). What could the
worst case?
A. Ο(n)
B. Ο(n2)
C. Ο(n3)
D. None of the above
157
Question 3
■ Binary search tree has best case run-time complexity of Ο(log n). What could the
worst case?
A. Ο(n)
B. Ο(n2)
C. Ο(n3)
D. None of the above
158
Question 4
■ An algorithm is?
A. a piece of code to be executed.
B. a loosely written code to make final code.
C. a step by step procedureto solve problem.
D. all of the above.
159
Question 4
■ An algorithm is?
A. a piece of code to be executed.
B. a loosely written code to make final code.
C. a step by step procedureto solve problem.
D. all of the above.
160
Question 5
■ Quick sort running time depends on the selection of?
A. size of array
B. pivot element
C. sequenceof values
D. none of the above!
161
Question 5
■ Quick sort running time depends on the selection of?
A. size of array
B. pivot element
C. sequenceof values
D. none of the above!
162
Question 6
■ A queue data-structurecan be used for?
A. expression parsing
B. recursion
C. resourceallocation
D. all of the above
163
Question 6
■ A queue data-structurecan be used for?
A. expression parsing
B. recursion
C. resourceallocation
D. all of the above
164
Question 7
■ Program with highest run-time complexity is?
A. Tower of Hanoi
B. FibonacciSeries
C. Prime Number Series
D. None of the above
165
Question 7
■ Program with highest run-time complexity is?
A. Tower of Hanoi
B. FibonacciSeries
C. Prime Number Series
D. None of the above
166
Question 8
■ From a complete graph, by removing maximum _______________ edges, we can
constructa spanning tree?
A. e-n+1
B. n-e+1
C. n+e-1
D. e-n-1
167
Question 8
■ From a complete graph, by removing maximum _______________ edges, we can
constructa spanning tree?
A. e-n+1
B. n-e+1
C. n+e-1
D. e-n-1
168
Question 9
■ The worst case complexity of binary search matches with?
A. interpolation search
B. linear search
C. merge sort
D. none of the above
169
Question 9
■ The worst case complexity of binary search matches with?
A. interpolation search
B. linear search
C. merge sort
D. none of the above
170
Question 10
■ What is a hash table?
A. A structurethat maps values to keys.
B. A structurethat maps keys to values.
C. A structureused for storage.
D. A structureused to implement stack and queue.
171
Question 10
■ What is a hash table?
A. A structurethat maps values to keys.
B. A structurethat maps keys to values.
C. A structureused for storage.
D. A structureused to implement stack and queue.
172
Question 11
■ The recurrencerelation capturing the optimal time of the Tower of Hanoi problem
with n discs is
A. T(n) = 2T(n – 2) + 2
B. T(n) = 2T(n – 1) + n
C. T(n) = 2T(n/2) + 1
D. T(n) = 2T(n – 1) + 1
173
Question 11
■ The recurrencerelation capturing the optimal time of the Tower of Hanoi problem
with n discs is
A. T(n) = 2T(n – 2) + 2
B. T(n) = 2T(n – 1) + n
C. T(n) = 2T(n/2) + 1
D. T(n) = 2T(n – 1) + 1
174
Question 12
■ What does the following function do for a given Linked List with first node as head?
void fun1(structnode* head) {
if(head == NULL)
return;
fun1(head->next);
printf("%d ", head->data); }
A. Prints all nodes of linked lists
B. Prints all nodes of linked list in reverseorder
C. Prints alternate nodes of Linked List
D. Prints alternate nodes in reverseorder
175
Question 12
■ What does the following function do for a given Linked List with first node as head?
void fun1(structnode* head) {
if(head == NULL)
return;
fun1(head->next);
printf("%d ", head->data); }
A. Prints all nodes of linked lists
B. Prints all nodes of linked list in reverseorder
C. Prints alternate nodes of Linked List
D. Prints alternate nodes in reverseorder
176
Question 13
■ Which of the following points is/are true about Linked List data structure when it is
compared with array
A. Arrays have better cache locality that can make them better in terms of
performance
B. It is easy to insert and delete elements in Linked List
C. Randomaccess is not allowed in a typical implementation of Linked Lists
D. All of the above
177
Question 13
■ Which of the following points is/are true about Linked List data structure when it is
compared with array
A. Arrays have better cache locality that can make them better in terms of
performance
B. It is easy to insert and delete elements in Linked List
C. Randomaccess is not allowed in a typical implementation of Linked Lists
D. All of the above
178
Question 14
■ Which of the following sorting algorithms can be used to sort a random linked list
with minimum time complexity?
A. Insertion Sort
B. Quick Sort
C. Heap Sort
D. Merge Sort
179
Question 14
■ Which of the following sorting algorithms can be used to sort a random linked list
with minimum time complexity?
A. Insertion Sort
B. Quick Sort
C. Heap Sort
D. Merge Sort
180
Question 15
■ In the worst case, the number of comparisons needed to search a singly linked list
of length n for a given element is
A. log 2 n
B. n/2
C. log 2 n – 1
D. n
181
Question 15
■ In the worst case, the number of comparisons needed to search a singly linked list
of length n for a given element is
A. log 2 n
B. n/2
C. log 2 n – 1
D. n
182
Question 16
■ Which one of the following is an application of Stack Data Structure?
A. Managing function calls
B. The stock span problem
C. Arithmetic expression evaluation
D. All of the above
183
Question 16
■ Which one of the following is an application of Stack Data Structure?
A. Managing function calls
B. The stock span problem
C. Arithmetic expression evaluation
D. All of the above
184
Question 17
■ The result evaluating the postfix expression 10 5 + 60 6 / * 8 – is
A. 284
B. 213
C. 142
D. 71
185
Question 17
■ The result evaluating the postfix expression 10 5 + 60 6 / * 8 – is
A. 284
B. 213
C. 142
D. 71
186
Question 18
■ How many stacks are needed to implement a queue. Consider the situation where
no other data structurelike arrays, linked list is available to you.
A. 1
B. 2
C. 3
D. 4
187
Question 18
■ How many stacks are needed to implement a queue. Consider the situation where
no other data structurelike arrays, linked list is available to you.
A. 1
B. 2
C. 3
D. 4
188
Question 19
■ The most appropriate matching for the following pairs
X: depth first search 1: heap
Y: breadth-firstsearch 2: queue
Z: sorting 3: stack
A. X—1, Y—2 ,Z –3
B. X—3, Y—1, Z –2
C. X—3, Y—2, Z—1
D. X—2 ,Y—3 , Z--1
189
Question 19
■ The most appropriate matching for the following pairs
X: depth first search 1: heap
Y: breadth-firstsearch 2: queue
Z: sorting 3: stack
A. X—1, Y—2 ,Z –3
B. X—3, Y—1, Z –2
C. X—3, Y—2, Z—1
D. X—2 ,Y—3 , Z--1
190
Question 20
■ Aliasing in the context of programming languages refers to
A. multiple variables having the same memory location
B. multiple variables having the same value
C. multiple variables having the same identifier
D. multiple uses of the same variable
191
Question 20
■ Aliasing in the context of programming languages refers to
A. multiple variables having the same memory location
B. multiple variables having the same value
C. multiple variables having the same identifier
D. multiple uses of the same variable
192
Question 21
■ Randomized quicksortis an extension of quicksortwhere the pivot is chosen
randomly. What is the worst case complexity of sorting n numbers using randomized
quicksort?
A. O(n)
B. O(n Log n)
C. O(n^2)
D. O(n!)
193
Question 21
■ Randomized quicksortis an extension of quicksortwhere the pivot is chosen
randomly. What is the worst case complexity of sorting n numbers using randomized
quicksort?
A. O(n)
B. O(n Log n)
C. O(n^2)
D. O(n!)
194
Question 22
■ The number of leaf nodes in a rooted tree of n nodes, with each node having 0 or 3
children is:
A. n/2
B. (n - 1)/3
C. (n - 1)/2
D. (2n + 1)/3
195
Question 22
■ The number of leaf nodes in a rooted tree of n nodes, with each node having 0 or 3
children is:
A. n/2
B. (n - 1)/3
C. (n - 1)/2
D. (2n + 1)/3
196
Question 23
■ Suppose the numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are inserted in that order into an
initially empty binary search tree. The binary search tree uses the usual ordering on
natural numbers. What is the in-order traversal sequenceof the resultant tree ?
A. 7 5 1 0 3 2 4 6 8 9
B. 0 2 4 3 1 6 5 9 8 7
C. 0 1 2 3 4 5 6 7 8 9
D. 9 8 6 4 2 3 0 1 5 7
197
Question 23
■ Suppose the numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are inserted in that order into an
initially empty binary search tree. The binary search tree uses the usual ordering on
natural numbers. What is the in-order traversal sequenceof the resultant tree ?
A. 7 5 1 0 3 2 4 6 8 9
B. 0 2 4 3 1 6 5 9 8 7
C. 0 1 2 3 4 5 6 7 8 9
D. 9 8 6 4 2 3 0 1 5 7
198
Question 24
■ Consider the following graph. Among the following sequences, Which are depth first
traversals of the graph?
I) a b e g h f
II) a b f e h g
III) a b f h g e
IV) a f g h b e
A. I, II and IV only
B. I and IV only
C. II, III and IV only
D. I, III and IV only
199
Question 24
■ Consider the following graph. Among the following sequences, Which are depth first
traversals of the graph?
I) a b e g h f
II) a b f e h g
III) a b f h g e
IV) a f g h b e
A. I, II and IV only
B. I and IV only
C. II, III and IV only
D. I, III and IV only
200
Question 25
■ What is the weight of a minimum spanning tree of the following graph ?
A. 29
B. 31
C. 38
D. 41
201
Question 25
■ What is the weight of a minimum spanning tree of the following graph ?
A. 29
B. 31
C. 38
D. 41
202
Question 26
■ The best data structureto check whether an arithmetic expression has balanced
parentheses is a
A. Queue
B. Stack
C. Tree
D. List
203
Question 26
■ The best data structureto check whether an arithmetic expression has balanced
parentheses is a
A. Queue
B. Stack
C. Tree
D. List
204
Question 27
■ The elements 32, 15, 20, 30, 12, 25, 16 are inserted one by one in the given order
into a Max Heap. The resultant Max Heap is.
A. A
B. B
C. C
D. D
205
Question 27
■ The elements 32, 15, 20, 30, 12, 25, 16 are inserted one by one in the given order
into a Max Heap. The resultant Max Heap is.
A. A
B. B
C. C
D. D
206
Question 28
■ The time complexity of the following C function is (assume n > 0)
int recursive (int n) {
if (n == 1)
return (1);
else
return (recursive (n - 1) + recursive (n - 1)); }
A. O(n)
B. O(n log n)
C. O(n^2)
D. O(2^n)
207
Question 28
■ The time complexity of the following C function is (assume n > 0)
int recursive (int n) {
if (n == 1)
return (1);
else
return (recursive (n - 1) + recursive (n - 1)); }
A. O(n)
B. O(n log n)
C. O(n^2)
D. O(2^n)
208
Question 29
■ A program takes as input a balanced binary search tree with n leaf nodes and
computes the value of a function g(x) for each node x. If the cost of computing g(x) is
min{no. of leaf-nodes in left-subtreeof x, no. of leaf-nodes in right-subtreeof x} then
the worst-casetime complexity of the program is
A. Θ(n)
B. Θ(n Log n)
C. Θ(n^2)
D. Θ(n^2 log n)
209
Question 29
■ A program takes as input a balanced binary search tree with n leaf nodes and
computes the value of a function g(x) for each node x. If the cost of computing g(x) is
min{no. of leaf-nodes in left-subtreeof x, no. of leaf-nodes in right-subtreeof x} then
the worst-casetime complexity of the program is
A. Θ(n)
B. Θ(n Log n)
C. Θ(n^2)
D. Θ(n^2 log n)
210
CONTACT INFO

Contenu connexe

Tendances

Advanced Algorithms: Transform and conquer
Advanced Algorithms: Transform and conquerAdvanced Algorithms: Transform and conquer
Advanced Algorithms: Transform and conquerAbdulrazak Zakieh
 
Connectionist Temporal Classification
Connectionist Temporal ClassificationConnectionist Temporal Classification
Connectionist Temporal ClassificationJulius Hietala
 
PR-409: Denoising Diffusion Probabilistic Models
PR-409: Denoising Diffusion Probabilistic ModelsPR-409: Denoising Diffusion Probabilistic Models
PR-409: Denoising Diffusion Probabilistic ModelsHyeongmin Lee
 
Data-intensive bioinformatics on HPC and Cloud
Data-intensive bioinformatics on HPC and CloudData-intensive bioinformatics on HPC and Cloud
Data-intensive bioinformatics on HPC and CloudOla Spjuth
 
Generative adversarial network and its applications to speech signal and natu...
Generative adversarial network and its applications to speech signal and natu...Generative adversarial network and its applications to speech signal and natu...
Generative adversarial network and its applications to speech signal and natu...宏毅 李
 
Recent Trends in Deep Learning
Recent Trends in Deep LearningRecent Trends in Deep Learning
Recent Trends in Deep LearningSungjoon Choi
 
[PR12] PR-050: Convolutional LSTM Network: A Machine Learning Approach for Pr...
[PR12] PR-050: Convolutional LSTM Network: A Machine Learning Approach for Pr...[PR12] PR-050: Convolutional LSTM Network: A Machine Learning Approach for Pr...
[PR12] PR-050: Convolutional LSTM Network: A Machine Learning Approach for Pr...Taegyun Jeon
 
DiffuseMorph: Unsupervised Deformable Image Registration Using Diffusion Model
DiffuseMorph: Unsupervised Deformable Image Registration Using Diffusion ModelDiffuseMorph: Unsupervised Deformable Image Registration Using Diffusion Model
DiffuseMorph: Unsupervised Deformable Image Registration Using Diffusion ModelBoahKim2
 
Automata
AutomataAutomata
AutomataGaditek
 
Chapter01 introduction to Operating systems
Chapter01 introduction to Operating systems Chapter01 introduction to Operating systems
Chapter01 introduction to Operating systems amernassiri
 
Moore and mealy machine
Moore and mealy machineMoore and mealy machine
Moore and mealy machineMian Munib
 
Bài giảng logic bậc nhất first order logic
Bài giảng logic bậc nhất   first order logicBài giảng logic bậc nhất   first order logic
Bài giảng logic bậc nhất first order logicjackjohn45
 
Introduction to object detection
Introduction to object detectionIntroduction to object detection
Introduction to object detectionBrodmann17
 
The n Queen Problem
The n Queen ProblemThe n Queen Problem
The n Queen ProblemSukrit Gupta
 
Beginner's Guide to Diffusion Models..pptx
Beginner's Guide to Diffusion Models..pptxBeginner's Guide to Diffusion Models..pptx
Beginner's Guide to Diffusion Models..pptxIshaq Khan
 
Lecture Notes-Finite State Automata for NLP.pdf
Lecture Notes-Finite State Automata for NLP.pdfLecture Notes-Finite State Automata for NLP.pdf
Lecture Notes-Finite State Automata for NLP.pdfDeptii Chaudhari
 

Tendances (20)

Advanced Algorithms: Transform and conquer
Advanced Algorithms: Transform and conquerAdvanced Algorithms: Transform and conquer
Advanced Algorithms: Transform and conquer
 
Connectionist Temporal Classification
Connectionist Temporal ClassificationConnectionist Temporal Classification
Connectionist Temporal Classification
 
PR-409: Denoising Diffusion Probabilistic Models
PR-409: Denoising Diffusion Probabilistic ModelsPR-409: Denoising Diffusion Probabilistic Models
PR-409: Denoising Diffusion Probabilistic Models
 
Galios: Python Programming
Galios: Python Programming Galios: Python Programming
Galios: Python Programming
 
Data-intensive bioinformatics on HPC and Cloud
Data-intensive bioinformatics on HPC and CloudData-intensive bioinformatics on HPC and Cloud
Data-intensive bioinformatics on HPC and Cloud
 
Generative adversarial network and its applications to speech signal and natu...
Generative adversarial network and its applications to speech signal and natu...Generative adversarial network and its applications to speech signal and natu...
Generative adversarial network and its applications to speech signal and natu...
 
Recent Trends in Deep Learning
Recent Trends in Deep LearningRecent Trends in Deep Learning
Recent Trends in Deep Learning
 
[PR12] PR-050: Convolutional LSTM Network: A Machine Learning Approach for Pr...
[PR12] PR-050: Convolutional LSTM Network: A Machine Learning Approach for Pr...[PR12] PR-050: Convolutional LSTM Network: A Machine Learning Approach for Pr...
[PR12] PR-050: Convolutional LSTM Network: A Machine Learning Approach for Pr...
 
DiffuseMorph: Unsupervised Deformable Image Registration Using Diffusion Model
DiffuseMorph: Unsupervised Deformable Image Registration Using Diffusion ModelDiffuseMorph: Unsupervised Deformable Image Registration Using Diffusion Model
DiffuseMorph: Unsupervised Deformable Image Registration Using Diffusion Model
 
Automata
AutomataAutomata
Automata
 
Chapter01 introduction to Operating systems
Chapter01 introduction to Operating systems Chapter01 introduction to Operating systems
Chapter01 introduction to Operating systems
 
Question answering
Question answeringQuestion answering
Question answering
 
Lecture3 - Machine Learning
Lecture3 - Machine LearningLecture3 - Machine Learning
Lecture3 - Machine Learning
 
03 Single layer Perception Classifier
03 Single layer Perception Classifier03 Single layer Perception Classifier
03 Single layer Perception Classifier
 
Moore and mealy machine
Moore and mealy machineMoore and mealy machine
Moore and mealy machine
 
Bài giảng logic bậc nhất first order logic
Bài giảng logic bậc nhất   first order logicBài giảng logic bậc nhất   first order logic
Bài giảng logic bậc nhất first order logic
 
Introduction to object detection
Introduction to object detectionIntroduction to object detection
Introduction to object detection
 
The n Queen Problem
The n Queen ProblemThe n Queen Problem
The n Queen Problem
 
Beginner's Guide to Diffusion Models..pptx
Beginner's Guide to Diffusion Models..pptxBeginner's Guide to Diffusion Models..pptx
Beginner's Guide to Diffusion Models..pptx
 
Lecture Notes-Finite State Automata for NLP.pdf
Lecture Notes-Finite State Automata for NLP.pdfLecture Notes-Finite State Automata for NLP.pdf
Lecture Notes-Finite State Automata for NLP.pdf
 

Similaire à Algorithms and Data Structure 2018

Algorithms and Data Structure 2020
Algorithms and Data Structure 2020Algorithms and Data Structure 2020
Algorithms and Data Structure 2020Joud Khattab
 
مشروع تصميم امتحان القبول بقسم الحاسب الآلي بكلية المعلمين
مشروع تصميم امتحان القبول بقسم الحاسب الآلي بكلية المعلمينمشروع تصميم امتحان القبول بقسم الحاسب الآلي بكلية المعلمين
مشروع تصميم امتحان القبول بقسم الحاسب الآلي بكلية المعلمينemy2000_mhe
 
الاتصال بقواعد البيانات في الفيجوال بيسك دوت نت - مفاهيم أساسية
الاتصال بقواعد البيانات في الفيجوال بيسك دوت نت - مفاهيم أساسيةالاتصال بقواعد البيانات في الفيجوال بيسك دوت نت - مفاهيم أساسية
الاتصال بقواعد البيانات في الفيجوال بيسك دوت نت - مفاهيم أساسيةHemat Abo Hamer
 
Graduation project documentation
Graduation project documentationGraduation project documentation
Graduation project documentationTareq Mulla
 
حاسب الى الصف الف الثالث الاعدادى الفصل الدراسيى الاول
حاسب الى الصف الف الثالث الاعدادى الفصل الدراسيى الاول حاسب الى الصف الف الثالث الاعدادى الفصل الدراسيى الاول
حاسب الى الصف الف الثالث الاعدادى الفصل الدراسيى الاول ehabhelp
 
Second session software engineering algorithms
Second session software engineering   algorithmsSecond session software engineering   algorithms
Second session software engineering algorithmsAliMohammad155
 
العرض التقديمي.pptx
العرض التقديمي.pptxالعرض التقديمي.pptx
العرض التقديمي.pptxssuser4d2acd
 
Software Engineering 2020
Software Engineering 2020Software Engineering 2020
Software Engineering 2020Joud Khattab
 
تحضير طرق ربط الأجهزة في الشبكة المحلية+الشبكات اللاسلكية
تحضير طرق ربط الأجهزة في الشبكة المحلية+الشبكات اللاسلكيةتحضير طرق ربط الأجهزة في الشبكة المحلية+الشبكات اللاسلكية
تحضير طرق ربط الأجهزة في الشبكة المحلية+الشبكات اللاسلكيةNoor Mohammed
 

Similaire à Algorithms and Data Structure 2018 (16)

Algorithms and Data Structure 2020
Algorithms and Data Structure 2020Algorithms and Data Structure 2020
Algorithms and Data Structure 2020
 
Sd18 areejshater
Sd18 areejshaterSd18 areejshater
Sd18 areejshater
 
مشروع تصميم امتحان القبول بقسم الحاسب الآلي بكلية المعلمين
مشروع تصميم امتحان القبول بقسم الحاسب الآلي بكلية المعلمينمشروع تصميم امتحان القبول بقسم الحاسب الآلي بكلية المعلمين
مشروع تصميم امتحان القبول بقسم الحاسب الآلي بكلية المعلمين
 
Sd2 areej
Sd2 areejSd2 areej
Sd2 areej
 
الاتصال بقواعد البيانات في الفيجوال بيسك دوت نت - مفاهيم أساسية
الاتصال بقواعد البيانات في الفيجوال بيسك دوت نت - مفاهيم أساسيةالاتصال بقواعد البيانات في الفيجوال بيسك دوت نت - مفاهيم أساسية
الاتصال بقواعد البيانات في الفيجوال بيسك دوت نت - مفاهيم أساسية
 
Graduation project documentation
Graduation project documentationGraduation project documentation
Graduation project documentation
 
تقديمي للمعلم
تقديمي للمعلمتقديمي للمعلم
تقديمي للمعلم
 
Ccna
CcnaCcna
Ccna
 
حاسب الى الصف الف الثالث الاعدادى الفصل الدراسيى الاول
حاسب الى الصف الف الثالث الاعدادى الفصل الدراسيى الاول حاسب الى الصف الف الثالث الاعدادى الفصل الدراسيى الاول
حاسب الى الصف الف الثالث الاعدادى الفصل الدراسيى الاول
 
Second session software engineering algorithms
Second session software engineering   algorithmsSecond session software engineering   algorithms
Second session software engineering algorithms
 
الحل
الحلالحل
الحل
 
العرض التقديمي.pptx
العرض التقديمي.pptxالعرض التقديمي.pptx
العرض التقديمي.pptx
 
computer exam2
computer exam2computer exam2
computer exam2
 
Software Engineering 2020
Software Engineering 2020Software Engineering 2020
Software Engineering 2020
 
تحضير طرق ربط الأجهزة في الشبكة المحلية+الشبكات اللاسلكية
تحضير طرق ربط الأجهزة في الشبكة المحلية+الشبكات اللاسلكيةتحضير طرق ربط الأجهزة في الشبكة المحلية+الشبكات اللاسلكية
تحضير طرق ربط الأجهزة في الشبكة المحلية+الشبكات اللاسلكية
 
مصفوفه
مصفوفهمصفوفه
مصفوفه
 

Plus de Joud Khattab

Customer Engagement Management
Customer Engagement ManagementCustomer Engagement Management
Customer Engagement ManagementJoud Khattab
 
Design thinking and Role Playing
Design thinking and Role PlayingDesign thinking and Role Playing
Design thinking and Role PlayingJoud Khattab
 
Artificial Intelligence 2020
Artificial Intelligence 2020Artificial Intelligence 2020
Artificial Intelligence 2020Joud Khattab
 
Automata and Compiler 2020
Automata and Compiler 2020Automata and Compiler 2020
Automata and Compiler 2020Joud Khattab
 
Software Engineering 2018
Software Engineering 2018Software Engineering 2018
Software Engineering 2018Joud Khattab
 
Automate and Compiler 2018
Automate and Compiler 2018Automate and Compiler 2018
Automate and Compiler 2018Joud Khattab
 
Artificial Intelligence 2018
Artificial Intelligence 2018Artificial Intelligence 2018
Artificial Intelligence 2018Joud Khattab
 
Geospatial Information Management
Geospatial Information ManagementGeospatial Information Management
Geospatial Information ManagementJoud Khattab
 
Big Data for Development
Big Data for DevelopmentBig Data for Development
Big Data for DevelopmentJoud Khattab
 
Personality Detection via MBTI Test
Personality Detection via MBTI TestPersonality Detection via MBTI Test
Personality Detection via MBTI TestJoud Khattab
 
Optimization Techniques
Optimization TechniquesOptimization Techniques
Optimization TechniquesJoud Khattab
 
Network Address Translation (NAT)
Network Address Translation (NAT)Network Address Translation (NAT)
Network Address Translation (NAT)Joud Khattab
 
From Image Processing To Computer Vision
From Image Processing To Computer VisionFrom Image Processing To Computer Vision
From Image Processing To Computer VisionJoud Khattab
 
Social Networks Analysis
Social Networks AnalysisSocial Networks Analysis
Social Networks AnalysisJoud Khattab
 

Plus de Joud Khattab (20)

Customer Engagement Management
Customer Engagement ManagementCustomer Engagement Management
Customer Engagement Management
 
Design thinking and Role Playing
Design thinking and Role PlayingDesign thinking and Role Playing
Design thinking and Role Playing
 
Artificial Intelligence 2020
Artificial Intelligence 2020Artificial Intelligence 2020
Artificial Intelligence 2020
 
Automata and Compiler 2020
Automata and Compiler 2020Automata and Compiler 2020
Automata and Compiler 2020
 
Database 2020
Database 2020Database 2020
Database 2020
 
Software Engineering 2018
Software Engineering 2018Software Engineering 2018
Software Engineering 2018
 
Database 2018
Database 2018Database 2018
Database 2018
 
Automate and Compiler 2018
Automate and Compiler 2018Automate and Compiler 2018
Automate and Compiler 2018
 
Artificial Intelligence 2018
Artificial Intelligence 2018Artificial Intelligence 2018
Artificial Intelligence 2018
 
Data Storytelling
Data StorytellingData Storytelling
Data Storytelling
 
Geospatial Information Management
Geospatial Information ManagementGeospatial Information Management
Geospatial Information Management
 
Big Data for Development
Big Data for DevelopmentBig Data for Development
Big Data for Development
 
Personality Detection via MBTI Test
Personality Detection via MBTI TestPersonality Detection via MBTI Test
Personality Detection via MBTI Test
 
Fog Computing
Fog ComputingFog Computing
Fog Computing
 
Seasonal ARIMA
Seasonal ARIMASeasonal ARIMA
Seasonal ARIMA
 
Optimization Techniques
Optimization TechniquesOptimization Techniques
Optimization Techniques
 
Network Address Translation (NAT)
Network Address Translation (NAT)Network Address Translation (NAT)
Network Address Translation (NAT)
 
From Image Processing To Computer Vision
From Image Processing To Computer VisionFrom Image Processing To Computer Vision
From Image Processing To Computer Vision
 
Spark SQL
Spark SQLSpark SQL
Spark SQL
 
Social Networks Analysis
Social Networks AnalysisSocial Networks Analysis
Social Networks Analysis
 

Algorithms and Data Structure 2018

  • 1. ALGORITHMS AND DATA STRUCTURE ‫المعطيات‬ ‫وبنى‬ ‫الخوارزميات‬ ‫محور‬ By Eng. Joud Khattab
  • 2. Content ■ OOP. (‫التوجه‬ ‫غرضية‬ ‫)البرمجة‬ ■ Complexity. (‫)التعقيد‬ ■ Recursive Functions. ( ‫التوابع‬‫العودية‬ ) ■ Data Structure: (‫المعطيات‬ ‫)بنى‬ – Sequential: Linked List, Stacks, Queue. – Non Sequential: Tree, Graph. ■ Sort & Search Algorithms. (‫والترتيب‬ ‫البحث‬ ‫)خوارزميات‬ ■ Advance Algorithms Methods (‫متقدمة‬ ‫)خوارزميات‬ – Greedy, Backtracking, Dynamic, Divide & Conquer. ■ Hashing Function. (‫التقطيع‬ ‫)توابع‬ ■ MCQ. (‫)اختبارات‬ 2
  • 3. Definitions (‫)تعاريف‬ ■‫مسألة‬ ‫حل‬ ‫خوارزمية‬:‫م‬ ‫منتهية‬ ‫متتالية‬ ‫شكل‬ ‫على‬ ‫الحل‬ ‫لطريقة‬ ‫صوري‬ ‫توصيف‬ ‫هي‬‫ن‬ ‫محدد‬ ‫تسلسل‬ ‫حسب‬ ‫تنفذ‬ ،‫البسيطة‬ ‫العمليات‬. ■‫البرنامج‬:‫يقبله‬ ‫التي‬ ‫البرمجة‬ ‫لغات‬ ‫بإحدى‬ ‫معينة‬ ‫مسألة‬ ‫حل‬ ‫لخوارزمية‬ ‫توصيف‬ ‫هو‬‫ا‬ ‫الحاسوب‬. ■‫البرمجة‬ ‫لغة‬:‫بكتاب‬ ‫تسمح‬ ‫التي‬ ‫المعرفة‬ ‫والدالالت‬ ‫والقواعد‬ ‫المفردات‬ ‫من‬ ‫مجموعة‬ ‫هي‬‫ة‬ ‫الحاسوب‬ ‫على‬ ‫تنفيذه‬ ‫يمكن‬ ‫برنامج‬. ■‫المترجم‬:‫برنامج‬ ‫إلى‬ ‫ويحوله‬ ،‫معينة‬ ‫برمجة‬ ‫بلغة‬ ‫المكتوب‬ ‫البرنامج‬ ‫يفهم‬ ‫برنامج‬ ‫هو‬‫مكافئ‬ ‫بالمعالج‬ ‫الخاصة‬ ‫المجمع‬ ‫بلغة‬ ‫مكتوب‬‫الصغري‬‫للحاسوب‬. 3
  • 4. OBJECT ORIENTED PROGRAMMING (OOP) ‫التوجه‬ ‫غرضية‬ ‫البرمجة‬ 4
  • 5. Question 1 ■ Pure OOP can be implemented without using class in a program. A. True B. False 5
  • 6. Question 1 ■ Pure OOP can be implemented without using class in a program. A. True B. False 6
  • 7. Question 2 ■ What is the additional feature in classes that was not in struct? A. Data members B. Member functions C. Static data allowed D. Public access specifier 7
  • 8. Question 2 ■ What is the additional feature in classes that was not in struct? A. Data members B. Member functions C. Static data allowed D. Public access specifier 8
  • 9. Question 3 ■ Which of the following best defines a class? A. Parent of an object B. Instanceof an object C. Blueprint of an object D. Scope of an object 9
  • 10. Question 3 ■ Which of the following best defines a class? A. Parent of an object B. Instanceof an object C. Blueprint of an object D. Scope of an object 10
  • 11. Question 4 ■ Which Feature of OOP illustrated the code reusability? A. Polymorphism B. Abstraction C. Encapsulation D. Inheritance 11
  • 12. Question 4 ■ Which Feature of OOP illustrated the code reusability? A. Polymorphism B. Abstraction C. Encapsulation D. Inheritance 12
  • 13. Question 6 ■ Which among the following best describes encapsulation? A. It is a way of combiningvarious data members into a single unit B. It is a way of combiningvarious member functions into a single unit C. It is a way of combiningvarious data members and member functions into a single unit which can operate on any data D. It is a way of combiningvarious data members and member functions that operate on those data members into a single unit 13
  • 14. Question 6 ■ Which among the following best describes encapsulation? A. It is a way of combiningvarious data members into a single unit B. It is a way of combiningvarious member functions into a single unit C. It is a way of combiningvarious data members and member functions into a single unit which can operate on any data D. It is a way of combiningvarious data members and member functions that operate on those data members into a single unit 14
  • 15. Question 7 ■ Which of the two features match each other? A. Inheritanceand Encapsulation B. Encapsulation and Polymorphism C. Encapsulation and Abstraction D. Abstraction and Polymorphism 15
  • 16. Question 7 ■ Which of the two features match each other? A. Inheritanceand Encapsulation B. Encapsulation and Polymorphism C. Encapsulation and Abstraction D. Abstraction and Polymorphism 16
  • 17. Question 8 ■ What do you call the languages that support classes but not polymorphism? A. Class based language B. ProcedureOriented language C. Object-basedlanguage D. If classes are supported, polymorphism will always be supported 17
  • 18. Question 8 ■ What do you call the languages that support classes but not polymorphism? A. Class based language B. ProcedureOriented language C. Object-basedlanguage D. If classes are supported, polymorphism will always be supported 18
  • 19. Question 9 ■ If data members are private, what can we do to access them from the class object? A. Create public member functions to access those data members B. Create private member functions to access those data members C. Create protected member functions to access those data members D. Private data members can never be accessed from outside the class 19
  • 20. Question 9 ■ If data members are private, what can we do to access them from the class object? A. Create public member functions to access those data members B. Create private member functions to access those data members C. Create protected member functions to access those data members D. Private data members can never be accessed from outside the class 20
  • 21. Question 10 ■ Which among the following violates the principle of encapsulation almost always? A. Local variables B. Global variables C. Public variables D. Array variables 21
  • 22. Question 10 ■ Which among the following violates the principle of encapsulation almost always? A. Local variables B. Global variables C. Public variables D. Array variables 22
  • 23. Question 11 ■ Which among the following is not a necessarycondition for constructors? A. Its name must be same as that of class B. It must not have any return type C. It must contain a definition body D. It can contains arguments 23
  • 24. Question 11 ■ Which among the following is not a necessarycondition for constructors? A. Its name must be same as that of class B. It must not have any return type C. It must contain a definition body D. It can contains arguments 24
  • 25. Question 12 ■ How many types of constructorsare available for use in general (with respect to parameters)? A. 2 B. 3 C. 4 D. 5 25
  • 26. Question 12 ■ How many types of constructorsare available for use in general (with respect to parameters)? A. 2 B. 3 C. 4 D. 5 26
  • 27. Question 13 ■ Default constructor must be defined, if parameterized constructoris defined and the object is to be created without arguments. A. True B. False 27
  • 28. Question 13 ■ Default constructor must be defined, if parameterized constructoris defined and the object is to be created without arguments. A. True B. False 28
  • 29. Question 14 ■ If class C inherits class B. And B has inherited class A. Then while creating the object of class C, what will be the sequenceof constructors getting called? A. Constructorof C then B, finally of A B. Constructorof A then C, finally of B C. Constructorof C then A, finally B D. Constructorof A then B, finally C 29
  • 30. Question 14 ■ If class C inherits class B. And B has inherited class A. Then while creating the object of class C, what will be the sequenceof constructors getting called? A. Constructorof C then B, finally of A B. Constructorof A then C, finally of B C. Constructorof C then A, finally B D. Constructorof A then B, finally C 30
  • 32. Complexity (‫الخوارزميات‬ ‫)تعقيد‬ ■‫اساسيين‬ ‫بعاملين‬ ‫يرتبط‬: –‫الذاكرة‬ ‫حجم‬: ■‫يعالجها‬ ‫التي‬ ‫والمعطيات‬ ‫البرنامج‬ ‫لتخزين‬ ‫الالزمة‬. –‫التنفيذ‬ ‫زمن‬: ■‫كافة‬ ‫تعليماتها‬ ‫إنجاز‬ ‫من‬ ‫الخوارزمية‬ ‫لتنتهي‬ ‫الالزم‬ ‫الزمن‬. ■‫تعليمة‬ ‫كل‬ ‫لتنفيذ‬ ‫الالزم‬ ‫والزمن‬ ‫التعليمات‬ ‫عدد‬ ‫بحساب‬ ‫التنفيذ‬ ‫زمن‬ ‫يقاس‬. 32
  • 33. Complexity (‫الخوارزميات‬ ‫)تعقيد‬ ■‫منها‬ ‫عديدة‬ ‫عوامل‬ ‫على‬ ‫حاسوب‬ ‫على‬ ‫البرنامج‬ ‫تنفيذ‬ ‫زمن‬ ‫يعتمد‬: –‫ما‬ ‫بتجربة‬ ‫الخاصة‬ ‫المسألة‬ ‫معطيات‬. –‫جودة‬‫الرماز‬‫التنفيذي‬ ‫الملف‬ ‫بناء‬ ‫أجل‬ ‫من‬ ‫المترجم‬ ‫يولده‬ ‫الذي‬. –‫الحاسوب‬ ‫في‬ ‫المتوفرة‬ ‫التعليمات‬ ‫وسرعة‬ ‫طبيعة‬(‫المعالج‬ ‫في‬‫الصغري‬.) –‫المبرمج‬ ‫يكتبه‬ ‫الذي‬ ‫البرنامج‬ ‫جودة‬. –‫الخوارزمية‬ ‫فعالية‬.(‫المبرمج‬ ‫قدرة‬ ‫على‬ ‫تعتمد‬) 33
  • 34. Complexity (‫الخوارزميات‬ ‫)تعقيد‬ ■‫بعد‬ ‫تحديد‬ ‫يجب‬ ‫خوارزمية‬ ‫تنفيذ‬ ‫زمن‬ ‫لحساب‬(‫حجم‬ ‫او‬ ‫طول‬ ‫او‬)‫أجل‬ ‫من‬ ‫المسألة‬ ‫معطيات‬ ‫البعد‬ ‫هذا‬ ‫بداللة‬ ‫التعقيد‬ ‫درجة‬ ‫كتابة‬. ■‫أمثلة‬: –‫الحدود‬ ‫كثيرات‬ ‫مسائل‬:‫يكون‬‫الحدود‬ ‫كثيرات‬ ‫درجة‬ ‫هو‬ ‫البعد‬. –‫المصفوفات‬ ‫مسائل‬:‫يكون‬‫المصفوفة‬ ‫أبعاد‬ ‫بداللة‬ ‫البعد‬. –‫البيانات‬ ‫مسائل‬:‫يكون‬‫األسهم‬ ‫أو‬ ‫العقد‬ ‫عدد‬ ‫بداللة‬ ‫البعد‬‫مجموعها‬ ‫أو‬. –‫الفرز‬ ‫مسائل‬:‫يكون‬‫ترتيبها‬ ‫نريد‬ ‫التي‬ ‫العناصر‬ ‫عدد‬ ‫بداللة‬ ‫البعد‬. –‫القواعدي‬ ‫التحليل‬ ‫مسائل‬:‫يكون‬‫الكلمة‬ ‫طول‬ ‫بداللة‬ ‫البعد‬. 34
  • 35. Complexity (‫الخوارزميات‬ ‫)تعقيد‬ ■‫الرياضية‬ ‫القوانين‬ ‫ببعض‬ ‫تذكرة‬: 35
  • 36. Complexity (‫الخوارزميات‬ ‫)تعقيد‬ ■‫الرياضية‬ ‫القوانين‬ ‫ببعض‬ ‫تذكرة‬: 36
  • 38. Complexity (‫الخوارزميات‬ ‫)تعقيد‬ T(n) n n n log n n2 n3 n4 n10 2n 10 .01s .03s .1s 1s 10s 10s 1s 20 .02s .09s .4s 8s 160s 2.84h 1ms 30 .03s .15s .9s s 810s 6.83d 1s 40 .04s .21s 1.6s s 2.56ms 121d 18m 50 .05s .28s s s 6.25ms 3.1y 13d 100 .1s .66s 10s 1ms 100ms 3171y 41013 y 103 1s 9.96s 1ms 1s 16.67m 3.171013 y 3210283 y 104 s 130s 100ms 16.67m 115.7d 3.171023 y 105 s 1.66ms 10s 11.57d 3171y 3.171033 y 106 ms 19.92ms 16.67m 31.71y 3.17107 y 3.171043 y 38
  • 39. Complexity (‫الخوارزميات‬ ‫)تعقيد‬ Exercise 39 // a simple loop to calculate the sum of numbers in an array int i , sum; for (i = sum = 0; i<n ; i++) sum += a[i];
  • 40. Complexity (‫الخوارزميات‬ ‫)تعقيد‬ Exercise 40 int i = 0; int sum = 0; while (i < n) { sum = sum + a[i]; i = i + 1; } The body of this loop is executed for i = 0, 1, 2, … n-1. Hence, it is executed ((n-1) – 0) + 1 = n times Therefore, these 2 assignments are executed n times Answer: Number of Assignment Operations = 2 + n + n = 2n + 2 2 assignments
  • 41. Complexity (‫الخوارزميات‬ ‫)تعقيد‬ Rule 41 If a loop iterates for i = initial value to final value in step of 1 then: Number of iterations of the loop = final value – initial value + 1
  • 42. Complexity (‫الخوارزميات‬ ‫)تعقيد‬ Question 1 ■‫السؤال‬:‫تصاعديا‬ ‫التالية‬ ‫التعقيدات‬ ‫رتب‬ –n –n*n –n*logn –logn ■‫اإلجابة‬: .1logn .2n .3n*logn .4n*n 42
  • 43. Complexity (‫الخوارزميات‬ ‫)تعقيد‬ Question 2 ■‫السؤال‬:‫لقوة‬ ‫الرفع‬ ‫لخوارزمية‬ ‫تعقيد‬ ‫أصغر‬ –n –n*n –n*logn –logn ■‫اإلجابة‬: –logn 43
  • 44. Complexity (‫الخوارزميات‬ ‫)تعقيد‬ Question 2 int power (int x, int y) { int p = 1; for (int i = 1; i<=y; i++) { p = p*x; } return p; } int power2 (int x, int y) { int res = 1; int factor = x; while (y>0) { if (y%2==1) res = res*factor; factor = factor*factor; y = y / 2; } return res; } 44
  • 45. Complexity (‫الخوارزميات‬ ‫)تعقيد‬ ■‫التنفيذ‬ ‫زمن‬ ‫حسب‬ ‫المسائل‬ ‫تصنيف‬: –O(1):‫التقطيع‬ ‫جداول‬ ‫في‬ ‫البحث‬ ‫خوارزميات‬ ‫مثل‬. –Log n:‫الثنائي‬ ‫البحث‬ ‫خوارزميات‬. –n:‫التسلسلي‬ ‫البحث‬ ‫خوارزميات‬(‫فعالة‬.) –n * Log n:‫الجيدة‬ ‫الفرز‬ ‫خوارزميات‬. –n*n:‫المصفوفات‬ ‫ضرب‬(‫الحجم‬ ‫متوسطة‬ ‫معطيات‬ ‫تعالج‬.) –2 𝑛 :‫محدود‬ ‫حجم‬ ‫ذات‬ ‫معطيات‬ ‫حالة‬ ‫في‬ ‫إال‬ ‫تستخدم‬ ‫ال‬ ‫أسي‬. 45
  • 46. Complexity (‫الخوارزميات‬ ‫)تعقيد‬ ■‫تنفيذ‬ ‫زمن‬ ‫يكون‬ ‫التي‬ ‫هي‬ ‫معينة‬ ‫مسألة‬ ‫لحل‬ ‫الجيدة‬ ‫الخوارزميات‬ ‫إن‬ ‫نقول‬ ‫ان‬ ‫يمكن‬‫ها‬: –‫المعطيات‬ ‫حجم‬ ‫كان‬ ‫مهما‬ ‫ثابتا‬:‫التقطيع‬ ‫جداول‬ ‫في‬ ‫البحث‬ ‫خوارزميات‬ ‫مثل‬. –‫لوغاريتميا‬:‫الثنائي‬ ‫البحث‬ ‫خوارزميات‬ ‫مثل‬. –‫خطيا‬:‫التسلسلي‬ ‫البحث‬ ‫خوارزمية‬ ‫مثل‬. –‫مرتبة‬ ‫من‬n*log(n):‫الجيدة‬ ‫الفرز‬ ‫خوارزميات‬ ‫مثل‬. 46
  • 47. Complexity (‫الخوارزميات‬ ‫)تعقيد‬ Question 3 void Main() { for (int i=1; i<=n; i++) { m=m+1; p=p*2; l=l+3; } } ■‫السؤال‬: –‫الخوارزميات‬ ‫تعقيد‬ ‫احسب‬ ‫التالية‬: ■‫اإلجابة‬: 47
  • 48. Complexity (‫الخوارزميات‬ ‫)تعقيد‬ Question 4 void Main() { for (int i=1; i<=n; i++) { for (int j=1; j<=n; j++){ m=m+1; p=p*2; l=l+3; } } } ■‫السؤال‬: –‫الخوارزميات‬ ‫تعقيد‬ ‫احسب‬ ‫التالية‬: ■‫اإلجابة‬: 48
  • 49. Complexity (‫الخوارزميات‬ ‫)تعقيد‬ Question 5 void Main() { for (int i=1; i<=n; i++) { for (int j=1; j<=i; j++){ m=m+1; p=p*2; l=l+3; } } } ■‫السؤال‬: –‫الخوارزميات‬ ‫تعقيد‬ ‫احسب‬ ‫التالية‬: ■‫اإلجابة‬: 49
  • 50. Complexity (‫الخوارزميات‬ ‫)تعقيد‬ Question 6 void Main() { for (int cnt = 5 , i = 1 ; i <= n ; i++) for (int j = 1 ; j <= n ; j= j * 2) cnt++; } } ■‫السؤال‬: –‫الخوارزميات‬ ‫تعقيد‬ ‫احسب‬ ‫التالية‬: ■‫اإلجابة‬: –n * log n 50
  • 52. Recursive Functions ( ‫الخوارزميات‬‫العودية‬ ) ■‫للتوقف‬ ‫شرط‬ ‫بتحقق‬ ‫االستدعاء‬ ‫وينتهي‬ ‫نفسها‬ ‫تستدعي‬ ‫خوارزمية‬ ‫هي‬. ■‫أمثلة‬: –‫فيبوناتشي‬ ‫اعداد‬ ‫حساب‬. –‫أكريمان‬ ‫سلسلة‬. –‫هانوي‬ ‫أبراج‬. –‫المنحنيات‬ ‫رسم‬. –‫الثمانية‬ ‫الوزراء‬ ‫مسألة‬. –‫هلبرت‬ ‫منحنيات‬. ■‫الخوارزمية‬ ‫تعقيد‬ ‫حساب‬ ‫يؤول‬‫العودية‬‫تراجعية‬ ‫معادلة‬ ‫حل‬ ‫إلى‬. 52
  • 53. Recursive Functions ( ‫الخوارزميات‬‫العودية‬ ) ■‫اإلجرائيات‬ ‫من‬ ‫نوعين‬ ‫بين‬ ‫التمييز‬ ‫يمكن‬‫العودية‬: –‫ذات‬ ‫اإلجرائيات‬‫العودية‬‫المباشرة‬: ■‫إجرائية‬ ‫عن‬ ‫نقول‬P‫إنها‬‫عودية‬‫لنفسها‬ ‫صريح‬ ‫استدعاء‬ ‫تحوي‬ ‫كانت‬ ‫إذا‬ ‫مباشرة‬. –‫ذات‬ ‫اإلجرائيات‬‫العودية‬‫المباشرة‬ ‫غير‬: ■‫إجرائية‬ ‫عن‬ ‫نقول‬P‫إنها‬‫عودية‬‫أخرى‬ ‫إجرائية‬ ‫تستدعي‬ ‫كانت‬ ‫إذا‬ ‫مباشرة‬ ‫غير‬Q ‫تستدعي‬P. 53
  • 54. Recursive Functions ( ‫الخوارزميات‬‫العودية‬ ) ■‫االستدعاء‬ ‫عملية‬‫العودي‬‫المطلوبة‬ ‫الذاكرة‬ ‫وحجم‬ ‫التنفيذ‬ ‫الزمن‬ ‫حيث‬ ‫من‬ ‫مكلفة‬. ■‫خوارزمية‬ ‫تحويل‬ ‫يمكن‬‫عودية‬‫مكدس‬ ‫باستخدام‬ ‫تكرارية‬ ‫إلى‬ –‫ث‬ ‫فيه‬ ‫عودي‬ ‫استدعاء‬ ‫لكل‬ ‫المحلية‬ ‫والمتحوالت‬ ‫الدخل‬ ‫معامالت‬ ‫تخزين‬ ‫يتم‬ ‫حيث‬‫م‬ ‫االستدعاء‬ ‫بعد‬ ‫التعليمات‬ ‫بقية‬ ‫لتنفيذ‬ ‫المكدس‬ ‫عناصر‬ ‫حذف‬ ‫يتم‬‫العودي‬‫عل‬‫هذه‬ ‫ى‬ ‫األول‬ ‫إلى‬ ‫األخير‬ ‫االستدعاء‬ ‫من‬ ‫التنفيذ‬ ‫يتم‬ ‫حيث‬ ‫العناصر‬. 54
  • 55. Recursive Functions ( ‫الخوارزميات‬‫العودية‬ ) Question 1 ■‫السؤال‬:‫االستدعاء‬ ‫كان‬ ‫إذا‬ ‫النتيجة‬ ‫ما‬f(33,33) int f(int x, int y) ( if (x==1) return y; else if (y==1) return x; else if (x%2==0) return y+f(x/2,y); else return x+f(x,y/2); } ■‫اإلجابة‬: 198 55
  • 56. Recursive Functions ( ‫الخوارزميات‬‫العودية‬ ) Question 2 ■‫السؤال‬:‫االستدعاء‬ ‫كان‬ ‫إذا‬ ‫النتيجة‬ ‫ما‬f(1024*1024) Public int f(int n) { int x=0; for (int i=2; i<=n ; i*=2) { x++; } return x; } ■‫اإلجابة‬: 20 56
  • 57. Recursive Functions ( ‫الخوارزميات‬‫العودية‬ ) Question 3 ■‫السؤال‬:‫االستدعاء‬ ‫كان‬ ‫إذا‬ ‫النتيجة‬ ‫ما‬f(33) Int f(int n) { for (int i=1;i<=n;i++) { i=i+1; n=n-1; } return n; } ■‫اإلجابة‬: 22 57
  • 58. Recursive Functions ( ‫الخوارزميات‬‫العودية‬ ) Question 4 int fun(int n) { if (n == 4) return n; else return 2*fun(n+1);} int main() { printf("%d ", fun(2)); return 0; } 1. 4 2. 8 3. 16 4. Runtime error. 58
  • 59. Recursive Functions ( ‫الخوارزميات‬‫العودية‬ ) Question 4 int fun(int n) { if (n == 4) return n; else return 2*fun(n+1);} int main() { printf("%d ", fun(2)); return 0; } 1. 4 2. 8 3. 16 4. Runtime error. 59
  • 60. Recursive Functions ( ‫الخوارزميات‬‫العودية‬ ) Question 5 int fun(int x, int y) { if (x == 0) return y; return fun(x - 1, x + y); } int main() { printf("%d ", fun(4,3)); return 0; } 1. 13 2. 12 3. 9 4. 10 60
  • 61. Recursive Functions ( ‫الخوارزميات‬‫العودية‬ ) Question 5 int fun(int x, int y) { if (x == 0) return y; return fun(x - 1, x + y); } int main() { printf("%d ", fun(4,3)); return 0; } 1. 13 2. 12 3. 9 4. 10 The rule: x(x+1)/2 + y 61
  • 62. Recursive Functions ( ‫الخوارزميات‬‫العودية‬ ) Question 6 n = 25 void fun(int n) { if (n == 0) return; printf("%d", n%2); fun(n/2); } 1. 11001 2. 10011 3. 11111 4. 00000 62
  • 63. Recursive Functions ( ‫الخوارزميات‬‫العودية‬ ) Question 6 n = 25 void fun(int n) { if (n == 0) return; printf("%d", n%2); fun(n/2); } 1. 11001 2. 10011 3. 11111 4. 00000 63
  • 64. Recursive Functions ( ‫الخوارزميات‬‫العودية‬ ) Question 7 n = 25 void fun(int n) { if (n == 0) return; fun(n/2); printf("%d", n%2); } 1. 11001 2. 10011 3. 11111 4. 00000 64
  • 65. Recursive Functions ( ‫الخوارزميات‬‫العودية‬ ) Question 7 n = 25 void fun(int n) { if (n == 0) return; fun(n/2); printf("%d", n%2); } 1. 11001 2. 10011 3. 11111 4. 00000 65
  • 66. Recursive Functions ( ‫الخوارزميات‬‫العودية‬ ) Question 8 What does the following function do? int fun(int x, int y) { if (y == 0) return 0; return (x + fun(x, y-1)); } 1. x+y 2. x+x*y 3. x*y 4. x^y 66
  • 67. Recursive Functions ( ‫الخوارزميات‬‫العودية‬ ) Question 8 What does the following function do? int fun(int x, int y) { if (y == 0) return 0; return (x + fun(x, y-1)); } 1. x+y 2. x+x*y 3. x*y 4. x^y 67
  • 68. Recursive Functions ( ‫الخوارزميات‬‫العودية‬ ) Question 9 Predict the output of following program int f(int n) { if(n <= 1) return 1; if(n%2 == 0) return f(n/2); return f(n/2) + f(n/2+1); } int main() { printf("%d", f(11)); return 0; } 1. Stack overflow 2. 3 3. 4 4. 5 68
  • 69. Recursive Functions ( ‫الخوارزميات‬‫العودية‬ ) Question 9 Predict the output of following program int f(int n) { if(n <= 1) return 1; if(n%2 == 0) return f(n/2); return f(n/2) + f(n/2+1); } int main() { printf("%d", f(11)); return 0; } 1. Stack overflow 2. 3 3. 4 4. 5 69
  • 70. Recursive Functions ( ‫الخوارزميات‬‫العودية‬ ) Question 10 Consider the following: f(5) int fun (int n) { int x=1, k; if (n==1) return x; for (k=1; k<n; ++k) x = x + fun(k) * fun(n – k); return x; } 1. 0 2. 26 3. 51 4. 71 70
  • 71. Recursive Functions ( ‫الخوارزميات‬‫العودية‬ ) Question 10 Consider the following: f(5) int fun (int n) { int x=1, k; if (n==1) return x; for (k=1; k<n; ++k) x = x + fun(k) * fun(n – k); return x; } 1. 0 2. 26 3. 51 4. 71 71
  • 72. Recursive Functions ( ‫الخوارزميات‬‫العودية‬ ) Question 11 If get(6) function is being called. how many times will the get() function be invoked? void get (int n) { if (n < 1) return; get(n-1); get(n-3); printf("%d", n); } 1. 15 2. 25 3. 35 4. 45 72
  • 73. Recursive Functions ( ‫الخوارزميات‬‫العودية‬ ) Question 11 If get(6) function is being called. how many times will the get() function be invoked? void get (int n) { if (n < 1) return; get(n-1); get(n-3); printf("%d", n); } 1. 15 2. 25 3. 35 4. 45 73 get(6) [25 Calls] / [17 Calls] get(5) get(3) [7 Calls] / get(4) get(2) [5 Calls] / [7 Calls] get(3) get(1) [3 Calls] / get(2) get(0) / [3 Calls] get(1) get(-1) / get(0) get(-2)
  • 75. Linked List (‫الخطية‬ ‫)السالسل‬ ■‫النمط‬ ‫نفس‬ ‫من‬ ‫عناصر‬ ‫تخزين‬ ‫من‬ ‫تمكن‬ ‫معطيات‬ ‫بنية‬ ‫هي‬. ■‫السالسل‬ ‫ميزات‬: –‫مسبقا‬ ‫طولها‬ ‫لتحديد‬ ‫ضرورة‬ ‫ال‬. –‫واالضافة‬ ‫الحذف‬ ‫عند‬ ‫السلسلة‬ ‫عناصر‬ ‫ازاحة‬ ‫إلى‬ ‫نحتاج‬ ‫ال‬. ■‫سيئاتها‬: –‫عنصر‬ ‫إلى‬ ‫المباشر‬ ‫النفاذ‬ ‫يمكن‬ ‫ال‬,‫إلى‬ ‫تحتاج‬ ‫وبالتالي‬n‫االحوال‬ ‫أسوأ‬ ‫في‬ ‫مقارنة‬ ‫عملية‬ ‫العنصر‬ ‫إلى‬ ‫للوصول‬. –‫المؤشرات‬ ‫لتخزين‬ ‫إضافي‬ ‫تخزين‬ ‫حجم‬ ‫إلى‬ ‫نحتاج‬. ■‫مثل‬ ‫للسالسل‬ ‫أخرى‬ ‫أنواع‬ ‫يوجد‬: –‫الدائرية‬ ‫السالسل‬circular linked list. –‫االرتباط‬ ‫المضاعفة‬ ‫السالسل‬doubly linked list. 75
  • 76. Linked List (‫الخطية‬ ‫)السالسل‬ 76 A B C Head A data pointer node
  • 77. Linked List (‫الخطية‬ ‫)السالسل‬ V.S. Ordinary and Dynamic Arrays Ordinary Arrays ■‫الميزات‬: –‫واستخدام‬ ‫إنشاء‬ ‫جدا‬ ‫السهل‬ ‫من‬. –‫عنصر‬ ‫أي‬ ‫إلى‬ ‫المباشر‬ ‫الوصول‬. ■‫السيئات‬: –‫وقت‬ ‫في‬ ‫ا‬ً‫معروف‬ ‫حجمها‬ ‫يكون‬ ‫أن‬ ‫يجب‬ ‫ف‬ ‫تغييره‬ ‫يمكن‬ ‫وال‬ ،‫البرمجي‬ ‫التحويل‬‫ي‬ ‫التشغيل‬ ‫وقت‬. –‫كل‬ ‫نقل‬ ‫حذفه‬ ‫أو‬ ‫عنصر‬ ‫إدراج‬ ‫يتطلب‬ ‫األخرى‬ ‫العناصر‬. Dynamic Arrays ■‫الميزات‬: –‫إنشائها‬ ‫جدا‬ ‫السهل‬ ‫من‬. –‫عنصر‬ ‫أي‬ ‫إلى‬ ‫المباشر‬ ‫الوصول‬. –‫التشغيل‬ ‫وقت‬ ‫في‬ ‫الحجم‬ ‫تحديد‬ ‫يمكن‬. ■‫السيئات‬: –‫كل‬ ‫نقل‬ ‫حذفه‬ ‫أو‬ ‫عنصر‬ ‫إدراج‬ ‫يتطلب‬ ‫األخرى‬ ‫العناصر‬. –‫تتخطى‬ ‫أن‬ ‫المصفوفة‬ ‫على‬ ‫كان‬ ‫إذا‬ ‫تشكيل‬ ‫إعادة‬ ‫فيجب‬ ، ‫األولي‬ ‫الحجم‬ ‫جديد‬ ‫موقع‬ ‫إلى‬ ‫بأكملها‬ ‫المصفوفة‬. 77
  • 78. Stack (‫)المكدس‬ ■‫ندع‬ ‫واحدة‬ ‫جهة‬ ‫من‬ ‫تتم‬ ‫والحذف‬ ‫اإلضافة‬ ‫عمليات‬ ‫إن‬ ‫غير‬ ،‫السلسلة‬ ‫تشبه‬ ‫خطية‬ ‫بنية‬ ‫هي‬‫وها‬ ‫المكدس‬ ‫قمة‬. ■‫النمط‬ ‫نفس‬ ‫من‬ ‫عناصر‬ ‫تخزين‬ ‫من‬ ‫تمكن‬ ‫معطيات‬ ‫بنية‬ ‫هي‬. ■‫مبدأ‬ ‫تتبع‬: –First In last Out (FILO) –Last In First Out (LIFO) ■‫عليه‬ ‫االساسية‬ ‫العمليات‬: –‫قمته‬ ‫خالل‬ ‫من‬ ‫العناصر‬ ‫إلى‬ ‫الوصول‬ ‫يمكن‬head)) –‫القمة‬ ‫إلى‬ ‫عنصر‬ ‫إضافة‬push)) –‫القمة‬ ‫من‬ ‫عنصر‬ ‫حذف‬pop)) ■‫الخطية‬ ‫السالسل‬ ‫أو‬ ‫المصفوفات‬ ‫باستخدام‬ ‫المكدس‬ ‫تمثيل‬ ‫يمكن‬. 78
  • 80. Stack (‫)المكدس‬ ■‫المكدسات‬ ‫تطبيقات‬: –‫الصفحات‬ ‫سجل‬‫المزارة‬‫االنترنت‬ ‫متصفح‬ ‫في‬. –‫النصوص‬ ‫محررات‬ ‫في‬ ‫التراجع‬ ‫عمليات‬. –‫الرياضية‬ ‫التعابير‬ ‫في‬ ‫األقواس‬ ‫أزواج‬ ‫ربط‬. –‫قيم‬ ‫وحساب‬ ‫الملحقة‬ ‫التعابير‬ ‫إلى‬ ‫النظامية‬ ‫الرياضية‬ ‫التعابير‬ ‫من‬ ‫التحويل‬‫هذه‬ ‫ة‬ ‫التعابير‬. –‫تابع‬ ‫جسم‬ ‫في‬ ‫تابع‬ ‫استدعاء‬. –‫أوال‬ ‫العمق‬ ‫بطريقة‬ ‫والبيانات‬ ‫األشجار‬ ‫في‬ ‫التجول‬. –‫المترجمات‬ ‫في‬ ‫األخطاء‬ ‫مكدس‬. 80
  • 81. Stack (‫)المكدس‬ Infix (‫)نظامي‬ Prefix (‫)مصدر‬ Postfix (‫)ملحق‬ A+B +AB AB+ A+B*C +A*BC ABC*+ A*(B+C) *A+BC ABC+* A*B+C +*ABC AB*C+ A+B*C+D-E*F -++A*BCD*EF ABC*+D+EF*- (A+B)*(C+D-E)*F **+AB-+CDEF AB+CD+E-*F* 81
  • 82. Stack (‫)المكدس‬ ■ Infix to Postfix: 1. Scan the infix expression from left to right. 2. If the scanned character is an operand, output it. 3. Else, 1. If the precedence of the scanned operator is greater than the precedence of the operator in the stack (or the stack is empty), push it. 2. Else, Pop the operator from the stack until the precedence of the scanned operator is less-equal to the precedence of the operator residing on the top of the stack. Push the scanned operator to the stack. 4. If the scanned character is an ‘(‘, push it to the stack. 5. If the scanned character is an ‘)’, pop and output from the stack until an ‘(‘ is encountered. 6. Repeat steps 2-6 until infix expression is scanned. 7. Pop and output from the stack until it is not empty. 82
  • 83. Stack (‫)المكدس‬ ■ Infix to Prefix: 1. Reversethe infix expression i.e A+B*C will become C*B+A. ■ Note while reversing each ‘(‘ will become ‘)’ and each ‘)’ becomes ‘(‘. 2. Obtain the postfix expression of the modified expression i.e CB*A+. 3. Reversethe postfix expression. Hence in our example prefix is +A*BC. 83
  • 84. Stack (‫)المكدس‬ ■ Postfix Calculation: 1. Create a stack to store operands (values). 2. Scan the given expression and do following for every scanned element. 1. If the elementis a number, push it into the stack. 2. If the elementis a operator, pop operands for the operator from stack. Evaluate the operator and push the result back to the stack. 3. When the expression is ended, the number in the stack is the final answer. 84
  • 85. Stack (‫)المكدس‬ ■ Prefix Calculation: 1. Put a pointer P at the end of the end. 2. If character at P is an operand push it to Stack. 3. If the character at P is an operator pop two elements from the Stack. Operate on these elements accordingto the operator, and push the result back to the Stack. 4. Decrement P by 1 and go to Step 2 as long as there are characters left to be scanned in the expression. 5. The Resultis stored at the top of the Stack. 85
  • 86. Queue (‫)األرتال‬ ■‫الرت‬ ‫ذيل‬ ‫ندعوها‬ ‫جهة‬ ‫في‬ ‫تجري‬ ‫اإلضافة‬ ‫عمليات‬ ‫أن‬ ‫غير‬ ‫السلسلة‬ ‫تشبه‬ ‫خطية‬ ‫بنية‬ ‫هي‬‫ل‬. ‫الرتل‬ ‫بداية‬ ‫نسميها‬ ‫التي‬ ‫المعاكسة‬ ‫الجهة‬ ‫في‬ ‫الحذف‬ ‫ويجري‬. ■‫النمط‬ ‫نفس‬ ‫من‬ ‫عناصر‬ ‫تخزين‬ ‫من‬ ‫تمكن‬ ‫معطيات‬ ‫بنية‬ ‫هي‬. ■‫مبدأ‬ ‫تتبع‬: –First In First Out (FIFO) –Last In Last Out (LILO) ■‫عليه‬ ‫االساسية‬ ‫العمليات‬: –‫بدايته‬ ‫خالل‬ ‫من‬ ‫العناصر‬ ‫إلى‬ ‫الوصول‬ ‫يمكن‬(head)‫نهايته‬ ‫أو‬(tail.) –‫النهاية‬ ‫إلى‬ ‫عنصر‬ ‫إضافة‬(enqueue.) –‫البداية‬ ‫من‬ ‫عنصر‬ ‫حذف‬(dequeue.) ■‫الخطية‬ ‫السالسل‬ ‫أو‬ ‫المصفوفات‬ ‫باستخدام‬ ‫االرتال‬ ‫تمثيل‬ ‫يمكن‬. 86
  • 88. Queue (‫)األرتال‬ ■‫األرتال‬ ‫تطبيقات‬: –‫الكمبيوتر‬ ‫في‬ ‫والمخرجات‬ ‫المدخالت‬ ‫مخازن‬(‫والطابعة‬ ‫المفاتيح‬ ‫لوحة‬.) –‫في‬ ‫أوال‬ ‫بالعرض‬ ‫التجول‬ ‫خوارزميات‬ ‫مثل‬ ‫الخوارزميات‬ ‫في‬ ‫مساعدة‬ ‫معطيات‬ ‫بنية‬ ‫والبيانات‬ ‫األشجار‬. –‫اليومية‬ ‫الحياة‬ ‫أرتال‬ ‫محاكاة‬. 88
  • 89. Queue (‫)األرتال‬ ■‫االولويات‬ ‫رتل‬: –‫الرتل‬ ‫إلى‬ ‫األولوية‬ ‫عامل‬ ‫يضاف‬. –‫اوال‬ ‫األعلى‬ ‫األولوية‬ ‫ذو‬ ‫العنصر‬ ‫تخديم‬ ‫يتم‬. –‫مثال‬: ■‫للحاالت‬ ‫األولوية‬ ‫تكون‬ ‫حيث‬ ‫الطبيب‬ ‫عيادة‬ ‫في‬ ‫االنتظار‬ ‫أرتال‬‫اإلسعافية‬. 89
  • 90. NON SEQUENTIAL DATA STRUCTURES ‫التسلسلية‬ ‫غير‬ ‫المعطيات‬ ‫بنى‬ 90
  • 91. Trees (‫)االشجار‬ ■‫الشجرة‬ ‫نعرف‬: –‫عقد‬ ‫نسميها‬ ‫العناصر‬ ‫من‬ ‫مجموعة‬ ‫بأنها‬nodes))، –‫بروابط‬ ‫بعضها‬ ‫بين‬ ‫مرتبطة‬(Links)، –‫هرميا‬ ‫تنظيما‬ ‫ومنظمة‬(Hierarchical)، –‫حلقات‬ ‫يحتوي‬ ‫ال‬(Cycles)،‫مغلقة‬ –‫جذر‬ ‫نسميها‬ ‫ووحيدة‬ ‫مميزة‬ ‫عقدة‬ ‫توجد‬ ‫إنه‬ ‫أي‬(Root.) ■‫أمثلة‬: –‫مجلدات‬ ‫في‬ ‫الملفات‬ ‫تنظيم‬. –‫حسابية‬ ‫عبارة‬ ‫تمثيل‬. 91
  • 92. Binary Trees (‫الثنائية‬ ‫)األشجار‬ ■‫األكثر‬ ‫علي‬ ‫ابنان‬ ‫عقدة‬ ‫لكل‬ ‫فيها‬ ‫يكون‬ ‫األشجار‬ ‫من‬ ‫خاصة‬ ‫حالة‬: –‫داخلية‬ ‫عقد‬ ‫األقل‬ ‫على‬ ‫ولد‬ ‫لها‬ ‫التي‬ ‫العقد‬ ‫نسمي‬. –‫أوراق‬ ‫أو‬ ‫خارجية‬ ‫عقد‬ ‫ولد‬ ‫أي‬ ‫لها‬ ‫ليس‬ ‫التي‬ ‫العقد‬ ‫نسمي‬(Leaf.) –‫طريقا‬ ‫نسمي‬(Path)‫العقد‬ ‫من‬ ‫متتالية‬ ‫كل‬. –‫األوراق‬ ‫وإحدى‬ ‫الجذر‬ ‫بين‬ ‫يصل‬ ‫طريق‬ ‫كل‬ ‫فرعا‬ ‫نسمي‬. 92
  • 93. Binary Trees (‫الثنائية‬ ‫)األشجار‬ ■‫مهمة‬ ‫تعاريف‬: –‫الشجرة‬ ‫درجة‬:‫عقدة‬ ‫ألوالد‬ ‫األعظمي‬ ‫العدد‬( .‫هي‬ ‫ثنائية‬ ‫شجرة‬ ‫درجة‬2) –‫الشجرة‬ ‫حجم‬:‫عقدها‬ ‫عدد‬. –‫عقدة‬ ‫ارتفاع‬:‫الجذر‬ ‫الى‬ ‫العقدة‬ ‫من‬ ‫الواصل‬ ‫الطريق‬ ‫في‬ ‫االتصاالت‬ ‫عدد‬. –‫الشجرة‬ ‫ارتفاع‬:‫فيها‬ ‫عقدة‬ ‫ارتفاع‬ ‫أطول‬. –‫الشجرة‬ ‫عرض‬:‫ما‬ ‫مستوى‬ ‫في‬ ‫للعقد‬ ‫األعظم‬ ‫العدد‬. –‫التجول‬ ‫مسافة‬:‫عقدها‬ ‫ارتفاعات‬ ‫مجموع‬ ‫هو‬ ‫الشجرة‬ ‫ضمن‬. –‫الخارجي‬ ‫التجول‬ ‫مسافة‬:‫أوراقها‬ ‫ارتفاعات‬ ‫مجموع‬. –‫الداخلي‬ ‫التجول‬ ‫مسافة‬:‫الداخلية‬ ‫عقدها‬ ‫ارتفاعات‬ ‫مجموع‬. 93
  • 94. Binary Trees (‫الثنائية‬ ‫)األشجار‬ Exercise ■‫الشجرة‬ ‫جذر‬:n1 ■‫االيسر‬ ‫االبن‬:n2 ■‫األيمن‬ ‫االبن‬:n4 ■‫األوراق‬:n8 – n10 – n5 – n9 ■‫الداخلية‬ ‫العقد‬:n1 – n2 – n3 – n4 – n6 – n7 ■‫عقدة‬ ‫ارتفاع‬: n1=0 , n2=n4=1 , n3=n5=n7=2 ■‫الشجرة‬ ‫ارتفاع‬:4 ■‫التجول‬ ‫مسافة‬:22 ■‫الخارجي‬ ‫التجول‬ ‫مسافة‬:13 ■‫الداخلي‬ ‫التجول‬ ‫مسافة‬:9 94
  • 95. Binary Trees (‫الثنائية‬ ‫)األشجار‬ ■‫الخاصة‬ ‫الثنائية‬ ‫األشجار‬ ‫بعض‬: –‫الخطية‬ ‫الثنائية‬ ‫الشجرة‬:‫األ‬ ‫على‬ ‫واحد‬ ‫ولد‬ ‫منها‬ ‫عقدة‬ ‫لكل‬ ‫يكون‬ ‫ثنائية‬ ‫شجرة‬‫كثر‬. –‫التامة‬ ‫الثنائية‬ ‫الشجرة‬:‫المستوى‬ ‫في‬ ‫واحدة‬ ‫عقدة‬ ‫تحوي‬ ‫شجرة‬ ‫كل‬0‫في‬ ‫وعقدتين‬ ‫المستوى‬1‫المستوى‬ ‫في‬ ‫عقد‬ ‫وأربع‬2‫و‬2 𝑘 ‫المستوى‬ ‫في‬ ‫عقدة‬k. ■(‫ارتفاعها‬ ‫تامة‬ ‫بشجرة‬ ‫العقد‬ ‫عدد‬h‫هو‬2ℎ+1 − 1) –‫الكاملة‬ ‫الثنائية‬ ‫الشجرة‬:‫بال‬ ‫مليئة‬ ‫مستوياتها‬ ‫كل‬ ‫تكون‬ ‫ثنائية‬ ‫شجرة‬ ‫كل‬‫ماعدا‬ ‫عقد‬ ‫فراغات‬ ‫على‬ ‫يحوي‬ ‫أن‬ ‫يمكن‬ ‫األخير‬ ‫المستوى‬. 95
  • 96. Binary Trees (‫الثنائية‬ ‫)األشجار‬ 96 Binary search trees Not a binary search tree 5 10 30 2 25 45 5 10 45 2 25 30 5 10 30 2 25 45
  • 97. Binary Trees (‫الثنائية‬ ‫)األشجار‬ Question 1 ■‫السؤال‬:‫عقدها‬ ‫عدد‬ ‫خطية‬ ‫شجرة‬ ‫ارتفاع‬ ‫هو‬ ‫ما‬n‫؟‬ ■‫اإلجابة‬:n-1 ■‫السؤال‬:‫تامة‬ ‫ثنائية‬ ‫شجرة‬ ‫أوراق‬ ‫عدد‬ ‫كم‬‫؟‬ ■‫اإلجابة‬:2h‫حيث‬h‫الشجرة‬ ‫ارتفاع‬ ‫هو‬ ■‫السؤال‬:‫؟‬ ‫تامة‬ ‫ثنائية‬ ‫لشجرة‬ ‫العقد‬ ‫عدد‬ ‫كم‬ ■‫اإلجابة‬: 97 ෍ 𝒊=𝟎 𝒊=𝒉 𝟐𝒊 = 𝟐 𝒉+𝟏 − 𝟏
  • 98. Binary Trees (‫الثنائية‬ ‫)األشجار‬ Question 2 ■‫السؤال‬:‫ثنائية؟‬ ‫بحث‬ ‫شجرة‬ ‫ضمن‬ ‫للبحث‬ ‫حالة‬ ‫أسوأ‬ ‫ماهي‬ ■‫اإلجابة‬:‫أعظمية‬ ‫التجول‬ ‫مسافة‬ ‫تكون‬ ‫حيث‬ ‫خطية‬ ‫الشجرة‬ ‫تكون‬ ‫عندما‬. ■‫السؤال‬:‫ثنائية؟‬ ‫بحث‬ ‫شجرة‬ ‫ضمن‬ ‫للبحث‬ ‫حالة‬ ‫أفضل‬ ‫ماهي‬ ■‫اإلجابة‬:‫التجول‬ ‫مسافة‬ ‫تكون‬ ‫متوازنة‬ ‫او‬ ‫كاملة‬ ‫الشجرة‬ ‫تكون‬ ‫عندما‬‫أصغرية‬. 98
  • 99. Binary Search Trees (‫الثنائية‬ ‫البحث‬ ‫)أشجار‬ ■‫التالية‬ ‫الخواص‬ ‫تحقق‬ ‫ثنائية‬ ‫شجرة‬ ‫هي‬: –‫محددة‬ ‫عقدة‬ ‫أجل‬ ‫من‬n‫أصغر‬ ‫هي‬ ‫اليسارية‬ ‫الجزئية‬ ‫الشجرة‬ ‫في‬ ‫المخزنة‬ ‫القيم‬ ‫كل‬ ‫العقدة‬ ‫في‬ ‫المخزنة‬ ‫القيمة‬ ‫من‬n. –‫محددة‬ ‫عقدة‬ ‫أجل‬ ‫من‬n‫أكب‬ ‫هي‬ ‫اليمينية‬ ‫الجزئية‬ ‫الشجرة‬ ‫في‬ ‫المخزنة‬ ‫القيم‬ ‫كل‬‫ر‬ ‫العقدة‬ ‫في‬ ‫المخزنة‬ ‫القيمة‬ ‫من‬n. 99
  • 100. Binary Search Trees (‫الثنائية‬ ‫البحث‬ ‫)أشجار‬ Inserting Method 100
  • 101. Binary Search Trees (‫الثنائية‬ ‫البحث‬ ‫)أشجار‬ Representation Methods ■‫الثنائية‬ ‫األشجار‬ ‫تمثيل‬ ‫طرق‬: –‫المؤشرات‬ ‫طريق‬ ‫عن‬. –‫أعمدتها‬ ‫وعدد‬ ‫العقد‬ ‫بعدد‬ ‫أسطرها‬ ‫عدد‬ ‫يكون‬ ‫حيث‬ ‫المصفوفات‬ ‫طريق‬ ‫عن‬3‫حيث‬ ‫اال‬ ‫رقم‬ ‫الثالث‬ ‫وفي‬ ‫اليميني‬ ‫االبن‬ ‫رقم‬ ‫والثاني‬ ‫العقدة‬ ‫رقم‬ ‫األول‬ ‫العمود‬ ‫يحوي‬‫بن‬ ‫اليسار‬. 101
  • 102. Binary Search Trees (‫الثنائية‬ ‫البحث‬ ‫)أشجار‬ Question 3 ■‫السؤال‬:‫تعتبر‬ ‫التالية‬ ‫الشجر‬ ‫من‬ ‫أي‬"‫ثنائية‬ ‫بحث‬ ‫شجرة‬"‫؟‬ ■‫اإلجابة‬:b‫و‬c 102
  • 103. Binary Search Trees (‫الثنائية‬ ‫البحث‬ ‫)أشجار‬ Question 4 ■‫السؤال‬:‫المصفوفات؟؟‬ ‫طريقة‬ ‫سيئة‬ ‫ماهي‬ ■‫اإلجابة‬:‫الكاملة‬ ‫أو‬ ‫التامة‬ ‫غير‬ ‫األشجار‬ ‫حالة‬ ‫في‬ ‫للذاكرة‬ ‫كبير‬ ‫هدر‬ ‫فيها‬. 103
  • 104. Binary Search Trees (‫الثنائية‬ ‫البحث‬ ‫)أشجار‬ AVL (Height-balanced Trees) ■ An AVL tree (or height-balanced tree) is a binary search tree such that: – The height of the left and right subtrees of the root differ by at most 1 – The left and right subtrees of the root are AVL trees 104
  • 105. Binary Search Trees (‫الثنائية‬ ‫البحث‬ ‫)أشجار‬ DFS ■‫الشجرة‬ ‫في‬ ‫التجول‬ ‫طرق‬: –‫أوال‬ ‫بالعمق‬ ‫التجول‬(depth first search) 105 33 -1 6 22 16 8 7 45 start ‫التجول‬ ‫في‬ ‫العقد‬ ‫زيارة‬ ‫ترتيب‬VLR: 22,6,-1,8,7,16,33,45
  • 106. Binary Search Trees (‫الثنائية‬ ‫البحث‬ ‫)أشجار‬ DFS ■‫الشجرة‬ ‫في‬ ‫التجول‬ ‫طرق‬:‫أوال‬ ‫بالعمق‬ ‫التجول‬(depth first search) –VLR‫الترتي‬ ‫ويدعى‬ ،‫اليمينية‬ ‫ثم‬ ‫اليسارية‬ ‫الجزئية‬ ‫الشجرة‬ ‫ثم‬ ‫العقدة‬ ‫يزور‬‫المصدر‬ ‫ب‬ (preorder.) –VRL‫اليسارية‬ ‫ثم‬ ‫اليمينية‬ ‫الجزئية‬ ‫الشجرة‬ ‫ثم‬ ‫العقدة‬ ‫يزور‬. –LVR‫الترتي‬ ‫ويدعى‬ ‫اليمينية‬ ‫ثم‬ ‫العقدة‬ ‫ثم‬ ‫اليسارية‬ ‫الجزئية‬ ‫الشجرة‬ ‫يزور‬‫المتناظر‬ ‫ب‬ (inorder.) –RVL –LRV‫الملحق‬ ‫الترتيب‬ ‫ويدعى‬(postorder.) –RLV 106
  • 107. Binary Search Trees (‫الثنائية‬ ‫البحث‬ ‫)أشجار‬ BFS ■‫الشجرة‬ ‫في‬ ‫التجول‬ ‫طرق‬: –‫أوال‬ ‫بالعرض‬ ‫التجول‬(breadth first search.) 107 33 -1 6 22 16 8 7 45 ‫التجول‬ ‫في‬ ‫العقد‬ ‫زيارة‬ ‫ترتيب‬VLR: 22,6,33,-1,8,45,7,16
  • 109. Graphs (‫)البيانات‬ ■‫كعقد‬ ‫العناصر‬ ‫تخزن‬ ‫معطيات‬ ‫بنية‬ ‫هي‬vertex‫العق‬ ‫بين‬ ‫العالقات‬ ‫تمثل‬ ‫وصالت‬ ‫بينها‬ ‫ويوجد‬‫د‬ edges. ■‫النظ‬ ‫الناحية‬ ‫من‬ ‫التسلسلية‬ ‫غير‬ ‫الهرمية‬ ‫للبنى‬ ‫تعميما‬ ‫البيانات‬ ‫اعتبار‬ ‫يمكن‬‫فاألشجار‬ ،‫رية‬ ‫البيانات‬ ‫من‬ ‫خاصة‬ ‫حالة‬ ‫هي‬(‫حلقات‬ ‫تحوي‬ ‫ال‬ ‫بيانات‬.) 109
  • 110. Graphs (‫)البيانات‬ ■‫مكان‬ ‫كل‬ ‫في‬ ‫البيانات‬ ‫تتواجد‬: –‫الحواسيب‬ ‫شبكات‬ ،‫الجوية‬ ‫الخطوط‬ ،‫الطرق‬ ‫شبكات‬. ■‫الصنعي‬ ‫الذكاء‬ ‫في‬ ‫المسائل‬ ‫حل‬ ‫منهجيات‬ ‫أهم‬ ‫من‬: –‫بيان‬ ‫في‬ ‫بحث‬ ‫مسألة‬ ‫إلى‬ ‫المسألة‬ ‫تحويل‬. 110
  • 111. Graphs (‫)البيانات‬ ■‫العقدة‬ ‫درجة‬Deg(v)‫إليها‬ ‫والواردة‬ ‫العقدة‬ ‫من‬ ‫الصادرة‬ ‫األسهم‬ ‫عدد‬ ‫هي‬ –‫حيث‬Deg-(v)‫الواردة‬ ‫الدرجة‬ ‫هي‬. –‫و‬Deg+(v)‫الصادرة‬ ‫الدرجة‬ ‫هي‬. ■‫عقدتين‬ ‫كل‬ ‫حالة‬ ‫في‬ ‫كان‬ ‫إذا‬ ‫بشدة‬ ‫متصل‬ ‫البيان‬ ‫يكون‬v,w‫من‬ ‫طريق‬ ‫يوجد‬v‫إلى‬w‫و‬ ‫من‬w‫إلى‬v ■‫يحوي‬ ‫موجه‬ ‫غير‬ ‫بيان‬ ‫في‬ ‫األعظمي‬ ‫الروابط‬ ‫عدد‬n‫يساوي‬ ‫عقدة‬ 𝑛(𝑛−1) 2 ‫هذه‬ ‫وفي‬ ‫تام‬ ‫إنه‬ ‫البيان‬ ‫عن‬ ‫نقول‬ ‫الحالة‬(Complete.) 111
  • 112. Graphs (‫)البيانات‬ ■‫التجاور‬ ‫مصفوفة‬ ‫باستخدام‬ ‫البيان‬ ‫تمثيل‬(adjacency matrix.) 112 The Graph The Adjacency Matrix
  • 113. Graphs (‫)البيانات‬ ■‫التجاور‬ ‫قائمة‬ ‫باستخدام‬ ‫البيان‬ ‫تمثيل‬(adjacency list.) 113The Adjacency List The Graph
  • 114. Graphs (‫)البيانات‬ Question 1 ■‫السؤال‬:‫التجاور؟‬ ‫قائمة‬ ‫بطريقة‬ ‫ممثلة‬ ‫لعقدة‬ ‫مجاورة‬ ‫عقد‬ ‫عن‬ ‫البحث‬ ‫كلفة‬ ‫ماهي‬ ■‫الجواب‬:O(deg(v)) ■‫السؤال‬:‫التج‬ ‫قائمة‬ ‫بطريقة‬ ‫ممثل‬ ‫بيان‬ ‫في‬ ‫لعقدة‬ ‫الواردة‬ ‫الروابط‬ ‫مسح‬ ‫كلفة‬ ‫ماهي‬‫اور؟‬ ■‫الجواب‬:O(V+E)‫جدا‬ ‫مكلف‬ 114
  • 115. Graphs (‫)البيانات‬ ■‫البيان‬ ‫في‬ ‫التجول‬ ‫طرق‬: –‫أوال‬ ‫العمق‬(DFS.) –‫أوال‬ ‫العرض‬(BFS:)‫الوصول‬ ‫ألن‬ ‫موزون‬ ‫غير‬ ‫بيان‬ ‫في‬ ‫طريق‬ ‫أقصر‬ ‫إليجاد‬ ‫طريقة‬ ‫هي‬ ‫للروابط‬ ‫عدد‬ ‫أقل‬ ‫بمسح‬ ‫يتم‬ ‫للعقد‬. 115
  • 119. Graphs (‫)البيانات‬ Dijkstra 120 1 6 2 5 4 3 8 9 7 4 7 12 3 4 5 11 8 6 9 2 1 2 3 4 5 6 7 8 9 0 ? ? ? ? ? ? ? ? -1 -1 -1 -1 -1 -1 -1 -1 -1 Processed distance predecessor
  • 120. Graphs (‫)البيانات‬ Topological Sort ■‫الفرز‬‫الطبولوجي‬‫األسهم‬ ‫كل‬ ‫تكون‬ ‫حيث‬ ‫ورودها‬ ‫درجات‬ ‫حسب‬ ‫العقد‬ ‫ترتيب‬ ‫على‬ ‫يعتمد‬‫او‬ ‫اليمين‬ ‫هي‬ ‫واحدة‬ ‫جهة‬ ‫إلى‬ ‫تؤشر‬ ‫الوصالت‬. ■‫حلقات‬ ‫على‬ ‫يحتوي‬ ‫الذي‬ ‫البيان‬ ‫على‬ ‫تطبيقها‬ ‫يمكن‬ ‫ال‬. ■‫مثال‬: –‫لجامعة‬ ‫السنوي‬ ‫المنهاج‬. 121
  • 121. Graphs (‫)البيانات‬ topological sort 122 1 2 4 5 3 6 7 8 next 0 1 2 3 4 5 6 7 8 2 2 3 2 1 1 0 2 Node Indegree LIST 7
  • 122. Graphs (‫)البيانات‬ minimum spanning tree ■‫االرتباط‬ ‫شجرة‬‫األصغرية‬:‫الروابط‬ ‫من‬ ‫ممكنة‬ ‫كلفة‬ ‫بأقل‬ ‫البيان‬ ‫تربط‬ ‫شجرة‬ ‫هي‬. ■‫فقط‬ ‫المترابط‬ ‫البيان‬ ‫حالة‬ ‫في‬ ‫الشجرة‬ ‫هذه‬ ‫تتواجد‬. ■Prim’s algorithm 123 a c e d b 2 45 9 6 4 5 5 a c e d b 2 45 9 6 4 5 5
  • 124. Bubble Sort Algorithm ( ‫الفرز‬ ‫خوارزمية‬‫الفقاعي‬ ) ■‫الفرز‬ ‫خوارزمية‬ ‫تعقيد‬‫الفقاعي‬: –‫األحوال‬ ‫أحسن‬:‫حيث‬ ‫مفروزة‬ ‫المصفوفة‬ ‫تكون‬ ‫عندما‬‫اليوجد‬‫اسناد‬ ‫عمليات‬. –‫األحوال‬ ‫أسوأ‬:‫المصفوفة‬ ‫تكون‬ ‫عندما‬‫مرتية‬‫عمليات‬ ‫عدد‬ ‫يكون‬ ‫عكسي‬ ‫ترتيب‬ ‫مرتبة‬ ‫من‬ ‫االسناد‬O(n*n). –‫الوسطية‬ ‫الحالة‬:ً‫عشوائيا‬ ‫مرتبة‬ ‫المصفوفة‬ ‫تكون‬ ‫عندما‬O(n*n). 125
  • 125. Selection Sort Algorithm (‫باالختيار‬ ‫الفرز‬ ‫)خوارزمية‬ ■‫الخوارزمية‬ ‫تعقيد‬: –‫المقارنة‬ ‫عمليات‬ ‫عدد‬O(n*n) –‫التبديل‬ ‫عمليات‬ ‫عدد‬O(n) ■‫مكل‬ ‫غير‬ ‫المقارنة‬ ‫وعملية‬ ‫مكلفة‬ ‫التبديل‬ ‫عملية‬ ‫كانت‬ ‫حالة‬ ‫وفي‬ ‫صغير‬ ‫لحجم‬ ‫جيدة‬‫فة‬. 126
  • 126. Insertion Sort Algorithm (‫باإلضافة‬ ‫الفرز‬ ‫)خوارزمية‬ ■‫باإلضافة‬ ‫الفرز‬ ‫خوارزمية‬ ‫تعقيد‬: –‫األحوال‬ ‫أحسن‬:‫اسناد‬ ‫عمليات‬ ‫يوجد‬ ‫ال‬ ‫حيث‬ ‫مفروزة‬ ‫المصفوفة‬ ‫تكون‬ ‫عندما‬O(1) ‫المقارنة‬ ‫وعمليات‬O(n). –‫األحوال‬ ‫أسوأ‬:‫المصفوفة‬ ‫تكون‬ ‫عندما‬‫مرتية‬‫عمليات‬ ‫عدد‬ ‫يكون‬ ‫عكسي‬ ‫ترتيب‬ ‫مرتبة‬ ‫من‬ ‫االسناد‬O(n*n). –‫الوسطية‬ ‫الحالة‬:ً‫عشوائيا‬ ‫مرتبة‬ ‫المصفوفة‬ ‫تكون‬ ‫عندما‬O(n*n). 127
  • 127. Merge Sort Algorithm (‫بالدمج‬ ‫الفرز‬ ‫)خوارزمية‬ ■‫تسد‬ ‫فرق‬ ‫مبدأ‬ ‫تعتمد‬. ■‫يلي‬ ‫بما‬ ‫تقوم‬ ‫عوديه‬ ‫خوارزمية‬ ‫هي‬: –،‫جزئين‬ ‫إلى‬ ‫المصفوفة‬ ‫تقسم‬ –،‫حدا‬ ‫على‬ ‫جزء‬ ‫كل‬ ‫تفرز‬ –‫المفروزين‬ ‫الجزئيين‬ ‫تدمج‬ ‫مفروزة‬ ‫واحدة‬ ‫مصفوفة‬ ‫في‬. ■‫الخوارزمية‬ ‫تعقيد‬: –O(nlog(n))‫دوما‬ 128
  • 128. Quick Sort Algorithm (‫السريع‬ ‫الفرز‬ ‫)خوارزمية‬ ■‫الخوارزمية‬ ‫تعقيد‬: –O(nlog(n))‫الوسطية‬ ‫والحالة‬ ‫األحوال‬ ‫أحسن‬ ‫في‬. –O(n*n)‫األحوال‬ ‫أسوأ‬ ‫في‬. 129
  • 130. SEARCH ALGORITHMS ‫في‬ ‫البحث‬ ‫خوارزميات‬‫مصفوقة‬ 131
  • 131. Search Algorithms Binary V.S. Sequential ■‫التسلسلي‬ ‫البحث‬:‫تعقيده‬O(n) ■‫الثنائي‬ ‫البحث‬:‫تعقيده‬O(log(n)) 132
  • 133. Backtracking Algorithms (‫التراجعية‬ ‫)الخوارزميات‬ ■‫خوارزميات‬ ‫هي‬‫عودية‬‫المشاكل‬ ‫حل‬ ‫في‬ ‫والخطأ‬ ‫التجريب‬ ‫طريقة‬ ‫تعتمد‬. ■‫الخطوات‬ ‫من‬ ‫مجموعة‬ ‫طريق‬ ‫عن‬ ‫للمسألة‬ ‫النهائي‬ ‫الحل‬ ‫ببناء‬ ‫الطريقة‬ ‫هذه‬ ‫تتلخص‬: –،‫التالية‬ ‫للخطوة‬ ‫المتاحة‬ ‫اإلمكانيات‬ ‫نحدد‬ ‫الخطوة‬ ‫في‬ –،‫أحدها‬ ‫ننتقي‬ ‫بأن‬ ‫اإلمكانيات‬ ‫هذه‬ ‫ندرس‬ ‫ثم‬ –،‫النهائي‬ ‫الحل‬ ‫في‬ ‫التالية‬ ‫الخطوة‬ ‫أنها‬ ‫على‬ ‫نسجلها‬ ‫و‬ –‫الخطوة‬ ‫هذه‬ ‫على‬ ً‫اعتمادا‬ ‫الخوارزمية‬ ‫نتابع‬ ‫و‬. –‫مسدو‬ ‫طريق‬ ‫إلى‬ ‫يؤدي‬ ‫أو‬ ،‫النهائي‬ ‫الحل‬ ‫إلى‬ ‫يقود‬ ‫ال‬ ‫اختيارنا‬ ‫أن‬ ‫لنا‬ ‫يظهر‬ ‫عندما‬،‫د‬ –‫الخطوة‬ ‫هذه‬ ‫عن‬ ‫نعدل‬. ■‫مرتين‬ ‫الخطأ‬ ‫في‬ ‫الوقوع‬ ‫تجنب‬ ‫في‬ ‫يفيد‬ ‫أخطاء‬ ‫سجل‬ ‫بناء‬ ‫العملية‬ ‫هذه‬ ‫تشبه‬. 134
  • 134. Backtracking Algorithms (‫التراجعية‬ ‫)الخوارزميات‬ ■‫مثال‬: –‫الشطرنج‬ ‫حصان‬ ‫جولة‬ ‫مسألة‬: ■‫باست‬ ‫وذلك‬ ‫الحصان‬ ‫فيه‬ ‫يقف‬ ‫معين‬ ‫موقع‬ ‫من‬ ‫الشطرنج‬ ‫رقعة‬ ‫لتغطية‬ ‫طريقة‬ ‫إيجاد‬‫خدام‬ ‫واحدة‬ ‫مرة‬ ‫مربع‬ ‫بكل‬ ‫المرور‬ ‫وبشرط‬ ‫الشطرنج‬ ‫في‬ ‫المعروفة‬ ‫الحصان‬ ‫حركات‬. –‫الثمانية‬ ‫الوزراء‬ ‫مسألة‬: ■‫لألخر‬ ‫مهدد‬ ‫واحد‬ ‫اي‬ ‫فيها‬ ‫يكون‬ ‫ال‬ ‫بحيث‬ ‫الشطرنج‬ ‫رقعة‬ ‫على‬ ‫وزراء‬ ‫ثمانية‬ ‫توزيع‬. –‫األمثل‬ ‫الخيار‬ ‫مسألة‬: ■‫معاي‬ ‫وفق‬ ‫األمثل‬ ‫بالحل‬ ‫واالحتفاظ‬ ‫معينة‬ ‫لمسألة‬ ‫ايجادها‬ ‫يمكن‬ ‫التي‬ ‫الحلول‬ ‫مقارنة‬‫ير‬ ‫للمسألة‬ ‫محددة‬. 135
  • 135. Greedy Algorithms (‫الشجعة‬ ‫)الخوارزميات‬ ■‫هي‬‫خوارزمية‬‫على‬ ‫تستند‬ ‫التي‬‫المهني‬ ‫الحدس‬‫اإلمكاني‬ ‫اختيار‬ ‫طريقه‬ ‫عن‬ ‫يتم‬ ‫الذي‬‫ة‬ ‫الحالية‬ ‫المرحلة‬ ‫في‬ ‫المرئية‬ ‫األفضل‬(‫الحالية‬ ‫المرحلة‬ ‫في‬ ‫األمثل‬ ‫الحل‬),‫األخ‬ ‫دون‬ ‫من‬‫ذ‬ ‫الحل‬ ‫تكملة‬ ‫على‬ ‫الخطوة‬ ‫هذه‬ ‫تأثير‬ ‫بالحسبان‬. ■‫للمسألة‬ ‫األمثل‬ ‫الحل‬ ‫إلى‬ ‫المرحلي‬ ‫األمثل‬ ‫الحل‬ ‫يقودنا‬ ‫ال‬ ‫قد‬. ■‫خياراتها‬ ‫عن‬ ‫تتراجع‬ ‫ان‬ ‫يكن‬ ‫ال‬ ‫الجشع‬ ‫خوارزمية‬.‫بينها‬ ‫الرئيسي‬ ‫الفرق‬ ‫هو‬ ‫هذا‬ ‫وبين‬‫الديناميكية‬ ‫البرمجة‬. ■‫وأشجار‬ ‫برايم‬ ‫خوارزمية‬ ‫مثل‬‫هوفمان‬. 136
  • 136. Divide and Conquer (‫تسد‬ ‫فرق‬ ‫)خوارزميات‬ ■‫عمل‬‫خوارزمية‬‫مسألتين‬ ‫إلى‬ ‫عودي‬ ‫بشكل‬ ‫المسألة‬ ‫تقسيم‬ ‫طريق‬ ‫عن‬ ‫تسد‬ ‫فرق‬ ‫فيه‬ ‫بما‬ ‫بسيطة‬ ‫الجزئية‬ ‫المسائل‬ ‫تصبح‬ ‫حتى‬ ،‫النوع‬ ‫نفس‬ ‫من‬ ‫أكثر‬ ‫أو‬ ‫جزئيتين‬‫الكفاية‬ ‫مباشر‬ ‫بشكل‬ ‫لتحل‬.‫ا‬ ‫للمسألة‬ ً‫حال‬ ‫لتعطي‬ ‫الجزئية‬ ‫المسائل‬ ‫حلول‬ ‫تدمج‬ ‫ثم‬ ‫ومن‬‫لجزئية‬. ■‫امثلة‬: –‫الثنائي‬ ‫البحث‬ ‫خوارزميات‬. –‫بالدمج‬ ‫الفرز‬ ‫خوارزميات‬. –‫السريع‬ ‫الفرز‬ ‫خوارزميات‬. –‫هانوي‬ ‫ابراج‬ ‫خوارزمية‬. 137
  • 137. Dynamic Programming (‫الديناميكية‬ ‫)البرمجة‬ ■‫المسألة‬ ‫من‬ ‫مختلفة‬ ‫أجزاء‬ ‫حل‬ ‫إلى‬ ‫بحاجة‬ ‫نحن‬ ،‫ما‬ ‫مسألة‬ ‫لحل‬(‫فرعية‬ ‫مسائل‬)‫ومن‬ ، ‫شامل‬ ‫حل‬ ‫على‬ ‫للحصول‬ ‫الفرعية‬ ‫المسائل‬ ‫حلول‬ ‫جمع‬ ‫ثم‬. ■‫الديناميكية‬ ‫البرمجة‬ ‫نهج‬: –،‫فقط‬ ‫واحدة‬ ‫مرة‬ ‫فرعية‬ ‫مسألة‬ ‫كل‬ ‫حل‬ ‫عن‬ ‫البحث‬ –‫الحسابات‬ ‫عدد‬ ‫تقليل‬ ‫وبالتالي‬:‫حف‬ ‫يتم‬ ،‫ما‬ ‫فرعية‬ ‫مسألة‬ ‫حل‬ ‫حساب‬ ‫تم‬ ‫حالما‬،‫ظه‬ ‫استرجاعه‬ ‫ببساطة‬ ‫يتم‬ ،‫نفسه‬ ‫للحل‬ ‫الحاجة‬ ‫عند‬ ‫القادمة‬ ‫المرة‬ ‫وفي‬. ■‫وتق‬ ‫الثانوية‬ ‫للمسائل‬ ‫السابقة‬ ‫الحلول‬ ‫ستدرس‬ ‫الديناميكية‬ ‫البرمجة‬ ‫خوارزميات‬‫بدمجها‬ ‫وم‬ ‫حله‬ ‫المراد‬ ‫للمسائلة‬ ‫حل‬ ‫افضل‬ ‫علي‬ ‫للحصول‬. 138
  • 139. Hashing Tables (‫التقطيع‬ ‫)جداول‬ ■‫موقعه‬ ‫إلى‬ ‫المباشر‬ ‫النفاذ‬ ‫خالل‬ ‫من‬ ‫بحث‬ ‫خوارزمية‬ ‫إلى‬ ‫الحاجة‬ ‫دون‬ ‫مباشرة‬ ‫عنصر‬ ‫لإليجاد‬ ‫طريقة‬ ‫هي‬. ■‫التالية‬ ‫التطبيقات‬ ‫برمجة‬ ‫في‬ ‫فعالة‬ ‫بنية‬ ‫هي‬: –‫معجم‬ ‫عمليات‬ ‫إلى‬ ‫تحتاج‬ ‫التي‬. –‫بالترتيب‬ ‫تهتم‬ ‫ال‬ ‫التي‬ ‫االمثلة‬. ■‫المفتاح‬ ‫ذو‬ ‫العنصر‬ ‫يخزن‬k‫الخانة‬ ‫في‬h(k)‫حيث‬h‫الخانة‬ ‫دليل‬ ‫لحساب‬ ‫يستخدم‬ ‫الذي‬ ‫التقطيع‬ ‫تابع‬ ‫هو‬ ‫المفتاح‬ ‫من‬k. ■‫التقطيع‬ ‫تابع‬ ‫قيمة‬ ‫نفس‬ ‫لهما‬ ‫مفتاحين‬ ‫وجود‬ ‫حال‬ ‫في‬ ‫تصادم‬ ‫يحدث‬ ‫قد‬. ■‫من‬ ‫ازواج‬ ‫شكل‬ ‫على‬ ‫القيم‬ ‫تخزن‬(key, value) ■‫القيم‬ ‫من‬ ‫سلسلة‬ ‫حول‬ ‫للتجول‬ ‫جيدة‬ ‫بنية‬ ‫ليست‬ ‫التقطيع‬ ‫جداول‬( .Iteration) ■‫مناسبة‬ ‫فرضيات‬ ‫ضمن‬ ‫تقطيع‬ ‫جدول‬ ‫في‬ ‫عنصر‬ ‫عن‬ ‫والبحث‬ ‫إلضافة‬ ‫المتوقع‬ ‫الزمن‬O(1)( .‫الوسطية‬ ‫الحالة‬) 140
  • 140. Hashing Tables V.S. Other Data Structures ■ We want to implement the dictionary operations Insert(), Delete() and Search()/Find() efficiently. ■ Arrays: – can accomplish in O(n) time – but are not space efficient (assumes we leave empty space for keys not currently in dictionary) ■ Binary search trees – can accomplish in O(log n) time – are space efficient. ■ Hash Tables: – A generalization of an array that under some reasonable assumptions is O(1) for Insert/Delete/Search of a key 141
  • 141. Hashing Tables (‫التقطيع‬ ‫)جداول‬ ■‫المباشرة‬ ‫العنونة‬ ‫باستخدام‬ ‫المعلومات‬ ‫تخزين‬ ‫عن‬ ‫مثال‬: ■‫مفتاح‬ ‫لكل‬ ‫الجدول‬ ‫من‬ ‫موقع‬ ‫منح‬ ‫نستطيع‬ ‫عندما‬ ‫تستخدم‬(‫ت‬ ‫المتوقع‬ ‫العناصر‬ ‫عدد‬‫خزينها‬ ‫للعناصر‬ ‫الكلي‬ ‫العدد‬ ‫من‬ ‫قريب‬. 142 Anas - Mazzah 0000001 6131467 6131468 6131469 6131470 9999997 9999998 9999999 Each phone number uniquely determines an array element 0000000 Record for phone number 6131496 For technical or commercial reasons, certain numbers may not be assigned to any subscriber
  • 142. Hashing Tables (‫التقطيع‬ ‫)جداول‬ ■ For example, if we hash keys 0…1000 into a hash table with 5 entries and use h(key) = key mod 5 , we get the following sequenceof events: 143 0 1 2 3 4 key data Insert 2 2 … 0 1 2 3 4 key data Insert 21 2 … 21 … 0 1 2 3 4 key data Insert 34 2 … 21 … 34 … Insert 54 There is a collisionat array entry #4 ???
  • 143. Hashing Tables (‫التقطيع‬ ‫)جداول‬ ■‫التصادم‬ ‫لحل‬ ‫الربط‬ ‫تقنية‬: –‫خطية‬ ‫سلسلة‬ ‫في‬ ‫التقطيع‬ ‫تابع‬ ‫قيمة‬ ‫نفس‬ ‫لها‬ ‫التي‬ ‫العناصر‬ ‫جميع‬ ‫توضع‬. (Hashing with Chaining) –‫المتصادمة‬ ‫العناصر‬ ‫سلسلة‬ ‫بداية‬ ‫إلى‬ ‫مؤشر‬ ‫الجدول‬ ‫خانة‬ ‫تصبح‬. 144
  • 144. Hashing Tables (‫التقطيع‬ ‫)جداول‬ Hashing with Chaining 145 0 1 2 3 4 other key key data Insert 54 2 21 54 34 CHAIN 0 1 2 3 4 Insert 101 2 21 54 34 101
  • 145. Hashing Tables (‫التقطيع‬ ‫)جداول‬ Hashing with Chaining ■ What is the running time to insert/search/delete? – Insert: It takes O(1) time to computethe hash function and insert at head of linked list – Search: It is proportional to max linked list length – Delete: Same as search 146
  • 146. Hashing Tables (‫التقطيع‬ ‫)جداول‬ Question 1 ■‫السؤال‬:‫عنصر؟‬ ‫إضافة‬ ‫عملية‬ ‫كلفة‬ ‫ماهي‬ ■‫الجواب‬:O(1)‫السلسلة‬ ‫رأس‬ ‫في‬ ‫المتصادمة‬ ‫للعناصر‬ ‫اإلضافة‬ ‫تتم‬ ‫حيث‬. ■‫السؤال‬:‫األحوال؟‬ ‫أسوأ‬ ‫في‬ ‫والحذف‬ ‫البحث‬ ‫عملية‬ ‫كلفة‬ ‫ماهي‬ ■‫الجواب‬:‫االحوال‬ ‫أسوأ‬ ‫في‬ ‫وهي‬ ‫المتصادمة‬ ‫للعناصر‬ ‫الخطية‬ ‫السلسلة‬ ‫بطول‬ ‫تتعلق‬O(n)‫عند‬ ‫سيء‬ ‫تقطيع‬ ‫تابع‬ ‫اختيار‬. ■‫السؤال‬:‫واالضافة؟‬ ‫للحذف‬ ‫الوسطية‬ ‫الكلفة‬ ‫ماهي‬ ■‫الجواب‬:‫م‬ ‫توزع‬ ‫يحقق‬ ‫تقطيع‬ ‫تابع‬ ‫استخدام‬ ‫عند‬ ‫السلسلة‬ ‫في‬ ‫الوسطي‬ ‫العناصر‬ ‫عدد‬ ‫هو‬‫نتظم‬ ‫وهو‬n/m‫حيث‬n‫و‬ ‫الكلي‬ ‫العناصر‬ ‫عدد‬m‫الجدول‬ ‫في‬ ‫الخانات‬ ‫عدد‬. 147
  • 147. Hashing Function (‫التقطيع‬ ‫)توابع‬ ■‫التقسيم‬ ‫طريقة‬: –‫المفتاح‬ ‫بربط‬k‫قسمة‬ ‫باقي‬ ‫تساوي‬ ‫التي‬ ‫بالخانة‬k‫على‬m. –h(k)=k%m –‫قيم‬ ‫تجنب‬ ‫يجب‬m‫قوى‬ ‫من‬2‫عنها‬ ‫بعيدة‬ ‫أولية‬ ‫أعداد‬ ‫واختيار‬. ■‫الضرب‬ ‫طريقة‬: –‫المفتاح‬ ‫نضرب‬k‫بثابت‬A‫حيث‬0<A<1‫القيمة‬ ‫هذه‬ ‫نضرب‬ ‫ثم‬ ‫الكسري‬ ‫الجزء‬ ‫ونأخذ‬ ‫ب‬m‫الناتج‬ ‫العدد‬ ‫من‬ ‫الطبيعي‬ ‫الجزء‬ ‫ونأخذ‬. –‫للعدد‬ ‫قيمة‬ ‫أي‬ ‫اختيار‬ ‫يمكن‬m. 148
  • 149. Question 1 ■ For a binary search algorithm to work, it is necessarythat the array (list) must be? A. sorted B. unsorted C. in a heap D. popped out of stack 153
  • 150. Question 1 ■ For a binary search algorithm to work, it is necessarythat the array (list) must be? A. sorted B. unsorted C. in a heap D. popped out of stack 154
  • 151. Question 2 ■ Which one of the below mentioned is linear data structure? A. Queue B. Stack C. Arrays D. All of the above 155
  • 152. Question 2 ■ Which one of the below mentioned is linear data structure? A. Queue B. Stack C. Arrays D. All of the above 156
  • 153. Question 3 ■ Binary search tree has best case run-time complexity of Ο(log n). What could the worst case? A. Ο(n) B. Ο(n2) C. Ο(n3) D. None of the above 157
  • 154. Question 3 ■ Binary search tree has best case run-time complexity of Ο(log n). What could the worst case? A. Ο(n) B. Ο(n2) C. Ο(n3) D. None of the above 158
  • 155. Question 4 ■ An algorithm is? A. a piece of code to be executed. B. a loosely written code to make final code. C. a step by step procedureto solve problem. D. all of the above. 159
  • 156. Question 4 ■ An algorithm is? A. a piece of code to be executed. B. a loosely written code to make final code. C. a step by step procedureto solve problem. D. all of the above. 160
  • 157. Question 5 ■ Quick sort running time depends on the selection of? A. size of array B. pivot element C. sequenceof values D. none of the above! 161
  • 158. Question 5 ■ Quick sort running time depends on the selection of? A. size of array B. pivot element C. sequenceof values D. none of the above! 162
  • 159. Question 6 ■ A queue data-structurecan be used for? A. expression parsing B. recursion C. resourceallocation D. all of the above 163
  • 160. Question 6 ■ A queue data-structurecan be used for? A. expression parsing B. recursion C. resourceallocation D. all of the above 164
  • 161. Question 7 ■ Program with highest run-time complexity is? A. Tower of Hanoi B. FibonacciSeries C. Prime Number Series D. None of the above 165
  • 162. Question 7 ■ Program with highest run-time complexity is? A. Tower of Hanoi B. FibonacciSeries C. Prime Number Series D. None of the above 166
  • 163. Question 8 ■ From a complete graph, by removing maximum _______________ edges, we can constructa spanning tree? A. e-n+1 B. n-e+1 C. n+e-1 D. e-n-1 167
  • 164. Question 8 ■ From a complete graph, by removing maximum _______________ edges, we can constructa spanning tree? A. e-n+1 B. n-e+1 C. n+e-1 D. e-n-1 168
  • 165. Question 9 ■ The worst case complexity of binary search matches with? A. interpolation search B. linear search C. merge sort D. none of the above 169
  • 166. Question 9 ■ The worst case complexity of binary search matches with? A. interpolation search B. linear search C. merge sort D. none of the above 170
  • 167. Question 10 ■ What is a hash table? A. A structurethat maps values to keys. B. A structurethat maps keys to values. C. A structureused for storage. D. A structureused to implement stack and queue. 171
  • 168. Question 10 ■ What is a hash table? A. A structurethat maps values to keys. B. A structurethat maps keys to values. C. A structureused for storage. D. A structureused to implement stack and queue. 172
  • 169. Question 11 ■ The recurrencerelation capturing the optimal time of the Tower of Hanoi problem with n discs is A. T(n) = 2T(n – 2) + 2 B. T(n) = 2T(n – 1) + n C. T(n) = 2T(n/2) + 1 D. T(n) = 2T(n – 1) + 1 173
  • 170. Question 11 ■ The recurrencerelation capturing the optimal time of the Tower of Hanoi problem with n discs is A. T(n) = 2T(n – 2) + 2 B. T(n) = 2T(n – 1) + n C. T(n) = 2T(n/2) + 1 D. T(n) = 2T(n – 1) + 1 174
  • 171. Question 12 ■ What does the following function do for a given Linked List with first node as head? void fun1(structnode* head) { if(head == NULL) return; fun1(head->next); printf("%d ", head->data); } A. Prints all nodes of linked lists B. Prints all nodes of linked list in reverseorder C. Prints alternate nodes of Linked List D. Prints alternate nodes in reverseorder 175
  • 172. Question 12 ■ What does the following function do for a given Linked List with first node as head? void fun1(structnode* head) { if(head == NULL) return; fun1(head->next); printf("%d ", head->data); } A. Prints all nodes of linked lists B. Prints all nodes of linked list in reverseorder C. Prints alternate nodes of Linked List D. Prints alternate nodes in reverseorder 176
  • 173. Question 13 ■ Which of the following points is/are true about Linked List data structure when it is compared with array A. Arrays have better cache locality that can make them better in terms of performance B. It is easy to insert and delete elements in Linked List C. Randomaccess is not allowed in a typical implementation of Linked Lists D. All of the above 177
  • 174. Question 13 ■ Which of the following points is/are true about Linked List data structure when it is compared with array A. Arrays have better cache locality that can make them better in terms of performance B. It is easy to insert and delete elements in Linked List C. Randomaccess is not allowed in a typical implementation of Linked Lists D. All of the above 178
  • 175. Question 14 ■ Which of the following sorting algorithms can be used to sort a random linked list with minimum time complexity? A. Insertion Sort B. Quick Sort C. Heap Sort D. Merge Sort 179
  • 176. Question 14 ■ Which of the following sorting algorithms can be used to sort a random linked list with minimum time complexity? A. Insertion Sort B. Quick Sort C. Heap Sort D. Merge Sort 180
  • 177. Question 15 ■ In the worst case, the number of comparisons needed to search a singly linked list of length n for a given element is A. log 2 n B. n/2 C. log 2 n – 1 D. n 181
  • 178. Question 15 ■ In the worst case, the number of comparisons needed to search a singly linked list of length n for a given element is A. log 2 n B. n/2 C. log 2 n – 1 D. n 182
  • 179. Question 16 ■ Which one of the following is an application of Stack Data Structure? A. Managing function calls B. The stock span problem C. Arithmetic expression evaluation D. All of the above 183
  • 180. Question 16 ■ Which one of the following is an application of Stack Data Structure? A. Managing function calls B. The stock span problem C. Arithmetic expression evaluation D. All of the above 184
  • 181. Question 17 ■ The result evaluating the postfix expression 10 5 + 60 6 / * 8 – is A. 284 B. 213 C. 142 D. 71 185
  • 182. Question 17 ■ The result evaluating the postfix expression 10 5 + 60 6 / * 8 – is A. 284 B. 213 C. 142 D. 71 186
  • 183. Question 18 ■ How many stacks are needed to implement a queue. Consider the situation where no other data structurelike arrays, linked list is available to you. A. 1 B. 2 C. 3 D. 4 187
  • 184. Question 18 ■ How many stacks are needed to implement a queue. Consider the situation where no other data structurelike arrays, linked list is available to you. A. 1 B. 2 C. 3 D. 4 188
  • 185. Question 19 ■ The most appropriate matching for the following pairs X: depth first search 1: heap Y: breadth-firstsearch 2: queue Z: sorting 3: stack A. X—1, Y—2 ,Z –3 B. X—3, Y—1, Z –2 C. X—3, Y—2, Z—1 D. X—2 ,Y—3 , Z--1 189
  • 186. Question 19 ■ The most appropriate matching for the following pairs X: depth first search 1: heap Y: breadth-firstsearch 2: queue Z: sorting 3: stack A. X—1, Y—2 ,Z –3 B. X—3, Y—1, Z –2 C. X—3, Y—2, Z—1 D. X—2 ,Y—3 , Z--1 190
  • 187. Question 20 ■ Aliasing in the context of programming languages refers to A. multiple variables having the same memory location B. multiple variables having the same value C. multiple variables having the same identifier D. multiple uses of the same variable 191
  • 188. Question 20 ■ Aliasing in the context of programming languages refers to A. multiple variables having the same memory location B. multiple variables having the same value C. multiple variables having the same identifier D. multiple uses of the same variable 192
  • 189. Question 21 ■ Randomized quicksortis an extension of quicksortwhere the pivot is chosen randomly. What is the worst case complexity of sorting n numbers using randomized quicksort? A. O(n) B. O(n Log n) C. O(n^2) D. O(n!) 193
  • 190. Question 21 ■ Randomized quicksortis an extension of quicksortwhere the pivot is chosen randomly. What is the worst case complexity of sorting n numbers using randomized quicksort? A. O(n) B. O(n Log n) C. O(n^2) D. O(n!) 194
  • 191. Question 22 ■ The number of leaf nodes in a rooted tree of n nodes, with each node having 0 or 3 children is: A. n/2 B. (n - 1)/3 C. (n - 1)/2 D. (2n + 1)/3 195
  • 192. Question 22 ■ The number of leaf nodes in a rooted tree of n nodes, with each node having 0 or 3 children is: A. n/2 B. (n - 1)/3 C. (n - 1)/2 D. (2n + 1)/3 196
  • 193. Question 23 ■ Suppose the numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are inserted in that order into an initially empty binary search tree. The binary search tree uses the usual ordering on natural numbers. What is the in-order traversal sequenceof the resultant tree ? A. 7 5 1 0 3 2 4 6 8 9 B. 0 2 4 3 1 6 5 9 8 7 C. 0 1 2 3 4 5 6 7 8 9 D. 9 8 6 4 2 3 0 1 5 7 197
  • 194. Question 23 ■ Suppose the numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are inserted in that order into an initially empty binary search tree. The binary search tree uses the usual ordering on natural numbers. What is the in-order traversal sequenceof the resultant tree ? A. 7 5 1 0 3 2 4 6 8 9 B. 0 2 4 3 1 6 5 9 8 7 C. 0 1 2 3 4 5 6 7 8 9 D. 9 8 6 4 2 3 0 1 5 7 198
  • 195. Question 24 ■ Consider the following graph. Among the following sequences, Which are depth first traversals of the graph? I) a b e g h f II) a b f e h g III) a b f h g e IV) a f g h b e A. I, II and IV only B. I and IV only C. II, III and IV only D. I, III and IV only 199
  • 196. Question 24 ■ Consider the following graph. Among the following sequences, Which are depth first traversals of the graph? I) a b e g h f II) a b f e h g III) a b f h g e IV) a f g h b e A. I, II and IV only B. I and IV only C. II, III and IV only D. I, III and IV only 200
  • 197. Question 25 ■ What is the weight of a minimum spanning tree of the following graph ? A. 29 B. 31 C. 38 D. 41 201
  • 198. Question 25 ■ What is the weight of a minimum spanning tree of the following graph ? A. 29 B. 31 C. 38 D. 41 202
  • 199. Question 26 ■ The best data structureto check whether an arithmetic expression has balanced parentheses is a A. Queue B. Stack C. Tree D. List 203
  • 200. Question 26 ■ The best data structureto check whether an arithmetic expression has balanced parentheses is a A. Queue B. Stack C. Tree D. List 204
  • 201. Question 27 ■ The elements 32, 15, 20, 30, 12, 25, 16 are inserted one by one in the given order into a Max Heap. The resultant Max Heap is. A. A B. B C. C D. D 205
  • 202. Question 27 ■ The elements 32, 15, 20, 30, 12, 25, 16 are inserted one by one in the given order into a Max Heap. The resultant Max Heap is. A. A B. B C. C D. D 206
  • 203. Question 28 ■ The time complexity of the following C function is (assume n > 0) int recursive (int n) { if (n == 1) return (1); else return (recursive (n - 1) + recursive (n - 1)); } A. O(n) B. O(n log n) C. O(n^2) D. O(2^n) 207
  • 204. Question 28 ■ The time complexity of the following C function is (assume n > 0) int recursive (int n) { if (n == 1) return (1); else return (recursive (n - 1) + recursive (n - 1)); } A. O(n) B. O(n log n) C. O(n^2) D. O(2^n) 208
  • 205. Question 29 ■ A program takes as input a balanced binary search tree with n leaf nodes and computes the value of a function g(x) for each node x. If the cost of computing g(x) is min{no. of leaf-nodes in left-subtreeof x, no. of leaf-nodes in right-subtreeof x} then the worst-casetime complexity of the program is A. Θ(n) B. Θ(n Log n) C. Θ(n^2) D. Θ(n^2 log n) 209
  • 206. Question 29 ■ A program takes as input a balanced binary search tree with n leaf nodes and computes the value of a function g(x) for each node x. If the cost of computing g(x) is min{no. of leaf-nodes in left-subtreeof x, no. of leaf-nodes in right-subtreeof x} then the worst-casetime complexity of the program is A. Θ(n) B. Θ(n Log n) C. Θ(n^2) D. Θ(n^2 log n) 210