Dsa Lab File Exp 15 DSF Till Further
Dsa Lab File Exp 15 DSF Till Further
#include <stdlib.h> S Z
struct Node {
int vertex;
};
struct AdjList {
};
struct Graph {
int vertices;
};
newNode->vertex = vertex;
newNode->next = NULL;
return newNode;
}
// Function to create a graph with a given number of vertices
graph->vertices = vertices;
graph->array[i].head = NULL;
return graph;
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;
/*
newNode = createNode(src);
newNode->next = graph->array[dest].head;
graph->array[dest].head = newNode;
*/
visited[i] = 0;
int queue[graph->vertices];
queue[++rear] = startVertex;
visited[startVertex] = 1;
while (temp) {
if (!visited[adjacentVertex]) {
queue[++rear] = adjacentVertex;
visited[adjacentVertex] = 1;
temp = temp->next;
free(visited);
}
// Driver program
int main() {
scanf("%d", &vertices);
scanf("%d", &edges);
int startVertex;
scanf("%d", &startVertex);
BFS(graph, startVertex);
return 0;
}
Impimenting dfs using graph
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
struct Graph {
int numVertices;
int visited[MAX_VERTICES];
};
newNode->next = NULL;
return newNode;
graph->numVertices = vertices;
graph->adjList[i] = NULL;
graph->visited[i] = 0;
return graph;
newNode->next = graph->adjList[src];
graph->adjList[src] = newNode;
newNode = createNode(src);
newNode->next = graph->adjList[dest];
graph->adjList[dest] = newNode;
graph->visited[vertex] = 1;
if (graph->visited[adjVertex] == 0) {
DFS(graph, adjVertex);
temp = temp->next;
int main() {
// Example usage
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 3);
addEdge(graph, 2, 4);
DFS(graph, 0);
return 0;
}
Experiment 16 prims kruskal's MST
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
struct Edge {
};
struct Graph {
int numVertices;
int adjMatrix[MAX_VERTICES][MAX_VERTICES];
};
// Function to create a new graph
graph->numVertices = vertices;
graph->adjMatrix[i][j] = 0;
return graph;
void addEdge(struct Graph* graph, int src, int dest, int weight) {
graph->adjMatrix[src][dest] = weight;
graph->adjMatrix[dest][src] = weight;
min = key[v];
minIndex = v;
}
}
return minIndex;
int parent[MAX_VERTICES];
int key[MAX_VERTICES];
int mstSet[MAX_VERTICES];
key[i] = INT_MAX;
mstSet[i] = 0;
key[0] = 0;
parent[0] = -1;
mstSet[u] = 1;
for (int v = 0; v < graph->numVertices; v++) {
parent[v] = u;
key[v] = graph->adjMatrix[u][v];
printMSTPrim(graph, parent);
if (parent[i] == -1)
return i;
parent[xSet] = ySet;
int v = graph->numVertices;
int e = 0;
int i = 0;
if (graph->adjMatrix[row][col] != 0) {
edges[i].src = row;
edges[i].dest = col;
edges[i].weight = graph->adjMatrix[row][col];
i++;
int parent[v];
for (int j = 0; j < v; j++) {
parent[j] = -1;
if (x != y) {
result[e++] = nextEdge;
unionSets(parent, x, y);
printMSTKruskal(result, e);
int main() {
// Example usage
int vertices = 4;
addEdge(graph, 0, 1, 10);
addEdge(graph, 0, 2, 6);
addEdge(graph, 0, 3, 5);
addEdge(graph, 1, 3, 15);
addEdge(graph, 2, 3, 4);
// Find and print Minimum Spanning Tree using Prim's algorithm
primMST(graph);
kruskalMST(graph);
return 0;
}
Experiment 17
Implementing shortest path algorithms to
compare single source shortest path and all pairs
shortest path
#include <stdio.h>
#include <limits.h>
// from the set of vertices not yet included in the shortest path tree
min = dist[v];
min_index = v;
return min_index;
int dist[V]; // The output array dist[i] holds the shortest distance from src to i
int sptSet[V]; // sptSet[i] will be true if vertex i is included in the shortest path tree or the shortest
distance from src to i is finalized
dist[i] = INT_MAX;
sptSet[i] = 0;
dist[src] = 0;
// Pick the minimum distance vertex from the set of vertices not yet processed.
sptSet[u] = 1;
// and the total weight of path from src to v through u is less than the current value of dist[v]
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v])
printSolution(dist, V);
int dist[V][V];
dist[i][j] = graph[i][j];
}
}
if (dist[i][j] == INT_MAX)
printf("INF\t");
else
printf("%d\t", dist[i][j]);
printf("\n");
int main() {
{INT_MAX, 0, 3, INT_MAX},
dijkstra(graph, 0);
floydWarshall(graph);
return 0;
}
Experiment 18
Implementing traversal in threaded binary system
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
if (newNode == NULL) {
exit(EXIT_FAILURE);
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
if (root) {
threadedInorder(root->left, prev);
(*prev)->right = root;
(*prev)->isThreaded = 1;
*prev = root;
if (!root->isThreaded) {
threadedInorder(root->right, prev);
while (current) {
while (current->left) {
current = current->left;
while (current->isThreaded) {
current = current->right;
current = current->right;
// Driver program
int main() {
// Construct a threaded binary tree
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
root->right->left = createNode(6);
root->right->right = createNode(7);
threadedInorder(root, &prev);
threadedInorderTraversal(root);
printf("\n");
return 0;
}
Experiment 19
Implimenting huffman coding using
binary tree
// C program for Huffman Coding
#include <stdio.h>
#include <stdlib.h>
struct MinHeapNode {
char data;
unsigned freq;
struct MinHeap {
unsigned size;
unsigned capacity;
return temp;
}
minHeap->size = 0;
minHeap->capacity = capacity;
if (smallest != idx) {
swapMinHeapNode(&minHeap->array[smallest],
&minHeap->array[idx]);
minHeapify(minHeap, smallest);
}
}
{
struct MinHeapNode* temp = minHeap->array[0];
minHeap->array[0] = minHeap->array[minHeap->size - 1];
--minHeap->size;
minHeapify(minHeap, 0);
return temp;
}
++minHeap->size;
int i = minHeap->size - 1;
while (i
&& minHeapNode->freq
< minHeap->array[(i - 1) / 2]->freq) {
minHeap->array[i] = minHeapNode;
}
int n = minHeap->size - 1;
int i;
printf("\n");
}
{
return !(root->left) && !(root->right);
}
minHeap->size = size;
buildMinHeap(minHeap);
return minHeap;
}
{
struct MinHeapNode *left, *right, *top;
struct MinHeap* minHeap
= createAndBuildMinHeap(data, freq, size);
while (!isSizeOne(minHeap)) {
left = extractMin(minHeap);
right = extractMin(minHeap);
top->left = left;
top->right = right;
insertMinHeap(minHeap, top);
}
return extractMin(minHeap);
}
if (root->left) {
arr[top] = 0;
printCodes(root->left, arr, top + 1);
}
if (root->right) {
arr[top] = 1;
printCodes(root->right, arr, top + 1);
}
if (isLeaf(root)) {
{
struct MinHeapNode* root
= buildHuffmanTree(data, freq, size);
int main()
{
printf ("huffman codeing using binary tree ");
return 0;
}
Experiment 20
Implimenting hanaoi tree using recurssion to
compare interactive and recursive methods
#include <stdio.h>
if (n == 1) {
return;
}
hanoiRecursive(n - 1, source, destination, auxiliary);
int move;
scanf("%d", &move);
int main() {
int n;
scanf("%d", &n);
// Recursive method
printf("\nRecursive method:\n");
// Interactive method
printf("\nInteractive method:\n");
return 0;