You are on page 1of 17

Data Structures – Midterm - NTHU 2021 Fall Name

Ch.5 Trees ~ Ch.6 Graphs Student ID

1. (13%) There are two ways to build a heap from an input: Bottom-up and Top-
down.
(a) (5%) Given an input array: [1, 3, 6, 12, 7, 9, 4, 14, 16, 11, 20, 2]. Draw the
resulting max-heap built from the array using the Bottom-up method.
(b) (5%) Based on the result from 1.(a), draw the resulting max-heap after each
of the following operations (in the given order).
I. delete-max()
II. insert(8)
III. insert(15)
IV. delete-max()
V. delete-max()
(c) (3%, 1% each) What is the time complexity (in big-O) of the operations
from (a) and (b)?
(1) Build a max-heap with bottom-up method using an array data
structure.
(2) Delete-max()
(3) Insert()

ans:
(a) Bottom-up: Build a binary tree with the given array, and heapify all sub-trees in
order from the bottom.

(b) We will give you full credit if you have the correct final heap. Only when you
got it wrong would we check each of your sub-question.
(c)
I. O(n)
II. O(log n)
III. O(log n)

2. (8%) Given an array A = [15, 29, 68, 52, 21, 7, 2, 60, 59, 100].
(a) (4%) Build a binary search tree (BST) by sequentially inserting elements of
A (starting at A[0]) into an initially empty BST. Draw the final BST after all
elements are inserted.
(b) (4%) Following the tree from 2.(a), draw the resulting BST after each of the
following operation (in the given order). When deleting a non-leaf node
with two children, please always try first replace with a (correct) node from
its left sub-tree.
I. Delete(7)
II. Delete(29)
III. Delete(68)
Ans:
(a)

(b) We will give you full credit if you have the correct final BST. Only when you got
it wrong would we check each of your sub-question.
3. (6%) Suppose we represent a tree of degree 𝑘 with n nodes using the
following list structure, in which each node has one Data field and 𝑘 Link
fields.

(a) (3%) What is the number of unused Link fields of this data structure?
(b) (3%) We usually adopt the “Left-Child-Right-Sibling” method followed by
a 45-degree rotation to transform such a tree into a binary tree (i.e., k = 2)
for memory utilization improvement. Draw the transformed (binary) tree of
the following tree.
Ans:
(a) Total Link = n ∗ k, and total used Link = n − 1. Thus, total used Link = n ∗ k −
(n − 1).
(b)

4. (10%, 2% for each) Which of the following statement(s) is correct?


𝑉∗(𝑉−1)
(a) A complete directed graph has exactly edges, where 𝑉 is the
2

number of vertices.
(b) There will be exactly 𝑉 edges in a connected, acyclic, and undirected
graph, where 𝑉 is the number of vertices.
(c) A complete binary tree has exactly 2𝑘 − 1 nodes, where 𝑘 is the depth of
the tree.
(d) Let G be an undirected graph with 33 edges. Suppose G has 8 vertices of
degree 4, 5 vertices of degree 3, 7 vertices of degree 2, and the remaining
vertices are of degree 1. We conclude that there are 6 vertices of degree 1.
(e) Consider a binary tree with the following post-order and in-order sequences.
The last node of the tree visited by a pre-order traversal is 9.
Post-order: 3 13 6 7 12 18 2 4 10 5 9 11 15 20
In-order: 13 3 18 6 12 7 20 2 10 4 15 5 11 9
Ans: (e)
𝑉∗(𝑉−1)
(a) Complete “undirected” graph has edges, while complete directed graph
2

has 𝑉 ∗ (𝑉 − 1) edges.
(b) “A connected, acyclic, and undirected graph” is just a tree. A tree has 𝑉 − 1
edges.
(c) A “full” binary tree has exactly 2𝑘 − 1 nodes
(d) Since all edges are shared by two vertices, an edge will contribute 2 vertex
degrees to the graph, i.e, 66 = 8*4 + 5*3 + 7*2 + x*1. We have x = 5 as the
number of vertices of degree 1.
(e) The binary tree is:

The pre-order is: 20 18 13 3 12 6 7 15 10 2 4 11 5 9. Thus, the last visited node is


9.

5. (10%) Given the following directed graph G.


