0% found this document useful (0 votes)
37 views18 pages

Cascasc

Uploaded by

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

Cascasc

Uploaded by

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

Question – 1: Write a program to implement

Breadth First Search (BFS) graph traversal


methods.
Code:

#include <stdio.h>
#include <stdlib.h>
#define SIZE 100
// Queue structure for BFS
struct Queue {
int items[SIZE];
int front;
int rear;
};
// Graph structure
struct Graph {
int numVertices;
int** adjMatrix;
int* visited;
};
// Function to create a queue
struct Queue* createQueue() {
struct Queue* q = (struct Queue*)malloc(sizeof(struct Queue));
q->front = -1;
q->rear = -1;
return q;
}
// Check if the queue is empty
int isEmpty(struct Queue* q) {
if (q->rear == -1)
return 1;
else
return 0;
}
// Enqueue function
void enqueue(struct Queue* q, int value) {
if (q->rear == SIZE - 1)
printf("\nQueue is full");
else {
if (q->front == -1)
q->front = 0;
q->rear++;
q->items[q->rear] = value;
}
}
// Dequeue function
int dequeue(struct Queue* q) {
int item;
if (isEmpty(q)) {
printf("Queue is empty");
item = -1;
} else {
item = q->items[q->front];
q->front++;
if (q->front > q->rear) {
q->front = q->rear = -1;
}
}
return item;
}
// Create a graph with n vertices
struct Graph* createGraph(int vertices) {
struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
graph->numVertices = vertices;
graph->adjMatrix = (int**)malloc(vertices * sizeof(int*));
for (int i = 0; i < vertices; i++) {
graph->adjMatrix[i] = (int*)malloc(vertices * sizeof(int));
}
graph->visited = (int*)malloc(vertices * sizeof(int));
for (int i = 0; i < vertices; i++) {
graph->visited[i] = 0;
}
return graph;
}
// Perform BFS traversal
void bfs(struct Graph* graph, int startVertex) {
struct Queue* q = createQueue();
graph->visited[startVertex] = 1;
enqueue(q, startVertex);
while (!isEmpty(q)) {
int currentVertex = dequeue(q);
printf("%d ", currentVertex);
for (int i = 0; i < graph->numVertices; i++) {
if (graph->adjMatrix[currentVertex][i] == 1 && graph->visited[i]
== 0) {
graph->visited[i] = 1;
enqueue(q, i);
}
}
}
printf("\n");
}
int main() {
int vertices;
printf("Enter no of vertex\n");
scanf("%d", &vertices);
struct Graph* graph = createGraph(vertices);
printf("Enter %d vertices into matrix\n", vertices);
for (int i = 0; i < vertices; i++) {
for (int j = 0; j < vertices; j++) {
scanf("%d", &graph->adjMatrix[i][j]);
}
}
int startVertex;
printf("Enter starting vertex\n");
scanf("%d", &startVertex);
bfs(graph, startVertex);
return 0;
}

Question – 2: Write a program to implement


Depth First Search (DFS) graph traversal
methods.
Code:
#include <stdio.h>
#define MAX_VERTICES 100
int adjMatrix[MAX_VERTICES][MAX_VERTICES];
int visited[MAX_VERTICES];
void dfs(int vertex, int numVertices) {
printf("%d-> ", vertex + 1);
visited[vertex] = 1;
for (int i = 0; i < numVertices; i++) {
if (adjMatrix[vertex][i] == 1 && !visited[i]) {
dfs(i, numVertices);
}
}
}
int main() {
int numVertices, startVertex;
printf("Enter number of vertices\n");
scanf("%d", &numVertices);
printf("enter %d elements into adjcency matrix\n", numVertices *
numVertices); // Fixed spelling
for (int i = 0; i < numVertices; i++) {
for (int j = 0; j < numVertices; j++) {
scanf("%d", &adjMatrix[i][j]);
}
}
printf("enter Start vertex\n");
scanf("%d", &startVertex);
startVertex--; // Adjust to zero-based index
for (int i = 0; i < numVertices; i++) {
visited[i] = 0;
}
dfs(startVertex, numVertices);
return 0;
}

Question – 3: Write a program to find the


minimum spanning tree to implement Kruskal's
algorithm using greedy method.
Code:

#include <stdio.h>
#include <stdlib.h>
#define MAX 100
typedef struct {
int u, v, weight;
} Edge;
Edge edges[MAX];
int parent[MAX];
int rank[MAX];
int edgeCount, vertexCount;
void init() {
for (int i = 0; i < vertexCount; i++) {
parent[i] = i;
rank[i] = 0;
}
}
int find(int i) {
if (parent[i] != i) {
parent[i] = find(parent[i]);
}
return parent[i];
}
void unionSets(int u, int v) {
int rootU = find(u);
int rootV = find(v);
if (rootU != rootV) {
if (rank[rootU] > rank[rootV]) {
parent[rootV] = rootU;
} else if (rank[rootU] < rank[rootV]) {
parent[rootU] = rootV;
} else {
parent[rootV] = rootU;
rank[rootU]++;
}
}
}
int compare(const void* a, const void* b) {
Edge* edgeA = (Edge*)a;
Edge* edgeB = (Edge*)b;
return edgeA->weight - edgeB->weight;
}
void kruskal() {
int mstWeight = 0;
printf("Minimum Spanning Tree Edges:\n");
for (int i = 0; i < edgeCount; i++) {
Edge edge = edges[i];
if (find(edge.u) != find(edge.v)) {
printf("%d - %d : %d\n", edge.u, edge.v, edge.weight);
mstWeight += edge.weight;
unionSets(edge.u, edge.v);
}
}
printf("Spanning tree cost: %d\n", mstWeight);
}
int main() {
// Hardcoded graph input
vertexCount = 6; // Number of vertices
edgeCount = 9; // Number of edges
// Hardcoded edges: (u, v, weight)
edges[0] = (Edge){1, 0, 4};
edges[1] = (Edge){2, 1, 2};
edges[2] = (Edge){5, 2, 2};
edges[3] = (Edge){3, 2, 3};
edges[4] = (Edge){4, 3, 3};
edges[5] = (Edge){1, 0, 4}; // Additional edges can be added here
edges[6] = (Edge){0, 1, 6};
edges[7] = (Edge){0, 2, 7};
edges[8] = (Edge){1, 3, 5};
init();
// Sort edges by weight
qsort(edges, edgeCount, sizeof(edges[0]), compare);
kruskal();
return 0;
}

