Sei sulla pagina 1di 11

Kuvempu University

Assignment 1 for B.Sc. (IT) & M.Sc. (IT) Courses


(Academic Year 2005 - II Cycle)

Subject: Algorithms
Subject Code: BSIT - 41

Assignment: TA (Compulsory)

1. Bring out the importance of algorithms in the field of computer science.

Algorithm plays an important role in computer programming. Programming involves


various activities from the stage of conceiving the problem up to the stage of creating a
model to solve the problem. The formal representation of this model as a sequence of
instructions is called as an algorithm and coded algorithm in a specific computer
language is called a program. By writing algorithms a computer application can be
developed which produces the desired result and the correct output from the given
input.

2. List out the characteristics of an algorithm.

There are five different properties of Algorithm:


• Input
• Output
• Definiteness
• Effectiveness
• Termination

3. What is validation of an algorithm?

Once an algorithm has been devised, it becomes necessary to show how it works. And
if we require correct answer to all possible, legal inputs the simple way to get results is
code it into a program but converting an algorithm into program is a time consuming
process. Hence, it is essential to be reasonably sure about the effectiveness of
algorithm before it is coded and at the algorithm level this process is called
“Validation”.

4. What does algorithm-profiling mean?

Algorithm profiling or performance measurement is the process of executing a


program on different datasets to measure the time and the space that it takes to
compute the results.

5. Find out the address of the element A (4,5,2) where A is a 3-dimensional array with
subscript limits 1≤ i ≤6, 1≤ j ≤7 and 0≤ k ≤4 when A is in row major addressing. Assume
base address is 1000 and word size is 2.

A=B+{∑ (ij-lj) Pj} w


J=1

© NIIT Page 1 of 11
n

Where Pj=∏ (uk-lk +1)


K=j+1

6. List out at least 5 real life instances where stack operations are being used.

a) Books of Library
b) Set of Cards
c) Set of CDs
d) Set of Tapes
e) Dinning Plates

7. List out at least 5 real life instances where queue and circular queue operations are
being used.

Queue Operations: 1. Ticket line


2. Traffic
3. Audience in a Theatre
4. Customer at Petrol Pumps
5. Hotels

Circular Operations: 1. Menu of Mobiles


2. F1 Race
3. Call center Shift
4. A Network
5. Planets in Universe

8. Bring out the advantages of circular queue over linear queue.

In circular queue you can come directly to the front from rear where in linear queue it is
not possible so it was the main advantage of circular queue, which saves time.

9. List out the differences between linear data structures and non-linear data structures.

Major difference between linear data structure and non – linear data structure is as
shown as below :
• In linear data structure every data element has got exactly two neighbors
or two adjacent elements where in non – linear data structure data
elements are allowed to have more than two adjacent elements.

10. Design an algorithm to check if a given graph is connected.

© NIIT Page 2 of 11
11. What are the properties of a tree?

Properties of a tree are:


1) There is one and only one path between every pair of vertices in a tree, T.
2) A tree with n vertices has n-1 edges.
3) Any connected graph with n vertices and n-1 edges is a tree.
4) A graph is a tree if and only if it is minimally connected.
5) A tree is connected graph without any circuits.
6) It doesn’t have any self-loops.
7) It doesn’t have any parallel edges as they from circuits.

12. Sketch all binary trees with six pendent vertices.

13. List out 10 situations that can be represented by means of graphs. Explain what each
vertex and edge represent.

Graphs can be used when tracing out routes between places. Each vertex represents
the place and the edge represents the various routes that can be taken between the
places.

It can be used in 3D graphics of objects where the vertex represents the corners of the
object and the edge represents the distance between these points.

© NIIT Page 3 of 11
Kuvempu University

Laboratory Assignment 1
(Academic Year 2005 - II Cycle)

Subject: Algorithms
Subject Code: BSIT - 41

Assignment: TA (Compulsory)

1. Implement the algorithm for computing the row major address of the element A(i1, i2, i3,
…, in) where A is a n-dimensional matrix with subscript limits l1≤ i1 ≤ u1, l2≤ i2 ≤ u2, l3≤ i3 ≤
u3 …. ln≤ in ≤ un. Assume base address, as B and word size is w.

Algorithm: Address Computation

Input:
1. n, dimensions
2. u1, u2,u3…un n upper limits.
3. l1,l2,l3…ln n lower limits.
4. w, word size
5. B, base address

Output: ‘A’ address of the element at (i1, i2, i3… in)

Method:
N

A=B+{ ∑(ij-lj)Pj}w
n

Where Pj=∏(uk-lk +1)


K=j+1
Algorithm Ends.

2. Implement stack operations.

Stack implementation as a class

# include<iostream.h>
# include<process.h>
# include<conio.h>
# define SIZE 20

