Vous êtes sur la page 1sur 10

TUTORIAL SESSION 6 - SOLUTION

GRAPH and SORTING

Question 1.

Figure 1.

Given a directed graph as illustrated in Figure 1, use the Dijkstra algorithm to find shortest paths
from d to all vertices of the graph. Let d(x) be the shortest distance currently found from d to vertex
x. Give the values d(x) for each iteration when applying the algorithms by filling the following
Table 1.

considered d(a) d(b) d(c) d(d) d(e) d(f) d(g) d(h) d(i)
vertex
d  3 7 0 5 5   2
i  3 3 0 5 5 4 6 2
b  3 3 0 5 5 4 6 2
c  3 3 0 5 5 4 6 2
g  3 3 0 5 5 4 6 2
e  3 3 0 5 5 4 6 2
f 7 3 3 0 5 5 4 6 2
h 7 3 3 0 5 5 4 6 2
a 7 3 3 0 5 5 4 6 2

Question 2. Use the Prim’s algorithm to find a spanning minimum tree for the graph given in
Figure 2.
Figure 2

Question 3. In which case, the following sorting algorithm is fastest/slowest and what is the
complexity in that case? Explain.

Assume that the insertion sort, selection sort and bubble sort algorithms are performed on
contiguous lists (i.e. array), and the merge sort is performed on a linked list.

Fastest Slowest
Insertion sort
The input is an ordered list. At every step, the The input is a reverse-ordered list. At
element at current is only compared to the every step, the element at current is
element at (current-1). compared to every elements preceded it.

Comparisons: C=n-1=O(n) Comparisons: C=n(n-1)/2=O(n2)


Moves: M=0 or M=2(n-1)=O(n) Moves: M=(3+4+5+...(n-1+2)) = (n-
1)(n+4)/2 = O(n2)
(If we change the algorithm so that it does not
copy out the data in line 1.2.1 and copy in the data (At each i, we have to copy the data out to
in line 1.2.4 in the lecture slide every time, in this the variable tmp, and move (i-1) elements
fastest case, the number of moves is 0. Otherwise, forward, and copy the tmp to the first
we need 2 moves at each step. Then the number position.)
of moves is 2(n-1).)
Selection sort
This algorithm performs the same in all cases.

C=n(n-1)/2=O(n2)
M=3(n-1)=O(n)

At each iteration, we have to compare all the remained elements to select the next smallest
elements, follows by an exchange (3 moves) of the current position and the smallest position.

For an ordered list input, if we change the algorithm so that it does not exchange the elements if
the smallest position and the current position are the same, the number of move is M=0.

Bubble sort
The input is an ordered list. The input is an reverse-ordered list.

The algorithm stops after the first iteration. C=n(n-1)/2=O(n2)


M=n(n-1)/2 = O(n2).
C=n-1=O(n)
M=0 At each step, we have to compare and
move the next smallest element up to the
beginning.

Merge sort
This algorithm performs the same in all cases.

C=O(nlog2 n)
M=0

We only have to change pointers, not move elements.

Question 4.

For a given “nearly” sorted list, which sorting algorithm (insertion, selection, merge sort) should be
used? Why?

According to question 3, the “insertion sort” is the best. The number of comparisons is around O(n)
and the number of moves is around O(1) (when we prevent the 2 copy actions in line 1.2.1 and 1.2.4
in the algorithm in the lecture slide).

Note that, in this case, the numbers of comparisons for selection sort and merge sort are O(n 2) and
O(nlog2n), respectively. The numbers of moves are O(n) and O(1).

Further note that, if the merge sort is for an array, the number of moves should be O(nlog 2 n). That is
because we have to copy data into another array in partitioning and copy it back to the original array
in merging. Using the same idea of calculation the number of comparisons, it is easy to show that
the number of moves in this case is O(nlog2 n).

Question 5.

Given a list = {13, 27, 8, 3, 21, 17, 28, 32, 91, 72, 23, 35}, show the sorting process step-by-step of
the following algorithm. What are the number of comparisons and number of moving elements (an
exchange of 2 elements is considered as 3 moves). Which is the best algorithm in this case?

a. insertion sort

