You are on page 1of 205

ALGORITHM 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. Instance of 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. Instance of 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 combining various data members into a single unit
B. It is a way of combining various member functions into a single unit
C. It is a way of combining various data members and member functions into a
single unit which can operate on any data
D. It is a way of combining various 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 combining various data members into a single unit
B. It is a way of combining various member functions into a single unit
C. It is a way of combining various data members and member functions into a
single unit which can operate on any data
D. It is a way of combining various 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. Inheritance and 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. Inheritance and 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. Procedure Oriented language
C. Object-based language
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. Procedure Oriented language
C. Object-based language
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 necessary condition 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 necessary condition 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 constructors are available for use in general (with respect to
parameters)?
A. 2
B. 3
C. 4
D. 5

25
Question 12

■ How many types of constructors are 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 constructor is defined and the


object is to be created without arguments.
A. True
B. False

27
Question 13

■ Default constructor must be defined, if parameterized constructor is 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 sequence of constructors getting called?
A. Constructor of C then B, finally of A
B. Constructor of A then C, finally of B
C. Constructor of C then A, finally B
D. Constructor of 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 sequence of constructors getting called?
A. Constructor of C then B, finally of A
B. Constructor of A then C, finally of B
C. Constructor of C then A, finally B
D. Constructor of A then B, finally C

30
‫‪COMPLEXITY‬‬
‫تعقيد الخوارزميات‬

‫‪31‬‬
‫)تعقيد الخوارزميات( ‪Complexity‬‬

‫يرتبط بعاملين اساسيين‪:‬‬


‫■ حجم الذاكرة‪.‬‬
‫■ زمن التنفيذ‪:‬‬
‫– تحديد بعد أو حجم معطيات المسألة من أجل كتابة درجة التعقيد بداللة البعد واختيار‬
‫أنواع العمليات التي يتناسب زمن التنفيذ مع عددها‪.‬‬
‫– أمثلة‪:‬‬
‫مسائل كثيرات الحدود البعد هو درجة كثيرات الحدود‪.‬‬ ‫■‬
‫مسائل المصفوفات بالبعد بداللة أبعاد المصفوفة‪.‬‬ ‫■‬
‫مسائل البيانات البعد بداللة عدد العقد أو األسهم‪.‬‬ ‫■‬
‫مسائل الفرز البعد بداللة عدد العناصر التي نريد ترتيبها‪.‬‬ ‫■‬
‫مسائل التحليل القواعدي البعد بداللة طول الكلمة‪.‬‬ ‫■‬

‫‪32‬‬
‫)تعقيد الخوارزميات( ‪Complexity‬‬

‫■ تذكرة ببعض القوانين الرياضية‪:‬‬

‫‪33‬‬
‫)تعقيد الخوارزميات( ‪Complexity‬‬

‫■ تذكرة ببعض القوانين الرياضية‪:‬‬

‫‪34‬‬
‫)تعقيد الخوارزميات( ‪Complexity‬‬

‫‪35‬‬
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1013y
103 1s 9.96s 1ms 1s 16.67m 3.171013y 3210283y
104 s 130s 100ms 16.67m 115.7d 3.171023y
105 s 1.66ms 10s 11.57d 3171y 3.171033y
106 ms 19.92ms 16.67m 31.71y 3.17107y 3.171043y

36
Complexity (‫)تعقيد الخوارزميات‬
Exercise

// 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];

37
Complexity (‫)تعقيد الخوارزميات‬
Exercise
2
int i = 0; assignments
int sum = 0; The body of this loop is
executed for i = 0, 1, 2, … n-1.
while (i < n) Hence, it is executed
{ ((n-1) – 0) + 1 = n times
sum = sum + a[i];
i = i + 1;
} Therefore, these 2
assignments are
executed n times

Answer: Number of Assignment Operations =


2 + n + n = 2n + 2
38
Complexity (‫)تعقيد الخوارزميات‬
Rule

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

39
‫)تعقيد الخوارزميات( ‪Complexity‬‬
‫‪Question 1‬‬
‫■ السؤال‪ :‬رتب التعقيدات التالية تصاعديا‬
‫– ‪n‬‬
‫– ‪n*n‬‬
‫– ‪n*logn‬‬
‫– ‪Logn‬‬
‫■ اإلجابة‪:‬‬
‫‪log n .1‬‬
‫‪n .2‬‬
‫‪n*logn .3‬‬
‫‪n*n .4‬‬

‫‪40‬‬
‫)تعقيد الخوارزميات( ‪Complexity‬‬
‫‪Question 2‬‬
‫■ السؤال‪ :‬أصغر تعقيد لخوارزمية الرفع لقوة‬
‫– ‪n‬‬
‫– ‪n*n‬‬
‫– ‪n*logn‬‬
‫– ‪Logn‬‬
‫■ اإلجابة‪:‬‬
‫– ‪logn‬‬

‫‪41‬‬
Complexity (‫)تعقيد الخوارزميات‬
Question 2
int power (int x, int y) { int power2 (int x, int y) {
int p = 1; int res = 1;
for (int i = 1; i<=y; i++) { int factor = x;
p = p*x; } while (y>0) {
return p; } if (y%2==1)
res = res*factor;
factor = factor*factor;
y = y / 2; }
return res; }

42
‫)تعقيد الخوارزميات( ‪Complexity‬‬

‫تصنيف المسائل حسب زمن التنفيذ‪:‬‬ ‫■‬


‫– )‪ : O(1‬مثل خوارزميات البحث في جداول التقطيع‪.‬‬
‫– ‪ : Log n‬خوارزميات البحث الثنائي‪.‬‬
‫– ‪ : n‬مثل خوارزميات البحث التسلسلي (فعالة)‪.‬‬
‫– ‪ : n * Log n‬مثل خوارزميات الفرز الجيدة‪.‬‬
‫– ‪ : n*n‬مثل ضرب المصفوفات (تعالج معطيات متوسطة الحجم)‪.‬‬
‫– 𝑛‪ : 2‬أسي ال تستخدم إال في حالة معطيات ذات حجم محدود‪.‬‬

‫‪43‬‬
‫)تعقيد الخوارزميات( ‪Complexity‬‬
‫‪Question 3‬‬
‫)(‪void Main‬‬ ‫■ السؤال‪:‬‬
‫{‬ ‫– احسب تعقيد الخوارزميات التالية‪:‬‬
‫)‪for (int i=1; i<=n; i++‬‬
‫{‬ ‫■ اإلجابة‪:‬‬
‫;‪m=m+1‬‬
‫;‪p=p*2‬‬
‫;‪l=l+3‬‬
‫}‬
‫}‬

‫‪44‬‬
‫)تعقيد الخوارزميات( ‪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‬‬
‫}‬

‫‪45‬‬
‫)تعقيد الخوارزميات( ‪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‬‬
‫}‬

‫‪46‬‬
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 –
}

47
RECURSIVE FUNCTIONS
‫الخوارزميات العودية‬

48
‫‪Recursive Functions‬‬
‫)الخوارزميات العودية(‬
‫■ هي خوارزمية تستدعي نفسها وينتهي االستدعاء بتحقق شرط للتوقف‪.‬‬
‫■ أمثلة‪:‬‬
‫– حساب اعداد فيبوناتشي‪.‬‬
‫– سلسلة أكريمان‪.‬‬
‫– أبراج هانوي‪.‬‬
‫– رسم المنحنيات‪.‬‬
‫– مسألة الوزراء الثمانية‪.‬‬
‫■ يؤول حساب تعقيد الخوارزمية العودية إلى حل معادلة تراجعية‪.‬‬

‫‪49‬‬
‫‪Recursive Functions‬‬
‫)الخوارزميات العودية(‬
‫■ يمكن التمييز بين نوعين من اإلجرائيات العودية‪:‬‬
‫– اإلجرائيات ذات العودية المباشرة‪:‬‬
‫نقول عن إجرائية ‪ P‬إنها عودية مباشرة إذا كانت تحوي استدعاء صريح لنفسها‪.‬‬ ‫■‬
‫– اإلجرائيات ذات العودية غير المباشرة‪:‬‬
‫نقول عن إجرائية ‪ P‬إنها عودية غير مباشرة إذا كانت تستدعي إجرائية أخرى ‪Q‬‬ ‫■‬
‫تستدعي ‪. P‬‬

‫‪50‬‬
‫‪Recursive Functions‬‬
‫)الخوارزميات العودية(‬
‫■ عملية االستدعاء العودي مكلفة من حيث الزمن التنفيذ وحجم الذاكرة المطلوبة‪.‬‬
‫■ يمكن تحويل خوارزمية عودية إلى تكرارية باستخدام مكدس‬
‫– حيث يتم تخزين معامالت الدخل والمتحوالت المحلية لكل استدعاء عودي فيه ثم‬
‫يتم حذف عناصر المكدس لتنفيذ بقية التعليمات بعد االستدعاء العودي على هذه‬
‫العناصر حيث يتم التنفيذ من االستدعاء األخير إلى األول‪.‬‬

‫‪51‬‬
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

52
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

53
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

54
Recursive Functions (‫)الخوارزميات العودية‬
Question 4
int fun(int n) { 1. 4
if (n == 4) 2. 8
return n; 3. 16
else return 2*fun(n+1); } 4. Runtime error.

int main() {
printf("%d ", fun(2));
return 0; }

55
Recursive Functions (‫)الخوارزميات العودية‬
Question 4
int fun(int n) { 1. 4
if (n == 4) 2. 8
return n; 3. 16
else return 2*fun(n+1); } 4. Runtime error.

int main() {
printf("%d ", fun(2));
return 0; }

56
Recursive Functions (‫)الخوارزميات العودية‬
Question 5
int fun(int x, int y) { 1. 13
if (x == 0) 2. 12
return y; 3. 9
return fun(x - 1, x + y); } 4. 10
int main() {
printf("%d ", fun(4,3));
return 0; }

57
Recursive Functions (‫)الخوارزميات العودية‬
Question 5
int fun(int x, int y) { 1. 13
if (x == 0) 2. 12
return y; 3. 9
return fun(x - 1, x + y); } 4. 10
int main() {
printf("%d ", fun(4,3)); The rule: x(x+1)/2 + y
return 0; }

58
Recursive Functions (‫)الخوارزميات العودية‬
Question 6
n = 25 1. 11001
2. 10011
void fun(int n) { 3. 11111
if (n == 0) 4. 00000
return;
printf("%d", n%2);
fun(n/2); }

59
Recursive Functions (‫)الخوارزميات العودية‬
Question 6
n = 25 1. 11001
2. 10011
void fun(int n) { 3. 11111
if (n == 0) 4. 00000
return;
printf("%d", n%2);
fun(n/2); }

60
Recursive Functions (‫)الخوارزميات العودية‬
Question 7
n = 25 1. 11001
2. 10011
void fun(int n) { 3. 11111
if (n == 0) 4. 00000
return;
fun(n/2);
printf("%d", n%2); }

61
Recursive Functions (‫)الخوارزميات العودية‬
Question 7
n = 25 1. 11001
2. 10011
void fun(int n) { 3. 11111
if (n == 0) 4. 00000
return;
fun(n/2);
printf("%d", n%2); }

62
Recursive Functions (‫)الخوارزميات العودية‬
Question 8
What does the following function do? 1. x+y
int fun(int x, int y) 2. x+x*y
{ 3. x*y
if (y == 0) return 0; 4. x^y
return (x + fun(x, y-1));
}

63
Recursive Functions (‫)الخوارزميات العودية‬
Question 8
What does the following function do? 1. x+y
int fun(int x, int y) 2. x+x*y
{ 3. x*y
if (y == 0) return 0; 4. x^y
return (x + fun(x, y-1));
}

64
Recursive Functions (‫)الخوارزميات العودية‬
Question 9
Predict the output of following program 1. Stack overflow
int f(int n) { 2. 3
if(n <= 1) 3. 4
return 1; 4. 5
if(n%2 == 0)
return f(n/2);
return f(n/2) + f(n/2+1); }
int main() {
printf("%d", f(11));
return 0; }

65
Recursive Functions (‫)الخوارزميات العودية‬
Question 9
Predict the output of following program 1. Stack overflow
int f(int n) { 2. 3
if(n <= 1) 3. 4
return 1; 4. 5
if(n%2 == 0)
return f(n/2);
return f(n/2) + f(n/2+1); }
int main() {
printf("%d", f(11));
return 0; }

66
Recursive Functions (‫)الخوارزميات العودية‬
Question 10
Consider the following: f(5) 1. 0
int fun (int n) 2. 26
{ 3. 51
int x=1, k; 4. 71
if (n==1) return x;
for (k=1; k<n; ++k)
x = x + fun(k) * fun(n – k);
return x;
}

67
Recursive Functions (‫)الخوارزميات العودية‬
Question 10
Consider the following: f(5) 1. 0
int fun (int n) 2. 26
{ 3. 51
int x=1, k; 4. 71
if (n==1) return x;
for (k=1; k<n; ++k)
x = x + fun(k) * fun(n – k);
return x;
}

68
Recursive Functions (‫)الخوارزميات العودية‬
Question 11
If get(6) function is being called. how 1. 15
many times will the get() function be
2. 25
invoked?
3. 35
void get (int n) {
4. 45
if (n < 1) return;
get(n-1);
get(n-3);
printf("%d", n); }

69
Recursive Functions (‫)الخوارزميات العودية‬
Question 11
If get(6) function is being called. how 1. 15
many times will the get() function be
2. 25
invoked?
get(6) [25 Calls]
3. 35
void get (int n) { / \
[17 Calls] get(5) get(3) [7 Calls]
4. 45
if (n < 1) return; / \
get(4) get(2) [5 Calls]
get(n-1); / \
[7 Calls] get(3) get(1) [3 Calls]
get(n-3); / \
get(2) get(0)
printf("%d", n); } / \
[3 Calls] get(1) get(-1)
/ \
get(0) get(-2)

70
SEQUENTIAL
DATA STRUCTURE
‫بنى المعطيات التسلسلية‬

71
‫)السالسل الخطية( ‪Linked List‬‬

‫هي بنية معطيات تمكن من تخزين عناصر من نفس النمط‪.‬‬ ‫■‬


‫ميزات السالسل‪:‬‬ ‫■‬
‫– ال ضرورة لتحديد طولها مسبقا‪.‬‬
‫– ال نحتاج إلى ازاحة عناصر السلسلة عند الحذف واالضافة‪.‬‬
‫سيئاتها‪:‬‬ ‫■‬
‫– ال يمكن النفاذ المباشر إلى عنصر‪ ,‬وبالتالي تحتاج إلى ‪ n‬عملية مقارنة في أسوأ االحوال‬
‫للوصول إلى العنصر‪.‬‬
‫– نحتاج إلى حجم تخزين إضافي لتخزين المؤشرات‪.‬‬
‫يوجد أنواع أخرى للسالسل مثل‪:‬‬ ‫■‬
‫– السالسل الدائرية ‪. circular linked list‬‬
‫– السالسل المضاعفة االرتباط ‪. doubly linked list‬‬

‫‪72‬‬
Linked List (‫)السالسل الخطية‬

A B C

Head

node

data pointer

73
‫)السالسل الخطية( ‪Linked List‬‬
‫‪V.S. Ordinary and Dynamic Arrays‬‬

‫‪Ordinary Arrays‬‬ ‫‪Dynamic Arrays‬‬


‫الميزات‪:‬‬ ‫■‬ ‫الميزات‪:‬‬ ‫■‬
‫– من السهل جدا إنشاء واستخدام‪.‬‬ ‫– من السهل جدا إنشائها‪.‬‬
‫– الوصول المباشر إلى أي عنصر‪.‬‬ ‫– الوصول المباشر إلى أي عنصر‪.‬‬
‫– يمكن تحديد الحجم في وقت التشغيل‪.‬‬
‫السيئات‪:‬‬ ‫■‬
‫ً‬ ‫السيئات‪:‬‬ ‫■‬
‫معروفا في‬ ‫– يجب أن يكون حجمها‬
‫وقت التحويل البرمجي‪ ،‬وال يمكن‬ ‫– يتطلب إدراج عنصر أو حذفه نقل كل‬
‫تغييره في وقت التشغيل‪.‬‬ ‫العناصر األخرى‪.‬‬
‫– يتطلب إدراج عنصر أو حذفه نقل كل‬ ‫– إذا كان على المصفوفة أن تتخطى الحجم‬
‫األولي ‪ ،‬فيجب إعادة تشكيل المصفوفة‬
‫العناصر األخرى‪.‬‬ ‫بأكملها إلى موقع جديد‪.‬‬

‫‪74‬‬
‫)المكدس( ‪Stack‬‬

‫هي بنية خطية تشبه السلسلة‪ ،‬غير إن عمليات اإلضافة والحذف تتم من جهة واحدة ندعوها‬ ‫■‬
‫قمة المكدس‪.‬‬
‫هي بنية معطيات تمكن من تخزين عناصر من نفس النمط‪.‬‬ ‫■‬
‫تتبع مبدأ ‪:‬‬ ‫■‬
‫– )‪First In last Out (FILO‬‬
‫– )‪Last In First Out (LIFO‬‬
‫العمليات االساسية عليه‪:‬‬ ‫■‬
‫– يمكن الوصول إلى العناصر من خالل قمته )‪) head‬‬
‫– إضافة عنصر إلى القمة )‪) push‬‬
‫– حذف عنصر من القمة )‪) pop‬‬
‫يمكن تمثيل المكدس باستخدام المصفوفات أو السالسل الخطية‪.‬‬ ‫■‬

