Total of this assignment is 198pts, but 100% =... assignment is worth 7%. Some solutions below are just sketches.

Transcription

Total of this assignment is 198pts, but 100% =... assignment is worth 7%. Some solutions below are just sketches.
CS/SE 2C03. Sample solutions to the assignment 2.
Total of this assignment is 198pts, but 100% = 181pts. There are bonus 17 points. Each
assignment is worth 7%. Some solutions below are just sketches.
If you think your solution has been marked wrongly, write a short memo stating
where marking in wrong and what you think is right, and resubmit to me during
class, office hours, or just slip under the door to my office. The deadline for a
complaint is 2 weeks after the assignment is marked and returned.
1.[10] Suppose you have the double-link list L = 9,16, 4, 1 and its array implementation
(rows: next, key, prev) from page 18 of Lecture Notes 3 (#72 on the top right of
page). Consider the second list L1 = 5, 4, 2, 3, 8.
a.[7]
Put the list L1 in the same array as L. Assume that the first element on the
list of free location is the column 1 of the array (see pages 20, 21, 23 of
Lecture Notes 3), and place 5 in it. Next place 4,2,3,8 in this order.
b.[3]
Delete the first element of L and add it to L1.
Give all steps.
Solutions:
List L and stack of free memory free (red):
L = 7 (location of the first element of the list ‘L’)
free = 1 (location of the first element of the stack ‘free’)
1
2
3
4
5
6
7
8
9
10
11
12
13
4
3
/
6
2
8
5
9
10
11
12
13
/
key
4
1
16
9
prev
5
2
7
1
next
Lists L and L1 (green), and stack of free memory free (red):
L=7
L1 = 1
free = 10
1
1
2
3
4
5
6
7
8
9
10
11
12
13
next
4
3
/
6
2
8
5
9
/
11
12
13
/
key
5
4
1
4
16
2
9
3
8
prev
/
5
2
1
7
4
1
6
8
After deleting the first element of L
L=5
L1 = 1
free = 7
1
2
3
4
5
6
7
8
9
10
11
12
13
next
4
3
/
6
2
8
10
9
/
11
12
13
/
key
5
4
1
4
16
2
3
8
prev
/
5
2
1
7
4
6
8
and adding it to L1:
L=5
L1 = 1
free = 10
1
2
3
4
5
6
7
8
9
10
11
12
13
next
4
3
/
6
2
8
/
9
7
11
12
13
/
key
5
4
1
4
16
2
9
3
8
prev
/
5
2
1
7
4
9
6
8
2
2[20]. a.[15] Sort the following numbers using the heap sort:
10, 21, 13, 30, 16, 5, 10, 25, 3, 20, 8, 40, 0, 7, 30, 16
Illustrate all the steps. This is easy however labour consuming exercise, but it will
give a good idea how INSERT(k) and DELETEMIN work for the heap.
b.[5]
Repeat (a) but with non-recursive Merge Sort.
Solution to (a) [15]
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Solution to (b) [5].
22
3.[10] Sort the below graph topologically. Illustrate all major steps.
Sketch of a sample solution:
4.[10] List all the different binary trees with 5 nodes. What is their average height?
Trees of height 4: 16
Trees of height 3: 20
Trees of height 2: 6
Average = (4*16+3*20+2*6)/42 = 3.24
23
Consider the weighted directed graph below. It will be used in questions 5, 6, 7, 9.
4
3
h
5
2
a
i
3
1
b
j
2
7
2
4
3
5
f
4
1
5
3
3
l
c
5
e
3
2
d
1
k
4
5.[10] Represent the above graph
a.[5] by an adjacency matrix giving arc costs. Assume rows and columns are
ordered alphabetically.
b.[5] by a linked adjacency list with arc costs indicated. Assume that the list
(vector) of vertices and the vertices on all adjacency lists are ordered
alphabetically.
a.[5]
a
a
b
c
d 3
e
f 4
h
i
j
k
l
b c D e f h i
2
5
5
3
2
3
2
4
3
1
j k l
4
5
1
7
3
2
1
4
3
5
empty = 
24
b.[5]
a
b
2
h
5
j
b
d
5
f
3
c
b
2
d
a
3
c
3
e
b
4
f
3

f
a
4
d
1

h
l
7

i
b
3

j
c
2
i
1

k
e
4
j
5

l
e
3
4
l

5



25
e
2
k
1

