Download PART 2

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Control table wikipedia , lookup

Hash table wikipedia , lookup

Quadtree wikipedia , lookup

Binary search tree wikipedia , lookup

Transcript
Cairo University
Faculty of Computers and Information
CS214 - Data Structures – Dr. Mohammad El-Ramly
Final Exam – 60 Marks
Spring 2011
20 June 2011 – 2 hrs
Instructions




‫ابدأ بسم هللا و توكل على هللا و أجب على قدر استطاعتك و ال تقلق و اقرأ كل األسئلة أوال بعناية شديدة‬
Answer part 1 and 2 questions from part 2. ‫أجب الجزء األول كامال و سؤالين من الجزء الثانى‬
Your answer must respond to all parts of the question and address the underlined bold question words.
‫تأكد من وجود إجابة لكل جزء مكتوب بخط ثقيل و تحته خط‬
Today is the last day to complain about coursework. ‫اليوم آخر يوم لنظر مشاكل الطالب‬
__________________________________________________________________________________________
PART 1 – Answer ALL Questions
_______________________________________________________________________________________________________________________________________
Question 1 –General (16 marks) –
1. Data structures subject is concerned with data organization and fast access to this data in memory.
Given the following data structures,
(1) An ordered linked-list.
(2) An (almost balanced) binary search tree, where every node has two pointers to its two children
and a pointer to its parent.
(3) A max heap
(4) An unsorted array
(5) A perfect hash table (where every key is hashed to a unique index)
(i) Which of these data structures (one or more) performs better in the each of the following tasks?
And why?
a) Retrieving the item with maximum key.
b) Retrieving a range of data, e.g., m consecutive ordered items (‫) مثال ألف بيان مرتب و متتابع‬, where
m is a non-negative integer.
c) Finding and deleting a specific item from the data structure.
d) Insertion of m consecutive ordered items which are all guaranteed to be greater than the
maximum data item stored in the data structure
(ii) What is the order of magnitude of each data structure in each task in terms of n (input size) and
m (number of items to insert or delete)? Put your answer in a table whose columns are data
structures and whose rows are these tasks and whose cells are the O(…) and explain very briefly
how you calculated each complexity.
Ret. Max Key
OLL
O(1) – last
item
BST
O(log n) – must
go to far right
Retrieving a
range of data,
e.g., m
consecutive
ordered items
O(n + m) –
O(n) to find
the first item
and O(m) to
get the next
m items
O(log n + m) –
O(log n) to find
the first item and
O(m) to traverse
tree to get next m
items
Finding and
deleting a
specific item
from the data
structure
Insertion of m
consecutive
ordered items, all
are larger than
the largest item
O(n)
O(log n) - until
reaching the item
O(m) –
because we
know where
to insert (at
end)
O(log n + m) –
O(log n) to find
the max item aft
far right and O(m)
to add new m
items
Max Heap
O(1) –
already root
element of
heap
O(n m) –
O(n) to get
the first item
and repeat
this m times
to get the
next item
O(n) – No
order of
items
Ùnsorted Array
O(n) – must
search through
the entire array
Hash Table
O(n) – No specific
order of items. must
search through the
entire table
O(n m) – O(n)
O(n m) – O(1) to get
to get the first
the first item and
item and repeat
loop m times, each
this m times to
time to find the next
get the next item item in range in O(n)
O(n) – We do
not know where
it is.
O(1) – Directly
access it and delete
it.
O(m log n) –
put m items
and each
takes o(log n)
to move from
bottom up
O(m) – It does
not matter where
to insert. We
insert m items,
each at the next
empty array in
the cell in O(1).
O(m) – It does not
matter where to
insert, we will insert
m items, each of
them in O(1)
Question 2 – Hash Tables (16 marks)
(i)
Explain how double hashing works?
(ii)
What advantage does double hashing have over open addressing with linear and quadratic probing?
(iii)
Assume that we want to insert the following data items in a hash table of size 11.
Keys:
24,
19,
99,
123,
232,
33,
73
We will use the following hashing function:

h1 (k) = k % 11
In case of collision, we use double hashing using function h2 (k) and the following probe sequence:
 p(k, i) = h1 (k) + i * h2(k)
 where h2 (k) = k / 11 (Integer division with no remainder, e.g. 115 / 11 = 5)
how the table after inserting these items and explain how collisions were resolved with double hashing
in each case collision occurred.
__________________________________________________________________________________________
PART 2 – Answer ONLY TWO Questions
__________\____________________________________________________________________________________________________________________________
_
Question 3 – Complexity Analysis / Graphs (16 marks)
(i)
Hardware vendor XYZ Corp. claims that their latest computer will run 64 times faster than that of their
competitor, Prunes, Inc. If the Prunes, Inc. computer can execute a program on input of size n in one
hour, what size input can XYZ’s computer execute in one hour for each algorithm with the following
growth rate equations? How did you calculate your answer?
(a) n
(b)
n2
(c) 2n
(ii)
Given Prim’s algorithm below,
(a) What is the order of magnitude of this algorithm?
(b) Apply the algorithm to find the minimum spanning tree of this graph. Show the detailed steps of
your work.
\
Question 4 – AVL Trees (16 marks)
(i)
Given the following AVL tree, calculate the balance factor
of each node
(ii)
Insert 5 in its proper place and recalculate the balance factor
of each node (draw the tree again).
(iii)
Do the necessary rotations to re-balance the tree. Show the
detailed steps and explain how rotation happens.
(iv)
Remove 14 and do the necessary rotations to re-balance the
tree. Show the detailed steps and explain how rotation
happens.
Question 5 – Complexity Analysis / Graphs (16 marks)
(i)
Find the complexity of this function used to find the kth integer in an unordered array of integers.
Explain how you calculated it.
(ii)
Assume two implementations of a graph data structure. Assume that the weights on the graph edges are
of type double and the take 8 bytes. Assume that a pointer takes 4 bytes. Assume the following two
cases of a graph:
(1) A highly connected graph with 100 nodes and 4000 edges
(2) A lightly connected graph with 100 nodes and 200 edges
For each case, calculate the storage size required to store the data in
(a) An adjacency List
(b) An adjacency Matrix
And decide which representation takes less storage in each case.