13 27 8 3 21 17 28 32 91 72 23 35 C=0, M=0

13 27 8 3 21 17 28 32 91 72 23 35 C=1, M=0
8 13 27 3 21 17 28 32 91 72 23 35 C=2, M=4

3 8 13 27 21 17 28 32 91 72 23 35 C=3, M=5

3 8 13 21 27 17 28 32 91 72 23 35 C=2, M=3

3 8 13 17 21 27 28 32 91 72 23 35 C=3, M=4

3 8 13 17 21 27 28 32 91 72 23 35 C=1, M=0

3 8 13 17 21 27 28 32 91 72 23 35 C=1, M=0

3 8 13 17 21 27 28 32 91 72 23 35 C=1, M=0

3 8 13 17 21 27 28 32 72 91 23 35 C=2, M=3

3 8 13 17 21 23 27 28 32 72 91 35 C=6, M=7

3 8 13 17 21 23 27 28 32 35 72 91 C=3, M=4

Number of comparisons: 25
Number of moving: 30

b. selection sort

3 27 8 13 21 17 28 32 91 72 23 35 C=11, M=3

3 8 27 13 21 17 28 32 91 72 23 35 C=10, M=3

3 8 13 27 21 17 28 32 91 72 23 35 C=9, M=3

3 8 13 17 21 27 28 32 91 72 23 35 C=8, M=3

3 8 13 17 21 27 28 32 91 72 23 35 C=7, M=0

3 8 13 17 21 23 28 32 91 72 27 35 C=6, M=3

3 8 13 17 21 23 27 32 91 72 28 35 C=5, M=3

3 8 13 17 21 23 27 28 91 72 32 35 C=4, M=3

3 8 13 17 21 23 27 28 32 72 91 35 C=3, M=3

3 8 13 17 21 23 27 28 32 35 91 72 C=2, M=3

3 8 13 17 21 23 27 28 32 35 72 91 C=1, M=3

Number of comparisons: 66
Number of moving: 30

c. heap sort
13 27 8 3 21 35 28 32 91 72 23 17 C=1, M=3

13 27 8 3 72 35 28 32 91 21 23 17 C=2, M=3

13 27 8 91 72 35 28 32 3 21 23 17 C=2, M=3

13 27 35 91 72 17 28 32 3 21 23 8 C=3, M=6

13 91 35 32 72 17 28 27 3 21 23 8 C=4, M=6

91 72 35 32 23 17 28 27 3 21 13 8 C=6, M=9

72 32 35 27 23 17 28 8 3 21 13 91 C=6, M=12

35 32 28 27 23 17 13 8 3 21 72 91 C=4, M=9

32 27 28 21 23 17 13 8 3 35 72 91 C=6, M=9

28 27 17 21 23 3 13 8 32 35 72 91 C=4, M=9

27 23 17 21 8 3 13 28 32 35 72 91 C=4, M=9

23 21 17 13 8 3 27 28 32 35 72 91 C=4, M=9

21 13 17 3 8 23 27 28 32 35 72 91 C=4, M=9

17 13 8 3 21 23 27 28 32 35 72 91 C=2, M=6

13 3 8 17 21 23 27 28 32 35 72 91 C=2, M=6

8 3 13 17 21 23 27 28 32 35 72 91 C=1, M=3

3 8 13 17 21 23 27 28 32 35 72 91 C=0, M=3

Number of comparisons: 57
Number of moving: 84

d. bubble sort

3 13 27 8 17 21 23 28 32 91 72 35 C=11, M=24

3 8 13 27 17 21 23 28 32 35 91 72 C=10, M=12

3 8 13 17 27 21 23 28 32 35 72 91 C=9, M=6

3 8 13 17 21 27 23 28 32 35 72 91 C=8, M=3

3 8 13 17 21 23 27 28 32 35 72 91 C=7, M=3

3 8 13 17 21 23 27 28 32 35 72 91 C=6, M=0
...

Number of comparisons: 66
Number of moving: 48

e. merge sort

13, 27, 8, 3, 21, 17 28, 32, 91, 72, 23, 35


