drmeelhennawy.files.wordpress.com  · Web view2-Show that O(n lg 2 n) is the solution to the...

19
Complexity Analysis List the following functions from highest to lowest order. O(1) , O(n 2 ), O(n 3 ), O(n), O(log n), O(n log n), O(n 3/2 ) Solution: O(n 3 ) O(n 2 ) O(n 3/2 ) O(n log 2 n) O(n) O(log 2 n) O(1) Recursion 1. Show that the solution of T (n) = T ( n/2 ) + 1 is O(lg 2 n). (Cormen, 2002) Solution: Given: T(n) = T(n/2) + 1 Then, we can write: T(n/2) = T(n/4) + 1 So, T(n) = [T(n/4) + 1] + 1 = T(n/4) + 2 Also, we can write: T(n/4) = T(n/8) + 1 So, T(n) = [T(n/8) + 2 ] + 1 = T(n/8) + 3 = T(n/2 3 ) + 3 In general, T(n) = T(n/2 k ) + k Continue until reaching to the unit leave which means that (n/2 k ) = 1, i. e. n = 2 k Taking the log of both sides we have: k = log 2 (n) 1

Transcript of drmeelhennawy.files.wordpress.com  · Web view2-Show that O(n lg 2 n) is the solution to the...

Page 1: drmeelhennawy.files.wordpress.com  · Web view2-Show that O(n lg 2 n) is the solution to the “exact” recurrence for merge sort.(Cormen, 2002)

Complexity AnalysisList the following functions from highest to lowest order. O(1) , O(n2), O(n3), O(n), O(log n), O(n log n), O(n3/2)Solution:

O(n3)O(n2)O(n3/2)O(n log2n)O(n)O(log2n)O(1)

Recursion1. Show that the solution of T (n) = T ( n/2 ) + 1 is O(lg2 n). (Cormen, 2002)Solution:Given: T(n) = T(n/2) + 1

Then, we can write: T(n/2) = T(n/4) + 1

So, T(n) = [T(n/4) + 1] + 1 = T(n/4) + 2

Also, we can write: T(n/4) = T(n/8) + 1

So, T(n) = [T(n/8) + 2 ] + 1 = T(n/8) + 3

= T(n/23) + 3

In general, T(n) = T(n/2k) + k

Continue until reaching to the unit leave which means that (n/2k) = 1, i. e. n = 2k

Taking the log of both sides we have: k = log2(n)

Then: T(n) = T(n/2k) + k

= T(1) + Log2(n)

So, the complexity of T (n) = T ( n/2 ) + 1 is O ( log2 n)

1

Page 2: drmeelhennawy.files.wordpress.com  · Web view2-Show that O(n lg 2 n) is the solution to the “exact” recurrence for merge sort.(Cormen, 2002)

2-Show that O(n lg2 n) is the solution to the “exact” recurrence for merge sort. (Cormen, 2002)Solution:

Using Mathematical ProofThe divide-and-Conquer formula dictates that:

O (1) if n = 1,T (n) =

2T (n/2) + O (n) if n > 1.

Or it can be rewritten as:

1 if n = 1,T (n) =

2T (n/2) + n if n > 1,Accordingly:

T(1) = 1

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

Then, we can write: T(n/2) = 2T(n/4) + n/2

So, T(n) = 2 [2T(n/4) + n/2] + n

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

= 4 T(n/4) + 2n

2

Page 3: drmeelhennawy.files.wordpress.com  · Web view2-Show that O(n lg 2 n) is the solution to the “exact” recurrence for merge sort.(Cormen, 2002)

Also, we can write: T(n/4) = 2T(n/8) + n/4

So, T(n) = 4 [2T(n/8) + n/4] + 2n

= 8T(n/8) + 3n

It can be written as: T(n) = 23 T(n/23) + 3n

In general, T(n) = 2k T(n/2k) + kn

Continue until reaching to the unit leave which means that (n/2k) = 1, i. e. n = 2k

Taking the log of both sides we have: k = log2(n)

Then, T(n) = 2log2nT(1) + n log2 n, since 2log2n = 1, then:

T(n) = n log2 n

3-Fibonacci Number sequence is (0,1,1,2,3,5,8,13,21,….). Use iterative and recursive methods to generate any Fibonacci and compare between the two methods.Solution:

Recursive Methodint F (int n){ If ( n == 0 || n == 1)

return (n);else

return F(n-1) + F(n-2);}

Recursive method efficiency in big-O notation is O(2n)Iterative Methodint F (int n){ int Fib;

int F0=0;int F1=1;if (n == 0 || n == 1)

return n;elsefor (int i=2; I <= n ; i++){ Fib = F0 + F1;

F0 = F1;F1 = Fib;

}return Fib;

}Iterative method efficiency in big-O notation is O(n)

3

Page 4: drmeelhennawy.files.wordpress.com  · Web view2-Show that O(n lg 2 n) is the solution to the “exact” recurrence for merge sort.(Cormen, 2002)

Insertion Sort1. Show the results of each pass of InsertionSort applied to the list [7, 3, 9, 4, 2, 5, 6, 1,

8]. (Jeffrey, 2008)Solution:

Pass 1 7 3 9 4 2 5 6 1 8Pass 2 3 7 9 4 2 5 6 1 8Pass 3 3 7 9 4 2 5 6 1 8Pass 4 3 4 7 9 2 5 6 1 8Pass 5 2 3 4 7 9 5 6 1 8Pass 6 2 3 4 5 7 9 6 1 8Pass 7 2 3 4 5 6 7 9 1 8Pass 8 1 2 3 4 5 6 7 9 8Pass 9 1 2 3 4 5 6 7 8 9

2. Show the results of each pass of InsertionSort applied to the list [3, 5, 2, 9, 8, 1, 6, 4, 7]. (Jeffrey, 2008).

Solution:Pass 1 3 5 2 9 8 1 6 4 7Pass 2 3 5 2 9 8 1 6 4 7Pass 3 2 3 5 9 8 1 6 4 7Pass 4 2 3 5 9 8 1 6 4 7Pass 5 2 3 5 8 9 1 6 4 7Pass 6 1 2 3 5 8 9 6 4 7Pass 7 1 2 3 5 6 8 9 4 7Pass 8 1 2 3 4 5 6 8 9 7Pass 9 1 2 3 4 5 6 7 8 9

3. Given an array A = [2 4 8 1 9 5], answer the following:A. Write Insertion sort algorithm.B. When the worst case and best case of the insertion sort algorithm occur and

compare between the two cases.C. Rewrite the INSERTION-SORT procedure to sort into non-increasing

instead of non-decreasing order.Solution:

A. The Insertion sort algorithm.

4

Page 5: drmeelhennawy.files.wordpress.com  · Web view2-Show that O(n lg 2 n) is the solution to the “exact” recurrence for merge sort.(Cormen, 2002)

B. When the worst case and best case of the insertion sort algorithm occur and compare between the two cases.The Worst case occurs if the array is reversed sorted O(n2).The Best case occurs if the array is already sorted O(n).

C. Rewrite the INSERTION-SORT procedure to sort into non-increasing instead of non-decreasing order Insertion Sort(A)for j=2 to n

do key = A[j]i = j-1while i > 0 and A[i] < key, do:

{ A[i+1] = A[i] i = i - 1}

A[i+1] = key

Merge Sort1. Show the results of each pass of MergeSort applied to the list [3, 5, 2, 9, 8, 1, 6,

4]. (Jeffrey, 2008)Solution:

5

3 5 8 1 6 42 9

3 5 1 8 4 62 9

2 3 1 4 6 85 9

2 5 6 8 93 4

Page 6: drmeelhennawy.files.wordpress.com  · Web view2-Show that O(n lg 2 n) is the solution to the “exact” recurrence for merge sort.(Cormen, 2002)

2. Create an ordering of the numbers 1 through 8 that will cause MergeSort to do the worst-case number of comparisons of 17. (Hint: Work backward through the sorting process.) (Jeffrey, 2008).

SolutionOne ordering of the numbers 1 through 8 that will cause the worst case of 17 comparisons is the list [1, 5, 3, 7, 2, 6, 4, 8]. Another such ordering would be [7, 3, 5, 1, 8, 4, 6, 2].

3. Compare between insertion , merge, and bubble sort techniques as the following:A. State when the worst case occurred and the complexity of the each algorithm

in that case?B. State when the Best case occurred and the complexity of the each algorithm

in that case?C. What is the best algorithm?

Solution:Bubble Merge Insertion

A. Worst case

When the array reversed sortedO(n2)

Number of comparisons=number of

swaps≈n2/2

When the array reversed sorted

O(n log n)number of swaps less

than in best case

When the array reversed sorted

O(n2)

B. Best Case

When the array already sortedO(n2)

Number of comparisons≈n2/2

number of swaps=0

When the array already sorted

O(n log n)

When the array already sorted

O(n)

C. Best algorithm is merge sort

Matrix multiplication1- Write an algorithm to multiply two matrices with the same dimensions.

Solution:

6

Page 7: drmeelhennawy.files.wordpress.com  · Web view2-Show that O(n lg 2 n) is the solution to the “exact” recurrence for merge sort.(Cormen, 2002)

Graph algorithms1- A. Draw the following graph: G = ({1, 2, 3, 4, 5, 6}, {{1, 2}, {1, 4}, {2, 5}, {2, 6}, {3, 4}, {3, 5}, {3, 6}, {4, 5}, {4, 6}, {5, 6}}).

B. Build the adjacency matrix for the above graph.C. Build the adjacency list for the above graph. (Jeffrey, 2008)

Solution:A. Draw the following graph: G = ({1, 2, 3, 4, 5, 6}, {{1, 2}, {1, 4}, {2, 5}, {2, 6}, {3, 4},

{3, 5}, {3, 6}, {4, 5}, {4, 6}, {5, 6}}).

B. Adjacency Matrix

C. Adjacency List

7

Page 8: drmeelhennawy.files.wordpress.com  · Web view2-Show that O(n lg 2 n) is the solution to the “exact” recurrence for merge sort.(Cormen, 2002)

2- A. Draw the following digraph: G = ({1, 2, 3, 4, 5}, {(1, 2), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 2), (3, 4), (3, 5), (4, 1), (4, 2), (4, 5), (5, 2), (5, 3), (5, 4)}). B. Build the adjacency matrix for the above digraph.C. Build the adjacency list for the above digraph. (Jeffrey, 2008)

Solution:A. Draw the following digraph: G = ({1, 2, 3, 4, 5}, {(1, 2), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 2), (3, 4), (3, 5), (4, 1), (4, 2), (4, 5), (5, 2), (5, 3), (5, 4)}).

B. Adjacency Matrix

C. Adjacency List

8

Page 9: drmeelhennawy.files.wordpress.com  · Web view2-Show that O(n lg 2 n) is the solution to the “exact” recurrence for merge sort.(Cormen, 2002)

1 2

36

45

3- Answer the following:A. Give the set description for the following graph.

B. List all of the paths between node 1 and node 5 in the above graph.C. List all of the cycles that start at node 3 in the above graph.D. Build the adjacency matrix for the above graph.E. Build the adjacency list for the above graph. (Jeffrey, 2008 page 147, 149)

Solution:A. The description is:

G = ( {1, 2, 3, 4, 5, 6}, { {1, 2}, {1, 4}, {1, 6}, {2, 3}, {2, 4}, {2, 6}, {3, 4}, {4, 5}, {4, 6}, {5, 6} } )

B. The paths

C. The cycles

9

Page 10: drmeelhennawy.files.wordpress.com  · Web view2-Show that O(n lg 2 n) is the solution to the “exact” recurrence for merge sort.(Cormen, 2002)

1 2

53

76

4

D. The adjacency Matrix

E. Adjacency List

4- Answer the following:A. Give the set description for the following digraph:

B. List all of the paths between node 1 and node 4 in the above digraph.C. List all of the cycles that start at node 7 in the above digraph.D. Build the adjacency matrix for the digraph in question 4 of Section 6.1.2.E. Build the adjacency list for the digraph in question 4 of Section 6.1.2.

(Jeffrey, 2008 page 147, 149)Solution:

A. The description is:G = ( {1, 2, 3, 4, 5, 6, 7}, { (1, 2), (1, 3), (1, 4), (2, 1), (2, 4), (2, 7), (3, 4), (4, 6), (5, 2), (5, 7), (6, 3), (6, 7), (7, 4), (7, 5) } )

10

Page 11: drmeelhennawy.files.wordpress.com  · Web view2-Show that O(n lg 2 n) is the solution to the “exact” recurrence for merge sort.(Cormen, 2002)

B. The Paths

C. The Cycles

D. Adjacency Matrix

E. Adjacency List

11

Page 12: drmeelhennawy.files.wordpress.com  · Web view2-Show that O(n lg 2 n) is the solution to the “exact” recurrence for merge sort.(Cormen, 2002)

Depth-First and Breadth-First Traversal1- For the following graphs, give the order that the nodes will be first visited when

doing a breadth-first traversal starting at the node labeled with a 1. (Jeffrey, 2008)

A. 1, 2, 4, 5, 7, 3, 6

B. 1, 4, 5, 2, 3, 7, 6

C. 1, 2, 4, 5, 3, 7, 6

D. 1, 2, 4, 5, 3, 6, 7

2- For the graphs in the above question, give the order that the nodes will be first visited when doing a depth-first traversal starting at the node labeled with a 1. (Jeffrey, 2008)

Solution:A. 1, 2, 5, 4, 3, 6, 7

B. 1, 4, 2, 3, 7, 6, 5

C. 1, 2, 3, 4, 6, 5, 7

D. 1, 2, 3, 7, 4, 6, 5

12

Page 13: drmeelhennawy.files.wordpress.com  · Web view2-Show that O(n lg 2 n) is the solution to the “exact” recurrence for merge sort.(Cormen, 2002)

Minimum Spanning Tree Algorithm 1- Find the minimum spanning tree using the Dijkstra-Prim algorithm for the

following graphs starting at node A. Show all steps. (Jeffrey, 2008)

Solution:

13

Page 14: drmeelhennawy.files.wordpress.com  · Web view2-Show that O(n lg 2 n) is the solution to the “exact” recurrence for merge sort.(Cormen, 2002)

14

Page 15: drmeelhennawy.files.wordpress.com  · Web view2-Show that O(n lg 2 n) is the solution to the “exact” recurrence for merge sort.(Cormen, 2002)

-----------------------------------------------------------------------------------2- Find the minimum spanning tree using the Kruskal algorithm for the graphs in

question 1. Show all steps. (Jeffrey, 2008)Solution:

15

Page 16: drmeelhennawy.files.wordpress.com  · Web view2-Show that O(n lg 2 n) is the solution to the “exact” recurrence for merge sort.(Cormen, 2002)

16