‫‪75‬‬
‫)المكدس( ‪Stack‬‬

‫‪76‬‬
‫)المكدس( ‪Stack‬‬

‫■ تطبيقات المكدسات‪:‬‬
‫– سجل الصفحات المزارة في متصفح االنترنت‪.‬‬
‫– عمليات التراجع في محررات النصوص‪.‬‬
‫– ربط أزواج األقواس في التعابير الرياضية‪.‬‬
‫– التحويل من التعابير الرياضية النظامية إلى التعابير الملحقة وحساب قيمة هذه‬
‫التعابير‪.‬‬
‫– استدعاء تابع في جسم تابع‪.‬‬
‫– التجول في األشجار والبيانات بطريقة العمق أوال‪.‬‬
‫– مكدس األخطاء في المترجمات‪.‬‬

‫‪77‬‬
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*

78
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.

79
Stack (‫)المكدس‬

■ Infix to Prefix:
1. Reverse the 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. Reverse the postfix expression. Hence in our example prefix is +A*BC.

80
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 element is a number, push it into the stack.
2. If the element is 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.

81
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 according to 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 Result is stored at the top of the Stack.

82
‫)األرتال( ‪Queue‬‬

‫■ هي بنية خطية تشبه السلسلة غير أن عمليات اإلضافة تجري في جهة ندعوها ذيل الرتل‪.‬‬
‫ويجري الحذف في الجهة المعاكسة التي نسميها بداية الرتل‪.‬‬
‫هي بنية معطيات تمكن من تخزين عناصر من نفس النمط‪.‬‬ ‫■‬
‫تتبع مبدأ ‪:‬‬ ‫■‬
‫– )‪First In First Out (FIFO‬‬
‫– )‪Last In Last Out (LILO‬‬
‫العمليات االساسية عليه‪:‬‬ ‫■‬
‫– يمكن الوصول إلى العناصر من خالل بدايته (‪ ) head‬أو نهايته (‪.) tail‬‬
‫– إضافة عنصر إلى النهاية (‪.) enqueue‬‬
‫– حذف عنصر من البداية (‪.) dequeue‬‬
‫يمكن تمثيل االرتال باستخدام المصفوفات أو السالسل الخطية‪.‬‬ ‫■‬

‫‪83‬‬
‫)األرتال( ‪Queue‬‬

‫‪84‬‬
‫)األرتال( ‪Queue‬‬

‫■ تطبيقات األرتال‪:‬‬
‫– مخازن المدخالت والمخرجات في الكمبيوتر ( لوحة المفاتيح والطابعة)‪.‬‬
‫– بنية معطيات مساعدة في الخوارزميات مثل خوارزميات التجول بالعرض أوال في‬
‫األشجار والبيانات‪.‬‬
‫– محاكاة أرتال الحياة اليومية‪.‬‬

‫‪85‬‬
‫)األرتال( ‪Queue‬‬

‫■ رتل االولويات‪:‬‬
‫– يضاف عامل األولوية إلى الرتل‪.‬‬
‫– يتم تخديم العنصر ذو األولوية األعلى اوال‪.‬‬
‫– مثال‪:‬‬
‫أرتال االنتظار في عيادة الطبيب حيث تكون األولوية للحاالت اإلسعافية‪.‬‬ ‫■‬

‫‪86‬‬
NON SEQUENTIAL
DATA STRUCTURES
‫بنى المعطيات غير التسلسلية‬

87
‫)االشجار( ‪Trees‬‬

‫■ نعرف الشجرة‪:‬‬
‫– بأنها مجموعة من العناصر نسميها عقد )‪،)nodes‬‬
‫– مرتبطة بين بعضها بروابط (‪،)Links‬‬
‫– ومنظمة تنظيما هرميا (‪،)Hierarchical‬‬
‫– ال يحتوي حلقات (‪ )Cycles‬مغلقة‪،‬‬
‫– أي إنه توجد عقدة مميزة ووحيدة نسميها جذر (‪.)Root‬‬
‫■ أمثلة‪:‬‬
‫– تنظيم الملفات في مجلدات‪.‬‬
‫– تمثيل عبارة حسابية‪.‬‬