13,27,8 3,21,17 28,32,91 72,23,35
13,27 8 3,21 17 28,32 91 72,23 35
13 27 8 3 21 17 28 32 91 72 23 35
13,37 8 3,21 17 28,32 91 23,72 35
8,13,37 3,17,21 28,32,91 23,35,72
3,8,13,17,21,37 23,28,32,35,72,91
3,8,13,17,21,23,28,32,35,37,72,91

f. quick sort1

17 27 8 3 21 13 28 32 91 72 23 35 C=0, M=3

17 8 27 3 21 13 28 32 91 72 23 35 C=1, M=3

17 8 3 27 21 13 28 32 91 72 23 35 C=1, M=3

17 8 3 13 21 27 28 32 91 72 23 35 C=2, M=3

17 8 3 13 21 27 28 32 91 72 23 35 C=6, M=0

13 8 3 17 21 27 28 32 91 72 23 35 C=0, M=3

8 13 3 17 21 27 28 32 91 72 23 35 C=0, M=3

8 3 13 17 21 27 28 32 91 72 23 35 C=2, M=3

3 8 13 17 21 27 28 32 91 72 23 35 C=0, M=3

3 8 13 17 32 27 28 21 91 72 23 35 C=0, M=3

3 8 13 17 32 27 28 21 23 72 91 35 C=6, M=3

3 8 13 17 32 27 28 21 23 72 91 35 C=1, M=0

3 8 13 17 23 27 28 21 32 72 91 35 C=0, M=3

3 8 13 17 27 23 28 21 32 72 91 35 C=0, M=3

3 8 13 17 27 23 21 28 32 72 91 35 C=3, M=3

1
The pivot is chosen as described in the slides
3 8 13 17 21 23 27 28 32 72 91 35 C=0, M=3

3 8 13 17 21 23 27 28 32 72 91 35 C=1, M=0

3 8 13 17 21 23 27 28 32 91 72 35 C=0, M=3

3 8 13 17 21 23 27 28 32 91 72 35 C=2, M=0

3 8 13 17 21 23 27 28 32 35 72 91 C=0, M=3

3 8 13 17 21 23 27 28 32 35 72 91 C=1, M=0

Number of comparisons: 26
Number of moving: 48

Question 6.

Repeat question 5 for the following list: {13, 3, 8, 21, 27, 23, 17, 28, 32, 35, 91, 72}.

a. insertion sort

13 3 8 21 27 23 17 28 32 35 91 72 C=0, M=0

3 13 8 21 27 23 17 28 32 35 91 72 C=1, M=3

3 8 13 21 27 23 17 28 32 35 91 72 C=2, M=3

3 8 13 21 27 23 17 28 32 35 91 72 C=1, M=0

3 8 13 21 27 23 17 28 32 35 91 72 C=1, M=0

3 8 13 21 23 27 17 28 32 35 91 72 C=2, M=3

3 8 13 17 21 23 27 28 32 35 91 72 C=4, M=5

3 8 13 17 21 23 27 28 32 35 91 72 C=1, M=0

3 8 13 17 21 23 27 28 32 35 91 72 C=1, M=0

3 8 13 17 21 23 27 28 32 35 91 35 C=1, M=0

3 8 13 17 21 23 27 28 32 35 91 72 C=1, M=0

3 8 13 17 21 23 27 28 32 35 72 91 C=2, M=3
Number of comparisons: 17
Number of moving: 17

b. selection sort

3 13 8 21 27 23 17 28 32 35 91 72 C=11, M=3

3 8 13 21 27 23 17 28 32 35 91 72 C=10, M=3

3 8 13 21 27 23 17 28 32 35 91 72 C=9, M=0

3 8 13 17 27 23 21 28 32 35 91 72 C=8, M=3

3 8 13 17 21 23 27 28 32 35 91 72 C=7, M=3

3 8 13 17 21 23 27 28 32 35 91 72 C=6, M=0

3 8 13 17 21 23 27 28 32 35 91 72 C=5, M=0

3 8 13 17 21 23 27 28 32 35 91 72 C=4, M=0

3 8 13 17 21 23 27 28 32 35 91 72 C=3, M=0

3 8 13 17 21 23 27 28 32 35 91 72 C=2, M=0

3 8 13 17 21 23 27 28 32 35 72 91 C=1, M=3

