M269-Final-Fall 23-24-Key

.pdf
School
Arab Open University, Beirut**We aren't endorsed by this school
Course
ITC M269
Subject
Computer Science
Date
Jan 6, 2025
Pages
8
Uploaded by BarristerStarAntelope42
M269 / Final Page 1of 82023/2024 FallM269 Algorithms, Data Structures and ComputabilityFinal Examination Key Fall Semester 2023/2024 Date: 30/Dec./2023 Time Allowed: 3 Hours Number of Exam Pages: 8 (including this cover sheet)Instructions:This exam has 3 parts. Write your answer in the answer booklets. Answers given on the exam paper will not be marked. Student handbooks are NOT permitted in this examination. The use of electronic devices that could have a memory is NOT permitted. At the end of the examination, check that you have written your student ID, name and your section number on the first page.
Background image
M269 / Final Page 2of 82023/2024 FallPART 1: ALL QUESTIONS ARE REQUIRED [10 Marks] Question 1: (10 marks) Mark the following statements are true or false 1) A graph is a collection of nodes, called vertices and line segments called edges that connect pair of nodes. (T)2) Linear search in an array A[n] involves (log n) comparisons. (F)3) Minimal spanning tree algorithm belongs to the brute-force paradigm (F)4) KMP searching algorithm is faster than the Linear search (T)5) Searching in a list for an unavailable value will lead to average case performance. (F)6) The sequence [20, 15, 18, 7, 9, 5, 12, 3, 6, 2] is a max-heap. (T)7) To represent relationship between friends in a social community, a tree data structure is suitable. (F)8) Given 11 sorted items in an array, using binary search, the average number of searches that will be required to find an item in such array is 5. (F)9) The worst-case performance of looking up an element in a hash table is O(n), where n is the number of elements. (T)10) In the breadth first traversal, we process all of a vertex’s descendants before we move to an adjacent vertex. (F)PART 2: ALL QUESTIONS ARE REQUIRED [40 Marks] Question 2: (15 marks) a) What is the complexity of the following tasks? (7marks: 1 mark each) 1) Searching for an element in a sorted list using Binary-Search algorithm, where the list is of length n. 2) Inserting an element in a balanced Binary-Search-Tree of size n. 3) Searching for a pattern of size minto a string of size nusing linearalgorithm. 4) Finding the kthelement in a list of size n using Quick-Select algorithm. 5) Searching for a pattern of size minto a string of size nusing KMP algorithm. 6) Traversing a graph of order nusing Breath-First Search algorithm. 7) Building adjacency matrix for a graph of order nand size m. b) Give a short answer to the following questions: (8marks: 2 marks each) 1) For the purpose of searching for a specific element in a given list, state in which case the usage of linear searching would be better than binary search? 2) If several elements are competing for the same bucket in the hash table, what is the process called? State which two techniques can be used to avoid this scenario?3) Given an array arr= [10, 12, 14, 25, 33, 42, 55] and key= 10, state in how many iterations would the linear search and binary search reach to the key?4) State the algorithm paradigms to which the following algorithms belong: i. linear search ii. binary search Answer:
Background image
M269 / Final Page 3of 82023/2024 Falla) What is the complexity of the following tasks? (5marks: 1 mark each) 1) O(log n) 2) O(log n) 3) O(nm) 4) O(n2) 5) O(n+m) 6) O(n+m)7) O(n2) b) The short answers 1) For the purpose of searching for a specific element in a given list, explain a case in which the usage of linear searching will be better than binary search? When the list has only a few elements and When performing a single search in an unordered list (2mark) CBM note: if the student gives: Any one of the listed cases worth 2 marksA numeric example without any explanation worth 1 mark2) If several elements are competing for the same bucket in the hash table, what is it called? List two techniques to avoid this scenario? a. It is called "Collision" (1 mark) b. the two techniques are: (Open addressing or Probing ) and chaining. (1mark) 3) Given an array arr = [10, 12, 14, 25, 33, 42, 55] and key = 10; In how many iterations would the linear search and binary search reach to the key? a. Linear search: 1 iteration (1mark) b. Binary search: 3 iterations (1 mark) 4) Determine the algorithm paradigm to which does the linear search and binary search belong. a. Linear search: Brute force (1mark) b. Binary search: Dived and conquer (1 mark) Question 3: (15 marks) a) For each of the following scenarios choose the “best” data structure that should be used, and give a rationale for your answer: (10 marks: 1 for the data structure, and 1 for the rationale) 1) A word editing software retain previous editing steps. 2) Depicting the connections between friends in a school. 3) A shop serves customers on a first-come, first-served basis. 4) Catalog of elements that undergo frequent changes. 5) Depicting the hierarchy of authority in a company. b) How the following array [9, 8, 23, 14, 17] will look like after applying Build_ Max Heap function? Once this function is implemented, how many cycle it would take to get the maximum element in this array? (5 marks) Answer
Background image
M269 / Final Page 4of 82023/2024 Falla) The best data structures are as follows: (10 marks: 1 for the data structure, and 1 for the rationale) [Note: the rationale can take many forms. In the following model answer, I just give an example] 1) Stack, because it works using last in-first-out mechanism 2) Graph, because the use of nodes in a graph is similar to people in reality where the relations between them can be represented through edges between nodes. 3) Queue, because the queue supports the FIFO serving mechanism. 4) Linked-list, because the cost of inserting/deleting a node in a linked list is far less than its counterpart in other data-structures 5) Tree, because the chain of command in a company is a hierarchy structure with different levels of authorities in a pyramid shape. The tree on the other side afford this structure through root, children nodes, and so on. CBM note: the rationale that the student may give, can take many forms. The above listed rationales are just one possible answer. So please read and judge. b) The array will look like this: (3 marks) It will not need any cycle as the maximum element will be at the first element on the array. (2 marks) Question 4: (10 marks) a) The following definitions describe different paradigms of algorithms. For each definition, state the name of the paradigm and give a name of a famous algorithm that belongs to this paradigm 1) All possible candidates are systematically listed and exhaustively checked to determine the final solution. (2 marks) 2) Several smaller sub-problems are partitioned and solved independently. A final solution is formed by combining the solutions of these sub-problems. (2 marks) 3) In order to construct larger sub-problem solutions, the smallest sub-problems are solved first and the results are stored. (2 marks) 4) A sequence of steps leads to the solution. Optimal steps are selected locally at each step without considering if they will lead to the final global optimal solution. (2 marks) b) Each of the following phrases describes a particular classes of problems, determine it. (total 2 marks: 1 mark each) 1) Theybelong to a class of problems that can be easily solved by a computer as they have a yes/no answer.
Background image
M269 / Final Page 5of 82023/2024 Fall2) Theybelong to a class of problems that are easy to verify, but are hard to solve in the sense that it would take very long time for the computer to come up with an answer. Answer: a) The algorithm paradigm: 1) Brute-forceparadigm. (1 mark) Ex:Selection sort algorithm (1 mark)2) Divide & conquerparadigm. (1 mark) Ex: Merge sort algorithm (1 mark)3) Dynamic Programmingparadigm. (1 mark) Ex: Floyd-Warshall algorithm (1 mark)4) Greedyparadigm. (1 mark) Ex: Minimal spanning tree algorithm (1 mark) CBM note: the student may give other algorithm as example, so be open mind. b) The (total 2 marks: 1 mark each) P-Problem. (1 mark) NP-Problem. (1 mark) PART 3: ALL QUESTIONS ARE REQUIRED [50 Marks] Question 5: (40 marks) Consider the weighted graph illustrated in Fig.1 Figure 1 weighted graph a) Does this graph contain Euler circuit or/and Euler path? Why? (4 marks) b) What is the sequence of visited node in BFS, and DFS if both algorithms start from node A? (4 marks) c) Draw the minimum spanning tree of this graph. (6 marks). What is the name and complexity of such algorithm? (4 mark) d) Use the Dijkstra's algorithm to determine the shortest paths from node (A) to all other cities (8 marks). Determine the shortest path and cost from node A to node H (2 marks).
Background image
M269 / Final Page 6of 82023/2024 Fall[Hint: use the Dijkstra's table to implement the algorithm step by step to show which node will be added in sequence] e) Implement the proper algorithm to find the shortest paths between all pairs of nodes [Compute D0, D1, D2]. Mention the name of the algorithm that could be used and its complexity (12 marks) Answer: a) The graph has neither Euler path nor Euler circuit, as most of the vertices are with odd degrees. (4 marks: 2 marks for the Euler path/circuit, and 2 marks for the reason) b) BFS: A, B, C, E, D, H, F, G (2 marks) CBM Note: if partially right (1 mark) DFS: A, B, D, C, E, F, G, H (2 marks) CBM Note: if partially right (1 mark) c) Kruskal's algorithm (1 mark) and its complexity is O(E log V) or O(E log E), where E is the number of edges, and V is the number of vertices. (3 mark) CBM Note: The first 5 right edges (any of the above) deserve 1 mark the last 2 right edges deserve 0.5 marktotal (6 marks) d) The Dijkstra's algorithm iteration Initial 1234 5 6 7 Priority From 0 A C F D E H B A0 0 0 0 0 0 0 0 B14 14 14 13 13 13 C4 D7 7 E11 11 11 11 F6 G 13 13 13 13 13 H 15 11 11 CBM Note: every right cell highlighted in yellow deserves 1 mark + the table itself worth 1 mark total (8 marks)
Background image
M269 / Final Page 7of 82023/2024 FallThe path from node A to H is: AC D H (1 mark) The cost is: 11 (1 mark) e) To find the shortest paths between all pairs of nodes we can use the FloydWarshall algorithm (1 mark) with complexity O(V3), where V is the set of vertices. (2 marks) [Finding the shortest path between all pairs of nodes: 9 marks] Applying the algorithm results in the following matrices: D0A B C D E F G H A 0 14 4 11 B 14 0 6 C 4 0 3 8 2 D 6 3 0 5 4 E 11 8 0 11 19 F 5 11 0 7 9 G 19 7 0 8 H 11 4 9 8 0 (3 marks) Partially correct (1 mark) D1A B C D E F G H A 0 14 4 11 B 14 0 6 C 4 0 3 8 2 D 6 3 0 5 4 E 11 8 0 11 19 F 5 11 0 7 9 G 19 7 0 8 H 11 4 9 8 0 (3 marks) Partially correct (1 mark) D2A B C D E F G H A 0 14 4 11 B 14 0 6 C 4 0 3 8 2 D 6 3 0 5 4 E 11 8 0 11 19 F 5 11 0 7 9 G 19 7 0 8 H 11 4 9 8 0 (3 marks) Partially correct (1 mark) Question 6: (10 Marks) There is a group of birds, with equivalent number of nests aligned on a straight line. At certain point of time, each bird must fly to a nest. Given that the number of nests is
Background image
M269 / Final Page 8of 82023/2024 Fallequal to the number of birds (one nest fit for only one bird), each bird takes 1 second to fly one meter to the left or the right direction. For simplicity, assume that the positions of birds and nests are presented on the x-axis, where marks are one meter away from the other, your job is to calculate the least possible time needed to distribute any given number of birds on a corresponding number of nets. Input:Bird #1 Bird #2 Bird #3 Bird #4 Bird #5 positions of Birds 7 -3 2 -1 4 Nest #1 Nest #2 Nest #3 Nest #4 Nest #5 positions of Nests 0 3 5 -3 -2 Output:2 seconds Explanation:Assign Bird #1 to Nest # 3: Time taken is 2 seconds Assign Bird #2 to Nest # 4: Time taken is 0 second Assign Bird #3 to Nest # 1: Time taken is 1 second Assign Bird #4 to Nest # 5: Time taken is 1 second Assign Bird #5 to Nest # 2: Time taken is 1 second Since, the birds will all move together in the same initial time, the time needed for all the birds to fly to their nests is 2 seconds. Answer the following questions: a) Design an algorithm to solve this problem (6 marks)b) Derive the complexity of your algorithm. (4 marks) Answer: Marking guide: the problem is tricky with many possible solutions. The student will get mark based on the efficiency of his/her algorithm as follows: First scenario: The brute-force approach Use two nested loops to calculate the distance between every bird to every nest with O(n2) Then to choose the right nest for every birdwith another O(n2) [The 6 marks are given for such algorithm] If the student provide any other solution with less complexity it worth the full mark. CBM Note: If the student did Some effort without clear output worth 3 marksin totalThe brute-force approach with complexity O(n2) worth 6 marksin totalAny other better and clear solution with less complexity (with proof)worth 10 marksEnd of questions
Background image