Sunteți pe pagina 1din 22

Rayan DankarID:201302670Computer Algorithms Assig.

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-1) = T(n-2) + 2(n-1) / 5

T(n) = T(n-2) + 2(n-1) / 5 + 2n / 5


= T(n-2) + 4n/5 2/5

T(n-2) = T(n-3) + 2(n-2) / 5

T(n) = T(n-3) + 2(n-2) / 5 + 4n/5 2/5


= T(n-3) + 6n/5 6/5

T(n-3) = T(n-4) + 2(n-3) / 5

T(n) = T(n-4) + 2(n-3) / 5 + 6n/5 6/5


= T(n-4) + 8n / 5 12 / 5
.
.
.

n-i = 1 i = n-1

T(n) = T(n-i) + 2*i*n / 5 2(i 1) / 5


T(n) = T(1) + 2(n-1)(n) / 5 2(n-2) / 5
T(n) = T(1) + 2n2 / 5 4n / 5 + 4 / 5
O( n2 )

Page 1 of 22

Rayan DankarID:201302670Computer Algorithms Assig.

T (1) = 1
T (n) = 3 * T (n / 2) + 2
T(n) = 3T(n / 2) + 2

T(n / 2) = 3T(n / 4) + 2

T(n) = 3[ 3T(n / 4) + 2 ] + 2
= 9T(n / 4) + 3*2 + 2

T(n / 4) = 3T(n / 8) + 2

T(n) = 9[ 3T(n / 8) + 2 ] + 3*2 + 2


= 27T(n / 8) + 9*2 + 3*2 + 2

T(n / 8) = 3T(n / 16 ) +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 ]
.
.
.
i

i-1

i-2

n / 2i = 1 n = 2i i = log2
n

T(n) = 3 * T(n / 2 ) + 2[3 + 3 + . 3 ]


T(n) = 3i * T(n / 2i ) + 2[3i 1 ]
T(n) = 3log2 n * T(1) + 2 [3log2 n 1 ]
O(3log2 n)

Page 2 of 22

Rayan DankarID:201302670Computer Algorithms Assig.

T (n) = n +
T (1) = 1
T (n) = n +
T (n-1) = n 1 +

T(i)
T(i)

T(n) T(n-1) = 1 + T(n-1)


T(n) = 2T(n-1) + 1
T(n-1) = 2T(n-2) + 1
T(n) = 2T(n-1) + 1
T(n-2) = 2T(n-3) +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
.
.
.

n-i = 1 i= n -1

T(n) = 2i * T(n-i) + 2i+1 1


T(n) = 2n-1 * T(1) + 2n 1
O( 2n )

Page 3 of 22

Rayan DankarID:201302670Computer Algorithms Assig.

Question 2
Convert the following postfix expression to infix form:
598+46**7+*
Expression
598+46**7+*
98+46**7+*
8+46**7+*
+46**7+*

46**7+*
6**7+*

**7+*

*7+*

7+*
+*

*
NULL

Stack
NULL
5
9
5
8
9
5
9+8
5
4
9+8
5
6
4
9+8
5
4*6
9+8
5
(9+8)*4*6
5
7
(9+8)*4*6
5
(9+8)*4*6+7
5
5+((9+8)*4*6+7)

5 * ((9 + 8) * 4 * 6 + 7)

Page 4 of 22

Rayan DankarID:201302670Computer Algorithms Assig.

Question 3:
Given the following Binary search tree, Show the order of the visited nodes when we traverse
this tree using:

Preorder traversal:
root left right :
FBADCEGIH

Inorder traversal:
left root right :
ABCDEFGHI

Postorder traversal:
left right root :
ACEDBHIGF

Page 5 of 22

Rayan DankarID:201302670Computer Algorithms Assig.

Question 4
Draw the binary search tree that results from inserting the keys 12, 17, 20, 13, 14, 10, 11, 8 into
an initially empty tree.

Draw the
delet
ing
the
node
with
key
valu
e 16
from
the
follo
win
g
tree:

binary tree that result from

Page 6 of 22

Rayan DankarID:201302670Computer Algorithms Assig.

Question 5
What is a heap, what are the conditions that should be specified inside a heap, and what is the
relation 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 (min
heap) or vice versa (max heap).
Parent:
Left child: 2* + 1
Right child: 2* 1
Given the following max heap:

Illustrate the
effect of:
a. Insert
ing a
node
whos
e key
is P
into
the
follo
wing
heap

Page 7 of 22

Rayan DankarID:201302670Computer Algorithms Assig.

b. Removing the largest node from the resulting heap

Question 6
1) Show the detailed workings for the first two partitioning of the following array during
Quicksort.
The answer should show the behavior of variables as the partitioning proceeds.

Quick Sort
Page 8 of 22

Rayan DankarID:201302670Computer Algorithms Assig.

publicvoid quicksort(ITEM[] a, intL, int R)


