Académique Documents
Professionnel Documents
Culture Documents
CSE2135
Assignment
CSE2135
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:
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
#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);
}
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
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
Assignment
CSE2135
B
3
4
6
D
4
Edg
e
{B,D
}
{D,F
}
{F,A
}
{A,E
}
{E,C
}
Weight
B
3
4
D
4
7
4
6
Total:
24
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;
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;
12