You are on page 1of 29

PROGRAM 1

Objective: Program for Linear Search, Binary Search, Selection Sort, Insertion Sortand Bubble
Sort.

Code:
#include <stdio.h>
// Linear Search
int linearSearch(int arr[], int size, int target)
{ for (int i = 0; i < size; i++) {
if (arr[i] == target)
{ return i;
}
}
return -1; // Target not found
}
// Binary Search (Assumes a sorted array) int
binarySearch(int arr[], int size, int target) {
int low = 0, high = size - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] < target)
{ low = mid + 1;
} else {
high = mid - 1;
}
}
return -1; // Target not found
}
// Selection Sort
void selectionSort(int arr[], int size) { for
(int i = 0; i < size - 1; i++) {
int minIdx = i;
for (int j = i + 1; j < size; j++) {
if (arr[j] < arr[minIdx]) {
minIdx = j;
}
}
int temp = arr[i];
arr[i] = arr[minIdx];
arr[minIdx] = temp;
}
}
// Insertion Sort
void insertionSort(int arr[], int size) { for
(int i = 1; i < size; i++) {
int key = arr[i];

Ananya Singh
21002701300032
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
// Bubble Sort
void bubbleSort(int arr[], int size)
{ for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++)
{ if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int size = sizeof(arr) / sizeof(arr[0]);
int target = 22;
// Linear Search
int linearResult = linearSearch(arr, size, target); if
(linearResult != -1) {
printf("Element %d found at index %d using Linear Search.\n", target, linearResult);
} else {
printf("Element %d not found using Linear Search.\n", target);
}
// Binary Search
int binaryResult = binarySearch(arr, size, target); if
(binaryResult != -1) {
printf("Element %d found at index %d using Binary Search.\n", target, binaryResult);
} else {
printf("Element %d not found using Binary Search.\n", target);
}

// Selection Sort
selectionSort(arr, size);
printf("Sorted array using Selection Sort: "); for
(int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
// Insertion Sort
int arrInsertion[] = {12, 11, 13, 5, 6};
int sizeInsertion = sizeof(arrInsertion) / sizeof(arrInsertion[0]); insertionSort(arrInsertion,
sizeInsertion);
printf("Sorted array using Insertion Sort: "); for
(int i = 0; i < sizeInsertion; i++) {
printf("%d ", arrInsertion[i]);
}

Ananya Singh
21002701300032
printf("\n");

// Bubble Sort
bubbleSort(arr, size);
printf("Sorted array using Bubble Sort: "); for
(int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}

Ananya Singh
21002701300032
PROGRAM 2

Objective: Program for Quick Sort, Merge Sort and Heap


Code:
#include <stdio.h>

void swap(int* a, int* b)


{ int t = *a;
*a = *b;
*b = t;
}

int partition(int arr[], int low, int high)


{ int pivot = arr[high];
int i = (low - 1);

for (int j = low; j <= high - 1; j++)


{ if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high)
{ if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
void merge(int arr[], int left, int middle, int right) { int
i, j, k;
int n1 = middle - left + 1;
int n2 = right - middle; int
L[n1], R[n2];

for (i = 0; i < n1; i++)


{ L[i] = arr[left + i];
}
for (j = 0; j < n2; j++) {
R[j] = arr[middle + 1 + j];
}
i = 0;
j = 0;

Ananya Singh
21002701300032
k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] =
L[i]; i++;
} else {
arr[k] = R[j]; j+
+;
} k+
+;
}
while (i < n1) {
arr[k] =
L[i]; i++;
k++;
}

while (j < n2) {


arr[k] =
R[j]; j++;
k++;
}
}
void mergeSort(int arr[], int left, int right) { if
(left < right) {
int middle = left + (right - left) / 2;

mergeSort(arr, left, middle);


mergeSort(arr, middle + 1, right);
merge(arr, left, middle, right);
}
}
void heapify(int arr[], int size, int i)
{ int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;

if (left < size && arr[left] > arr[largest])


{ largest = left;
}
if (right < size && arr[right] > arr[largest])
{ largest = right;
}
if (largest != i) {
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, size, largest);
}
}
void heapSort(int arr[], int size) {
for (int i = size / 2 - 1; i >= 0; i--)
{ heapify(arr, size, i);

Ananya Singh
21002701300032
}

for (int i = size - 1; i >= 0; i--)


{ int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}
int main()
{ int
choice;
printf("Select a sorting algorithm:\n");
printf("1. Quick Sort\n");
printf("2. Merge Sort\n");
printf("3. Heap Sort\n");
scanf("%d", &choice);
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int size = sizeof(arr) / sizeof(arr[0]);

switch (choice)
{ case 1:
quickSort(arr, 0, size - 1);
printf("Sorted array using Quick Sort: ");
break;
case 2:
mergeSort(arr, 0, size - 1);
printf("Sorted array using Merge Sort: ");
break;
case 3:
heapSort(arr, size);
printf("Sorted array using Heap Sort: ");
break;
default:
printf("Invalid choice.\n");
return 1;
}
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}

Ananya Singh
21002701300032
PROGRAM 3

Objective: Program for Shell Sort and Counting

Code:
#include <stdio.h>
// Shell Sort
void shellSort(int arr[], int size) {
for (int gap = size / 2; gap > 0; gap /= 2) { for
(int i = gap; i < size; i++) {
int temp = arr[i];
int j;
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) { arr[j]
= arr[j - gap];
}
arr[j] = temp;
}
}
}
// Counting Sort
void countingSort(int arr[], int size) { int
max = arr[0];
for (int i = 1; i < size; i++)
{ if (arr[i] > max) {
max = arr[i];
}
}
int count[max + 1];
int output[size];

for (int i = 0; i <= max; i++)


{ count[i] = 0;
}
for (int i = 0; i < size; i++) {
count[arr[i]]++;
}
for (int i = 1; i <= max; i++)
{ count[i] += count[i - 1];
}
for (int i = size - 1; i >= 0; i--) {
output[count[arr[i]] - 1] = arr[i];
count[arr[i]]--;
}
for (int i = 0; i < size; i++)
{ arr[i] = output[i];
}
}
int main() {

Ananya Singh
21002701300032
int arr1[] = {12, 34, 54, 2, 3};
int size1 = sizeof(arr1) / sizeof(arr1[0]);
shellSort(arr1, size1);
printf("Sorted array using Shell Sort: ");
for (int i = 0; i < size1; i++) {
printf("%d ", arr1[i]);
}
printf("\n");
int arr2[] = {4, 2, 2, 8, 3, 3, 1};
int size2 = sizeof(arr2) / sizeof(arr2[0]);
countingSort(arr2, size2);
printf("Sorted array using Counting Sort: "); for
(int i = 0; i < size2; i++) {
printf("%d ", arr2[i]);
}
printf("\n");
return 0;
}

Ananya Singh
21002701300032
PROGRAM 4

Objective- Program for BFS and DFS on a graph represented.

Code:
#include <stdio.h>
#include <stdlib.h>

#define MAX_VERTICES 100

struct Node {
int data;
struct Node* next;
};

struct Graph {
int numVertices;
struct Node* adjacencyList[MAX_VERTICES];
};

// Function to create a new node


struct Node* createNode(int data)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct
Node)); newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to create a graph with a given number of vertices


struct Graph* createGraph(int vertices) {
struct Graph* graph = (struct Graph*)malloc(sizeof(struct
Graph)); graph->numVertices = vertices;

for (int i = 0; i < vertices; i++)


graph->adjacencyList[i] =
NULL;

return graph;
}

// Function to add an edge to an undirected graph


void addEdge(struct Graph* graph, int src, int dest) {
// Add edge from source to destination
struct Node* newNode = createNode(dest);
newNode->next = graph-
>adjacencyList[src]; graph-
Ananya Singh 2100270130032
>adjacencyList[src] = newNode;

Ananya Singh 2100270130032


// Add edge from destination to source since the graph is undirected
newNode = createNode(src);
newNode->next = graph-
>adjacencyList[dest]; graph-
>adjacencyList[dest] = newNode;
}

// Function to print the adjacency list of the graph


void printGraph(struct Graph* graph) {
printf("Graph Representation (Adjacency List):\n");
for (int i = 0; i < graph->numVertices; i++) {
struct Node* current = graph->adjacencyList[i];
printf("Vertex %d: ", i);
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
}

// Breadth-First Search
void BFS(struct Graph* graph, int startVertex)
{ int visited[MAX_VERTICES] = {0};
int queue[MAX_VERTICES];
int front = -1, rear = -1;

visited[startVertex] = 1;
queue[++rear] = startVertex;

printf("\nBreadth-First Search starting from vertex %d:\n", startVertex);

while (front != rear) {


int currentVertex = queue[++front];
printf("%d ", currentVertex);

struct Node* temp = graph->adjacencyList[currentVertex];


while (temp != NULL) {
int adjVertex = temp-
>data; if (!
visited[adjVertex]) {
visited[adjVertex] = 1;
queue[++rear] = adjVertex;
}
temp = temp->next;
}
}

printf("\n");
Ananya Singh 2100270130032
}

// Depth-First Search (recursive)


void DFSRecursive(struct Graph* graph, int currentVertex, int visited[])
{ visited[currentVertex] = 1;
printf("%d ", currentVertex);

struct Node* temp = graph->adjacencyList[currentVertex];


while (temp != NULL) {
int adjVertex = temp-
>data; if (!
visited[adjVertex]) {
DFSRecursive(graph, adjVertex, visited);
}
temp = temp->next;
}
}

// Depth-First Search (wrapper function)


void DFS(struct Graph* graph, int startVertex)
{ int visited[MAX_VERTICES] = {0};

printf("\nDepth-First Search starting from vertex %d:\n",


startVertex); DFSRecursive(graph, startVertex, visited);
printf("\n");
}

int main() {
// Create a graph with 5 vertices
struct Graph* graph =
createGraph(5);

// Add edges to the graph


addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 3);
addEdge(graph, 1, 4);

// Print the graph


printGraph(graph);

// Perform BFS starting from vertex 0


BFS(graph, 0);

// Perform DFS starting from vertex 0


DFS(graph, 0);

return 0;
}