(a) (2%) Show the adjacency matrix of G?
(b) (2%) Draw the adjacency list of G. (list the vertices in the ascending order)
(c) (3%) Show the visiting sequence of DFS traversal of G starting from node
0. Break the ties by picking the vertex with the smallest value.
(d) (3%) Consider G as an undirected graph (i.e., all edges are not directed
anymore). Give the visiting sequence of BFS traversal on G starting from
node 0. Break the ties by picking the vertex with the smallest value.
Ans:
0 1 0 1 0 0 0 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 1 0
(a) 0 0 0 0 1 0 0 1
0 0 0 0 0 0 0 1
0 0 1 0 0 0 0 1
0 0 0 0 0 1 0 0
[0 0 0 0 0 0 1 0]
(b)
(c) 0, 1, 6, 5, 2, 7, 3, 4
(d) 0, 1, 3, 6, 4, 7, 2, 5
Note that BFS normally uses queues to store visited vertices, thus we can’t just
choose the smallest vertex in the same level. For example, since we choose 1
before 3, we can’t choose 4 before 6.
6. (10%) Given an undirected graph G.

(a) (3%) Draw the minimum spanning tree (MST) of G


(b) (1%) What is the total weight of the MST?
(c) (3%) If you apply Kruskal’s algorithm, what shall be the order of edges
selected to generate MST? (Use (u,v) to denote an edge that connects
vertices u and v.)
(d) (3%) If you apply Prim’s algorithm starting from vertex 3, what shall be the
order of edges selected to generate MST?
Ans:
(a)

(b) 9+11+12+13+16+17+19 = 97
(c) (5,8), (3,5), (6,8), (1,2), (1,4), (2,5), (7,8)
(d) (3,5), (5,8), (6,8), (2,5), (1,2), (1,4), (7,8)
7. (10%, 2% for each) Which of the following statement(s) is correct?
(a) Suppose we have found the shortest path P from a vertex A to B. If we now
double weights of all edges in the graph, P shall remain the shortest path
from A to B.
(b) Suppose we have found the shortest path P from a vertex A to B. If we now
add a constant positive value k to every edge in the graph, P shall remain
the shortest path from A to B..
(c) Since Dijkstra’s algorithm finds us the shortest path from a single source to
all other vertices, whenever we need the all-pair shortest paths, we could
just run Dijkstra’s algorithm on every vertex once to obtain the same result
as Floyd-Warshall algorithm.
(d) Sollin’s algorithm is a greedy algorithm.
(e) The disjoint-set data structure is commonly adopted by Kruskal’s algorithm.
Ans: (a), (d), (e)
(a) We have already found the shortest path, so we don’t have to worry about what
algorithm we use to find it. Think of the doubling operation as some kinde of
unit transform (such as kilometers to meters.)
(b) Suppose P has n edges and there is another path P’ from A to B with n’ edges,
where n > n’. The total weight of P will increase n*k while the total weight of P’
will increase n’*k. Thus P might gain more weights than P’ and flip the order
between them.
(c) Dijkstra’s algorithm can’t be applied to a graph with negative “edge weights”,
while Floyd-Warshall can.
(d) True.
(e) We usually use disjoint-set data structure to check whether a new edge will cause
a cycle.

8. (8%) Among all paths from A to I with at most 4 edges, find the path with the
minimum total cost and answer the following questions.
(a) (3%) What is the minimum total cost of the path?
(b) (5%) What is the visiting order of vertices and the edges connecting the
vertices of the path? (if there are multiple min-cost paths, pick any one.)
Ans:
You may use a combination of priority queues and the dijkstra algorithm to solve the
problem.
(Reference: https://stackoverflow.com/questions/40157977/shortest-path-in-a-
directed-weighted-graph-that-uses-at-most-k-vertices)
(Reference: https://stackoverflow.com/questions/69356130/given-a-graph-return-the-
shortest-path-from-start-to-end-that-has-at-most-k-edg)

Or run the Bellman-Ford algorithm Loop for 4 times, and we can find the shotest path
with at most 4 edges.
Loop A B C D E F G H I
#1 0 15 ∞ 20 ∞ ∞ ∞ ∞ ∞
#2 0 15 30 18 23 ∞ 30 ∞ ∞
#3 0 15 30 18 21 26 26 31 ∞
#4 0 15 28 18 21 24 24 27 31
A path (within 4 edges) from A to I has minimum total cost = 31 (with 4 edges), and
the path is:

9. (5%) Prove that the edge with the second smallest weight must be in the
minimum spanning tree (suppose all edges have unique weights, and the graph
has at least two edges).
Ans:
Let (u,v) be the edge with the second smallest weight. Suppose (u,v) is not part
of the minimum spanning tree T, there is exactly one path P from u to v in T, and P
surely contains at least two edges. Since (u,v) has the second smallest weight, one of
those edges E in P must have greater weight than (u,v). If we remove E from T and
add (u,v) to T to form a new tree called T’, T’ is a spanning tree with strictly smaller
total weight than the original “minimum” spanning tree T.
Since we made a contradiction, we know that (u,v) must be in the minimum
spanning tree.

(Reference: https://www.quora.com/How-can-I-prove-that-every-minimum-spanning-
tree-of-a-non-directed-edge-weighted-graph-contains-edge-u-v-by-using-the-
swapping-technique-Assume-that-edge-u-v-has-the-second-smallest-weight)

10. (20%) Machine Learning is surely one of the trending topics in recent years.
Machine Learning means to train a computer to “learn” some capabilities, such
as handwriting classifications, that normally only humans can do. One aspect of
Machine Learning that received a major breakthrough in the last decade was
Deep Learning, which utilizes a graph-liked data structure called Neural
Network (NN). A NN consists of an input layer, an output layer, and a number of
middle layers. The neurons (vertices in NNs are often called “neurons”) in each
layer are connected to neurons only in its adjacent layers via some weighted
edges, and edges points only towards the next layer. For example, a simple NN
shown below has only 1 middle layer with 3 neurons (labeled x, y, and z). For
brevity, the edge weights are not shown. An NN used in real applications
normally has multiple middle layers, that is what the word “Deep” implies in
Deep Learning.

For NN operations, first we feed the input data to the NN’s input-layer
neurons, and then make all the data “propagate forward” via the directed
weighted edges through each middle layer, and produce an output data stored in
NN’s output-layer neurons. The forward propagation works like this: whenever a
neuron u passes its data to its destination neuron v through an edge (u,v) with
weight 𝒘𝒖𝒗 , we add 𝐮 ∗ 𝒘𝒖𝒗 to the destination neuron v’s data. Since a
neuron could have multiple incident edges, the froward propagation calculation
is,
v = ∑𝒖∈𝑈 𝒖 ∗ 𝒘𝒖𝒗 , where U = {x | (x, v) exists}.
For example, with the following NN structure,
the forward propagation from the input layer to the middle layer is:
𝐼1
𝑀1 0.2 0.3 0.5 0 𝐼2
[ ]=[ ][ ] ,
𝑀2 0 0.2 0.3 0.5 𝐼2
𝐼4

and the forward propagation from the middle layer to the output layer is:
𝑂1 0.8 0.2 𝑀1
[ ]=[ ][ ] ,
𝑂2 0.2 0.8 𝑀2
We use the matrix form above to represent the calculation process. Now, we feed
an input data [𝐼1 , 𝐼2 , 𝐼3 , 𝐼4 ] = [1,0,1,1] into the NN, and we get,
𝑀1 0.7
[ ]=[ ]
𝑀2 0.8
𝑂1 0.72
[ ]=[ ]
𝑂2 0.78
Thereby, we get our output [𝑂1 , 𝑂2 ] = [0.72, 0.78]. The interpretation of
such output is that a number 𝑂𝑖 stored in the output neuroni means how sure the
NN think the input is related to the result that the output neuroni represents. In
practice, the output would contain a set of numbers range from 0 to 1. Normally,
1 means absolutely related, 0 means absolutely not related, and any number
between 0~1 means somewhat related. Thus, we usually refer to the number
stored in the output neurons as “score”, and the process of an NN mapping an
input into a set of scores (via forward propagations) as “Inference”, since an NN
essentially computes a score to represent how likely a result is, but not an
absolute answer.
One example of such “inference” is the hand-writing classification, where
NNs are asked to infer what the hand-written number is in a given image. An NN
for such application is shown below, where it takes 784 inputs and output 10
scores to neurons labeled 0 to 9.

(credit: youtube 3Blue1Brown)


An NN is said to be accurate if the highest score (i.e., close to 1) appears in
the correct output neuron. For example, after inputting an image of “3”, the
neuron labeled “3” should have highest score, such as 0.9, and all other 9
neurons should have lower scores, such as 0.1.
We now try to tweak an NN of our own. We define our question to be
“whether a 2x2 image contains the number 1 or not”. We use the following
NN, named Neural Network 1, to perform inference (the same NN as one of our
above examples.)