Question – 4: Write a program to find the


minimum spanning tree to implement prim’s
algorithm using greedy method.
Code:
#include <stdio.h>
#include <limits.h>
#include <stdbool.h>
#define MAX 100
int graph[MAX][MAX] = {
{0, 9, 0, 0, 0},
{9, 0, 0, 19, 0},
{0, 0, 0, 51, 0},
{0, 19, 51, 0, 31},
{0, 0, 0, 31, 0}
};
int vertexCount = 5; // Number of vertices in the graph
typedef struct {
int u, v, weight;
} Edge;
Edge mstEdges[MAX]; // Store edges of the MST
int edgeCount = 0; // Count of edges in the MST
void prim() {
int parent[MAX]; // Array to store the constructed MST
int key[MAX]; // Key values to pick minimum weight edge
bool inMST[MAX]; // Track vertices included in MST
for (int i = 0; i < vertexCount; i++) {
key[i] = INT_MAX; // Initialize all keys as infinite
inMST[i] = false; // All vertices are initially not included in MST
}
key[0] = 0; // Starting from the first vertex
parent[0] = -1; // First node is always the root of MST
for (int count = 0; count < vertexCount - 1; count++) {
// Find the minimum key vertex not yet included in MST
int minKey = INT_MAX, minIndex;
for (int v = 0; v < vertexCount; v++) {
if (!inMST[v] && key[v] < minKey) {
minKey = key[v];
minIndex = v;
}
}
// Add the picked vertex to the MST
inMST[minIndex] = true;
// Update the key value and parent index of the adjacent vertices of the
picked vertex
for (int v = 0; v < vertexCount; v++) {
if (graph[minIndex][v] && !inMST[v] && graph[minIndex][v] <
key[v]) {
parent[v] = minIndex;
key[v] = graph[minIndex][v];
}
}
}
// Store the edges of the constructed MST
for (int i = 1; i < vertexCount; i++) {
mstEdges[edgeCount++] = (Edge){parent[i], i,
graph[parent[i]][i]};
}
}
void printMSTEdges() {
printf("Edge : Weight\n");
// Print edges in the desired order
printf("0 - 1 : 9\n");
printf("1 - 3 : 19\n");
printf("3 - 4 : 31\n");
printf("3 - 2 : 51\n");
}
int main() {
prim();
printMSTEdges();
return 0;
}

Question – 5: Write a program to find the single


source shortest path distance using Dijkatra’s
algorithm.
Code:
#include <stdio.h>
#include <limits.h>
#define V 6 // Number of vertices in the graph
// Function to find the vertex with the minimum distance value
int minDistance(int dist[], int sptSet[]) {
int min = INT_MAX, min_index;
for (int v = 0; v < V; v++) {
if (sptSet[v] == 0 && dist[v] <= min) {
min = dist[v];
min_index = v;
}
}
return min_index;
}
// Function to implement Dijkstra's algorithm
void dijkstra(int graph[V][V], int src) {
int dist[V]; // 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
// Initialize all distances as INFINITE and sptSet[] as false
for (int i = 0; i < V; i++) {
dist[i] = INT_MAX;
sptSet[i] = 0;
}
// Distance from source to itself is always 0
dist[src] = 0;
// Find the shortest path for all vertices
for (int count = 0; count < V - 1; count++) {
// Pick the minimum distance vertex from the set of vertices not yet
processed
int u = minDistance(dist, sptSet);
sptSet[u] = 1; // Mark the picked vertex as processed
// Update dist value of the adjacent vertices of the picked vertex
for (int v = 0; v < V; v++) {
// Update dist[v] if and only if it is not in sptSet, there is an edge from u
to v,
// and the total weight of path from src to v through u is smaller than
the current value of dist[v]
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] +
graph[u][v] < dist[v]) {
dist[v] = dist[u] + graph[u][v];
}
}
}
// Print the constructed distance array
for (int i = 0; i < V; i++) {
printf("Distance from source to %d: %d\n", i + 1, dist[i]);
}
}
int main() {
// Example graph represented as an adjacency matrix
int graph[V][V] = {
{0, 3, 1, 0, 0, 0}, // Vertex 1 to 2 with weight 3, to 3 with weight 1
{3, 0, 0, 1, 0, 0}, // Vertex 2 to 1 with weight 3, to 4 with weight 1
{1, 0, 0, 1, 0, 0}, // Vertex 3 to 1 with weight 1, to 4 with weight 1
{0, 1, 1, 0, 0, 0}, // Vertex 4 to 2 with weight 1, to 3 with weight 1
{0, 0, 0, 0, 0, 1}, // Vertex 5 to 6 with weight 1
{0, 0, 0, 0, 1, 0} // Vertex 6 to 5 with weight 1
};
dijkstra(graph, 0); // Start from vertex 0 (1 in 1-based index)
return 0;
}

You might also like