Ocansey-0401200048 - Algorith and Structure
Ocansey-0401200048 - Algorith and Structure
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;
}
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 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)