Design and Analysis of Algorithm
Design and Analysis of Algorithm
int main() {
int n, i;
int arr[n];
ARJ-BCA-COLLAGE, ILKAL 1
Design and Analysis of Algorithm 2024-25
printf("%d ", arr[i]);
}
selectionSort(arr, n);
Output
Enter the number of elements: 5
Enter 5 elements:
56732
Original Array: 5 6 7 3 2
Sorted Array: 2 3 5 6 7
ARJ-BCA-COLLAGE, ILKAL 2
Design and Analysis of Algorithm 2024-25
2. Write a program to perform travelling salesman problem.
#include <stdio.h>
#include <limits.h>
#define MAX_CITIES 10
void permute(int cities[], int start, int end, int distance[], int numCities, int *minCost,
int bestPath[]) {
int i, cost = 0;
if (start == end) {
// Calculate the cost of the current permutation
for (i = 0; i < end - 1; i++) {
cost += distance[cities[i] * numCities + cities[i + 1]];
}
cost += distance[cities[end - 1] * numCities + cities[0]]; // Return to starting city
int main() {
int numCities, i, j;
ARJ-BCA-COLLAGE, ILKAL 3
Design and Analysis of Algorithm 2024-25
scanf("%d", &numCities);
int cities[MAX_CITIES];
for (i = 0; i < numCities; i++) {
cities[i] = i; // Initialize cities array
}
return 0;
}
ARJ-BCA-COLLAGE, ILKAL 4
Design and Analysis of Algorithm 2024-25
Output
Enter the number of cities (max 10): 4
Enter the distance from city 1 to city 2: 20
Enter the distance from city 1 to city 3: 35
Enter the distance from city 1 to city 4: 42
Enter the distance from city 2 to city 3: 34
Enter the distance from city 2 to city 4: 30
Enter the distance from city 3 to city 4: 12
Minimum Cost: 97
Optimal Path: 1 2 4 3 1
ARJ-BCA-COLLAGE, ILKAL 5
Design and Analysis of Algorithm 2024-25
3. Write a program to perform knapsack problem using greedy solution.
#include <stdio.h>
typedef struct {
int weight;
int value;
double valuePerWeight; // Value-to-Weight ratio
} Item;
ARJ-BCA-COLLAGE, ILKAL 6
Design and Analysis of Algorithm 2024-25
printf("Maximum value in the knapsack: %.2f\n", totalValue);
}
int main() {
int n, capacity;
printf("Enter the number of items: ");
scanf("%d", &n);
Item items[n];
printf("Enter the weight and value for each item:\n");
for (int i = 0; i < n; i++) {
printf("Item %d:\n", i + 1);
printf("Weight: ");
scanf("%d", &items[i].weight);
printf("Value: ");
scanf("%d", &items[i].value);
}
fractionalKnapsack(items, n, capacity);
return 0;
}
Output
Enter the number of items: 3
Enter the weight and value for each item:
Item 1:
Weight: 10
Value: 60
Item 2:
Weight: 20
Value: 100
Item 3:
Weight: 30
Value: 120
Enter the knapsack capacity: 50
Maximum value in the knapsack: 240.00
ARJ-BCA-COLLAGE, ILKAL 7
Design and Analysis of Algorithm 2024-25
4. Write a program to implement DFs algorithm for a graph.
#include <stdio.h>
#include <stdbool.h>
typedef struct {
int vertices[MAX_VERTICES];
int adjacencyMatrix[MAX_VERTICES][MAX_VERTICES];
int numVertices;
} Graph;
ARJ-BCA-COLLAGE, ILKAL 8
Design and Analysis of Algorithm 2024-25
bool visited[MAX_VERTICES] = {false};
printf("DFS traversal starting from vertex %d: ", startVertex);
DFS(g, startVertex, visited);
}
int main() {
Graph g;
int numVertices, startVertex;
initGraph(&g, numVertices);
performDFS(&g, startVertex);
return 0;
}
Output
Enter the number of vertices: 4
Enter the adjacency matrix:
0110
1011
1101
0110
Enter the starting vertex for DFS: 0
DFS traversal starting from vertex 0: 0 1 2 3
ARJ-BCA-COLLAGE, ILKAL 9
Design and Analysis of Algorithm 2024-25
5. Write program to implement the BFS algorithm for a graph.
#include <stdio.h>
#include <stdbool.h>
#define MAX_VERTICES 100
typedef struct
{
int vertices[MAX_VERTICES];
int adjacencyMatrix[MAX_VERTICES][MAX_VERTICES];
int numVertices;
} Graph;
ARJ-BCA-COLLAGE, ILKAL 10
Design and Analysis of Algorithm 2024-25
printf("%d ", currentVertex);
for (int i = 0; i < g->numVertices; i++)
{
if (g->adjacencyMatrix[currentVertex][i] == 1 && !visited[i])
{
visited[i] = true;
queue[rear++] = i;
}
}
}
}
int main()
{
Graph g;
int numVertices, startVertex;
initGraph(&g, numVertices);
return 0;
}
ARJ-BCA-COLLAGE, ILKAL 11
Design and Analysis of Algorithm 2024-25
Output
Enter the number of vertices: 4
Enter the adjacency matrix:
0110
1011
1101
0110
Enter the starting vertex for BFS: 0
BFS traversal starting from vertex 0: 0 1 2 3
ARJ-BCA-COLLAGE, ILKAL 12
Design and Analysis of Algorithm 2024-25
6. Write a program to find minimum and maximum value in an array using
divide and conquer.
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int min;
int max;
} MinMax;
ARJ-BCA-COLLAGE, ILKAL 13
Design and Analysis of Algorithm 2024-25
return result;
}
int main() {
int n;
int *arr = (int *)malloc(n * sizeof(int)); // Dynamically allocate memory for the array
if (arr == NULL) {
printf("Memory allocation failed.\n");
return 1; // Exit with error code
}
Output
Enter the number of elements in the array: 5
Enter the elements of the array:
45 3 78 11 41
Minimum value in the array: 3
Maximum value in the array: 7
ARJ-BCA-COLLAGE, ILKAL 14
Design and Analysis of Algorithm 2024-25
7. Write a test program to implement Divide and Conquer Strategy for
Quick sort algorithm
#include <stdio.h>
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int n;
int arr[n];
ARJ-BCA-COLLAGE, ILKAL 15
Design and Analysis of Algorithm 2024-25
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
quickSort(arr, 0, n - 1);
printf("Sorted array using Quick Sort:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output
Enter the number of elements in the array: 5
Enter the elements of the array:
65 77 82 23 11
Sorted array using Quick Sort:
11 23 65 77 82
ARJ-BCA-COLLAGE, ILKAL 16
Design and Analysis of Algorithm 2024-25
8. Write a program to implement Merge sort algorithm for sorting a list of
integers in ascending order
#include <stdio.h>
int i = 0, j = 0, k = left;
ARJ-BCA-COLLAGE, ILKAL 17
Design and Analysis of Algorithm 2024-25
int main() {
int n;
int arr[n];
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
mergeSort(arr, 0, n - 1);
return 0;
}
Output
Enter the number of elements in the array: 5
Enter the elements of the array:
54 67 33 87 41
Sorted array using Merge Sort:
33 41 54 67 87
ARJ-BCA-COLLAGE, ILKAL 18
Design and Analysis of Algorithm 2024-25
9. Write C program that accepts the vertices and edges for a graph and
stores it as an adjacency matrix.
#include <stdio.h>
typedef struct {
int adjacencyMatrix[MAX_VERTICES][MAX_VERTICES];
int numVertices;
} Graph;
void displayGraph(Graph* g) {
printf("Adjacency Matrix:\n");
for (int i = 0; i < g->numVertices; i++) {
for (int j = 0; j < g->numVertices; j++) {
printf("%d ", g->adjacencyMatrix[i][j]);
}
printf("\n");
}
}
int main() {
Graph g;
int numVertices, numEdges;
ARJ-BCA-COLLAGE, ILKAL 19
Design and Analysis of Algorithm 2024-25
printf("Enter the number of vertices: ");
scanf("%d", &numVertices);
initGraph(&g, numVertices);
printf("Enter the number of edges: ");
scanf("%d", &numEdges);
displayGraph(&g);
return 0;
}
Output
Enter the number of vertices: 5
Enter the number of edges: 7
Enter the edges (start vertex end vertex):
01
02
12
13
23
34
40
Adjacency Matrix:
01100
00110
00010
00001
10000
ARJ-BCA-COLLAGE, ILKAL 20
Design and Analysis of Algorithm 2024-25
10. Implement function to print In-Degree, Out-Degree and to display that
adjacency matrix.
#include <stdio.h>
typedef struct {
int adjacencyMatrix[MAX_VERTICES][MAX_VERTICES];
int numVertices;
} Graph;
void displayGraph(Graph* g) {
printf("Adjacency Matrix:\n");
for (int i = 0; i < g->numVertices; i++) {
for (int j = 0; j < g->numVertices; j++) {
printf("%d ", g->adjacencyMatrix[i][j]);
}
printf("\n");
}
}
void calculateDegrees(Graph* g) {
printf("Vertex\tIn-Degree\tOut-Degree\n");
for (int i = 0; i < g->numVertices; i++) {
int inDegree = 0, outDegree = 0;
ARJ-BCA-COLLAGE, ILKAL 21
Design and Analysis of Algorithm 2024-25
for (int j = 0; j < g->numVertices; j++) {
inDegree += g->adjacencyMatrix[j][i];
outDegree += g->adjacencyMatrix[i][j];
}
printf("%d\t%d\t\t%d\n", i, inDegree, outDegree);
}
}
int main() {
Graph g;
int numVertices, numEdges;
initGraph(&g, numVertices);
displayGraph(&g);
calculateDegrees(&g);
return 0;
}
ARJ-BCA-COLLAGE, ILKAL 22
Design and Analysis of Algorithm 2024-25
Output
Enter the number of vertices: 5
Enter the number of edges: 7
Enter the edges (start vertex end vertex):
01
02
12
13
23
34
40
Adjacency Matrix:
01100
00110
00010
00001
10000
Vertex In-Degree Out-Degree
0 1 2
1 1 2
2 2 1
3 2 1
4 1 1
ARJ-BCA-COLLAGE, ILKAL 23