Ananya Singh 2100270130032


Output:
Graph Representation (Adjacency
List): Vertex 0: 2 1
Vertex 1: 4 3 0
Vertex 2: 0
Vertex 3: 1
Vertex 4: 1

Breadth-First Search starting from vertex 0:


02143

Depth-First Search starting from vertex 0:


02143

Ananya Singh 2100270130032


Program 5
Objective: Program for constructing a Minimum Cost Spanning Tree using Prim’s and
Kruskal’s algorithms

Code:
#include <stdio.h>
#include
<stdlib.h>
#include <limits.h>
#define V 5
// Structure to represent an edge in the
graph struct Edge {
int src, dest, weight;
};
// Structure to represent a subset for union-find
struct Subset {
int parent, rank;
};
// Function to compare two edges based on their weights
int compareEdges(const void *a, const void *b) {
return ((struct Edge*)a)->weight - ((struct Edge*)b)->weight;
}

// Function to find the subset of an element i


int find(struct Subset subsets[], int i) {
if (subsets[i].parent != i)
subsets[i].parent = find(subsets, subsets[i].parent);
return subsets[i].parent;
}
// Function to perform union of two subsets
void unionSets(struct Subset subsets[], int x, int y)
{ int xroot = find(subsets, x);
int yroot = find(subsets, y);
if (subsets[xroot].rank < subsets[yroot].rank)
subsets[xroot].parent = yroot;
else if (subsets[xroot].rank > subsets[yroot].rank)
subsets[yroot].parent = xroot;
else {
subsets[yroot].parent =
xroot; subsets[xroot].rank++;
}
}
// Function to construct a minimum-cost spanning tree using Kruskal's algorithm
void kruskalMST(int graph[V][V]) {
struct Edge
result[V]; int e = 0, i
= 0;
Ananya Singh 2100270130032
qsort(graph, V * V, sizeof(struct Edge), compareEdges);
struct Subset *subsets = (struct Subset*)malloc(V * sizeof(struct Subset));
for (int v = 0; v < V; v++) {
subsets[v].parent = v;
subsets[v].rank = 0;
}
while (e < V - 1 && i < V * V) {
struct Edge nextEdge = graph[i++];
int x = find(subsets, nextEdge.src);
int y = find(subsets, nextEdge.dest);
if (x != y) {
result[e++] = nextEdge;
unionSets(subsets, x, y);
}
}
printf("Kruskal's Algorithm - Minimum Cost Spanning Tree:\n");
for (int i = 0; i < e; i++)
printf("%d - %d %d \n", result[i].src, result[i].dest, result[i].weight);
free(subsets);
}
// Function to construct a minimum-cost spanning tree using Prim's algorithm
void primMST(int graph[V][V]) {
int parent[V];
int key[V];
int mstSet[V];
for (int i = 0; i < V; i++)
{ key[i] = INT_MAX;
mstSet[i] = 0;
}
key[0] = 0;
parent[0] = -1;
for (int count = 0; count < V - 1; count++)
{ int u, min = INT_MAX;
for (int v = 0; v < V; v++) {
if (mstSet[v] == 0 && key[v] < min)
{ min = key[v];
u = v;
}
}
mstSet[u] = 1;
for (int v = 0; v < V; v++) {
if (graph[u][v] && mstSet[v] == 0 && graph[u][v] < key[v]) {
parent[v] = u;
key[v] = graph[u][v];
}
}
}
printf("\nPrim's Algorithm - Minimum Cost Spanning Tree:\n");

Ananya Singh 2100270130032


for (int i = 1; i < V; i++)
printf("%d - %d %d \n", parent[i], i, graph[i][parent[i]]);
}
int main() {
int graph[V][V] = {
{0, 2, 0, 6, 0},
{2, 0, 3, 8, 5},
{0, 3, 0, 0, 7},
{6, 8, 0, 0, 9},
{0, 5, 7, 9, 0}
};

kruskalMST(graph);
primMST(graph);
return 0;
}
Output:
Kruskal's Algorithm - Minimum Cost Spanning Tree:
0-1 2
0-3 6
1-2 3
1-4 5

Prim's Algorithm - Minimum Cost Spanning Tree:


0-1 2
1-2 3
0-3 6
1-4 5

Ananya Singh 2100270130032


Program 6

Objective: Program for Single Source Shortest path using Dijkstra's Algorithm

Code:

#include <stdio.h>
#include <limits.h>
#define V 6
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;
}
void printSolution(int dist[]) {
printf("Vertex \t\t Distance from Source Vertex\
n"); for (int i = 0; i < V; i++)
printf("%d \t\t %d\n", i, dist[i]);
}
// Function to implement Dijkstra's algorithm for a given graph
void dijkstra(int graph[V][V], int src) {
int dist[V]; // Array to store the shortest distances from the source vertex
int sptSet[V];
for (int i = 0; i < V; i++)
{ dist[i] = INT_MAX;
sptSet[i] = 0;
}
dist[src] = 0;
// Find shortest path for all vertices
for (int count = 0; count < V - 1; count++)
{ int u = minDistance(dist, sptSet);
sptSet[u] = 1;
for (int v = 0; v < V; 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];
}
}
}
printSolution(dist);
}
int main() {

Ananya Singh 2100270130032


int graph[V][V] = {
{0, 1, 4, 0, 0, 0},
{0, 0, 2, 7, 0, 0},
{0, 0, 0, 0, 3, 0},
{0, 0, 0, 0, 0, 2},
{0, 0, 0, 4, 0, 1},
{0, 0, 0, 0, 0, 0}
};
int sourceVertex = 0; // Source vertex is set to 0 in this example

dijkstra(graph, sourceVertex);
return 0;
}

Output:
Vertex Distance from Source Vertex
0 0
1 1
2 3
3 7
4 10
5 9

Ananya Singh 2100270130032


Program 7
Objective: Program for All Pair Shortest Paths using Warshal’s and Floyd’s algorithms.

Code:
#include <stdio.h>
#include <limits.h>
#define V 4
void printSolution(int dist[][V]);
// Warshall's algorithm for All-Pairs Shortest
Paths void warshall(int graph[][V]) {
int dist[V][V];
for (int i = 0; i < V; i++)
for (int j = 0; j < V; j+
+)
dist[i][j] = graph[i][j];
for (int k = 0; k < V; k++) {
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i][k] != INT_MAX && dist[k][j] != INT_MAX && dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
printf("Warshall's Algorithm - All-Pairs Shortest Paths:\n");
printSolution(dist);
}

// Floyd's algorithm for All-Pairs Shortest


Paths void floyd(int graph[][V]) {
int dist[V][V];
for (int i = 0; i < V; i++)
for (int j = 0; j < V; j++)
dist[i][j] = graph[i][j];
for (int k = 0; k < V; k++) {
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i][k] != INT_MAX && dist[k][j] != INT_MAX && dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}

printf("\nFloyd's Algorithm - All-Pairs Shortest Paths:\n");


printSolution(dist);
}
void printSolution(int dist[][V])
{ for (int i = 0; i < V; i++) {

Ananya Singh 2100270130032


for (int j = 0; j < V; j++) {
if (dist[i][j] == INT_MAX)
printf("INF\t");
else
printf("%d\t", dist[i][j]);
}
printf("\n");
}
}
int main() {
int graph[V][V] = {
{0, 5, INT_MAX, 10},
{INT_MAX, 0, 3, INT_MAX},
{INT_MAX, INT_MAX, 0, 1},
{INT_MAX, INT_MAX, INT_MAX, 0}
};
warshall(graph);
floyd(graph);
return 0;
}
Output:
Warshall's Algorithm - All-Pairs Shortest Paths:
0 5 8 9
INF 0 3 4
INF INF 0 1
INF INF INF 0

Floyd's Algorithm - All-Pairs Shortest Paths:


0 5 8 9
INF 0 3 4
INF INF 0 1
INF INF INF 0

Ananya Singh 2100270130032


Program 8
Objective: Program for Graph Colouring, n-Queen.
Code for Graph Colouring:
#include <stdio.h>
#define V 4
void printSolution(int color[]);
int isSafe(int v, int graph[V][V], int color[], int c)
{ for (int i = 0; i < V; i++)
if (graph[v][i] && color[i] == c)
return 0;
return 1;
}
// Assign colors to vertices using backtracking
int graphColoringUtil(int graph[V][V], int m, int color[], int v) {
if (v == V)
return 1;
for (int c = 1; c <= m; c++) {
if (isSafe(v, graph, color, c))
{ color[v] = c;
if (graphColoringUtil(graph, m, color, v + 1))
return 1;
color[v] = 0;
}
}
return 0;
}
// Wrapper function for graph coloring
void graphColoring(int graph[V][V], int m)
{
int color[V];
for (int i = 0; i < V; i++)
color[i] = 0;
if (!graphColoringUtil(graph, m, color, 0))
printf("Solution does not exist\n");
else
printSolution(color);
}
void printSolution(int color[]) {
printf("Graph Coloring Solution:\n");
for (int i = 0; i < V; i++)
printf("Vertex %d --> Color %d\n", i, color[i]);
}
int main() {
int graph[V][V] = {
{0, 1, 1, 1},

Ananya Singh 2100270130032


{1, 0, 1, 0},
{1, 1, 0, 1},
{1, 0, 1, 0}
};
int m = 3; // Number of colors
graphColoring(graph, m);
return 0;
}
Output:
0
/\
1---3
\/
Graph Coloring Solution:
Vertex 0 --> Color 1
Vertex 1 --> Color 2
Vertex 2 --> Color 1
Vertex 3 --> Color 2

Code for N-queen:


#include <stdio.h>
#define N 4
void printSolution(int board[N][N]);
int isSafe(int board[N][N], int row, int col)
{ for (int i = 0; i < col; i++)
if (board[row][i])
return 0;
for (int i = row, j = col; i >= 0 && j >= 0; i--, j--)
if (board[i][j])
return 0;
for (int i = row, j = col; i < N && j >= 0; i++, j--)
if (board[i][j])
return 0;
return 1;
}
// Solve the N-Queens problem using backtracking
int solveNQUtil(int board[N][N], int col) {
if (col >= N)
return 1;

for (int i = 0; i < N; i++) {


if (isSafe(board, i, col))
{ board[i][col] = 1;
if (solveNQUtil(board, col + 1))
return 1;
board[i][col] = 0; // Backtrack
}

Ananya Singh 2100270130032


}
return 0;
}
// Wrapper function for solving N-Queens
void solveNQ() {
int board[N][N];
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
board[i][j] = 0;
if (!solveNQUtil(board, 0))
printf("Solution does not exist\n");
else
printSolution(board);
}
void printSolution(int board[N][N])
{ printf("N-Queens Solution:\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
printf("%d ", board[i][j]);
printf("\n");
}
}
int main() {
solveNQ();
return 0;
}
Output:
N-Queens Solution:
0010
1000
0001
0100

Ananya Singh 2100270130032


Program 9
Objective: Program for Hamiltonian Cycles and Sum of Subsets
Code:
#include <stdio.h>

// Hamiltonian Cycles

#define V 5

void printSolution(int path[]);

int isSafe(int v, int pos, int path[], int graph[V][V]) {

if (!graph[path[pos - 1]][v])

return 0;

for (int i = 0; i < pos; i++)

if (path[i] == v)

return 0;

return 1;

// Solve the Hamiltonian Cycle problem using backtracking

int hamiltonianCycleUtil(int graph[V][V], int path[], int pos) {

if (pos == V) {

if (graph[path[pos - 1]][path[0]] == 1)

return 1;

else

return 0;

for (int v = 1; v < V; v++) {

if (isSafe(v, pos, path, graph))

{ path[pos] = v;

Ananya Singh 2100270130032


if (hamiltonianCycleUtil(graph, path, pos + 1))

return 1;

path[pos] = -1; // Backtrack

return 0;

// Wrapper function for finding Hamiltonian Cycles

void hamiltonianCycle(int graph[V][V]) {

int path[V];

for (int i = 0; i < V; i++)

path[i] = -1;

path[0] = 0;

if (!hamiltonianCycleUtil(graph, path, 1))

printf("Hamiltonian Cycle Solution does not exist\

n"); else

printSolution(path);

void printSolution(int path[]) {

printf("Hamiltonian Cycle Solution:\n");

for (int i = 0; i < V; i++)

printf("%d ", path[i]);

printf("%d\n", path[0]);

#define N 4

void printSubset(int subset[], int size);

// Solve the Subset Sum problem using backtracking

int isSubsetSum(int set[], int n, int sum, int subset[], int index) {

Ananya Singh 2100270130032


if (sum == 0) {

printSubset(subset, index);

return 1;

if (index == n)

return 0;

subset[index] = set[index];

if (isSubsetSum(set, n, sum - set[index], subset, index + 1))

return 1;

return isSubsetSum(set, n, sum, subset, index + 1);

void subsetSum(int set[], int n, int sum)

{ int subset[N];

if (!isSubsetSum(set, n, sum, subset, 0))

printf("Subset Sum Solution does not exist\n");

void printSubset(int subset[], int size)

{ printf("Subset Sum Solution: ");

for (int i = 0; i < size; i++)

printf("%d ", subset[i]);

printf("\n");

int main() {

// Hamiltonian Cycles

int graph[V][V] = {

{0, 1, 0, 1, 0},

{1, 0, 1, 1, 1},

Ananya Singh 2100270130032


{0, 1, 0, 0, 1},

{1, 1, 0, 0, 1},

{0, 1, 1, 1, 0}

};

hamiltonianCycle(graph);

int set[N] = {10, 7, 5, 18};

int sum = 15;

subsetSum(set, N, sum);

return 0;

Output:
Hamiltonian Cycle Solution:

012430

Subset Sum Solution: 7 5 3

Ananya Singh 2100270130032


Program 10:
Objective: Program for String Matching using Rabin Karp Algorithm.
Code:
#include <stdio.h>
#include <string.h>
#define d 256 // Number of characters in the alphabet
void search(char pattern[], char text[], int q) {
int M = strlen(pattern);
int N = strlen(text);
int i, j;
int p = 0;
int t = 0;
int h = 1;
// Calculate h = (d^(M-1)) % q
for (i = 0; i < M - 1; i++)
h = (h * d) % q;
// Calculate hash value for pattern and the first window of
text for (i = 0; i < M; i++) {
p = (d * p + pattern[i]) % q;
t = (d * t + text[i]) % q;
}
for (i = 0; i <= N - M; i++)
{ if (p == t) {
for (j = 0; j < M; j++) {
if (text[i + j] != pattern[j])
break;
}
if (j == M)
printf("Pattern found at index %d\n", i);
}
if (i < N - M) {
t = (d * (t - text[i] * h) + text[i + M]) % q;
if (t < 0)
t = (t + q);
}
}
}
int main() {
char text[] = "AABAACAADAABAAABAA";
char pattern[] = "AABA";
int q = 101; // A prime number
search(pattern, text, q);

return 0;

Ananya Singh 2100270130032


}

Output:
Pattern found at index 0
Pattern found at index 9
Pattern found at index
13

Ananya Singh 2100270130032

You might also like