algorithm questions solution
Post on 02-Jun-2018
240 Views
Preview:
TRANSCRIPT
-
8/10/2019 Algorithm Questions Solution
1/22
Rayan DankarID:201302670Computer Algorithms Assig.
Page 1 of 22
Question 1
Solve the following recurrence relations (show every step in your solution)
T (1) = 1
T (n) = T (n-1) + 2* n / 5
T (n) = T (n-1) + 2n / 5
T(n) = T(n-2) + 2(n-1) / 5 + 2n / 5= T(n-2) + 4n/52/5
T(n) = T(n-3) + 2(n-2) / 5 + 4n/52/5
= T(n-3) + 6n/56/5
T(n) = T(n-4) + 2(n-3) / 5 + 6n/56/5
= T(n-4) + 8n / 512 / 5
.
.
.
T(n) = T(n-i) + 2*i*n / 52(i1) / 5
T(n) = T(1) + 2(n-1)(n) / 5 2(n-2) / 5T(n) = T(1) + 2n
2/ 54n / 5 + 4 / 5
O( n2)
T(n-1) = T(n-2) + 2(n-1) / 5
T(n-2) = T(n-3) + 2(n-2) / 5
T(n-3) = T(n-4) + 2(n-3) / 5
n-i = 1i = n-1
-
8/10/2019 Algorithm Questions Solution
2/22
Rayan DankarID:201302670Computer Algorithms Assig.
Page 2 of 22
T (1) = 1
T (n) = 3 * T (n / 2) + 2
T(n) = 3T(n / 2) + 2
T(n) = 3[ 3T(n / 4) + 2 ] + 2= 9T(n / 4) + 3*2 + 2
T(n) = 9[ 3T(n / 8) + 2 ] + 3*2 + 2
= 27T(n / 8) + 9*2 + 3*2 + 2
T(n) = 27[ 3T(n / 16 ) +2 ] + 9*2 +3*2 + 2
= 81T(n / 16) + 27*2 + 9*2 +3*2 + 2
= 81T(n / 16) + 2[ 27 + 9 +3 + 1 ].
..
T(n) = 3i
* T(n / 2i) + 2[3
i-1+ 3
i-2+ . 3
0]
T(n) = 3i
* T(n / 2i) + 2[3
i1 ]
T(n) = 3log
2n * T(1) + 2 [3
log2
n1 ]
O(3log
2n)
T(n / 2) = 3T(n / 4) + 2
T(n / 4) = 3T(n / 8) + 2
T(n / 8) = 3T(n / 16 ) +2
n / 2i= 1n = 2
ii = log2
n
-
8/10/2019 Algorithm Questions Solution
3/22
Rayan DankarID:201302670Computer Algorithms Assig.
Page 3 of 22
T (n) = n +
T (1) = 1
T (n) = n + T(i)
T (n-1) = n1 + T(i)
T(n)T(n-1) = 1 + T(n-1) T(n) = 2T(n-1) + 1
T(n) = 2T(n-1) + 1
T(n) = 2[ 2T(n-2) + 1 ] + 1
= 4T(n-2) + 2 +1
T(n) = 4[ 2T(n-3) +1 ] + 2 + 1
= 8T(n-3) + 4 + 2 +1
.
.
.
T(n) = 2i* T(n-i) + 2
i+11
T(n) = 2n-1
* T(1) + 2n1
O( 2n)
T(n-1) = 2T(n-2) + 1
T(n-2) = 2T(n-3) +1
n-i = 1i= n -1
-
8/10/2019 Algorithm Questions Solution
4/22
Rayan DankarID:201302670Computer Algorithms Assig.
Page 4 of 22
Question 2
Convert the following postfi x expression to i nf ix form:
5 9 8 + 4 6 * * 7 + *
Expression Stack
5 9 8 + 4 6 * * 7 + * NULL
9 8 + 4 6 * * 7 + * 5
8 + 4 6 * * 7 + * 95
+ 4 6 * * 7 + * 8
95
4 6 * * 7 + * 9+8
56 * * 7 + * 4
9+8
5
* * 7 + * 6
49+8
5
* 7 + * 4*69+8
5
7 + * (9+8)*4*65
+ * 7
(9+8)*4*65
* (9+8)*4*6+75
NULL 5+((9+8)*4*6+7)
5 * ((9 + 8) * 4 * 6 + 7)
-
8/10/2019 Algorithm Questions Solution
5/22
Rayan DankarID:201302670Computer Algorithms Assig.
Page 5 of 22
Question 3:
Given the following Binary search tree, Show the order of the visited nodes when we traversethis tree using:
Preorder traversal:
rootleftright :
FBADCEGIH
Inorder traversal:
leftrootright :
ABCDEFGHI
Postorder traversal:
leftrightroot :
ACEDBHIGF
-
8/10/2019 Algorithm Questions Solution
6/22
Rayan DankarID:201302670Computer Algorithms Assig.
Page 6 of 22
Question 4
Draw the binary search tree that results from inserting the keys 12, 17, 20, 13, 14, 10, 11, 8 intoan initially empty tree.
Draw the binary tree that result from
deletingthe
node
withkey
valu
e 16
fromthe
follo
wing
tree:
-
8/10/2019 Algorithm Questions Solution
7/22
Rayan DankarID:201302670Computer Algorithms Assig.
Page 7 of 22
Question 5
What is a heap, what are the conditions that should be specified inside a heap, and what is therelation between the indexes of parents and children inside of a heap.
A heap is a tree where the value of the node is less than or equal to that of its descendants (minheap) or vice versa (max heap).
Parent:
Left child: 2*+ 1
Right child: 2*1
Given the following max heap:
Illustrate the
effect of:
a. Inserting a
nodewhos
e key
is Pinto
the
follo
wing
heap
-
8/10/2019 Algorithm Questions Solution
8/22
Rayan DankarID:201302670Computer Algorithms Assig.
Page 8 of 22
b. Removing the largest node from the resulting heap
Question 6
1)Show the detailed workings for the fir st two parti tioning of the foll owing array duri ng
Quicksort.
The answer should show the behavior of vari ables as the parti tioning proceeds.
Quick Sort
-
8/10/2019 Algorithm Questions Solution
9/22
Rayan DankarID:201302670Computer Algorithms Assig.
Page 9 of 22
publi cvoid quicksort(I TEM [] a, intL , int R)
{
if (R left& & a[R] >= p) R--;
if (L < R) { int temp = a[L] ; a[L ] = a[R]; a[R] = temp; }
}
a[left] = a[R];
a[R] = p;
return R; }
0 1 2 3 4 5 6 7
A 5 3 1 9 8 2 4 7
left=
0
right=7 p=5 L=1
L=2L=3
R=7
R=6
Swap a[3] & a[6]
new a 5 3 1 4 8 2 9 7
left=0
right=7 p=5 L=3L=4
R=6R=5
Swap a[4] & a[5]
new a 5 3 1 4 2 8 9 7
-
8/10/2019 Algorithm Questions Solution
10/22
Rayan DankarID:201302670Computer Algorithms Assig.
Page 10 of 22
left=0
right=7 p=5 L=4L=5
R=5R=4
a[0]=a[4]a[4]=5
new a 2 3 1 4 5 8 9 7
I STOPPED BECAUSE THE GIVEN IS WRONG!
PARTITION TAKES ONE PARAMETER HOW COME IT IS
GIVEN 3???
2) When does the best case of Quicksort occurs, when does the worst caseoccurs (explain your answer showing the complexity in each case)
3) Using tree diagrams, show how the above array would be sorted using themax Heapsortalgorithm.
4) What is the complexity of Heapsort? Compare it with that of Quicksort.
Question 7
How many comparisons and swaps are needed to sort 10 numbers in ascendi ng order using?
1. Selection sort:
Select ion Sort
public void selectionSor t(int[ ] arr )
{
int i , j, minI ndex, tmp;
int n = arr .length;for (i = 0; i < n - 1; i++)
{
minI ndex = i;
for (j = i + 1; j < n; j++)
if (arr[j ] < arr [minI ndex])
minI ndex = j;
if (minI ndex != i )
-
8/10/2019 Algorithm Questions Solution
11/22
Rayan DankarID:201302670Computer Algorithms Assig.
Page 11 of 22
{
tmp = arr [i] ;
arr[i ] = arr[minI ndex];
arr[minI ndex] = tmp;
}
}}
a. I f the data is originall y sorted in ascending order
i = 0
minIndex = 0
i = 1
minIndex = 1
i = 2
minIndex = 2
i = 3
minIndex = 3
i = 4
minIndex =4
Comparison 9 9 9 9 9
minIndex = i minIndex = i minIndex = i minIndex = i minIndex = i
Swap 0 0 0 0 0
i = 5
minIndex = 5
i = 6
minIndex = 6
i = 7
minIndex = 7
i = 8
minIndex = 8
Total
Comparison 9 9 9 9 81
minIndex = i minIndex = i minIndex = i minIndex = i
Swap 0 0 0 0 0
b. I f the data is originall y sorted in descending order
i = 0minIndex = 0
j = 9minIndex = 9
i = 1minIndex = 1
j = 9minIndex = 9
i = 2minIndex = 2
j = 9minIndex = 9
i = 3minIndex = 3
j = 9minIndex = 9
i = 4minIndex = 4
j = 9minIndex = 9
Comparison 9 9 9 9 9
minIndex =! i minIndex! = i minIndex != i minIndex! = i minIndex != i
Swap 1 1 1 1 1
i = 5
minIndex = 5
j = 9
minIndex = 9
i = 6
minIndex = 6
j = 9
minIndex = 9
i = 7
minIndex = 7
j = 9
minIndex = 9
i = 8
minIndex = 8
j = 9
minIndex = 9
Total
Comparison 9 9 9 9 81
minIndex! = i minIndex != i minIndex! = i minIndex != iSwap 1 1 1 1 9
2. I nserti on sort:
-
8/10/2019 Algorithm Questions Solution
12/22
Rayan DankarID:201302670Computer Algorithms Assig.
Page 12 of 22
Ins ert ion Sort
void inser tionSort(i nt[ ] arr )
{
in t i, j, newValue;
for (i = 1; i < arr .length; i++){
newValue = arr[ i] ;
j = i;
while (j > 0 && arr[ j - 1] > newValue)
{
arr[ j] = arr[j - 1];
j--;
}
arr[ j] = newValue;
}
}
a. I f the data is originall y sorted in ascending order
i = 1 i = 2 i = 3 i = 4 i = 5
Comparison 9 9 9 9 9
Swap 0 0 0 0 0
i = 6 i = 7 i = 8 i = 9 Total
Comparison 9 9 9 9 81
Swap 0 0 0 0 0
b. If the data is originally sorted in descending order
i = 1 i = 2 i = 3 i = 4 i = 5
Comparison 9 9 9 9 9Swap 1 1 1 1 1
i = 6 i = 7 i = 8 i = 9 Total
Comparison 9 9 9 9 81
Swap 1 1 1 1 9
-
8/10/2019 Algorithm Questions Solution
13/22
Rayan DankarID:201302670Computer Algorithms Assig.
Page 13 of 22
3. Bubble sort:Bubble Sort
public void bubbleSort(int[] arr)
{
int j = 0;
int tmp;
for (int i = 0; i < arr.length-1; i++)
for (int j = arr.length-1; j > i; j--)
if (arr[j-1] > arr[j] )
{
tmp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = tmp;
}
}
a. I f the data is originall y sorted in ascending order
i = 0
j = 9
i = 1
j = 9
i = 2
j = 9
i = 3
j = 9
i = 4
j = 9
Comparison 9 8 7 6 5
arr[j-
1]!>arr[j]
arr[j-
1]!>arr[j]
arr[j-
1]!>arr[j]
arr[j-1]!>arr[j] arr[j-1]!>arr[j]
Swap 0 0 0 0 0
i = 5
j = 9
i = 6
j = 9
i = 7
j = 9
i = 8
j = 9
Total
Comparison 4 3 2 1 45
arr[j-1]!>arr[j] arr[j-1]!>arr[j] arr[j-1]!>arr[j] arr[j-1]!>arr[j]
Swap 0 0 0 0 0
b. I f the data is originall y sorted in descending order
i = 0j = 9
i = 1j = 9
i = 2j = 9
i = 3j = 9
i = 4j = 9
Comparison 9 8 7 6 5
arr[j-1] >arr[j] arr[j-1] >arr[j] arr[j-1] >arr[j] arr[j-1] >arr[j] arr[j-1] >arr[j]
Swap 1 1 1 1 1
-
8/10/2019 Algorithm Questions Solution
14/22
Rayan DankarID:201302670Computer Algorithms Assig.
Page 14 of 22
i = 5
j = 9
i = 6
j = 9
i = 7
j = 9
i = 8
j = 9
Total
Comparison 4 3 2 1 45
arr[j-1] >arr[j] arr[j-1] >arr[j] arr[j-1] >arr[j] arr[j-1] >arr[j]
Swap 1 1 1 1 9
-
8/10/2019 Algorithm Questions Solution
15/22
Rayan DankarID:201302670Computer Algorithms Assig.
Page 15 of 22
Question 8
The following functions are given:
(en)
240 * n
2n 14logn
427 * n
2+ 3 * n + 1 14(logn)
4100nlogn
a) Ar range these functions according to O notation.
(en)2
>
> 40 * n2n > 27 * n
2+ 3 * n + 1 > 100nlogn > 14(logn)
4>
14logn
4
b) Assume these functions are runtimes for 7 algori thms that all solve the same problem. For
which values of n (nearest in teger) whi ch algor ithm has the shor test runtime?
(e
n
)
2
: for n=0
40 * n2n : for n=0
14logn4: for n=1
27 * n2+ 3 * n + 1 : for n=0
14(logn)4
: for n=1
100nlogn : for n=1
: for n=0
c) Assume you have an algor ithm whose runtime is T(n) = n2log(n). For what ranges of n
(order of magni tude estimate) thi s algor ithm wil l r un in a second, an hour and a week on a
200MHz Processor, assuming one instructi on is executed in one clock cycle.
in one second: n2log(n) = 200 * 10
6
in one hour: n2log(n) = 200 * 10
6* 3600
in one hour: n2log(n) = 200 * 10
6* 3600 * 24 * 7
-
8/10/2019 Algorithm Questions Solution
16/22
Rayan DankarID:201302670Computer Algorithms Assig.
Page 16 of 22
Question 9
A divide-and-conquer algorithm for multi plying two N x N matrices reduces the calculation to
7 products of
matr ices and 18 matr ix additi ons of N x N matri ces.
A matr ix addition can be implemented by a double for -loop li ke this:
for i = 1 to N
for j =1 to N
a(i,j) = b(i,j) + c(i,j)
Give a recur rence soluti on for the runtime T (N) required to multi ply two N x N matri ces and
using Masters theorem, give an O-estimate for T (N).
T ( n ) = 7T (
) + 18n
2
Using Masters theorem:
a= 7 b=2 c=2
log27 > 2
( n log27)
-
8/10/2019 Algorithm Questions Solution
17/22
-
8/10/2019 Algorithm Questions Solution
18/22
Rayan DankarID:201302670Computer Algorithms Assig.
Page 18 of 22
b) Suppose now that the set S is given in sorted order. Design an algor i thm to
solve the pro blem in t im e O (n) .
i = 1;
j = n;
for( i = 1 j )
{ O ( n )
for ( j =n1 i )
{
if (S[i] + S[j] < x)
i++;
else if (S[i] + S[j] >x)
j;
else
break;
}
}
O ( n )
-
8/10/2019 Algorithm Questions Solution
19/22
-
8/10/2019 Algorithm Questions Solution
20/22
Rayan DankarID:201302670Computer Algorithms Assig.
Page 20 of 22
Consider the weighted undirected graph below.
1. In what order would the vertices be traversed, using breadth-first search starting atvertex E, assuming that adjacent vertices are visited in alphabetical order?
ECBADFG
2. In what order would the vertices be traversed, using depth-first search starting at vertexB, assuming that adjacent vertices are visited in alphabetical order?
BADCEGF
3. Illustrate the execution of Kruskals algorithm applied to the graph. Clearly show theresult after each step of the algorithm by listing the edges added in the order in which
they are added.
-
8/10/2019 Algorithm Questions Solution
21/22
Rayan DankarID:201302670Computer Algorithms Assig.
Page 21 of 22
2 + 3 + 7 + 7 + 9 + 12 +12
= 52
4.Illustrate the execution of Prims algorithmapplied to the graph, starting at vertex C. Clearly show the result after each step of the
algorithm by listing the edges added in the order in which they are added.
-
8/10/2019 Algorithm Questions Solution
22/22
Rayan DankarID:201302670Computer Algorithms Assig.
Page 22 of 22
3 + 7 + 9 + 12 + 2 + 12 + 7
= 52
5.Illustrate the execution of Dijkstras algorithm applied to the graph, starting at vertex
C. Clearly show the resul t after each step of the algori thm by li sting the edges added
in the order in which they are added.
top related