Professional Documents
Culture Documents
School of Computing
IT5003: Data Structure and Algorithm
Semester I, 2019/2020
Tutorial x Lab 7 Suggested Partial Solution
Heap and Graph
ANS:
a. Bubble up and down accordingly (from left to right of input integer list)
12
/ \
11 9
/ \ / \
4 10 6 8
/ \ / \ /
2 3 1 7 5
1 | Page
2. [Additional Heap Operations] Given a max-heap stored in items[0..size -1], write two
functions as follows. You can utilize the bubbleUp() and bubbleDown() functions given in the
lecture notes. Also, state the time complexity for each function.
ANS:
a.
#pseudocode outline
def updateKey( p, newVal ):
if p >= size or p < 0:
return #error
Worst Case Complexity: O( lg N ) - since height of tree, ℎ ∈ 𝑂(𝑙𝑜𝑔 𝑛)so the number
of ‘bubbling’ operations is bounded above by this.
b. def delete( p ):
if p >= size or p < 0:
return #error
Worst Case Complexity: O( lg N ) - similar reason as above, in fact all the operations
before the final updateKey part is clearly O(1) so does not affect time complexity
2 | Page
3. [Graph Traversals & Algorithms]
a h
2 6
4 4
1 1
b c d
2 3
1
e f
3 5
2
i g
a. Given the graph above, give one possible Breath-First Search (BFS) sequence that starts
at vertex “a”.
b. Give one possible Depth-First Search (DFS) sequence that starts at vertex “a”. Although
DFS is not formally covered in the lecture, the idea should be reasonably simple. Give it a
try?
d. Use Dijkstra’s shortest path algorithm on the graph using vertex "a" as the starting point.
You can use a table (example given below) for your working instead of the more graphical
tracing used in the lecture.
3 | Page
Note: The “S” column is the set of fixed nodes with confirmed shortest distance (i.e. the "red"
vertices"). “v” is the vertex picked in each round for updating its neighbors.
ANS:
d.
4 | Page