‫‪88‬‬
‫)األشجار الثنائية( ‪Binary Trees‬‬

‫■ حالة خاصة من األشجار يكون فيها لكل عقدة ابنان علي األكثر‪:‬‬
‫– نسمي العقد التي لها ولد على األقل عقد داخلية‪.‬‬
‫– نسمي العقد التي ليس لها أي ولد عقد خارجية أو أوراق (‪.)Leaf‬‬
‫– نسمي طريقا (‪ )Path‬كل متتالية من العقد‪.‬‬
‫– نسمي فرعا كل طريق يصل بين الجذر وإحدى األوراق‪.‬‬

‫‪89‬‬
‫)األشجار الثنائية( ‪Binary Trees‬‬

‫■ تعاريف مهمة‪:‬‬
‫– درجة الشجرة‪ :‬العدد األعظمي ألوالد عقدة‪( .‬درجة شجرة ثنائية هي ‪)2‬‬
‫– حجم الشجرة‪ :‬عدد عقدها‪.‬‬
‫– ارتفاع عقدة‪ :‬عدد االتصاالت في الطريق الواصل من العقدة الى الجذر‪.‬‬
‫– ارتفاع الشجرة‪ :‬أطول ارتفاع عقدة فيها‪.‬‬
‫– عرض الشجرة‪ :‬العدد األعظم للعقد في مستوى ما‪.‬‬
‫– مسافة التجول‪ :‬ضمن الشجرة هو مجموع ارتفاعات عقدها‪.‬‬
‫– مسافة التجول الخارجي‪ :‬مجموع ارتفاعات أوراقها‪.‬‬
‫– مسافة التجول الداخلي‪ :‬مجموع ارتفاعات عقدها الداخلية‪.‬‬

‫‪90‬‬
‫)األشجار الثنائية( ‪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 :‬‬ ‫■‬

‫‪91‬‬
‫)األشجار الثنائية( ‪Binary Trees‬‬

‫■ بعض األشجار الثنائية الخاصة‪:‬‬


‫– الشجرة الثنائية الخطية‪ :‬شجرة ثنائية يكون لكل عقدة منها ولد واحد على األكثر‪.‬‬
‫– الشجرة الثنائية التامة‪ :‬كل شجرة تحوي عقدة واحدة في المستوى ‪ 0‬وعقدتين في‬
‫المستوى ‪ 1‬وأربع عقد في المستوى ‪ 2‬و 𝑘‪ 2‬عقدة في المستوى ‪. k‬‬
‫(عدد العقد بشجرة تامة ارتفاعها ‪ h‬هو ‪) 2ℎ+1 − 1‬‬ ‫■‬
‫– الشجرة الثنائية الكاملة‪ :‬كل شجرة ثنائية تكون كل مستوياتها مليئة بالعقد ماعدا‬
‫المستوى األخير يمكن أن يحوي على فراغات‪.‬‬

‫‪92‬‬
Binary Trees (‫)األشجار الثنائية‬
5
10 10
2 45
5 30 5 45
30
2 25 45 2 25 30
10

25
Binary search trees Not a binary search tree

93
‫)األشجار الثنائية( ‪Binary Trees‬‬
‫‪Question 1‬‬
‫■ السؤال‪ :‬ما هو ارتفاع شجرة خطية عدد عقدها ‪ n‬؟‬
‫■ اإلجابة‪n-1 :‬‬

‫■ السؤال‪ :‬كم عدد أوراق شجرة ثنائية تامة؟‬


‫■ اإلجابة‪ 2^h :‬حيث ‪ h‬هو ارتفاع الشجرة‬

‫■ السؤال‪ :‬كم عدد العقد لشجرة ثنائية تامة ؟‬


‫𝒉=𝒊‬ ‫■ اإلجابة‪:‬‬
‫𝟏 ‪෍ 𝟐𝒊 = 𝟐𝒉+𝟏 −‬‬
‫𝟎=𝒊‬

‫‪94‬‬
‫)األشجار الثنائية( ‪Binary Trees‬‬
‫‪Question 2‬‬
‫■ السؤال‪ :‬ماهي أسوأ حالة للبحث ضمن شجرة بحث ثنائية؟‬
‫■ اإلجابة‪ :‬عندما تكون الشجرة خطية حيث تكون مسافة التجول أعظمية‪.‬‬

‫■ السؤال‪ :‬ماهي أفضل حالة للبحث ضمن شجرة بحث ثنائية؟‬


‫■ اإلجابة‪ :‬عندما تكون الشجرة كاملة او متوازنة تكون مسافة التجول أصغرية‪.‬‬

‫‪95‬‬
‫‪Binary Search Trees‬‬
‫)أشجار البحث الثنائية(‬
‫■ هي شجرة ثنائية تحقق الخواص التالية‪:‬‬
‫– من أجل عقدة محددة ‪ n‬كل القيم المخزنة في الشجرة الجزئية اليسارية هي أصغر‬
‫من القيمة المخزنة في العقدة ‪. n‬‬
‫– من أجل عقدة محددة ‪ n‬كل القيم المخزنة في الشجرة الجزئية اليمينية هي أكبر‬
‫من القيمة المخزنة في العقدة ‪. n‬‬

‫‪96‬‬
Binary Search Trees (‫)أشجار البحث الثنائية‬
Inserting Method

97
‫)أشجار البحث الثنائية( ‪Binary Search Trees‬‬
‫‪Representation Methods‬‬
‫■ طرق تمثيل األشجار الثنائية‪:‬‬
‫– عن طريق المؤشرات‪.‬‬
‫– عن طريق المصفوفات حيث يكون عدد أسطرها بعدد العقد وعدد أعمدتها ‪ 3‬حيث‬
‫يحوي العمود األول رقم العقدة والثاني رقم االبن اليميني وفي الثالث رقم االبن‬
‫اليسار‪.‬‬

‫‪98‬‬
‫)أشجار البحث الثنائية( ‪Binary Search Trees‬‬
‫‪Question 3‬‬
‫■ السؤال‪ :‬أي من الشجر التالية تعتبر "شجرة بحث ثنائية"؟‬
‫■ اإلجابة‪ b :‬و ‪c‬‬

‫‪99‬‬
‫)أشجار البحث الثنائية( ‪Binary Search Trees‬‬
‫‪Question 4‬‬
‫■ السؤال‪ :‬ماهي سيئة طريقة المصفوفات؟؟‬
‫■ اإلجابة‪ :‬فيها هدر كبير للذاكرة في حالة األشجار غير التامة أو الكاملة‪.‬‬

‫‪100‬‬
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

101
‫)أشجار البحث الثنائية( ‪Binary Search Trees‬‬
‫‪DFS‬‬
‫‪start‬‬ ‫‪22‬‬ ‫■ طرق التجول في الشجرة‪:‬‬
‫– التجول بالعمق أوال (‪)depth first search‬‬

‫‪6‬‬ ‫‪33‬‬ ‫ترتيب زيارة العقد في التجول ‪:VLR‬‬


‫‪22,6,-1,8,7,16,33,45‬‬

‫‪-1‬‬ ‫‪8‬‬ ‫‪45‬‬

‫‪7‬‬ ‫‪16‬‬

‫‪102‬‬
‫)أشجار البحث الثنائية( ‪Binary Search Trees‬‬
‫‪DFS‬‬
‫■ طرق التجول في الشجرة‪ :‬التجول بالعمق أوال (‪)depth first search‬‬
‫– ‪ VLR‬يزور العقدة ثم الشجرة الجزئية اليسارية ثم اليمينية‪ ،‬ويدعى الترتيب المصدر‬
‫(‪.)preorder‬‬
‫– ‪ VRL‬يزور العقدة ثم الشجرة الجزئية اليمينية ثم اليسارية‪.‬‬
‫– ‪ LVR‬يزور الشجرة الجزئية اليسارية ثم العقدة ثم اليمينية ويدعى الترتيب المتناظر‬
‫(‪.)inorder‬‬
‫– ‪RVL‬‬
‫– ‪ LRV‬ويدعى الترتيب الملحق (‪.)postorder‬‬
‫– ‪RLV‬‬

‫‪103‬‬
‫)أشجار البحث الثنائية( ‪Binary Search Trees‬‬
‫‪BFS‬‬
‫‪22‬‬
‫■ طرق التجول في الشجرة‪:‬‬
‫– التجول بالعرض أوال (‪.)breadth first search‬‬

‫‪6‬‬ ‫‪33‬‬
‫ترتيب زيارة العقد في التجول ‪:VLR‬‬
‫‪22,6,33,-1,8,45,7,16‬‬

‫‪-1‬‬ ‫‪8‬‬ ‫‪45‬‬

‫‪7‬‬ ‫‪16‬‬
‫‪104‬‬
‫‪GRAPHS‬‬
‫البيانات‬

‫‪105‬‬
‫)البيانات( ‪Graphs‬‬

‫■ هي بنية معطيات تخزن العناصر كعقد ‪ vertex‬ويوجد بينها وصالت تمثل العالقات بين العقد‬
‫‪.edges‬‬
‫■ يمكن اعتبار البيانات تعميما للبنى الهرمية غير التسلسلية من الناحية النظرية‪ ،‬فاألشجار‬
‫هي حالة خاصة من البيانات (بيانات ال تحوي حلقات)‪.‬‬

‫‪106‬‬
‫)البيانات( ‪Graphs‬‬

‫■ درجة العقدة )‪ Deg(v‬هي عدد األسهم الصادرة من العقدة والواردة إليها‬


‫– حيث )‪ Deg-(v‬هي الدرجة الواردة‪.‬‬
‫– و )‪ Deg+(v‬هي الدرجة الصادرة‪.‬‬

‫■ يكون البيان متصل بشدة إذا كان في حالة كل عقدتين ‪ v,w‬يوجد طريق من ‪ v‬إلى ‪ w‬و‬
‫من ‪ w‬إلى ‪v‬‬

‫)‪𝑛(𝑛−1‬‬
‫وفي هذه‬ ‫■ عدد الروابط األعظمي في بيان غير موجه يحوي ‪ n‬عقدة يساوي‬
‫‪2‬‬
‫الحالة نقول عن البيان إنه تام (‪.)Complete‬‬

‫‪107‬‬
Graphs (‫)البيانات‬

.)adjacency matrix( ‫■ تمثيل البيان باستخدام مصفوفة التجاور‬

The Graph
The Adjacency Matrix

108
Graphs (‫)البيانات‬

.)adjacency list( ‫■ تمثيل البيان باستخدام قائمة التجاور‬

The Graph

The Adjacency List 109


‫)البيانات( ‪Graphs‬‬
‫‪Question 1‬‬
‫السؤال‪ :‬ماهي كلفة البحث عن عقد مجاورة لعقدة ممثلة بطريقة قائمة التجاور؟‬ ‫■‬
‫■ الجواب‪O(deg(v)) :‬‬

‫■ السؤال‪ :‬ماهي كلفة مسح الروابط الواردة لعقدة في بيان ممثل بطريقة قائمة التجاور؟‬
‫■ الجواب‪ O(V+E) :‬مكلف جدا‬

‫‪110‬‬
‫)البيانات( ‪Graphs‬‬

‫طرق التجول في البيان‪:‬‬ ‫■‬


‫– العمق أوال (‪.)DFS‬‬
‫– العرض أوال (‪ :)BFS‬هي طريقة إليجاد أقصر طريق في بيان غير موزون ألن الوصول‬
‫للعقد يتم بمسح أقل عدد للروابط‪.‬‬

‫‪111‬‬
Graphs (‫)البيانات‬
DFS V.S BFS

112
Graphs (‫)البيانات‬
DFS V.S BFS

113
‫)البيانات( ‪Graphs‬‬
‫‪Dijkstra‬‬
‫خوارزمية ‪ Dijkstra‬إليجاد أقصر طريق في بيان موزون‪.‬‬ ‫■‬

‫‪115‬‬
Graphs (‫)البيانات‬ 2
Dijkstra 1 5
3
4 4 11
4

5 3 9 8
7
6 7
12 6 9
2
8

Processed 1 2 3 4 5 6 7 8 9
distance 0 ? ? ? ? ? ? ? ?
predecessor -1 -1 -1 -1 -1 -1 -1 -1 -1

116
‫)البيانات( ‪Graphs‬‬
‫‪topological sort‬‬
‫■ الفرز الطبولوجي يعتمد على ترتيب العقد حسب درجات ورودها حيث تكون كل األسهم او‬
‫الوصالت تؤشر إلى جهة واحدة هي اليمين‪.‬‬
‫■ ال يمكن تطبيقها على البيان الذي يحتوي على حلقات‪.‬‬
‫■ مثال‪:‬‬
‫– المنهاج السنوي لجامعة‪.‬‬

‫‪117‬‬
Graphs (‫)البيانات‬
topological sort 6 1

5 2 8 3

7 4

next 0

Node 1 2 3 4 5 6 7 8
Indegree 2 2 3 2 1 1 0 2
7

LIST
118
‫)البيانات( ‪Graphs‬‬
‫‪minimum spanning tree‬‬
‫■ شجرة االرتباط األصغرية‪ :‬هي شجرة تربط البيان بأقل كلفة ممكنة من الروابط‪.‬‬
‫■ تتواجد هذه الشجرة في حالة البيان المترابط فقط‪.‬‬
‫■ ‪Prim’s algorithm‬‬

‫‪9‬‬ ‫‪9‬‬
‫‪b‬‬ ‫‪b‬‬
‫‪a‬‬ ‫‪2‬‬ ‫‪6‬‬ ‫‪a‬‬ ‫‪2‬‬ ‫‪6‬‬
‫‪d‬‬ ‫‪d‬‬
‫‪4‬‬ ‫‪5‬‬ ‫‪4‬‬ ‫‪5‬‬
‫‪5‬‬ ‫‪4‬‬ ‫‪5‬‬ ‫‪4‬‬

‫‪5‬‬ ‫‪e‬‬ ‫‪5‬‬ ‫‪e‬‬


‫‪c‬‬ ‫‪c‬‬

‫‪119‬‬
SORTING ALGORITHMS
‫خوارزميات الترتيب‬

120
‫‪Bubble Sort Algorithm‬‬
‫)خوارزمية الفرز الفقاعي(‬
‫■ تعقيد خوارزمية الفرز الفقاعي‪:‬‬
‫– أحسن األحوال‪ :‬عندما تكون المصفوفة مفروزة حيث اليوجد عمليات اسناد‪.‬‬
‫– أسوأ األحوال‪ :‬عندما تكون المصفوفة مرتية ترتيب عكسي يكون عدد عمليات‬
‫االسناد من مرتبة )‪.O(n*n‬‬
‫– الحالة الوسطية‪ :‬عندما تكون المصفوفة مرتبة عشوائيا ً )‪.O(n*n‬‬

‫‪121‬‬
‫‪Selection Sort Algorithm‬‬
‫)خوارزمية الفرز باالختيار(‬
‫■ تعقيد الخوارزمية‪:‬‬
‫– عدد عمليات المقارنة )‪O(n*n‬‬
‫– عدد عمليات التبديل )‪O(n‬‬
‫■ جيدة لحجم صغير وفي حالة كانت عملية التبديل مكلفة وعملية المقارنة غير مكلفة‪.‬‬

‫‪122‬‬
‫‪Insertion Sort Algorithm‬‬
‫)خوارزمية الفرز باإلضافة(‬
‫■ تعقيد خوارزمية الفرز باإلضافة‪:‬‬
‫– أحسن األحوال‪ :‬عندما تكون المصفوفة مفروزة حيث ال يوجد عمليات اسناد )‪O(1‬‬
‫وعمليات المقارنة )‪.O(n‬‬
‫– أسوأ األحوال‪ :‬عندما تكون المصفوفة مرتية ترتيب عكسي يكون عدد عمليات‬
‫االسناد من مرتبة )‪.O(n*n‬‬
‫– الحالة الوسطية‪ :‬عندما تكون المصفوفة مرتبة عشوائيا ً )‪.O(n*n‬‬

‫‪123‬‬
‫‪Merge Sort Algorithm‬‬
‫)خوارزمية الفرز بالدمج(‬
‫■ تعتمد مبدأ فرق تسد‪.‬‬
‫■ هي خوارزمية عوديه تقوم بما يلي‪:‬‬
‫– تقسم المصفوفة إلى جزئين‪،‬‬
‫– تفرز كل جزء على حدا‪،‬‬
‫– تدمج الجزئيين المفروزين‬
‫في مصفوفة واحدة مفروزة‪.‬‬

‫■ تعقيد الخوارزمية ‪:‬‬


‫– ))‪ O(nlog(n‬دوما‬

‫‪124‬‬
‫‪Quick Sort Algorithm‬‬
‫)خوارزمية الفرز السريع(‬
‫■ تعقيد الخوارزمية‪:‬‬
‫– ))‪ O(nlog(n‬في أحسن األحوال والحالة الوسطية‪.‬‬
‫– )‪ O(n*n‬في أسوأ األحوال‪.‬‬

‫‪125‬‬
Sorting Algorithms

126
SEARCH ALGORITHMS
‫خوارزميات البحث‬

127
Search Algorithms
Binary V.S. Sequential
O(n) ‫ تعقيده‬: ‫■ البحث التسلسلي‬
O(log(n)) ‫ تعقيده‬: ‫■ البحث الثنائي‬

128
ADVANCE ALGORITHMS
‫خوارزميات متقدمة‬

129
‫‪Backtracking Algorithms‬‬
‫)الخوارزميات التراجعية(‬
‫■ هي خوارزميات عودية تعتمد طريقة التجريب والخطأ في حل المشاكل‪.‬‬
‫■ تتلخص هذه الطريقة ببناء الحل النهائي للمسألة عن طريق مجموعة من الخطوات‪:‬‬
‫– في الخطوة نحدد اإلمكانيات المتاحة للخطوة التالية‪،‬‬
‫– ثم ندرس هذه اإلمكانيات بأن ننتقي أحدها‪،‬‬
‫– و نسجلها على أنها الخطوة التالية في الحل النهائي‪،‬‬
‫– و نتابع الخوارزمية اعتمادا ً على هذه الخطوة‪.‬‬
‫– عندما يظهر لنا أن اختيارنا ال يقود إلى الحل النهائي‪ ،‬أو يؤدي إلى طريق مسدود‪،‬‬
‫– نعدل عن هذه الخطوة‪.‬‬
‫■ تشبه هذه العملية بناء سجل أخطاء يفيد في تجنب الوقوع في الخطأ مرتين‪.‬‬

‫‪130‬‬
‫‪Backtracking Algorithms‬‬
‫)الخوارزميات التراجعية(‬
‫■ مثال ‪:‬‬
‫– مسألة جولة حصان الشطرنج‪:‬‬
‫إيجاد طريقة لتغطية رقعة الشطرنج من موقع معين يقف فيه الحصان وذلك باستخدام‬ ‫■‬
‫حركات الحصان المعروفة في الشطرنج وبشرط المرور بكل مربع مرة واحدة‪.‬‬

‫– مسألة الوزراء الثمانية‪:‬‬


‫توزيع ثمانية وزراء على رقعة الشطرنج بحيث ال يكون فيها اي واحد مهدد لألخر‪.‬‬ ‫■‬

‫– مسألة الخيار األمثل‪:‬‬


‫مقارنة الحلول التي يمكن ايجادها لمسألة معينة واالحتفاظ بالحل األمثل وفق معايير‬ ‫■‬
‫محددة للمسألة‪.‬‬

‫‪131‬‬
‫‪Greedy Algorithms‬‬
‫)الخوارزميات الشجعة(‬
‫■ هي خوارزمية التي تستند على الحدس المهني الذي يتم عن طريقه اختيار اإلمكانية‬
‫األفضل المرئية في المرحلة الحالية( الحل األمثل في المرحلة الحالية)‪ ,‬من دون األخذ‬
‫بالحسبان تأثير هذه الخطوة على تكملة الحل‪.‬‬
‫■ قد ال يقودنا الحل األمثل المرحلي إلى الحل األمثل للمسألة‪.‬‬
‫■ خوارزمية الجشع ال يكن ان تتراجع عن خياراتها‪ .‬هذا هو الفرق الرئيسي بينها‬
‫وبين البرمجة الديناميكية‪.‬‬
‫■ مثل خوارزمية برايم وأشجار هوفمان‪.‬‬

‫‪132‬‬
‫‪Divide and Conquer‬‬
‫)خوارزميات فرق تسد(‬
‫■ عمل خوارزمية فرق تسد عن طريق تقسيم المسألة بشكل عودي إلى مسألتين‬
‫جزئيتين أو أكثر من نفس النوع‪ ،‬حتى تصبح المسائل الجزئية بسيطة بما فيه الكفاية‬
‫لتحل بشكل مباشر‪ .‬ومن ثم تدمج حلول المسائل الجزئية لتعطي حال ً للمسألة الجزئية‪.‬‬
‫■ امثلة‪:‬‬
‫– خوارزميات البحث الثنائي‪.‬‬
‫– خوارزميات الفرز بالدمج‪.‬‬
‫– خوارزميات الفرز السريع‪.‬‬
‫– خوارزمية ابراج هانوي‪.‬‬

‫‪133‬‬
‫‪Dynamic Programming‬‬
‫)البرمجة الديناميكية(‬
‫■ لحل مسألة ما‪ ،‬نحن بحاجة إلى حل أجزاء مختلفة من المسألة (مسائل فرعية)‪ ،‬ومن‬
‫ثم جمع حلول المسائل الفرعية للحصول على حل شامل‪.‬‬
‫■ نهج البرمجة الديناميكية‪:‬‬
‫– البحث عن حل كل مسألة فرعية مرة واحدة فقط‪،‬‬
‫– وبالتالي تقليل عدد الحسابات‪ :‬حالما تم حساب حل مسألة فرعية ما‪ ،‬يتم حفظه‪،‬‬
‫وفي المرة القادمة عند الحاجة للحل نفسه‪ ،‬يتم ببساطة استرجاعه‪.‬‬
‫■ خوارزميات البرمجة الديناميكية ستدرس الحلول السابقة للمسائل الثانوية وتقوم بدمجها‬
‫للحصول علي افضل حل للمسائلة المراد حله‪.‬‬

‫‪134‬‬
HASHING FUNCTION
(‫)جداول التقطيع‬

135
‫‪Hashing Tables‬‬
‫)جداول التقطيع(‬
‫هي طريقة لإليجاد عنصر مباشرة دون الحاجة إلى خوارزمية بحث من خالل النفاذ المباشر إلى موقعه‪.‬‬ ‫■‬
‫هي بنية فعالة في برمجة التطبيقات التالية‪:‬‬ ‫■‬
‫– التي تحتاج إلى عمليات معجم‪.‬‬
‫– االمثلة التي ال تهتم بالترتيب‪.‬‬
‫يخزن العنصر ذو المفتاح ‪ k‬في الخانة )‪ h(k‬حيث ‪ h‬هو تابع التقطيع الذي يستخدم لحساب دليل الخانة‬ ‫■‬
‫من المفتاح ‪. k‬‬
‫قد يحدث تصادم في حال وجود مفتاحين لهما نفس قيمة تابع التقطيع‪.‬‬ ‫■‬
‫تخزن القيم على شكل ازواج من (‪) key, value‬‬ ‫■‬
‫جداول التقطيع ليست بنية جيدة للتجول حول سلسلة من القيم‪) Iteration( .‬‬ ‫■‬

‫الزمن المتوقع إلضافة والبحث عن عنصر في جدول تقطيع ضمن فرضيات مناسبة )‪ ( . O(1‬الحالة الوسطية)‬ ‫■‬

‫‪136‬‬
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

137
Hashing Tables
(‫)جداول التقطيع‬
:‫■ مثال عن تخزين المعلومات باستخدام العنونة المباشرة‬
‫■ تستخدم عندما نستطيع منح موقع من الجدول لكل مفتاح (عدد العناصر المتوقع تخزينها‬
.‫قريب من العدد الكلي للعناصر‬

For technical or 0000000


commercial reasons, 0000001
certain numbers may not
6131467 Record for phone
be assigned to any
6131468 number 6131496
subscriber
6131469
6131470 Anas - Mazzah
Each phone number
uniquely determines an
array element 9999997
9999998
9999999 138
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 sequence of events:

Insert 2 Insert 21 Insert 34 Insert 54


key data key data key data

0 0 0 There is a
1 1 21 … 1 21 … collision at
array entry #4
2 2 … 2 2 … 2 2 …
3 3 3
4 4 4 34 … ???
139
‫‪Hashing Tables‬‬
‫)جداول التقطيع(‬
‫■ تقنية الربط لحل التصادم‪:‬‬
‫– توضع جميع العناصر التي لها نفس قيمة تابع التقطيع في سلسلة خطية‪.‬‬
‫)‪(Hashing with Chaining‬‬
‫– تصبح خانة الجدول مؤشر إلى بداية سلسلة العناصر المتصادمة‪.‬‬

‫‪140‬‬
Hashing Tables (‫)جداول التقطيع‬
Hashing with Chaining
Insert 54 other Insert 101
key key data
0 0
1 21 1 21 101
2 2 2 2
3 3
4 54 34 4 54 34

CHAIN

141
Hashing Tables (‫)جداول التقطيع‬
Hashing with Chaining
■ What is the running time to insert/search/delete?
– Insert: It takes O(1) time to compute the hash function and insert at head of
linked list
– Search: It is proportional to max linked list length
– Delete: Same as search

142
‫)جداول التقطيع( ‪Hashing Tables‬‬
‫‪Question 1‬‬
‫■ السؤال‪ :‬ماهي كلفة عملية إضافة عنصر؟‬
‫■ الجواب‪ O(1) :‬حيث تتم اإلضافة للعناصر المتصادمة في رأس السلسلة‪.‬‬

‫■ السؤال‪ :‬ماهي كلفة عملية البحث والحذف في أسوأ األحوال؟‬


‫■ الجواب‪ :‬تتعلق بطول السلسلة الخطية للعناصر المتصادمة وهي في أسوأ االحوال )‪O(n‬‬
‫عند اختيار تابع تقطيع سيء‪.‬‬

‫■ السؤال‪ :‬ماهي الكلفة الوسطية للحذف واالضافة؟‬


‫■ الجواب‪ :‬هو عدد العناصر الوسطي في السلسلة عند استخدام تابع تقطيع يحقق توزع‬
‫منتظم وهو ‪ n/m‬حيث ‪ n‬عدد العناصر الكلي و ‪ m‬عدد الخانات في الجدول‪.‬‬

‫‪143‬‬
‫)توابع التقطيع( ‪Hashing Function‬‬

‫■ طريقة التقسيم‪:‬‬
‫– بربط المفتاح ‪ k‬بالخانة التي تساوي باقي قسمة ‪ k‬على ‪. m‬‬
‫– ‪h(k)=k%m‬‬
‫– يجب تجنب قيم ‪ m‬من قوى ‪ 2‬واختيار أعداد أولية بعيدة عنها‪.‬‬
‫■ طريقة الضرب‪:‬‬
‫– نضرب المفتاح ‪ k‬بثابت ‪ A‬حيث ‪ 0<A<1‬ونأخذ الجزء الكسري ثم نضرب هذه القيمة‬
‫ب ‪ m‬ونأخذ الجزء الطبيعي من العدد الناتج‪.‬‬
‫– يمكن اختيار أي قيمة للعدد ‪.m‬‬

‫‪144‬‬
QUESTIONS

148
Question 1

■ For a binary search algorithm to work, it is necessary that the array (list) must be?
A. sorted
B. unsorted
C. in a heap
D. popped out of stack

149
Question 1

■ For a binary search algorithm to work, it is necessary that the array (list) must be?
A. sorted
B. unsorted
C. in a heap
D. popped out of stack

150
Question 2

■ Which one of the below mentioned is linear data structure?


A. Queue
B. Stack
C. Arrays
D. All of the above

151
Question 2

■ Which one of the below mentioned is linear data structure?


A. Queue
B. Stack
C. Arrays
D. All of the above

152
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

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

154
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 procedure to solve problem.
D. all of the above.

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 procedure to solve problem.
D. all of the above.

156
Question 5

■ Quick sort running time depends on the selection of?


A. size of array
B. pivot element
C. sequence of values
D. none of the above!

157
Question 5

■ Quick sort running time depends on the selection of?


A. size of array
B. pivot element
C. sequence of values
D. none of the above!

158
Question 6

■ A queue data-structure can be used for?


A. expression parsing
B. recursion
C. resource allocation
D. all of the above

159
Question 6

■ A queue data-structure can be used for?


A. expression parsing
B. recursion
C. resource allocation
D. all of the above

160
Question 7

■ Program with highest run-time complexity is?


A. Tower of Hanoi
B. Fibonacci Series
C. Prime Number Series
D. None of the above

161
Question 7

■ Program with highest run-time complexity is?


A. Tower of Hanoi
B. Fibonacci Series
C. Prime Number Series
D. None of the above

162
Question 8

■ From a complete graph, by removing maximum _______________ edges, we can


construct a spanning tree?
A. e-n+1
B. n-e+1
C. n+e-1
D. e-n-1

163
Question 8

■ From a complete graph, by removing maximum _______________ edges, we can


construct a spanning tree?
A. e-n+1
B. n-e+1
C. n+e-1
D. e-n-1

164
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

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

166
Question 10

■ A queue data-structure can be used for?


A. expression parsing
B. recursion
C. resource allocation
D. all of the above

167
Question 10

■ A queue data-structure can be used for?


A. expression parsing
B. recursion
C. resource allocation
D. all of the above

168
Question 11

■ What is a hash table?


A. A structure that maps values to keys.
B. A structure that maps keys to values.
C. A structure used for storage.
D. A structure used to implement stack and queue.

169
Question 11

■ What is a hash table?


A. A structure that maps values to keys.
B. A structure that maps keys to values.
C. A structure used for storage.
D. A structure used to implement stack and queue.

170
Question 12

■ The recurrence relation 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

171
Question 12

■ The recurrence relation 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

172
Question 13

■ What does the following function do for a given Linked List with first node as head?
void fun1(struct node* 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 reverse order
C. Prints alternate nodes of Linked List
D. Prints alternate nodes in reverse order

173
Question 13

■ What does the following function do for a given Linked List with first node as head?
void fun1(struct node* 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 reverse order
C. Prints alternate nodes of Linked List
D. Prints alternate nodes in reverse order

174
Question 14

■ 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. Random access is not allowed in a typical implementation of Linked Lists
D. All of the above

175
Question 14

■ 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. Random access is not allowed in a typical implementation of Linked Lists
D. All of the above

176
Question 15

■ 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

177
Question 15

■ 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

178
Question 16

■ 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

179
Question 16

■ 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

180
Question 17

■ 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

181
Question 17

■ 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

182
Question 18

■ The result evaluating the postfix expression 10 5 + 60 6 / * 8 – is


A. 284
B. 213
C. 142
D. 71

183
Question 18

■ The result evaluating the postfix expression 10 5 + 60 6 / * 8 – is


A. 284
B. 213
C. 142
D. 71

184
Question 19

■ How many stacks are needed to implement a queue. Consider the situation where
no other data structure like arrays, linked list is available to you.
A. 1
B. 2
C. 3
D. 4

185
Question 19

■ How many stacks are needed to implement a queue. Consider the situation where
no other data structure like arrays, linked list is available to you.
A. 1
B. 2
C. 3
D. 4

186
Question 20

■ The most appropriate matching for the following pairs


X: depth first search 1: heap
Y: breadth-first search 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

187
Question 20

■ The most appropriate matching for the following pairs


X: depth first search 1: heap
Y: breadth-first search 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

188
Question 21

■ 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

189
Question 21

■ 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

190
Question 22

■ Randomized quicksort is an extension of quicksort where 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!)

191
Question 22

■ Randomized quicksort is an extension of quicksort where 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!)

192
Question 23

■ 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

193
Question 23

■ 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

194
Question 24

■ 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 sequence of 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

195
Question 24

■ 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 sequence of 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

196
Question 25

■ Consider the following graph. Among the following sequences, Which are depth first
traversals of the graph?
I) abeghf
II) abfehg
III) abfhge
IV) afghbe
A. I, II and IV only
B. I and IV only
C. II, III and IV only
D. I, III and IV only

