final exam coen 352 partial solutions. a c e m a c e m fg h i
TRANSCRIPT
Final Exam COEN 352
Partial solutions
A
C E
M
A
C E
MF G H
I
15 2115 24 49 46 3052 1911 4723 24 29A
Consider the following array and apply:a) buble sortb) Quicksortc) Merge sort
What is the complexity of each sort?In you answer, explain each of the above sort algorithm and provide all the steps of it application.
30
15
10
50
7040
35 8042
v
a) What is an AVL tree? b) Is the tree given in figure xx verifies the proprieties of an AVL tree?c) Delete the item 10, provide the deletion algorithm and apply it step by stepd) Insert in the original tree the item 75, provide the insertion algorithm and apply it step by stepe) What is the complexity of the main operations of an AVL tree ADT?
1 2 3 4 5 6 7
T0 a T1 b T2 c T3
30
15 50
7040
35 8042
v
b
a
c
T0
T1
T2
T3
1
2
3
4
5
6
7
Unbalanced TreeCut/Link Restructure Algorithm
30
15 50
7040
35 8042
v
b
a
c
T0
T1
T2T3
1
2
3
4
5
6
7
1 2 3 4 5 6 7
T0 a T1 b T2 c T3
50
30
15 40
35 42
70
80
Cut/Link Restructure Algorithm
Balanced TreeAVL Tree
30
15
10
50
7040
35 8042
v75
30
15
10
50
7540
35 8042
v
70
• Define a {binary tree, complete binary tree, full binary tree}. • What is the maximal number of nodes on a given level of a binary tree? • What is the maximal number of nodes in a binary tree of depth D?
A B C D
E F G H
I J K L
M N
•Give the algorithm for a {DFS,BFS} of a graph. •List the nodes visited in the order of a {DFS,BFS} of this graph.
A B C D
E F G H
I J K L
M N
A B C D
E F G H
I J K L
M N
Starting from A ABEFC, IDG, JMNH, KL
Starting from AA,B,C,D,H,L,G,J,K,N,M,I,F,E** Solution is not unique**
BFS
DFS
A B C
F
ED
G
1522
303
712
9
12
127
2
13
A B C
F
ED
G
1522
303
712
9
12
127
2
13
3
10
9
16 15
17
(C,E)-3(C,D)-9(C,B)-15(C,G)-30(E,G)-7 (C,E,G)-10 (3+7)(E,D)-12 (C,E,D)-15(B,F)-2 (C,B,F)-17 (15 +2)(B,A)-22 (C,B,A)-37(D,A)-7 (C,D,A)-16 (9+7)(G,F)-12 (C,G,F)-22(A,F)-12 (C,D,A,F)-28
Shortest path
15 15 23 24 11 47 49 52 29 46 30
15 15 23 11 24 47 49 52 29 46 30
15 15 23 11 24 47 49 29 52 46 30
15 15 23 11 24 47 49 29 46 52 30
15 15 23 11 24 47 49 29 46 30 52
15 15 23 11 24 47 29 49 46 30 52
15 15 11 23 24 47 29 49 46 30 52
15 15 11 23 24 29 47 49 46 30 52
15 15 11 23 24 29 47 46 49 30 52
15 15 11 23 24 29 47 46 30 49 52
1st pass
2d pass
Bubble sort algorithm ( solution)
15 15 11 23 24 29 47 46 30 49 52
15 11 15 23 24 29 47 46 30 49 52
15 11 15 23 24 29 46 47 30 49 52
15 11 15 23 24 29 46 30 47 49 52
15 11 15 23 24 29 46 30 47 49 52
11 15 15 23 24 29 46 30 47 49 52
11 15 15 23 24 29 30 46 47 49 52
11 15 15 23 24 29 30 46 47 49 52
11 15 15 23 24 29 30 46 47 49 52
3d pass
4th pass
5th up to the last passNo swap
O(n2)
15 15 23 24 11 47 49 52 29 46 30
Quick Sort Algorithm (solution)
15 15 23 24 11 29 47 49 52 4630
L E G
15 15 23 24 11 29 47 49 5246
15 15 23 2411 47 49 52
15 15 23 244947
15 15 23
15 15Partition / Pivot Selection
The solution is not unique, it depends on the pivot selection
11 15 15 23 24 29 30 46 47 49 52
11 15 15 23 24 29 30
29 47 49 5246
15 15 23 241147 49 52
15 15 23 244947
15 15 23
15 15
Join/ Combine
11 15 15 23 24
47 49 5246
47
15 O(n log n)
15 15 23 24 11 47 49 52 29 46 30
15 15 23 24 11 47 49 52 29 46 30
15 15 23 24 11 47 49 52 29 46 30
15 15 23 24 11 47 49 52 29 46 30
15 15 23 24 11 47 49 52 29
15 15 11 2449 52
30 46
15 15 23 11 24 4729 49 52
11 15 15 23 24 47 29 30 46 49 52
11 15 15 23 24 29 30 46 47 49 52
Merge
Partition
O(n log n)