0% found this document useful (0 votes)
23 views11 pages

Ocansey-0401200048 - Algorith and Structure

1. The document contains an end of semester examination for a student taking an Algorithms and Data Structures course. It includes 4 questions on topics like graphs, binary heaps, queues, and Kruskal's algorithm. 2. Question 1 involves adjacency lists and matrices for a graph, and performing depth first search. Question 2 defines binary heaps and how they can implement priority queues. 3. Question 3 provides C code for basic queue operations like insert, delete, update and display. Question 4 involves representing graphs visually and applying Kruskal's minimum spanning tree algorithm.

Uploaded by

jenicemorgan79
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views11 pages

Ocansey-0401200048 - Algorith and Structure

1. The document contains an end of semester examination for a student taking an Algorithms and Data Structures course. It includes 4 questions on topics like graphs, binary heaps, queues, and Kruskal's algorithm. 2. Question 1 involves adjacency lists and matrices for a graph, and performing depth first search. Question 2 defines binary heaps and how they can implement priority queues. 3. Question 3 provides C code for basic queue operations like insert, delete, update and display. Question 4 involves representing graphs visually and applying Kruskal's minimum spanning tree algorithm.

Uploaded by

jenicemorgan79
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

END OF SEMESTER EXAMINATION, 2020/2021

NAME: OCANSEY EVANS


INDEX NUMBER: 0401200048
COURSE TITLE: ALGORITHMS AND DATA STRUCTURES
COURSE CODE: CICS214
SESSION: MORNING
LEVEL: 200
QUESTION 1
I. Adjacent list
Adjacency Matrix:-

A B C D E F

A 1 1 1 0 0 1

B 1 1 1 0 1 0

C 1 1 1 0 0 0

D 0 0 1 0 1 1

E 0 1 0 1 1 1

F 1 0 1 1 1 0
(b)
Performing depth first search taking node C as vertex.
DFS Sequence = CBAFED
Depth First Search Tree
Question 2
Binary heap is a binary tree (tree which have at most two child) with some constraint
1. It must be complete binary tree (means each node except the nodes at last level are fully filled
and
last level keys are as left as possible), this way of storing is very helpful in implementing binary
heap
by array.
2. The binary heap must follow an order which is also known as heap order, i.e. min heap or max
heap.
Heap order property is a way of ordering binary heap such that they follow an order, there are
two
types of order, min heap and max heap, in min heap parent nodes are always less that its child
node
and in max heap, parent node are always greater than its child nodes.
B. binary heap as effective implementation for priority queue,
in priority queue each items have some associated priority, and highest priority item are visited
first
than lowest priority item, and binary heap data structure is designed in such a way that it can
follow the
property of property of priority queue, in max heap each maximum node have priority that they
were
executed first compared to lowest node, thus binary heap provides implementation of priority
queue by
implementing max priority queue, similarly for min binary heap.
QUESTION 3

#include <stdio.h>

#define MAX 6
int queue[MAX];
int X=-1;
int Y=-1;

void insert(int ele)


{
if(X==-1 && Y==-1)
{
X = 0;
Y = 0;
queue[Y] = ele;
}
else if((Y+1) % MAX == X)
{
printf("Queue is overflow..");

}
else
{
Y = (Y + 1) % MAX;
queue[Y] = ele;
}
}

int delete()
{
if((X == -1) && (Y == -1))
{
printf("\nQueue is underflow..");
}
else if(X == Y)
{
printf("\nThe deleted element is %d\n", queue[X]);
X = -1;
Y = -1;
}
else
{
printf("\nThe deleted element is %d\n", queue[X]);
X = (X + 1) % MAX;
}
}

void update(int elem)


{
if((X == -1) && (Y == -1))
{
printf("\nQueue is underflow..");
}
else if(X == Y)
{
printf("\nThe changing element is %d\n", queue[X]);
X = -1;
Y = -1;
}
else
{
printf("\nThe changing element is %d\n", queue[X]);
X = (X + 1) % MAX;
}
if(X==-1 && Y==-1)
{
X = 0;
Y = 0;
queue[Y] = elem;
}
else if((Y+1) % MAX == X)
{
printf("Queue is overflow..");
}
else
{
Y = (Y + 1) % MAX;
queue[Y] = elem;
}

void display()
{
int i = X;
if(X == -1 && Y == -1)
{
printf("\n Queue is empty..");
}
else
{
printf("\nElements in a Queue are :");
while(i <= Y)
{
printf("%d,", queue[i]);
i = (i + 1) % MAX;
}
}
}

int main()
{
int choice = 1, e, c;
while(choice < 4 && choice != 0)
{
printf("1. Insert an queue");
printf("\n2: Delete an queue");
printf("\n3: change the queue");
printf("\n4: Display the queue");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter the inserting element: ");
scanf("%d", &e);
printf("\n");
insert(e);
break;
case 2:
delete();
printf("\n");
break;
case 3:
printf("Enter the New element: ");
scanf("%d", &c);
update(e);
break;
case 4:
display();
}
}
return 0;
}
Question 4
a.
G E

J
F

D
7

A 3 C
2 G

6
4 H
B

EDGES
(A, C) (C, J) (C, D) (D,B (B, H) (H, I) (H,G) (H,F) (F,E) AC J D HI G F E
E

b.
F
G
J

G
D

A H

B I

Kruskal’s = HIDBACJGEF
Edges
(H, I) (D, b (A, C) (B, H) (J, C) (H, G) (C, D) (J, E) (C, B (E, F)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy