14
1 CS300-201 Data Structure Spring 2012 2013 Answer Key- Assignment #2 Due Sunday, Feb 17th. Q1): For each of the following five program fragments: a. Give an analysis of the running time (Big-Oh will do). b. Implement the code in C++, and give the running time for several values of N(10k,100k,100k). c. Compare your analysis with the actual running times. sum = 0; for( i = 0; i < n; i++ ) for( j = 0; j < n; j++ ) sum++; sum = 0; for( i = 0; i < n; i++ ) for( j = 0; j < n * n; j++ ) sum++; sum = 0; for( i = 0; i < n; i++ ) for( j = 0; j < i; j++ ) sum++; sum = 0; for( i = 0; i < n; i++ ) for( j = 0; j < i * i; j++ ) for( k = 0; k < j; k++ ) sum++; sum = 0; for( i = 1; i < n; i++ ) for( j = 1; j < i * i; j++ ) if( j % i == 0 ) for( k = 0; k < j; k++ ) sum++;

CS300-201 Data Structure Spring 2012 2013 Answer Key ...portal.cs.ku.edu.kw/~shammeri/CS300_Hw2_Key.pdf · 1 CS300-201 Data Structure Spring 2012 – 2013 Answer Key- Assignment #2

Embed Size (px)

Citation preview

1

CS300-201 Data Structure

Spring 2012 – 2013

Answer Key- Assignment #2 Due Sunday, Feb 17th.

Q1): For each of the following five program fragments:

a. Give an analysis of the running time (Big-Oh will do).

b. Implement the code in C++, and give the running time for several values of

N(10k,100k,100k).

c. Compare your analysis with the actual running times.

sum = 0;

for( i = 0; i < n; i++ )

for( j = 0; j < n; j++ )

sum++;

sum = 0;

for( i = 0; i < n; i++ )

for( j = 0; j < n * n; j++ )

sum++;

sum = 0;

for( i = 0; i < n; i++ )

for( j = 0; j < i; j++ )

sum++;

sum = 0;

for( i = 0; i < n; i++ )

for( j = 0; j < i * i; j++ )

for( k = 0; k < j; k++ )

sum++;

sum = 0;

for( i = 1; i < n; i++ )

for( j = 1; j < i * i; j++ )

if( j % i == 0 )

for( k = 0; k < j; k++ )

sum++;

2

Q1 – a: Solution

//Student name

//Student Id

//Assignment #2 Question #1

//300-201/1 Data Structure

//Due date: Feb 17, 2013

//Implement the given algorithms in C++ and compare the Big-O

//Compare your analysis with the actual running time for each.

Code

Segment

No.

N = 10 N = 100 N = 1000 N = 10K N = 100K

1 T(n) = n*n = O(n2)

T(n) = n*n = O(n2)

T(n) = n*n = O(n2)

T(n) = n*n = O(n2)

T(n) = n*n = O(n2)

2 T(n) = n* (n * n) = O(n3) T(n) = n* (n * n) = O(n3)

T(n) = n* (n * n) = O(n3)

T(n) = n* (n * n) = O(n3)

same

3 T(n) = (c/2) *n*n = c*n2 = O(n2)

T(n) = (c/2) *n*n = c*n2 = O(n2)

T(n) = (c/2) *n*n = c*n2 = O(n2)

T(n) = (c/2) *n*n = c*n2

= O(n2)

same

4 outer loop iterates = n middle loop = (n

2)/2

inner loop = (n2)

/4 answer:

T(n) = n*(n2/2)*(n2/4)= c*n5 = O(n5)

outer loop iterates = n middle loop = (n

2)/2

inner loop = (n2)

/4 answer:

T(n) = n*(n2/2)*(n2/4)= c*n5 = O(n5)

same

same

same

5 - outer loop iterates = n+1 - middle loop iterates= [(n2)+1]/2 - inner loop iterates = (n+1)/2 True if j multiple of i; so it will be repeated i times c = constant number of operations at each iteration answer:

T(n) = c*(n+1)*[(n2)+1]/2*(n+1/2)=O(n4)

same

same

same

same

3

Q1 – b & c: Solution

//Student name

//Student Id

//Assignment #2 Question #1 b & c

//300-201/1 Data Structure

//Due date: Feb 17, 2013

//The following code gives the running time

// for each given algorithm at Q1 – Part1.

#include<time.h>

#include<stdio.h>

#include<iostream>

using namespace std;

int main()

{

int n=0, i,j,k, sum=0;

float x=0.0;

cout<<"Enter n"<<endl;

cin>>n;

// capture the start time before looing

clock_t tStart = clock();

for( i = 0; i < n; i++ )

for( j = 0; j < n * n; j++ )

sum++;

// compute the execution time after exit from the loops

x=(double)((clock()-tStart))/CLOCKS_PER_SEC;

printf("Time = %.5f\n",x);

system ("pause");

return 0;

}

Code

Segment

No.

N = 10 N = 100 N = 1000 N = 10K N = 100K

1.

O(n2) Less than

0.001 sec

Less than 0.001 sec

Less than 0.01

sec

< 0.17 sec

~ 17.4 sec

2.

O(n3) Less than 0.001 sec

Less than 0.001 sec < 1.62 sec

~ 1645 sec

< 455 hours

3.

O(n2) Less than 0.001 sec

Less than 0.001 sec Less than 0.001 sec

< 0.095 sec

~ 8.64 sec

4.

O(n5)

Less than

0.001 sec

~ 1.67 sec

~ 16700 sec

~ 3 days

172595 sec

days?

5.

O(n4)

~ 0.001 sec

~ 0.05 sec ~ 246.32 sec ~ 34725. sec

days?

4

Q2) Show that X62 can be computed with only eight multiplications.

Solution:

X62 = X20 * X42

X42 = X20 * X20 * X2

X20 = X10 * X10

X10 = X5 * X5

X5 = X2 * X2 * X

X2 = X * X

Q3) Give an efficient algorithm to determine if there exists an integer i such that Ai = i in an array of

integers A1 < A2 < A3 < · · · < AN. What is the running time of your algorithm?

Solution:

Worst case = O(log n)

Int BinarySearch (int array [ ], int i, int low, int high)

{

int middle;

while (low<=high)

{

middle=(low+high)/2;

if(i==array[middle])

return middle;

else if(i<array[middle])

high=middle-1;

else

low=middle+1;

return -1;

}

5

Q4) Assume that a singly linked list is implemented with a header node, but no tail node, and that it

maintains only a reference to the header node. Write a class that includes methods to:

a. return the size of the linked list

b. print the linked list

c. test if a value x is contained in the linked list

d. add a value x if it is not already contained in the linked list and maintain the linked list

sorted

e. remove a value x if it is contained in the linked list.

Solution:

/* Question 4 :

Write a class that includes methods to:

a. return the size of the linked list

b. print the linked list

c. test if a value x is contained in the linked list

d. add a value x if it is not already contained in the linked list and

maintain the linked list

sorted

e. remove a value x if it is contained in the linked list */

#include<iostream>

using namespace std;

bool search(int x);

int size();

void print();

void add(int x);

void remove(int x);

// Structure to identify a node

struct node

{

int data;

node * next;

node(int d, node *n)

{ data=d; next=n; }

}

// class declaration to form a linked list

class list

{

private:

node *head;

public:

// assigning Null to head

list()

{

head=NULL;

}/*end list*/

// return head address

6

node *get()

{

return head;

}// end get method

// return true if value is found or false otherwise

bool search(int x)

{

if(head==NULL)

return false;

else

{

node *ptr=head;

while(ptr!=NULL)

{

if(ptr->data==x)

return true;

ptr=ptr->next;

}

return false;

}

}// end search method

// return the size of the linked list

int size()

{

int count=0;

node *ptr=head;

if (ptr==NULL)

return count;

else

{

while(ptr!=NULL)

{

count++;

ptr=ptr->next;

}

}

return count;

}// end size method

// print the linked list elements

void print()

{

cout<<endl;

node *ptr=head;

if(ptr==NULL)

{

cout<<"No data found"<<endl;

}

else

{

while(ptr!=NULL)

{

cout<<ptr->data<<"->";

ptr=ptr->next;

7

}

}

cout<<"NULL"<<endl;

} // end print method

// add a new node to the linked list

void add(int x)

{

if(head==NULL)

{

head=new node(x,NULL);

}

else

{

node *q = head ;

if(x < head->data)

{

node *ptr=new node(x,head);

head = ptr;

}

else

{

while (q->next!=NULL)

{

if( x==q->data )

{ cout<<x<<" the number is exist "<<endl;

break;

}

else if( x < q->next->data)

{

node *ptr=new node(x,q->next);

q->next = ptr;

break ;

}

else

{ q = q->next ; }

}

if(q->next == NULL)

{

node *ptr = new node(x,q->next);

q->next = ptr;

}

}

}

}// end add method

// Remove a node from the linked list according to it's value;

void remove(int x)

{

if(head==NULL)

cout<<"No Data"<<endl;

else

8

{

if(head->data==x)

{

head=head->next;

}

else

{

node *ptr=head;

while(ptr->next!=NULL)

{

if(ptr->next->data==x)

{

ptr->next=ptr->next->next;

break;

}

else

{

ptr=ptr->next;

}

}

}

}

} //end Remove method

} // end of class list

/* main to execute program */

int main()

{

list l;

int number;

/*Instructions*/

cout<<" Choose"<<endl;

cout<<" 1 Return List's Size"<<endl;

cout<<" 2 Print list"<<endl;

cout<<" 3 Test existance of a value in list"<<endl;

cout<<" 4 Add value to list"<<endl;

cout<<" 5 Remove value from list"<<endl;

cout<<" 6 To exit "<<endl;

cout<<endl;

cin>>number;

while(number!=6)

{

if(number==1)

{ cout<<"Size = "<<l.size()<<endl; }

if(number==2)

{ l.print(); }

if(number==3)

{ int key;

cout<<"Enter value"<<endl;

cin>>key;

if(l.search(key))

9

{ cout<<"Exists"<<endl; }

else

{ cout<<"Does not exist"<<endl; }

}

if(number==4)

{ int number;

cout<<"Enter value & terminate by negative >> " <<endl;

cin>>number;

while( number>=0)

{

l.add(number);

cin>>number;

}

}

if(number==5)

{int number;

cout<<"Enter value to remove"<<endl;

cin>>number;

l.remove(number);

}

cout<<endl;

/*Menu to select an operation on a Linked List */

cout<<" Choose"<<endl;

cout<<" 1 Return List's Size"<<endl;

cout<<" 2 Print list"<<endl;

cout<<" 3 Test existance of a value in list"<<endl;

cout<<" 4 Add value to list"<<endl;

cout<<" 5 Remove value from list"<<endl;

cout<<" 6 To exit "<<endl;

cout<<endl;

system ("pause");

}

}

Q5) Given two sorted lists, L1 and L2, write a procedure to compute L1 ∩ L2 and a procedure to compute L1 ∪ L2 using only the basic list operations. Solution:

/* Question 5

Given two sorted lists, L1 and L2,

write a procedure to compute L1 ∩ L2 and a procedure to compute L1 ∪ L2 using only the basic list operations

*/

#include<iostream>

using namespace std;

// Struct of a Node

struct node

{

int data;

node * next;

node(int d, node *n)/*constructor*/

{ data=d; next=n; }

}

10

// Class List for creating a linked list

class list

{

private:

node *head;

public:

// assign NULL to the head

list()

{

head=NULL;

}/*end list*/

// return the head address

node *get()

{

return head;

}// end of get method

// Print the linked list*/

void print()

{

cout<<endl;

node *ptr=head;

if(ptr==NULL)

{

cout<<"No data found"<<endl;

}

else

{

while(ptr!=NULL)

{

cout<<ptr->data<<"->";

ptr=ptr->next;

}

}

cout<<"NULL"<<endl;

}// end of print method

// return true if given value is found or false otherwise

bool search(int x)

{

if(head==NULL)

return false;

else

{

node *ptr=head;

while(ptr!=NULL)

{

if(ptr->data==x)

return true;

ptr=ptr->next;

}

return false;

}

}// end of search method

11

// insert a value to the linked list

void add(int x)

{

if(head==NULL)

{

head=new node(x,NULL);

}

else

{

node *q = head ;

if(x < head->data)

{

node *ptr=new node(x,head);

head = ptr;

}

else

{

while (q->next!=NULL)

{

if( x==q->data )

{ cout<<x<<" the number is exist "<<endl;

break;

}

else if( x < q->next->data)

{

node *ptr=new node(x,q->next);

q->next = ptr;

break ;

}

else

{ q = q->next ; }

}

if(q->next == NULL)

{

node *ptr = new node(x,q->next);

q->next = ptr;

}

}

}

}// end of add method

// Method to print the intersection of two linked lists

void Intersect(list L1, list L2)

{

list result;

node *p = L1.get();

node *q = L2.get();

while(p!=NULL && q!=NULL)

{

if( p->data < q->data)

12

{

p = p->next;

}

else if( p->data == q->data )

{

result.add(p->data);

p=p->next;

}

else

{ q=q->next; }

}

cout<<"Intersection => "<<endl;

result.print();

} // end of method

// Method to print the union of a two linked list

void Union(list L1, list L2)

{

node *p=L1.get();

node *q=L2.get();

list result;

while(p!=NULL)

{

result.add(p->data);

p = p->next;

}

while(q!=NULL)

{

if( result.search(q->data)== false )

{

result.add(q->data);

q = q->next;

}

else

{

q = q->next;

}

}

cout<<"Union => "<<endl;

result.print();

}// end Union method

}; //end of class list

//main to execute program

int main()

{

list l1;

list l2;

list R;

int x;

int number;

13

cout<<"Enter L1 values & terminate by negative"<<endl;

cin>>x;

while(x>=0)

{

l1.add(x);

cin>>x;

}

cout<<"Enter L2 values & terminate by negative"<<endl;

cin>>x;

while(x>=0)

{

l2.add(x);

cin>>x;

}

// choose the proper choice

cout<<"Choose"<<endl;

cout<<"1 To print intersection"<<endl;

cout<<"2 To print Union"<<endl;

cout<<"3 To exit"<<endl;

cin>>number;

while(number != 3)

{

if(number==1)

{

R.Intersect(l1,l2);

}

if(number==2)

{

R.Union(l1,l2);

}

// choose the proper choice

cout<<"Choose"<<endl;

cout<<"1 To print intersection"<<endl;

cout<<"2 To print Union"<<endl;

cout<<"3 To exit"<<endl;

system ("pause");

}

}// end main

Q6) Compute the big-O for all methods in part 4 and 5.

Solution: Q6 a : Answer of Q4 Analysis: list method => O(1)

get method => O(1)

search => 2+2n= O(n)

14

size => 4+2n = O(n)

print => 4+2n = O(n)

add => 5+2n =O(n)

remove => 2+n=O(n)

Solution: Q6 b : Answer of Q5 Analysis:

list method => O(1)

get method => O(1)

print => 4+2n = O(n)

search => 2+2n= O(n)

add => 5+2n =O(n)

Union => 7+2n+2m

If (n>m) => O(n)

else => O(m)

Intersect => 8+n or 8+m

If (n<m) >= O(n)

else >= O(m)