Exp-3 & 4
Exp-3 & 4
Construct Min and Max Heap using arrays, delete any element and
display the content of the Heap.
#include <stdio.h>
#include <stdlib.h>
if (smallest != i) {
swap(&heap[i], &heap[smallest]);
minHeapify(heap, n, smallest);
}
}
buildMinHeap(minHeap, n);
buildMaxHeap(maxHeap, n);
displayMinHeap(minHeap, n);
displayMaxHeap(maxHeap, n);
displayMinHeap(minHeap, n);
displayMaxHeap(maxHeap, n);
return 0;
}
Explanation:
1. Min Heap Construction:
o minHeapify() maintains the heap property for a subtree rooted at
index i.
o buildMinHeap() builds the heap from the array by calling
minHeapify() for non-leaf nodes.
o deleteMinElement() finds and deletes an element from the min heap,
rebuilds it to maintain the heap structure.
2. Max Heap Construction:
o maxHeapify() maintains the heap property for a subtree rooted at
heaps.
Example Output:
#include <stdio.h>
int main() {
int n, start;
int adjMatrix[MAX][MAX];
return 0;
}
Explanation:
1. BFT Algorithm:
o Start from the start vertex.
o Enqueue the starting vertex, mark it as visited.
o Dequeue a vertex from the front of the queue.
o Explore all the adjacent vertices of the dequeued vertex that have not
been visited, mark them as visited, and enqueue them.
o Repeat the process until the queue is empty.
2. Queue Operations:
o enqueue(): Adds a vertex to the rear of the queue.
o dequeue(): Removes a vertex from the front of the queue.
o isEmptyQueue(): Checks whether the queue is empty.
3. Adjacency Matrix:
o adjMatrix[i][j] == 1 indicates an edge between vertex i and vertex j.
o If a vertex is found unvisited and there is an edge, it is enqueued.
Example Output:
In this example:
The input matrix represents a graph with 4 vertices, and there is an edge
between vertices 0-1, 0-2, and 1-3, 2-3.
Starting from vertex 0, the traversal order is: 0 -> 1 -> 2 -> 3.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int vertex;
};
int numVertices;
};
int stack[MAX];
void push(int v) {
if (top == MAX - 1) {
printf("Stack Overflow\n");
return;
stack[++top] = v;
int pop() {
if (top == -1) {
printf("Stack Underflow\n");
return -1;
}
return stack[top--];
int isEmptyStack() {
newNode->vertex = v;
newNode->next = NULL;
return newNode;
graph->numVertices = vertices;
graph->adjLists[i] = NULL;
graph->visited[i] = 0;
return graph;
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
graph->visited[start] = 1;
while (!isEmptyStack()) {
int v = pop();
while (temp) {
if (!graph->visited[adjVertex]) {
push(adjVertex);
graph->visited[adjVertex] = 1;
temp = temp->next;
}
printf("\n");
int main() {
scanf("%d", &vertices);
scanf("%d", &edges);
scanf("%d", &start);
// Perform Depth-First Traversal from the starting vertex
DFT_AdjList(graph, start);
return 0;
Explanation:
1. Graph Representation:
o Adjacency List: Each vertex has a linked list of adjacent vertices.
o A Node structure is used to represent each vertex in the adjacency list.
2. DFT Algorithm:
o Stack: DFT is implemented using a stack to explore vertices.
o Start from the given start vertex.
o Push the starting vertex onto the stack, mark it as visited.
o Pop a vertex from the stack, and print it.
o For each adjacent vertex that has not been visited, push it onto the
stack and mark it as visited.
o Repeat until the stack is empty.
3. Graph Creation:
o createGraph(): Initializes the graph with a specified number of
vertices.
o addEdge(): Adds an edge between two vertices in the adjacency list
for an undirected graph.
4. Stack Operations:
o push(): Adds a vertex to the top of the stack.
o pop(): Removes the top vertex from the stack.
o isEmptyStack(): Checks whether the stack is empty.
Example Output:
In this example: