Você está na página 1de 16

UNIT-I

1. Explain in detail various asymptotic notations with proper examples?


Asymptotic notations are mathematical notations used to specify the time/space complexities in
some approximate format.

1) Big oh notation
2) Omega notation
3) Theta notation
4) Little oh notation
5) Little omega notation
Big oh notation:
Big Oh notation is used to define the upper bound of an algorithm in terms of Time Complexity.

That means Big - Oh notation always indicates the maximum time required by an algorithm for
all input values. That means Big - Oh notation describes the worst case of an algorithm time
complexity.

The function f(n)=O(g(n)), if and only if, there exist positive constants c and no such that f(n) ≤
c * g(n) , for all n, n≥n0.

Example

Prove that 3n+2 = O(n)


Sol: assume 3n+2<= 4n
Let n=1, 5<=4 (false)
Let n=2, 8<=8 (true)
Let n=3, 11<=12(true) …….
For this, c=4, g(n)=n, k=2

Big - Omege Notation (Ω)


Big - Omega notation is used to define the lower bound of an algorithm in terms of Time
Complexity.

1
That means Big - Omega notation always indicates the minimum time required by an algorithm
for all input values. That means Big - Omega notation describes the best case of an algorithm
time complexity.

The function f(n)=Ω(g(n)), if and only if, there exists positive constants c and no such that

f(n) ≥ c * g(n) , for all n, n≥n0.

Prove that 3n+2 = Ω(n)


Assume 3n+2 >= 3n
Let n=1, 3(1)+2>=3(1), 5>=3(true)
Let n=2, 8>=6( true) and so on
From this, c=3 g(n)=n and k=1

Big - Theta Notation (Θ)


Big - Theta notation is used to define the average bound of an algorithm in terms of Time
Complexity.

That means Big - Theta notation always indicates the average time required by an algorithm for
all input values. That means Big - Theta notation describes the average case of an algorithm time
complexity.

The function f(n)=θ(g(n)), if and only if, there exists positive constants c1,c2 and no such that
C1*g(n)≤ f(n)≤c2 *g(n) , for all n, n≥n0.

Gaphical representation

2
Example:

Prove that, 3n+2=θ(n)


Assume 3n≤3n+2 ≤4n
Let n=1, 3 ≤ 5 ≤ 4 (false)
Let n=2, 6≤ 8 ≤ 8 (true)
Let n=3, 9≤ 11 ≤ 12 (true)
So.. From this, c1=3, c2= 4, g(n)=n, and k= 2

Little oh notation
The function f(n)=o(g(n)), if and only if, there exists positive constants c and no such that f(n) ‹
c * g(n) , for all n, n≥n0.
(or)
the function f(n)=o(g(n)) if and only if
lim f(n)/g(n)= θ
n--->∞

Little omega notation


The function f(n)=ω(g(n)), if and only if, there exists positive constants c and no such that f(n) ›
c * g(n) , for all n, n≥n0.
(or)
the function f(n)= ω(g(n)) if and only if
lim g(n)/f(n)= θ
n->∞

Common Asymptotic Notations


 Constant O(1)
 Logarithmic O(log n)
 Linear Ο(n)
 n logn Ο(n log n)
 Quadratic Ο(n2)
 Cubic Ο(n3)
 Polynomial nΟ(1)
 Exponential 2(n)

3
2. Explain multidimensional array with an example?
Ans) Multidimensional arrays:

If an array consists of two or more subscripts then it is called "Multidimensional array".


In generally a multidimensional array consists of three subscripts.

Declarationof Multi dimensional arrays:

The general format of a multidimensional array is:


Syntax: datatype ArrayName[size1][size2] - - - - - - [sizen];
Example: int k[2][3][4];

The above example is a three dimensional array. Here, compiler allocates memory as in
terms of tables.

Let m1, m2, - - - , mn are the sizes, then a multidimensional array can be defined as –
“Multidimensional array is a collection of m1 x m2 x - - - - x mn homogeneous data elements
that are stored in m1 x m2 x - - - - x mn successive memory locations”.

INITIALIZATION OF MULTIDIMENSIONAL ARRAYS

Three dimensional arrays can be initialized into two different ways.


1. Direct initialization
2. Through program execution

Direct Initialization:
1) The general form of initialization of a multidimensional array is:
Syntax: datatype ArrayName[size1][size2]- - - -[sizen] = {List of Values};

Here, List of Values is separated by comma operator.


Example: int k[2][3][2] = {10,20,30,40,50,60,70,80,90,10,11,12};

2) List of values can also be initialized in the form of a table representation as:
Example: int k[2][3][2] = { { {1,2}, {4,5},{6,7} }, { {8,9},{10,11},{12,13} } };

Through program execution:


/* PROGRAM TO READ A MULTDIMENSIONAL ARRAY AND PRINT IT */

main()
{
int m, n, p, i, j, k, x[10][10][10];
clrscr();
printf("\nEnter how many Tables:");

4
scanf("%d", &m);
printf("\nEnter how many rows:");
scanf("%d", &n);
printf("\nEnter how many columns:");
scanf("%d", &p);
printf("\nEnter Array Elements:");
for(i=0;i<m; i++)
{
for(j=0;j<n; j++)
{
for(k=0;k<p; k++)
scanf("%d", &x[i][j][k]);
}
}
printf("\n Array Elements Are:");
for(i=0;i<m; i++)
{
for(j=0; j<n; j++)
{
printf("\n");
for(k=0; k<p; k++)
printf("%5d",x[i][j][k]);
}
}
}

3. Explain various operations of double linked list?

Ans) Operations: In a Double linked list we perform the following operations...


1. Insertion
2. Deletion
3. Display

1. Insertion: In a double linked list, the insertion operation can be performed in three ways.
They are as follows...

5
 Inserting At Beginning of the list
Step1: Create a new node with given value
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode-->data = value;
newNode-->previous=NULL;
newNode-->next=NULL;

Step 2: Check whether list is empty or not (head==null)

Step 3: If it is empty then


head = newNode;

Step 4: If the list is not empty then


head->previous=newNode;
newNode->next=head;
head= newNode;

Example:

 Inserting At end of the list:


Step1: Create a new node with given value
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode-->data = value;
newNode-->previous=NULL;
newNode-->next=NULL;

Step 2: Check whether list is empty or not (head==null)

Step 3: If it is empty then


head = newNode;

6
Step 4: If the list is not empty then
struct Node *temp=head;
while(temp->next != NULL)
Repeat
temp=temp->next;
end Repeat
temp->next=newNode;
newNode->previous=temp;
Example:

 Inserting At specific position of the list


Step1: Create a new node with given value
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode-->data = value;
newNode-->previous=NULL;
newNode-->next=NULL;

Step 2: Check whether list is empty or not (head==null)

Step 3: If it is empty then


head = newNode;

Step 4: If the list is not empty then


struct Node *temp=head;
while(temp->data != loc)
Repeat
temp=temp->next;
end repeat
newNode->next=temp->next;
temp->next->previous=newNode;
temp->next=newNode;
newNode->previous=temp;

7
Example:

3. Display() :display all the elements in existing list


Step 1: Check whether list is empty or not (head==NULL)
if (head == NULL) then
display “ List is empty!!!”;

Step 2: if the list is not empty then,


struct Node *temp = head;
while(temp->next != NULL)
repeat
display temp->data
temp = temp->next;
end repeat
display temp->data

2. Deletion: In a double linked list, the deletion operation can be performed in three
ways. They are as follows...

 Deleting from Beginning of the list


STEP 1: Check whether list is empty or not (head==NULL)
STEP 2: if it is empty then,
display “ Linked list is empty !!!”
STEP 3: if it is not empty then
struct Node *temp=head;
if(temp->next==NULL)
head=NULL;
else
begin
head=temp->next;
free(temp);
end

8
 Deleting from the end of the list
STEP 1: Check whether list is empty or not (head==NULL)

STEP 2: if it is empty then,


display “ Linked list is empty !!!”

STEP 3: if it is not empty then


struct Node *temp1=head,*temp2;
if(temp1->next==NULL)
head=NULL;
else
begin
while(temp1->next!=NULL)
Repeat
temp2=temp1;
temp1=temp1->next;
end repeat
temp2->next=NULL;
end

 Deleting a specific node from list


STEP 1: Check whether list is empty or not (head==NULL)
STEP 2: if it is empty then,
display “ Linked list is empty !!!”
STEP 3: if it is not empty then
struct Node *temp1=head,*temp2;
while(temp1->data!=loc)
Repeat
if(temp1->next==NULL)
begin
printf("\n The selected element not in the list!!!");
goto FunctionEnd;
end
temp2=temp1;
temp1=temp1->next;
end Repeat
temp2->next=temp1->next;
free(temp1);
FunctionEnd:

9
4. What is linked list? Explain different types of linked list with neat diagrams?
Ans) Definition: Linked list is a linear data structure that contains sequence of elements such
that each element links to its next element in the sequence. Each element in a linked list is called
as "Node".

Types of Linked List:


Following are the various types of linked list.
1. Single Linked List − Item navigation is forward only.
2. Doubly Linked List − Items can be navigated forward and backward.
3. Circular Linked List − Last item contains link of the first element as next and the first
element has a link to the last element as previous.
a) Circular single linked list
b) Circular double linked list

Single linked list:


 Single linked list is a sequence of elements in which every element has link to its next
element in the sequence.
 In any single linked list, the individual element is called as "Node".
 Every "Node" contains two fields, data and link. The data field is used to store actual
value of that node and link field is used to store the address of the next node in the
sequence.

Example:

Double Linked list:


Double linked list is a sequence of elements in which every element has links to its previous
element and next element in the sequence.
In double linked list, every node has link to its previous node and next node. So,
we can traverse forward by using next field and can traverse backward by using previous
field. Every node in a double linked list contains three fields like previous link, data, next link.

10
Example:

Circular Linked list:


Circular Linked List is Divided into 2 Categories .
 Circular Single Linked List
 Circular Double Linked List

Circular single linked list: Circular single linked list is a sequence of elements in which
every element has link to its next element in the sequence and the last element has a link to
the first element in the sequence.

Circular Double Linked List:


In circular doubly linked list, the next pointer of the last node points to the first node and
the previous pointer of the first node points to the last node making the circular in both
directions.

11
5. a) Write a program to implement stack operations using linked list?
Ans) #include<stdio.h>
#include<conio.h>

struct Node
{
int data;
struct Node *next;
}*top = NULL;

void push(int);
void pop();
void display();

void main()
{
int choice, value;
clrscr();
printf("\n:: Stack using Linked List ::\n");
while(1){
printf("\n****** MENU ******\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d", &value);
push(value);
break;
case 2: pop(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("\n Wrong selection!!! Please try again!!!\n");
}
}
}

12
void push(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(top == NULL)
{
newNode->next = NULL;
top = newNode;
}
else
{
newNode->next = top;
top = newNode;
}
printf("\n Insertion is Success!!!\n");
}

void pop()
{
if(top == NULL)
printf("\n Stack is Empty!!!\n");
else{
struct Node *temp = top;
printf("\n Deleted element: %d", temp->data);
top = temp->next;
free(temp);
}
}

void display()
{
if(top == NULL)
printf("\n Stack is Empty!!!\n");
else{
struct Node *temp = top;
while(temp->next != NULL){
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL",temp->data);
}
}

13
b) Design an algorithm to count no.of nodes of a single linked list?
Step1: Check whether list is empty or not (head == NULL)

step 2: if list is empty then,


display "list is empty!!! There is no nodes in the list!!!"

Step 3: if the list is not empty then,


struct Node *temp=head;
while (temp->next != NULL)
repeat
count=count+1;
temp=temp->next;
end repeat
display "The no.of nodes are" count+1

6. a) Explain the process of inserting an element at various positions of single linked list?
Ans) 1. Insertion: In a single linked list, the insertion operation can be performed in
three ways. They are as follows...
 Inserting At Beginning of the list
Step1: Create a new node with given value
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;

Step 2: Check whether list is empty or not (head==null)

Step 3: If it is empty then


newNode->link = NULL;
head = newNode;

Step 4: If the list is not empty then


newNode->link = head;
head = newNode;

14
 Inserting At End of the list
Step1: Create a new node with given value
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode-> link = null;

Step 2: Check whether list is empty or not (head==null)

Step 3: If it is empty then


head = newNode;

Step 4: If the list is not empty then


struct Node *temp = head;
while(temp->link != NULL)
temp = temp->link;
temp->link = newNode;

 Inserting At Specific location in the list


Step1: Create a new node with given value
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;

Step 2: Check whether list is empty or not (head==null)

Step 3: If it is empty then


newNode->link = NULL;
head = newNode;
Step 4: If the list is not empty then
struct Node *temp = head;
while(temp->data != loc1)
temp = temp->link;
newNode->link = temp->link;
temp->link = newNode;

15
b) List out the advantages and disadvantages of lined list?
Ans)
Advantages of Linked List-

1. Insertions and deletions can be done easily.


2. It does not need movement of elements for insertion and deletion.
3. In it space is not wasted as we can get space according to our requirements.
4. Its size is not fixed.
5. It can be extended or reduced according to requirements.
6. It is less expensive.

Disadvantages of Linked List-

1. It requires more space as pointers are also stored along with information.
2. Different amount of time is required to access each element.
3. If we have to go to a particular element then we have to go through all those elements that
come before that element.
4. It is not easy to sort the elements stored in the linked list.

16

Você também pode gostar