Vous êtes sur la page 1sur 12

Assignment

CSE2135

Question 2(a): Provide all functional definitions for a circular queue?


To implement a circular queue data structure using array, perform the following
steps before implement actual operations.
Create a one dimensional array with a defined size. int data[10]
Define two integer variables 'front' and 'rear' and initialize both with '-1'.
int front = -1, rear = -1;
add(value) - Inserting value into the Circular Queue
In a circular queue, add () is a function which is used to insert an element into the
circular queue. In a circular queue, the new element is always inserted
at rear position.
The add () function takes one integer value as parameter and inserts that value into
the circular queue.
Step 1: Check whether queue is FULL. ((rear == SIZE-1 && front == 0) || (front ==
rear+1))
Step 2: If it is FULL,"Queue is Overflow and terminate the function.
Step 3: If it is NOT FULL, then check rear == SIZE - 1 && front != 0 if it is TRUE,
then set rear = -1.
Step 4: Increment rear value by one (rear++), set data[rear] = value and check
'front == -1' if it is TRUE, then set front = 0.
void add(int value)
{
if((front == 0 && rear == SIZE - 1) || (front == rear+1))
printf("\n Circular Queue Overflow!\n");
else{
if(rear == SIZE-1 && front != 0)
rear = -1;
data[++rear] = value;
if(front == -1)
front = 0;
}
}

remove() - Deleting a value from the Circular Queue


In a circular queue, remove() is a function used to delete an element from the
circular queue. In a circular queue, the element is always deleted
from front position. The remove() function doesn't take any value as parameter.
Step 1: Check whether queue is EMPTY. (front == -1 && rear == -1)
Step 2: If it is EMPTY, then display "Queue Underflow" and terminate the function.
Step 3: If it is NOT EMPTY, then display data[front] as deleted element and
increment the front value by one (front ++). Then check whether front == SIZE, if it
is TRUE, then set front = 0. Then check whether both front - 1 and rear are equal
(front -1 == rear), if it TRUE, then set both front and rear to '-1' (front = rear = -1).
void remove()
{

Student Name: Fouzia Nigar Sultana


Student ID: 13-0-52-020-048

Assignment

CSE2135

if(front == -1 && rear == -1)


printf("\n Circular Queue is Underflow!\n");
else{
printf("\n Deleted element : %d\n",data[front++]);
if(front == SIZE)
front = 0;
if(front-1 == rear)
front = rear = -1;
}
}

Question 2(b): Write a functional to delete operation in dynamically allocated


queue?
Dynamically allocated queue can be defined as
Struct queue
{int info;
Struct queue *next;
} *front, *rear
where front and rear hold the pointer for front and rear of the queue. To delete an
element from the queue, it should check whether the queue is empty or not. For
that we can define function underflow
int underflow()
{
if
(front==NULL)&& (rear==NULL)
{
printf(\n Queue is underflow \n);
return 0;
}
else
{
return (1);
}
}
The procedure dequeue delete the first element of queue by disconnecting the old
header from the queue. The first element list becomes the new dummy header cell.
Fro
nt

rea

34

45

Student
Name: Fouzia Nigar Sultana
r
Student ID: 13-0-52-020-048

67

23

Assignment

CSE2135

Void dequeue()
{
if (front==rear)
{
front= NULL;
rear=NULL;
}
else
{

front=frontnext

}
}
Question 2(c): Define priority queue. Describe the one way list representation of a
priority queue with suitable example and diagram.
A priority queue is a collection of elements such that each element has been
assigned a priority and such that the order in which elements are deleted and
processed comes from the following rules:
1. An element of higher priority is processed before any element of lower priority.
2. Two elements with same priority are processed according to the order in which
they were added to the queue.
One-Way List Representation of a Priority Queue in memory:

Each node in the list will contain three items of information:


o an information Feld INFO,
o a priority number PRN, and
o a link number LINK .
A node X precedes a node Y in the list
o when X has higher priority than Y
o when both have same priority but X was added to the list before
Y.

Example of Priority Queue


