You are on page 1of 3

Graphs Tutorial Solutions

1a) Cities are nodes. Edges are distances.

b) Gungahlin, Woden, Tuggeranong = 29km.

2 a) Do a breadth-first traversal, starting at V3. Shortest path to each other node is the height of that
node in the BFS tree
V3 to V2, V4, V5, V6 =1; V0 = 2; V1 = 3

b) V2 to V0 = 4
V1 = 6
V3 =5
V4 = 7
V5 =5
V6 = 9

c) unweighted = 3; weighted = 9 (v2 to v6)

d) 2

e) 4

f) For a directed graph, we use the out-neighbourhood: the vertices accessible by an edge from v0. The
out-neighbourhood is {v3, v1}

g) For a directed graph c = e/[k(k-1)], where e is the number of edges in the out-neighbourhood, and k is
the size of the out-neighbourhood. For v0, c = 1/2 = 0.5

h) {v2, v5, v6, v4}.

i) c = e/[k(k-1)] =3/12 = 0.25

j) v3, v2, v5, v6, v4, v0, v1

3. Two parts required:


Part 1: pre-processing:
Read in the dictionary and construct a graph where each word is a vertex
If there is a one letter transformation between two words then put an edge between
those words
Part 2: canChange(word1, word2)
Return true if there is a path from word1 to word2; false otherwise.
4. Answer the following:

a) What is the difference between a (min)heap and a binary search tree?

Binary search tree has left < root <= right; 2


Minheap has root < both left and right
1 3

b) Consider the tree at the right (top) 4


i) Is it a binary search tree or a minheap? 2
Binary tree
ii) Show the tree after the addition of 4 3

iii) Now show it after a call to removeMin() 4

c) Now consider the tree at the right (bottom) 1


i) Is it a binary search tree or a minheap?
2 3
minheap
ii) Show the three after the addition of 4 4
iii) Now show it after a call to removeMin() 2

4 3
d) What is the difference between a minheap and a maxheap?

e) What is a priority queue, and what alternative


implementations exist for it? Give three alternatives.
Priority queue is a queue with the additional property that nodes have a priority.
Ordering is determined first by priority and then by arrival time. It can be
implemented as a list of queues, as a binary heap using an array, or as a binary heap
using a node-link structure.

5. Given this heap of five elements


Priority 3

Priority 5 Priority 10

Priority 11 Priority 15
a)Draw the state of the heap when a new
element with priority 2 is placed in its proper location.
Priority 3

Priority 5 Priority 10

Priority 11 Priority 15 Priority 2


b) Then draw the state of the heap after it has
been rearranged to achieve the ordering property.
Priority 2

Priority 5 Priority 3

Priority 11 Priority 15 Priority 10

c) Suppose this heap is used as a priority queue. Suppose another element arrives
with priority 2. Where will it be stored?

In a queue with the current priority 2 node (the root in this case)

6. Define an in-place algorithm and describe one advantage of such an algorithm


over one that cannot be run in-place

An in place algorithm can be performed in the same memory as the initial data
structure (perhaps with a small amount of extra memory necessary). The advantage
is that significantly less than 2*n memory is required to run the algorithm.

You might also like