Professional Documents
Culture Documents
Algorithms and Data Structure
Algorithms and Data Structure
DATA STRUCTURE
محور الخوارزميات وبنى المعطيات
■ خوارزمية حل مسألة
– هي توصيف صوري لطريقة الحل على شكل متتالية منتهية من العمليات البسيطة ،تنفذ
حسب تسلسل محدد.
■ البرنامج
هو توصيف لخوارزمية حل مسألة معينة بإحدى لغات البرمجة التي يقبلها الحاسوب.
■ لغة البرمجة
– هي مجموعة من المفردات والقواعد والدالالت المعرفة التي تسمح بكتابة برنامج يمكن
تنفيذه على الحاسوب.
■ المترجم
– هو برنامج يفهم البرنامج المكتوب بلغة برمجة معينة ،ويحوله إلى برنامج مكافئ مكتوب
بلغة المجمع الخاصة بالمعالج الصغري للحاسوب.
■ المتغيرات التي يتم وضعها في الصف تقسم إلى ثالث فئات أساسية:
– :Local variablesهي المتغيرات التي يتم تعريفها بداخل أي دالة.
– :Instance variablesهي المتغيرات التي يتم تعريفها بداخل الكالس وخارج حدود
أي دالة.
– :Class variablesهي المتغيرات التي يتم تعريفها ك staticبداخل الكالس .وخارج
حدود أي دالة.
■ يمكن تطبيق مفهوم البرمجة غرضية التوجه في البرامج بدون استخدام الصفوف
.Aصح
.Bخطأ
■ يمكن تطبيق مفهوم البرمجة غرضية التوجه في البرامج بدون استخدام الصفوف
.Aصح
.Bخطأ
■ ما هي الخاصية الموجودة في البرمجة غرضيه التوجه التي توضح وتركز على مفهوم
)reusability( إعادة االستخدام في البرمجة
A. Polymorphism
B. Abstraction
C. Encapsulation
D. Inheritance
■ ما هي الخاصية الموجودة في البرمجة غرضيه التوجه التي توضح وتركز على مفهوم
)reusability( إعادة االستخدام في البرمجة
A. Polymorphism
B. Abstraction
C. Encapsulation
D. Inheritance
■ Abstraction ()التجريد
– Abstraction is a process where you show only “relevant” data and “hide”
unnecessary details of an object from the user.
■ Encapsulation ()التغليف
– Encapsulation is binding the data with the code that manipulates it. It keeps the
data and the code safe from external interference.
■ Inheritance ()الوراثة
– Inheritance is the mechanism by which an object acquires the some/all properties
of another object. It supports the concept of hierarchical classification.
■ Polymorphism ()تعدد األوجه
– Polymorphism means to process objects differently based on their data type.
■ مفهوم ال polymorphism
– المقصود منه بناء دالة تنفذ أوامر مختلفة على حسب الكائن الذي يمرر لها ك
argument
– تعني قدرة الكائن على أخذ عدة اشكال ،تحديدا قدرة الكائن على التحول إلى نوع
مشتق منه.
) وال تدعم تعدد االشكالclasses( ■ ماذا تسمي اللغة التي تدعم الصفوف
)؟polymorphism(
A. Class based language
B. Procedure oriented language
C. Object based language
D. If classes are supported, polymorphism will always be supported
) وال تدعم تعدد االشكالclasses( ■ ماذا تسمي اللغة التي تدعم الصفوف
)؟polymorphism(
A. Class based language
B. Procedure oriented language
C. Object based language
D. If classes are supported, polymorphism will always be supported
■ 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
■ 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
■ How many types of constructors are available for use in general (with respect to
parameters)?
A. 2
B. 3
C. 4
D. 5
■ How many types of constructors are available for use in general (with respect to
parameters)?
A. 2
B. 3
C. 4
D. 5
■ 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
■ 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
■ يعتمد زمن تنفيذ البرنامج على حاسب على عوامل عديدة منها:
– معطيات المسألة الخاصة بتجربة ما.
– جودة الرماز الذي يولده المترجم من أجل بناء الملف التنفيذي.
– طبيعة وسرعة التعليمات المتوفرة في الحاسوب (في المعالج الصغري).
– فعالية الخوارزمية( .تعتمد على قدرة المبرمج)
لحساب زمن تنفيذ خوارزمية يجب تحديد بعد (او طول او حجم) معطيات المسألة من أجل كتابة درجة ■
التعقيد بداللة هذا البعد.
أمثلة: ■
– مسائل كثيرات الحدود:
يكون البعد هو درجة كثيرات الحدود. ■
مسائل المصفوفات: –
يكون البعد بداللة أبعاد المصفوفة. ■
مسائل البيانات: –
يكون البعد بداللة عدد العقد أو األسهم أو مجموعها. ■
مسائل الفرز: –
يكون البعد بداللة عدد العناصر التي نريد ترتيبها. ■
مسائل التحليل القواعدي: –
يكون البعد بداللة طول الكلمة. ■
T(n)
n n n log n n2 n3 n4 n10 2n
10 .01s .03s .1s 1s 10s 10s 1s
20 .02s .09s .4s 8s 160s 2.84h 1ms
30 .03s .15s .9s s 810s 6.83d 1s
40 .04s .21s 1.6s s 2.56ms 121d 18m
50 .05s .28s s s 6.25ms 3.1y 13d
100 .1s .66s 10s 1ms 100ms 3171y 41013y
103 1s 9.96s 1ms 1s 16.67m 3.171013y 3210283y
104 s 130s 100ms 16.67m 115.7d 3.171023y
105 s 1.66ms 10s 11.57d 3171y 3.171033y
106 ms 19.92ms 16.67m 31.71y 3.17107y 3.171043y
■ يمكن ان نقول إن الخوارزميات الجيدة لحل مسألة معينة هي التي يكون زمن تنفيذها:
– ثابتا مهما كان حجم المعطيات :مثل خوارزميات البحث في جداول التقطيع.
– لوغاريتميا :مثل خوارزميات البحث الثنائي.
– خطيا :مثل خوارزمية البحث التسلسلي.
– من مرتبة ) :n*log(nمثل خوارزميات الفرز الجيدة.
int main() {
printf("%d ", fun(2));
return 0; }
int main() {
printf("%d ", fun(2));
return 0; }
int main() {
printf("%d ", fun(6));
return 0; }
int main() {
printf("%d ", fun(6));
return 0; }
int main() {
printf("%d ", fun(4,3));
return 0; }
int main() {
printf("%d", f(11));
return 0; }
int main() {
printf("%d", f(11));
return 0; }
A B C
Head
node
data pointer
■ هي بنية خطية تشبه السلسلة ،غير إن عمليات اإلضافة والحذف تتم من جهة واحدة ندعوها
قمة المكدس.
هي بنية معطيات تمكن من تخزين عناصر من نفس النمط. ■
■ تتبع مبدأ :
– )First In last Out (FILO
– )Last In First Out (LIFO
■ العمليات االساسية عليه:
– يمكن الوصول إلى العناصر من خالل قمته ))head
– إضافة عنصر إلى القمة ))push
– حذف عنصر من القمة ))pop
يمكن تمثيل المكدس باستخدام المصفوفات أو السالسل الخطية. ■
■ تطبيقات المكدسات:
– سجل الصفحات المزارة في متصفح االنترنت.
– عمليات التراجع في محررات النصوص.
– ربط أزواج األقواس في التعابير الرياضية.
– التحويل من التعابير الرياضية النظامية إلى التعابير الملحقة وحساب قيمة هذه
التعابير.
– استدعاء تابع في جسم تابع.
– التجول في األشجار والبيانات بطريقة العمق أوال.
– مكدس األخطاء في المترجمات.
■ 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 or equal 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. Then 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 to 6 until infix expression is scanned.
7. Pop and output from the stack until it is not empty.
■ 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.
■ 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.
■ Prefix Calculation:
1. Put a pointer P at the end of the end.
2. If character at P is a number 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.
■ Done!
■ Done!
هي بنية خطية تشبه السلسلة غير أن عمليات اإلضافة تجري في جهة ندعوها ذيل الرتل. ■
ويجري الحذف في الجهة المعاكسة التي نسميها بداية الرتل.
هي بنية معطيات تمكن من تخزين عناصر من نفس النمط. ■
■ تتبع مبدأ :
– )First In First Out (FIFO
– )Last In Last Out (LILO
■ العمليات االساسية عليه:
– يمكن الوصول إلى العناصر من خالل بدايته ( )headأو نهايته (.)tail
– إضافة عنصر إلى النهاية (.)enqueue
– حذف عنصر من البداية (.)dequeue
يمكن تمثيل االرتال باستخدام المصفوفات أو السالسل الخطية. ■
■ تطبيقات األرتال:
– مخازن المدخالت والمخرجات في الكمبيوتر (لوحة المفاتيح والطابعة).
– بنية معطيات مساعدة في الخوارزميات مثل خوارزميات التجول بالعرض أوال في
األشجار والبيانات.
– محاكاة أرتال الحياة اليومية.
االولويات: ■ رتل
يضاف عامل األولوية إلى الرتل. –
يتم تخديم العنصر ذو األولوية األعلى اوال. –
مثال: –
أرتال االنتظار في عيادة الطبيب حيث تكون األولوية للحاالت اإلسعافية. ■
■ نعرف الشجرة:
– بأنها مجموعة من العناصر نسميها عقد )،)nodes
– مرتبطة بين بعضها بروابط (،)Links
– ومنظمة تنظيما هرميا (،)Hierarchical
– ال يحتوي حلقات ( )Cyclesمغلقة،
– أي إنه توجد عقدة مميزة ووحيدة نسميها جذر (.)Root
■ أمثلة:
– تنظيم الملفات في مجلدات.
– تمثيل عبارة حسابية.
■ حالة خاصة من األشجار يكون فيها لكل عقدة ابنان علي األكثر:
– نسمي العقد التي لها ولد على األقل عقد داخلية.
– نسمي العقد التي ليس لها أي ولد عقد خارجية أو أوراق (.)Leaf
– نسمي طريقا ( )Pathكل متتالية من العقد.
– نسمي فرعا كل طريق يصل بين الجذر وإحدى األوراق.
■ تعاريف مهمة:
– درجة الشجرة :العدد األعظمي ألوالد عقدة( .درجة شجرة ثنائية هي )2
– حجم الشجرة :عدد عقدها.
– ارتفاع عقدة :عدد االتصاالت في الطريق الواصل من العقدة الى الجذر.
– ارتفاع الشجرة :أطول ارتفاع عقدة فيها.
– عرض الشجرة :العدد األعظم للعقد في مستوى ما.
– مسافة التجول :مجموع ارتفاعات عقدها.
– مسافة التجول الخارجي :مجموع ارتفاعات أوراقها.
– مسافة التجول الداخلي :مجموع ارتفاعات عقدها الداخلية.
10 2 45 10
5 30 30 5 45
2 25 45 10 2 25 30
25
Binary search tree Binary search tree Not a binary search tree
7 16
6 33
ترتيب زيارة العقد في التجول :VLR
22,6,33,-1,8,45,7,16
7 16
2021 By Eng. Joud Khattab 166
GRAPHS
البيانات
■ هي بنية معطيات تخزن العناصر كعقد vertexويوجد بينها وصالت تمثل العالقات بين العقد
.edges
■ يمكن اعتبار البيانات تعميما للبنى الهرمية غير التسلسلية من الناحية النظرية ،فاألشجار
هي حالة خاصة من البيانات (بيانات ال تحوي حلقات).
■ يكون البيان متصل بشدة إذا كان في حالة كل عقدتين v,wيوجد طريق من vإلى wو
من wإلى v
)𝑛(𝑛−1
وفي هذه ■ عدد الروابط األعظمي في بيان غير موجه يحوي nعقدة يساوي
2
الحالة نقول عن البيان إنه تام (.)Complete
The Graph
The Adjacency Matrix
The Graph
The
2021 AdjacencyByList
Eng. Joud Khattab 172
)البيانات( Graphs
Question 1
السؤال :ماهي كلفة البحث عن عقد مجاورة لعقدة ممثلة بطريقة قائمة التجاور؟ ■
■ الجوابO(deg(v)) :
■ السؤال :ماهي كلفة مسح الروابط الواردة لعقدة في بيان ممثل بطريقة قائمة التجاور؟
■ الجواب O(V+E) :مكلف جدا
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
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
2021 By Eng. Joud Khattab 180
)البيانات( Graphs
Minimum Spanning Tree
■ شجرة االرتباط األصغرية :هي شجرة تربط البيان بأقل كلفة ممكنة من الروابط.
■ تتواجد هذه الشجرة في حالة البيان المترابط فقط.
■ Prim’s algorithm
الزمن المتوقع إلضافة والبحث عن عنصر في جدول تقطيع ضمن فرضيات مناسبة )( .O(1الحالة الوسطية) ■
0 0 0 There is a
1 1 21 … 1 21 … collision at
2 2 … 2 2 … 2 2 … array entry
#4
3 3 3
4 4 4 34 … ???
2021 By Eng. Joud Khattab 201
Hashing Tables
)جداول التقطيع(
■ تقنية الربط لحل التصادم:
– توضع جميع العناصر التي لها نفس قيمة تابع التقطيع في سلسلة خطية.
)(Hashing with Chaining
– تصبح خانة الجدول مؤشر إلى بداية سلسلة العناصر المتصادمة.
CHAIN
■ طريقة التقسيم:
– بربط المفتاح kبالخانة التي تساوي باقي قسمة kعلى .m
– h(k)=k%m
– يجب تجنب قيم mمن قوى 2واختيار أعداد أولية بعيدة عنها.
■ طريقة الضرب:
– نضرب المفتاح kبثابت Aحيث 0<A<1ونأخذ الجزء الكسري ثم نضرب هذه القيمة
ب mونأخذ الجزء الطبيعي من العدد الناتج.
– يمكن اختيار أي قيمة للعدد .m
■ 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
■ 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
■ 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
■ 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
■ 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.
■ 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.
■ 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
■ 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
■ 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
■ 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
■ 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
■ 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
■ 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
■ 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
■ 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
■ 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
■ 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
■ 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
■ 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
■ 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
■ 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
■ 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
■ The best data structure to check whether an arithmetic expression has balanced
parentheses is a
A. Queue
B. Stack
C. Tree
D. List
■ The best data structure to check whether an arithmetic expression has balanced
parentheses is a
A. Queue
B. Stack
C. Tree
D. List
■ 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
■ 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
■ 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)
■ 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)