The main property of the one-way list representation of a priority queue is that the
element in the queue should be processed first always appears at the beginning of
the one way list.
Deleting an element from the priority queue is easy but insertion of element is more
complicated process.
BB
C
B
AA
Student
Name:
1 Fouzia Nigar Sultana
Start
C
2
A
Student ID: 13-0-52-020-048
C

D
D
D

Assignment

E
E
E

CSE2135

Question
G 2(d): Write
X a program to transverse a binary tree in
FF post-order.
preorder,
and
G in-order
5
4
F
G

In-order, Pre-order and Post-order Tree Traversal


In-order traversal method:
1. Visit Left Sub-Tree
2. Process Current Node
3. Visit Right Sub-Tree

Pre-order traversal method:


1. Process Current Node
2. Visit Left Sub-Tree
3. Visit Right Sub-Tree

Post-order traversal method:


1. Visit Left Sub-Tree
2. Visit Right Sub-Tree
3. Process Current Node

#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
struct node
{
int value;
node* left;
node* right;
};
struct node* root;
struct node* insert(struct node* r, int data);
void inOrder(struct node* r);
void preOrder(struct node* r);
void postOrder(struct node* r);
Student Name: Fouzia Nigar Sultana
Student ID: 13-0-52-020-048

Assignment

CSE2135