6.[20] Consider the above graph but without costs. Assume the same order of vertices on
adjacency lists as in 5b.
a.[10] Construct a depth-first forest;
b.[10] Construct a breadth-first forest
26
7.[10] Consider the above graph but without costs. Assume the same order of vertices on
adjacency lists as in 5b. Find all strongly connected components.
8.[10] Exercise 5 from page 108 of Kleinberg and Tardos.
Solution.
By the induction on the number of leaves. For one leaf the number of with two leaves
equals zero, and for two leaves this number of nodes with two children is 2. So the basic
step is done. Suppose it is true for n leaves. Where can we attach a new node in such a
way that the new tree has n+1 leaves? Definitely not to a node which is a leaf in the
initial n-leaves tree. If we do this, the former leaf will have a child so it will no longer be
a leaf so the new tree will have one node more but the number of leaves remains the same,
i.e. n. We cannot attach it to a node with two children (as our three must be binary).
Hence, we can only attach it to a node which has one child. Hence adding one leaf more
increases the number of nodes with 2 children also by one. So we are done.
Induction by the total number of nodes also works.
27
9.[20] Use the Dijkstra’s algorithm to find the shortest paths from a to the other vertices.
Also construct the P array (recovering the paths) step by step. Recover all paths.
Assume the list of adjacent vertices implementation and with alphabetic ordering
of each list. Use heap to implement the set V– S. Show all steps.
28
10.[20] In the graph above replace arrows by lines, i.e. translate it into a graph which is
not directed. Assume ordering as in 5.b.
a.[8] Find a minimum-cost spanning tree by Prim’s algorithm.
b.[10] Find a minimum-cost spanning tree by Kruskal’s algorithm.
c.[1] Forget about weights and find a depth-first spanning tree starting at a.
d.[1] Forget about weights and find a breadth-first spanning tree starting at a.
Give all steps.
A bonus of [7] for showing heap representation of V-S in the solution to (a).
A bonus of [10] for showing heap representation of union-find representation of
connected components in the solution to (b).
(c) is just 6(a) and (d) is just 6(b). Points are for observing this fact.
29
Solution to (a) (Prim’s):
30
Solution to (b) (Kruskal’s):
31
32
33
11.[20]
Exercise 6 from page 108 of Kleinberg and Tardos.
Note that if a graph G is a tree, i.e. connected and acyclic graph, then TBFS = TDFS=G,
where TBFS is Breadth-First Search tree of G and TDFS is Depth-First Search tree of G,
and this is true for any starting point and any particular run of each algorithm.
Hence if suffices to prove that if G is not a tree, i.e. if it contains a cycle, then there exist
TBFS and TDFS that are not identical. Because of the ‘there exist’ statement, we have a
choice of both a starting point and a particular run of the algorithm. Let a1, a2, …, ak,
where ak=a1 is the shortest cycle of G. Such cycle may not be unique but always exists if
G is not acyclic. Since this is a shortest cycle, if (ai,aj) is an edge of G then j=i+1 or i=k
and j=1. Assume that a starting vertex u=a1. Then edges (a1,a2) and (a1,ak-1) (since a1=ak)
belong to any TBFS that starts from u=a1. One possible TDFS is the one that initially add
edges (a1,a2), (a2,a3), … , (ak-2,ak-1) to itself. But since no tree contains a cycle, the edge
(a1,ak-1) does not belong to it. Hence this TDFS differs from any TBFS that starts from u=a1.
12.[5]
Exercise 1 from page 188 of Kleinberg and Tardos.
True. Kruskal’s algorithm picks c* in its first step.
13.[16]
a.[8]
Exercise 2 from page 188 of Kleinberg and Tardos.
Yes. If a<b then a2 < b2. Hence the total order of edges for the first case is exactly
the same as for the second. This means that the version Kruskal’s algorithm that
34
starts with sorting of edges, behaves identically for both cases, i.e. the same edges
are taken.
b.[8]
No. We know that (a+b)2 = a2 + 2ab +b2 > a2 + b2 . Hence it may happen that
a+b>c, but a2 + b2 < c2. For example 2+3=5>4, but 22+32=13< 16 = 42.
Consider a weighted directed graph G=(V,E) where V={a,b,c},
E={(a,2,b),(b,3,c),(a,4,c)}. Then the shortest path from a to c is just (a,c) and the
distance is 4 (the distance (a,b),(b,c) is 5). Consider graph G(2)=(V,E) where
V={a,b,c}, E={(a,4,b),(b,9,c),(a,16,c)}. Now the shortest path form a to c is
(a,b),(b,c) and the distance is 13 (the distance (a,c) is 16).
35