class stack
{
int a[SIZE];
int tos; // Top of Stack
© NIIT Page 4 of 11
public:
stack();
void push(int);
int pop();
int isempty();
int isfull();
};
stack::stack()
{
tos=0; //Initialize Top of Stack
}
int stack::isempty()
{
return (tos==0?1:0);
}
Int stack::isfull()
{
return (tos==SIZE?1:0);
}
void stack::push(int i)
{
if(!isfull())
{
a[tos]=i;
tos++;
}
else
{
cerr<<"Stack overflow error !
Possible Data Loss !";
}
}
int stack::pop()
{
if(!isempty())
{
return(a[--tos]);
}
else
{
cerr<<"Stack is empty! What to pop...!";
}
return 0;
}
void main()
{
stack s;
int ch=1,num;
while(ch!=0)
{
cout<<"Stack Operations Main Menu 1.Push 2.Pop 3.IsEmpty
4.IsFull 0.Exit";
cin>>ch;
switch(ch)
{
case 0:
exit(1); //Normal Termination of Program
© NIIT Page 5 of 11
case 1:
cout<<"Enter the number to push";
cin>>num;
s.push(num);
break;
case 2:
cout<<"Number popped from the stack is: "<<s.pop()<<endl;
break;
case 3:
(s.isempty())?(cout<<"Stack is empty."):(cout<<"Stack is not
empty.");
break;
case 4:
(s.isfull())?(cout<<"Stack is full."):(cout<<"Stack is not
full.");
break;
default:
cout<<"Illegal Option. Please try again";
}
}//end of while
getch();
}

3. Implement the linear queue and circular queue operations.

Implementing Linear Queue

#include <dos.h>
#include <stdlib.h>
#include <iostream.h>
#include <conio.h>
#include <windows.h>
#define MAX 5 // MAXIMUM CONTENTS IN QUEUE

class task
{
public:
virtual void dotask(){}
task(){}
int exists;
};
class note: public task
{
public:
notep(){exists=1;}
void dotask()
{
system("notepad");
}
};
class regt:public task
{
public:
regt(){exists=1;}
void dotask()
{
© NIIT Page 6 of 11
system("regedit");
}
};

class winex:public task


{
public:
winex(){exists=1;}
void dotask()
{
system("explorer");
}
};
class Bep:public task
{
public:
Bep(){exists=1;}
void dotask()
{
cout<<"a";
}
};
class MsBox:public task
{
private:
char* text;
char* caption;
int style;
public:
MsBox(char* ext,char* cap,int no)
{
text=ext;
caption=cap;
exists=1;
style=no;
}
void dotask()
{
MessageBox(0,text,caption,style);
}
};
class queue
{
private:
task *t[MAX];
int al;
int dl;
public:
int opt,opt1,a;
char te[255],capt[40];
queue()
{
dl=-1;
al=-1;
}
void del()
{
© NIIT Page 7 of 11
task* tmp;
if(dl==-1)
{
cout<<"Queue is Empty";
sleep(2);
}
else
{
t[dl]->exists=0;
for(int j=0;j<=al;j++)
{
if((j+1)<=al)
{
tmp=t[j+1];
t[j]=tmp;
}
else
{
t[al]->exists=0;
al--;
if(al==-1)
dl=-1;
else
dl=0;
}
}
}
}
void menu()
{
clrscr();
cout<<"1) Add Task 2)Execute Tasks 3)Exit Program ";
// int opt;
cin>>opt;
switch(opt)
{
case 1:
clrscr();
cout<<"1) Open Notepad";
cout<<"2) Open Explorer";
cout<<"3) Open Registry";
cout<<"4) Sound a Beep";
cout<<"5) MessageBox API";
cout<<"6) Back";
cin>>opt1;
if(opt1!=6)
add(opt1);
break;
case 2:
if(al!=-1 && dl!=-1)
{
for(int k=0;k<=al;k++)
{
if(t[k]->exists==1)
t[k]->dotask();
t[k]->exists=0;
}
© NIIT Page 8 of 11
al=dl=-1;
}
else
{
cout<<"Queue is Empty";
sleep(3);
}
break;
case 3:
exit(0);
break;
}
}
void add(int item)
{
if(dl==-1 && al==-1)
{
dl++;
al++;
}
else
{
al++;
if(al<MAX){}
else
{
cout<<"Queue is Full";
al--;
sleep(3);
return;
}
}
switch(item)
{
case 1:
t[al]=new notep;
break;

case 2:
t[al]=new winex;
break;

case 3:
t[al]=new regt;
break;

case 4:
t[al]=new Bep;
break;

case 5:
cout<<"Enter Style Number:";
cin>>a;
t[al]=new MsBox("Task Performed.”,” Queue Implementation”, a);
break;

default:
© NIIT Page 9 of 11
cout<<"Programming Error"; // No Possibility of this executing
};
}

};
void main()
{
queue a;
while(1)
{
a. menu();
}
}

--------------------------------------------------------------------------------------------------------------------
Implementing Circular Queue

# include<iostream.h>
# include<conio.h>
# define SIZE 20;

class queue
{
int a[SIZE];
int front;
int rear;
public:
queue();
~queue();
void insert(int i);
int remove();
int isempty();
int isfull();
};
queue::queue()
{
front=0;
rear=0;
}
queue::~queue()
{
delete []a;
}
void queue::insert(int i)
{
if(isfull())
{
cout<<"******Queue is FULL !!!No insertion allowed
further.******";
return;
}
a[rear] = i;
© NIIT Page 10 of 11
rear++;
}
int queue::remove()
{
if(isempty())
{
cout<<"******Queue Empty !!!Value returned will be garbage.
******";
return (-9999);
}
return(a[front++]);
}
int queue::isempty()
{
if(front == rear)
return 1;
else
return 0;
}
int queue::isfull()
{
if(rear == SIZE)
return 1;
else
return 0;
}
void main()
{
clrscr();
queue q;
q.insert(1);
q.insert(2);
cout<<""<<q.remove();
cout<<""<<q.remove();
cout<<""<<q.remove();
getch();
}

4. Implement the algorithm to find if a given graph is connected.

© NIIT Page 11 of 11

Potrebbero piacerti anche