int main()
{
root = NULL;
int n, v;
printf("\n Enter the number of element for Tree: ");
scanf("%d", &n);
for(int i=0; i<n; i++){
printf("\n Data %d: ", i+1);
scanf("%d", &v);
root = insert(root, v);
}
printf("\n Inorder Traversal: ");
inOrder(root);
printf("\n");
printf("\n Preorder Traversal: ");
preOrder(root);
printf("\n");
printf("\n Postorder Traversal: ");
postOrder(root);
printf("\n");
getch();
return 0;
}
struct node* insert(struct node* r, int data)
{
if(r==NULL)
{
r = (struct node*) malloc(sizeof(struct node));
r->value = data;
r->left = NULL;
r->right = NULL;
}
else if(data < r->value){
r->left = insert(r->left, data);
}
else {
r->right = insert(r->right, data);
}
return r;
}
void inOrder(struct node* r)
{
Student Name: Fouzia Nigar Sultana
Student ID: 13-0-52-020-048

Assignment

CSE2135

if(r!=NULL){
inOrder(r->left);
printf("%d ", r->value);
inOrder(r->right);
}

void preOrder(struct node* r)


{
if(r!=NULL){
printf("%d ", r->value);
preOrder(r->left);
preOrder(r->right);
}
}
void postOrder(struct node* r)
{
if(r!=NULL){
postOrder(r->left);
postOrder(r->right);
printf("%d ", r->value);
}
}

Student Name: Fouzia Nigar Sultana


Student ID: 13-0-52-020-048

Assignment

CSE2135

Question 3(a): Construct an adjacent matrix and adjacent list for the following
directed graph.

2
5
4

Adjacency Matrices
For a graph with |V| vertices, an adjacency matrix is a VV matrix of 0s and 1s,
where the entry in row i and column j is 1 if and only if the edge (i,j) is in the graph.
To indicate an edge weight, put it in the row i, column j entry, and reserve a special
value (perhaps null) to indicate an absent edge. The adjacency matrix for above
graph:
1
2
3
4
5

1
0
0
1
0
0

2
1
0
0
1
1

3
0
0
0
1
0

4
1
1
0
0
0

5
0
0
0
1
0

Adjacency Lists
Representing a graph with adjacency lists combines adjacency matrices with edge
lists. For each vertex i, store an array of the vertices adjacent to it. The adjacencylist representation of the above graph:
1
2 4
2

Student Name: Fouzia Nigar Sultana


Student ID: 13-0-52-020-048

Assignment

CSE2135

Question 3(b): Define a Spanning tree. Find the minimum spanning tree of the
following weighted graph.
A spanning tree T of an undirected graph G is a sub-graph that is a tree which
includes all of the vertices of G. So, given a graph G=(V,E), a sub-graph of G that is
connects all of the vertices and is a tree is called a spanning tree.
For example, graph G:
a

e
f

Remove edges until we are left with a tree: the result is a spanning tree.

e
f

Clearly, a spanning tree will have |V|1 edges, like any other tree.
Minimal Spanning Tree for the following weighted graph:

3
4

C
6

7
E

4
F

A minimum spanning tree in a connected weighted graph is a spanning tree that


has the smallest possible sum of weights of its edges.
Student Name: Fouzia Nigar Sultana
Student ID: 13-0-52-020-048

Assignment

CSE2135

B
3

4
6

D
4

Prims Algorithm An algorithm for finding a minimum spanning tree.


1. Begin by choosing any edge with smallest weight, putting it into the spanning
tree.
2. Successively add to the tree edges of minimum weight that are incident to a
vertex already in the tree, never forming a simple circuit with those edges
already in the tree.
3. Stop when n 1 edges have been added.
Choi
ce
1
2
3
4
5

Edg
e
{B,D
}
{D,F
}
{F,A
}
{A,E
}
{E,C
}

Weight
B

3
4

D
4

7
4

6
Total:
24

Kruskals Algorithm An algorithm for finding a minimum spanning tree.


1. Begin by choosing an edge in the graph with
Edg Weight
minimum weight.
e
2. Successively add edges with minimum weight that
{B,D
3
do not form a simple circuit with those edges
}
already chosen
2
{D,F
4
3. Stop after n 1 edges have been selected.
}
3
{A,E
4
B
A
}
4
{E,C
6
Student Name:
Fouzia
Nigar Sultana
9
}
C
4
7
Student ID: 13-0-52-020-048
5
{F,A
7
}
6
E
Total:
F
24
Choi
ce
1

3
D
4

Assignment

CSE2135

Question 3(c): Write a program for depth first and breadth first search algorithm.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define MAX 100
#define initial 1
#define waiting 2
#define visited 3
int n;
int adj[MAX][MAX];
int state[MAX];
void create_graph();
void BF_Traversal();
void BFS(int v);
int queue[MAX], front = -1,rear = -1;
void insert_queue(int vertex);
int delete_queue();
int isEmpty_queue();
int main()
{
create_graph();
BF_Traversal();
getch();
return 0;
}
void BF_Traversal()
{
int v;
for(v=0; v<n; v++)
state[v] = initial;
printf("\n Enter Start Vertex for BFS: ");
scanf("%d", &v);
BFS(v);
}
void BFS(int v)
{
int i;
insert_queue(v);
state[v] = waiting;
while(!isEmpty_queue())
Student Name: Fouzia Nigar Sultana
Student ID: 13-0-52-020-048

10

Assignment
{

CSE2135

v = delete_queue( );
printf(" %d ",v);
state[v] = visited;
for(i=0; i<n; i++)
{
if(adj[v][i] == 1 && state[i] == initial)
{
insert_queue(i);
state[i] = waiting;
}
}

}
printf("\n");
}
void insert_queue(int vertex)
{
if(rear == MAX-1)
printf("Queue Overflow\n");
else
{
if(front == -1)
front = 0;
rear = rear+1;
queue[rear] = vertex ;
}
}
int isEmpty_queue()
{
if(front == -1 || front > rear)
return 1;
else
return 0;
}
int delete_queue()
{
int delete_item;
if(front == -1 || front > rear)
{
printf("Queue Underflow\n");
exit(1);
}

delete_item = queue[front];
front = front+1;
return delete_item;

Student Name: Fouzia Nigar Sultana


Student ID: 13-0-52-020-048

11

Assignment

CSE2135

void create_graph()
{
int count,max_edge,origin,destin;
printf("\n Enter number of vertices : ");
scanf("%d",&n);
max_edge = n*(n-1);
for(count=1; count<=max_edge; count++)
{
printf("\n Enter edge %d( -1 -1 to quit ) : ",count);
scanf("%d %d",&origin,&destin);
if((origin == -1) && (destin == -1))
break;

if(origin>n || destin>n || origin<0 || destin<0)


{
printf("\n Invalid edge!\n");
count--;
}
else
{
adj[origin][destin] = 1;
}

Student Name: Fouzia Nigar Sultana


Student ID: 13-0-52-020-048

12

Vous aimerez peut-être aussi