Number of comparisons: 66
Number of moving: 15

c. heap sort

13 3 8 21 27 72 17 28 32 35 91 23 C=1, M=3

13 3 8 21 91 72 17 28 32 35 27 23 C=2, M=3

13 3 8 32 91 72 17 28 21 35 27 23 C=2, M=3

13 3 72 32 91 23 17 28 21 35 27 8 C=3, M=6

13 91 72 32 35 23 17 28 21 3 27 8 C=4, M=6

91 35 72 32 27 23 17 28 21 3 13 8 C=6, M=9

72 35 23 32 27 8 17 28 21 3 13 91 C=4, M=9

35 32 23 28 27 8 17 13 21 3 72 91 C=6, M=12

32 28 23 21 27 8 17 13 3 35 72 91 C=6, M=12
28 27 23 21 3 8 17 13 32 35 72 91 C=4, M=9

27 21 23 13 3 8 17 28 32 35 72 91 C=4, M=9

23 21 17 13 3 8 27 28 32 35 72 91 C=3, M=6

21 13 17 8 3 23 27 28 32 35 72 91 C=4, M=9

17 13 3 8 21 23 27 28 32 35 72 91 C=2, M=6

13 8 3 17 21 23 27 28 32 35 72 91 C=2, M=6

8 3 13 17 21 23 27 28 32 35 72 91 C=1, M=3

3 8 13 17 21 23 27 28 32 35 72 91 C=0, M=3

Number of comparisons: 54
Number of moving: 114

d. bubble sort

3 13 8 21 23 27 17 28 32 35 72 91 C=11, M=9

3 8 13 17 21 23 27 28 32 35 72 91 C=10, M=12

3 8 13 17 21 23 27 28 32 35 72 91 C=9, M=0

….

Number of comparisons: 66
Number of moving: 21

e. merge sort

13, 3, 8, 21, 27, 23 17, 28, 32, 35, 91, 72


13,3,8 21,27,23 17,28,32 35,91,72
13,3 8 21,27 23 17,28 32 35,91 72
13 3 8 21 27 23 17 28 32 35 91 72
3,13 8 21,27 23 17,28 32 35,91 72
8,13,37 3,17,21 28,32,91 23,35,72
3,8,13,17,21,37 23,28,32,35,72,91
3,8,13,17,21,23,28,32,35,37,72,91

f. quick sort2
23 3 8 21 27 13 17 28 32 35 91 72 C=0, M=3

23 3 8 21 13 27 17 28 32 35 91 72 C=5, M=3

2
The pivot is chosen as described in the slides
23 3 8 21 13 17 27 28 32 35 91 72 C=1, M=3

23 3 8 21 13 17 27 28 32 35 91 72 C=5, M=0

17 3 8 21 13 23 27 28 32 35 91 72 C=0, M=3

8 3 17 21 13 23 27 28 32 35 91 72 C=0, M=3

8 3 17 21 13 23 27 28 32 35 91 72 C=4, M=0

3 8 17 21 13 23 27 28 32 35 91 72 C=0, M=3

3 8 21 17 13 23 27 28 32 35 91 72 C=0, M=3

3 8 21 17 13 23 27 28 32 35 91 72 C=2, M=0

3 8 13 17 21 23 27 28 32 35 91 72 C=1, M=0

3 8 13 17 21 23 32 28 27 35 91 72 C=0, M=3

3 8 13 17 21 23 32 28 27 35 91 72 C=5, M=0

3 8 13 17 21 23 27 28 32 35 91 72 C=0, M=3

3 8 13 17 21 23 27 28 32 35 91 72 C=0, M=3

3 8 13 17 21 23 27 28 32 35 91 72 C=1, M=0

3 8 13 17 21 23 27 28 32 91 35 72 C=0, M=3

3 8 13 17 21 23 27 28 32 91 35 72 C=2, M=0

3 8 13 17 21 23 27 28 32 72 35 91 C=0, M=3

3 8 13 17 21 23 27 28 32 72 35 91 C=1, M=0

3 8 13 17 21 23 27 28 32 35 72 91 C=0, M=3

Number of comparisons: 27
Number of moving: 39

Vous aimerez peut-être aussi