197
Question 25

■ Consider the following graph. Among the following sequences, Which are depth first
traversals of the graph?
I) abeghf
II) abfehg
III) abfhge
IV) afghbe
A. I, II and IV only
B. I and IV only
C. II, III and IV only
D. I, III and IV only

198
Question 26

■ What is the weight of a minimum spanning tree of the following graph ?


A. 29
B. 31
C. 38
D. 41

199
Question 26

■ What is the weight of a minimum spanning tree of the following graph ?


A. 29
B. 31
C. 38
D. 41

200
Question 27

■ The best data structure to check whether an arithmetic expression has balanced
parentheses is a
A. Queue
B. Stack
C. Tree
D. List

201
Question 27

■ The best data structure to check whether an arithmetic expression has balanced
parentheses is a
A. Queue
B. Stack
C. Tree
D. List

202
Question 28

■ 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

203
Question 28

■ 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

204
Question 29

■ 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)

205
Question 29

■ 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)

206
Question 30

■ 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-subtree of x, no. of leaf-nodes in right-subtree of x} then
the worst-case time complexity of the program is
A. Θ(n)
B. Θ(n Log n)
C. Θ(n^2)
D. Θ(n^2 log n)

207
Question 30

■ 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-subtree of x, no. of leaf-nodes in right-subtree of x} then
the worst-case time complexity of the program is
A. Θ(n)
B. Θ(n Log n)
C. Θ(n^2)
D. Θ(n^2 log n)

208
CONTACT INFO

You might also like