Neural Network 1
And we define neuron 𝑂1 corresponds to “Yes”, and 𝑂2 corresponds to “No”.
That is, if the image looks like either of the following, we say it contains a “1”
(according to the blacked-out area).
or .
Other pixel layouts are not considered to be a “1”:

or or or .
And we feed the following 2x2 image pattern in Neural Network 1:

,
1, 𝑖𝑓 𝑏𝑜𝑥𝑖 𝑖𝑠 𝑏𝑙𝑎𝑐𝑘
where 𝐼𝑖 = {
0, 𝑖𝑓 𝑏𝑜𝑥𝑖 𝑖𝑠 𝑤ℎ𝑖𝑡𝑒
(a) (5%) Given the input image (Image 1) below,

Image 1
what is the output [𝑂1 , 𝑂2 ] of Neural Network 1?
(b) (2%) Following 10.(a), what is the inferred answer that Neural Network 1
produces?

Generally, the NNs are not perfect and may give ambiguous answers, when
multiple results have similar scores or simply predict wrong answers. For
example, for the following image (Image 2),
Image 2
The output score is [0.72, 0.78], where the interpretation could possibly be that
the image contains 1 AND not 1, only with a slight favor toward “not 1”. In such
a scenario, we want to modify the NN such that it outputs a more accurate and
distinguishable score. One way of doing such a modification is by asserting a
desired score in the output neuron and carefully adjust the edge weights and
produce the desired score. This process of edge weight adjustment is often called
“backward propagation”, since we essentially “input” a score from the output
neuron and backward propagate the calculated adjustments.

(c) (3%) Suppose we input Image 2 into Neural Network 1, and we want the
score of “Yes” to be 0, since the input doesn’t contain a 1. Calculate the
weight of the edge marked “?”. We denote the following NN as Neural
Network 2.

Neural Network 2
(d) (3%) Following the result of 10.(c), what is the output [𝑂1 , 𝑂2 ] of Neural
Network 2 if we reuse Image 1 from 10.(a) as input?

You may spot problems with the approach from questions 10.(c)(d).
Although we optimize the output score for Image 2, the score for Image 1
becomes confused. Thereby, randomly tweaking the NN and hope it will perform
well for a variety of inputs is not realistic. We need a systematic way to do so.
This is the “learning” part of Deep Learning comes in. We will not formally
explore how to train an NN to optimize the output, but you are encouraged to do
the research yourself.
We now try to improve the NN after each inference. One issue is the order
of neurons in the inference computation. For example, for Neural Network 1,
we can’t calculate 𝑂1 unless both 𝑀1 and 𝑀2 are calculated. Thus, we need to
first decide the topological order of all neurons in an NN.

(e) (7%) Please give the topological order of the following NN. Break the ties
by picking the neuron in the layer closest to the output, and the neuron with
the smallest index in the same layer. (Your sequence should be: 𝐼1 → ⋯,
where we consider “feeding inputs to neurons” as one kind of calculation)

Reminder: There are sub-questions (a)~(e) for question 10.

Ans:
(a) [𝐼1 , 𝐼2 , 𝐼3 , 𝐼4 ] = [1,0,1,0]
1
0.7 0.2 0.3 0.5 0 0
[ ]=[ ][ ]
0.3 0 0.2 0.3 0.5 1
0
𝑂1 0.8 0.2 0.7 0.62
[ ]=[ ][ ] = [ ]
𝑂2 0.2 0.8 0.3 0.38
(b) “Yes”
0 0.8 ? 0.7
(c) [ ]=[ ] [ ]. ? = −0.7
𝑂2 0.2 0.8 0.8
(d) [𝐼1 , 𝐼2 , 𝐼3 , 𝐼4 ] = [1,0,1,0]
1
0.7 0.2 0.3 0.5 0 0
[ ]=[ ][ ]
0.3 0 0.2 0.3 0.5 1
0
𝑂 0.8 −0.7 0.7 0.35
[ 1] = [ ][ ] = [ ]
𝑂2 0.2 0.8 0.3 0.38
(e) 𝐼1 → 𝐴1 → 𝐵1 → 𝑂1 → 𝑂2 → 𝐼2 → 𝐼3 → 𝐴2 → 𝐵4 → 𝑂5 → 𝐴3 → 𝐵2 → 𝑂4 →
𝐵3 → 𝑂3

You might also like