{
if (R<= l) return;
int i = partition(a, L, R);
quicksort(a, L, i-1);
quicksort(a, i+1, R);
}
void partition(int[] a)
{
int left = 0, right = a.length - 1;
int p = a[left], L = left + 1, R = right;
while (L < R)
{
while (L< right && a[L] < p) L++;
while (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
5
left=
0

new a 5
left=
0
new a 5

1
2
3
3
1
9
right=7 p=5 L=1
L=2
L=3
3
1
4
right=7 p=5 L=3
L=4
3
1
4

4
5
6
7
8
2
4
7
R=7 Swap a[3] & a[6]
R=6
8
2
9
7
R=6 Swap a[4] & a[5]
R=5
2
8
9
7
Page 9 of 22

Rayan DankarID:201302670Computer Algorithms Assig.

left=
0
new a 2

right=7 p=5 L=4 R=5 a[0]=a[4]


L=5 R=4 a[4]=5
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 case

occurs (explain your answer showing the complexity in each case)


3) Using tree diagrams, show how the above array would be sorted using the

max 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 ascending order using?
1. Selection sort:

Selection Sort
public void selectionSort(int[] arr)
{
int i, j, minIndex, tmp;
int n = arr.length;
for (i = 0; i < n - 1; i++)
{
minIndex = i;
for (j = i + 1; j < n; j++)
if (arr[j] < arr[minIndex])
minIndex = j;
if (minIndex != i)
Page 10 of 22

Rayan DankarID:201302670Computer Algorithms Assig.

{
tmp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = tmp;
}
}
}
a. If the data is originally sorted in ascending order
i=0
i=1
i=2
i=3
minIndex = 0 minIndex = 1 minIndex = 2 minIndex = 3
Comparison
Swap

Comparison
Swap

i=4
minIndex =4

9
minIndex = i
0

9
minIndex = i
0

9
minIndex = i
0

9
minIndex = i
0

9
minIndex = i
0

i=5
minIndex = 5

i=6
minIndex = 6

i=7
minIndex = 7

i=8
minIndex = 8

Total

9
minIndex = i
0

9
minIndex = i
0

9
minIndex = i
0

9
minIndex = i
0

81

b. If the data is originally sorted in descending order


i=0
i=1
i=2
i=3
minIndex = 0 minIndex = 1 minIndex = 2 minIndex = 3
j=9
j=9
j=9
j=9
minIndex = 9 minIndex = 9 minIndex = 9 minIndex = 9
Comparison
9
9
9
9
minIndex =! i minIndex! = i minIndex != i minIndex! = i
Swap
1
1
1
1

Comparison
Swap

i=5
minIndex = 5
j=9
minIndex = 9
9
minIndex! = i
1

i=6
minIndex = 6
j=9
minIndex = 9
9
minIndex != i
1

i=7
minIndex = 7
j=9
minIndex = 9
9
minIndex! = i
1

2. Insertion sort:
Page 11 of 22

i=8
minIndex = 8
j=9
minIndex = 9
9
minIndex != i
1

i=4
minIndex = 4
j=9
minIndex = 9
9
minIndex != i
1
Total

81
9

Rayan DankarID:201302670Computer Algorithms Assig.

Insertion Sort
void insertionSort(int[] arr)
{
int 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. If the data is originally sorted in ascending order


i=1
i=2
i=3
i=4
Comparison
Swap

9
0
i=6

Comparison
Swap

9
0
i=7

9
0

9
0

9
0

i=8
9
0

i=5
9
0

i=9

Total

9
0

9
0

81
0

b. If the data is originally sorted in descending order


i=1
Comparison
Swap

i=2
9
1

i=6
Comparison
Swap

i=3
9
1

i=7
9
1

i=4
9
1

9
1

i=8
9
1

9
1

i=9
9
1

Page 12 of 22

i=5

Total
9
1

81
9

Rayan DankarID:201302670Computer Algorithms Assig.

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. If the data is originally sorted in ascending order

Comparison

Swap

Comparison
Swap

i=0
j=9

i=1
j=9

i=2
j=9

i=3
j=9

i=4
j=9

9
arr[j1]!>arr[j]
0

8
arr[j1]!>arr[j]
0

7
arr[j1]!>arr[j]
0

6
arr[j-1]!>arr[j]

5
arr[j-1]!>arr[j]

i=5
j=9

i=6
j=9

i=7
j=9

i=8
j=9

4
arr[j-1]!>arr[j]
0

3
arr[j-1]!>arr[j]
0

2
arr[j-1]!>arr[j]
0

1
arr[j-1]!>arr[j]
0

0
Total
45
0

b. If the data is originally sorted in descending order

Comparison
Swap

i=0
j=9

i=1
j=9

i=2
j=9

i=3
j=9

i=4
j=9

9
arr[j-1] >arr[j]
1

8
arr[j-1] >arr[j]
1

7
arr[j-1] >arr[j]
1

6
arr[j-1] >arr[j]
1

5
arr[j-1] >arr[j]
1

Page 13 of 22

Rayan DankarID:201302670Computer Algorithms Assig.

Comparison
Swap

i=5
j=9

i=6
j=9

i=7
j=9

i=8
j=9

4
arr[j-1] >arr[j]
1

3
arr[j-1] >arr[j]
1

2
arr[j-1] >arr[j]
1

1
arr[j-1] >arr[j]
1

Page 14 of 22

Total
45
9

Rayan DankarID:201302670Computer Algorithms Assig.

Question 8
The following functions
(e n)2

40 * n2 n

14logn4

are given:
27 * n2 + 3 * n + 1

14(logn)4

100nlogn

a) Arrange these functions according to O notation.


(e n)2 >

> 40 * n2 n

> 27 * n2 + 3 * n + 1

> 100nlogn > 14(logn)4 >

14logn4

b) Assume these functions are runtimes for 7 algorithms that all solve the same problem. For
which values of n (nearest integer) which algorithm has the shortest runtime?
(e n)2 : for n=0
40 * n2 n : 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 algorithm whose runtime is T(n) = n2log(n). For what ranges of n
(order of magnitude estimate) this algorithm will run in a second, an hour and a week on a
200MHz Processor, assuming one instruction is executed in one clock cycle.
in one second: n2log(n) = 200 * 106
in one hour: n2log(n) = 200 * 106 * 3600
in one hour: n2log(n) = 200 * 106 * 3600 * 24 * 7

Page 15 of 22

Rayan DankarID:201302670Computer Algorithms Assig.

Question 9
A divide-and-conquer algorithm for multiplying two N x N matrices reduces the calculation to
7 products of
matrices and 18 matrix additions of N x N matrices.
A matrix addition can be implemented by a double for-loop like this:
for i = 1 to N
for j =1 to N
a(i,j) = b(i,j) + c(i,j)
Give a recurrence solution for the runtime T (N) required to multiply two N x N matrices and
using Masters theorem, give an O-estimate for T (N).
T ( n ) = 7T ( ) + 18n2
Using Masters theorem:
a= 7

b=2

c=2

log2 7 > 2

( n log2 7 )

Page 16 of 22

Rayan DankarID:201302670Computer Algorithms Assig.

Question 10
Consider the following problem: The input is a set S containing n real numbers
and one real number x.
a) Design an algorithm to determine whether there are two elements of S whose
sum is exactly x. The algorithm should run in time O (n log n)
QuickSort (S);

O ( n log n)

i = 1;
j = n;
for( i = 1 j )
{

O (n)
for ( j =n 1 i )
{
if (S[i] + S[j] < x)
i++;
else if (S[i] + S[j] >x)
j;
else
break;
}

O ( n log n ) + O ( n ) = O ( n log n)

Page 17 of 22

Rayan DankarID:201302670Computer Algorithms Assig.

b) Suppose now that the set S is given in sorted order. Design an algorithm to
solve the problem in time O (n).
i = 1;
j = n;
for( i = 1 j )
O(n)

{
for ( j =n 1 i )
{
if (S[i] + S[j] < x)
i++;
else if (S[i] + S[j] >x)
j;
else
break;
}
}

O(n)

Page 18 of 22

Rayan DankarID:201302670Computer Algorithms Assig.

Question 11
Trace the following function Ruler for l = 0, r = 8, and h = 3.
Public void ruler (int l, int r, int h)
{
int m = (l + r)/2;
if (h > 0)
{
mark(m, h);
ruler(m, r, h-1);
ruler(l, m, h-1);
}
}

1
m=4
h=3>0
ruler (4, 8, 2)
ruler (0, 4, 2)

m=6
h=2>0
ruler (6, 8, 1)
ruler (0, 6, 1)

m=7
h=1>0
ruler (7, 8, 0)
ruler (0, 7, 0)

4
m=7
h=0 !>0

5
m=3
h=0 !>0

m=2
h=2>0
ruler (2, 8, 1)
ruler (0, 2, 1)

10
m=2
h=2>0
ruler (2, 8, 1)
ruler (0, 2, 1)

m=3
h=1>0
ruler (3, 8, 0)
ruler (0, 3, 0)

11

12

m=5
h=1>0
ruler (5, 8, 0)
ruler (0, 5, 0)

m=6
h= 0 ! > 0

14

13

m=1
h=1>0
ruler (1, 8, 0)
ruler (0, 1, 0)

m=0
h= 0 ! > 0

Question 12

Page 19 of 22

7
m=5
h= 0 ! > 0

8
m=1
h= 0 ! > 0

Rayan DankarID:201302670Computer Algorithms Assig.

Consider the weighted undirected graph below.

1. In what order would the vertices be traversed, using breadth-first search starting at
vertex 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 vertex
B, assuming that adjacent vertices are visited in alphabetical order?
BADCEGF
3. Illustrate the execution of Kruskals algorithm applied to the graph. Clearly show the
result after each step of the algorithm by listing the edges added in the order in which
they are added.

Page 20 of 22

Rayan DankarID:201302670Computer Algorithms Assig.

2 + 3 + 7 + 7 + 9 + 12 +12
= 52

4. Illustrate the execution of Prims algorithm


applied 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.

Page 21 of 22

Rayan DankarID:201302670Computer Algorithms Assig.

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 result after each step of the algorithm by listing the edges added
in the order in which they are added.

Page 22 of 22