Analysis Design and Algorithms ADA
Analysis Design and Algorithms ADA
(Approved by AICTE, New Delhi, Govt. of Karnataka, Affiliated to Visvesvaraya Technological University, Belagavi)
#14, Ramohalli Cross, Kumbalgodu, Mysore Road, Bengaluru – 560074
VISION MISSION
To provide an open opportunity to young To consistently strive for Academic Excellence to
generation for evolving their core become a leading Institution in the field of
competencies helping them to build their Engineering, Management and Research to produce
career as Global professionals to be an competent and ethically sound manpower for the
Autonomous Institution by achieving benefit of Industry, Society, Nation and the Global
excellence in the field of higher education. Environment.
Compiled By:
Prof. Soniya R
Assistant Professor
Dept. of AI & ML
DEPARTMENT VISION
Contribute dedicated, skilled, intelligent Computer Engineers to architect strong India and the world
DEPARTMENT MISSION
Encourage the youths to pursue career in Computer domain with modern innovation and
ethics.
PEO 1: Graduates will have the expertise in analyzing real time problems and providing
appropriate solutions related to Computer Science & Engineering.
PEO 2: Graduates will have the knowledge of fundamental principles and innovative
technologies to succeed in higher studies, and research.
PEO 3: Graduates will continue to learn and to adapt technology developments combined
with deep awareness of ethical responsibilities in profession.
ADA LAB BCSL404
Program Outcomes
PO1 : Engineering Knowledge - Apply knowledge of mathematics, science, Engineering fundamentals and
computing skills to solve IT related engineering problems.
PO2: Problem Analysis - Identify, formulate, research literature and analyze complex computer science
engineering problems.
PO3: Design/Development of Solutions - Design software / hardware solutions for complex IT
problems to uplift the societal status of common man.
PO4 : Conduct Investigation of complex problems - Use research-based knowledge and methods including
design of software and or hardware experiments, analysis and interpretation of data, and synthesis of the
information to provide valid conclusions.
PO5 : Modern Tool Usage - Create/select an appropriate IT tool to model, implement and automate the complex
computational system.
PO6 : The engineer and Society - Apply reasoning informed by the contextual knowledge to assess societal
issues, cultural issues, and the consequent responsibilities relevant to the IT practice
PO7: Environment and sustainability - Understand the impact of IT solutions in environmental
context and demonstrate the knowledge of, and need for sustainable development.
PO8 : Ethics - Apply ethical principles in responsible way to follow the IT norms and cyber ethics.
PO9 : Individual and team work - Function effectively as an individual and as a member or leader in
diverse teams / multidisciplinary teams.
PO10 : Communication - Communicate effectively on complex engineering activities with the engineering
community and the society for the effective presentation and / or report generation.
PO11 : Project management & Finance - Demonstrate the knowledge of computing / managerial
principles to solve and manage IT projects.
PO12 : Life Long Learning - Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.
PSO2 : Apply standard Software Engineering practices and strategies in software project development
PSO3 : Demonstrate the knowledge of Discrete Mathematics, Data management and Data engineerin
13 Internals
Course objectives:
1. To design and implement various algorithms in C/C++ programming using suitable development
tools to address different computational challenges.
3. To Measure and compare the performance of different algorithms to determine their efficiency and
suitability for specific tasks
Course outcomes (Course Skill Set): At the end of the course, the student will be able to:
1. Develop programs to solve computational problems using suitable algorithm design strategy.
2. Compare algorithm design strategies by developing equivalent programs and observing running
times for analysis (Empirical)
4. Choose appropriate algorithm design techniques to develop solution to the computational and
complex problems.
5. Demonstrate and present the development of program, its execution and running time(s) and record
the results/inferences
Program 1
1.Design and implement C Program to find Minimum Cost Spanning Tree
of a given connected undirected graph using Kruskal's algorithm.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
if (x != y) {
mst[e++] = next_edge;
Union(subsets, x, y);
}
}
int main() {
int V, E;
printf("Enter number of vertices and edges: ");
scanf("%d %d", &V, &E);
kruskalMST(graph);
return 0;
}
OUTPUT:
Program 2
Design and implement C Program to find Minimum Cost Spanning Tree of
a given connected undirected graph using Prim's algorithm.
#include <stdio.h>
#include <limits.h>
#define V_MAX 100 // Maximum number of vertices
// Function to find the vertex with the minimum key value, from the set of vertices not yet included in
the MST
int minKey(int key[], int mstSet[], int V) {
int min = INT_MAX, min_index;
for (int v = 0; v < V; v++)
if (mstSet[v] == 0 && key[v] < min)
min = key[v], min_index = v;
return min_index;
}
// Function to print the constructed MST stored in parent[]
void printMST(int parent[], int n, int graph[V_MAX][V_MAX], int V) {
printf("Edge Weight\n");
for (int i = 1; i < V; i++)
printf("%d - %d %d \n", parent[i], i, graph[i][parent[i]]);
}
// Function to construct and print MST for a graph represented using adjacency matrix representation
void primMST(int graph[][V_MAX], int V) {
int parent[V_MAX]; // Array to store constructed MST
int key[V_MAX]; // Key values used to pick minimum weight edge in cut
int mstSet[V_MAX]; // To represent set of vertices not yet included in MST
// Always include first 1st vertex in MST. Make key 0 so that this vertex is picked as the first vertex
key[0] = 0;
parent[0] = -1; // First node is always the root of MST
// Update key value and parent index of the adjacent vertices of the picked vertex
// Consider only those vertices which are not yet included in the MST
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];
}
int main() {
int V, E;
printf("Enter the number of vertices and edges: ");
scanf("%d %d", &V, &E);
// Prompt the user to enter the source vertex, destination vertex, and weight for each edge
printf("Enter the source vertex, destination vertex, and weight for each edge:\n");
for (int i = 0; i < E; i++) {
int source, dest, weight;
scanf("%d %d %d", &source, &dest, &weight);
graph[source][dest] = weight;
graph[dest][source] = weight; // Since the graph is undirected
}
return 0;
}
OUTPUT:
Program 3
3.a. Design and implement C Program to solve All-Pairs Shortest Paths
problem using Floyd's algorithm.
PROGRAM:
#include<stdio.h>
int min(int,int);
void floyds(int p[10][10],int n) {
int i,j,k;
for (k=1;k<=n;k++)
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
if(i==j)
p[i][j]=0; else
p[i][j]=min(p[i][j],p[i][k]+p[k][j]);
}
int min(int a,int b) {
if(a<b)
return(a); else
return(b);
}
void main() {
int p[10][10],w,n,e,u,v,i,j;
for (i=1;i<=e;i++) {
printf("\n Enter the end vertices of edge%d with its weight \n",i);
scanf("%d%d%d",&u,&v,&w);
p[u][v]=w;
}
printf("\n Matrix of input data:\n");
for (i=1;i<=n;i++) {
for (j=1;j<=n;j++)
printf("%d \t",p[i][j]);
printf("\n");
}
floyds(p,n);
printf("\n Transitive closure:\n");
for (i=1;i<=n;i++) {
for (j=1;j<=n;j++)
printf("%d \t",p[i][j]);
printf("\n");
}
printf("\n The shortest paths are:\n");
for (i=1;i<=n;i++)
for (j=1;j<=n;j++) {
if(i!=j)
printf("\n <%d,%d>=%d",i,j,p[i][j]);
}
OUTPUT:
Transitive closure:
0 10 3 4
2 0 5 6
7 7 0 1
6 16 9 0
<1,2>=10
<1,3>=3
<1,4>=4
<2,1>=2
<2,3>=5
<2,4>=6
<3,1>=7
<3,2>=7
<3,4>=1
<4,1>=6
<4,2>=16
3b.Design and implement C Program to find the transitive closure using Warshal's algorithm.
PROGRAM:
#include<stdio.h>
#include<math.h>
int max(int, int);
void warshal(int p[10][10], int n) {
int i, j, k;
for (k = 1; k <= n; k++)
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
p[i][j] = max(p[i][j], p[i][k] && p[k][j]);
}
if (a > b)
return (a);
else
return (b);
}
void main() {
int p[10][10] = { 0 }, n, e, u, v, i, j;
OUTPUT:
gedit 3b.c
gcc 3b.c
./a.out
Enter the number of vertices:5
Enter the number of edges:11
Transitive closure:
1 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 1 0 1 0
0 1 1 1 1
Program 4
4. Design and implement C Program to find shortest paths from a given vertex in a
weighted connected graph to other vertices using Dijkstra's algorithm.
#include <stdio.h>
#include <stdbool.h>
#include <limits.h>
// A function to find the vertex with the minimum distance value, from the set of vertices not yet
included in the shortest path tree
int minDistance(int dist[], bool sptSet[], int V) {
int min = INF, min_index;
for (int v = 0; v < V; v++)
if (sptSet[v] == false && dist[v] <= min)
min = dist[v], min_index = v;
return min_index;
}
dist[src] = 0;
printf("Enter the source vertex, destination vertex, and weight for each edge:\n");
for (int i = 0; i < E; i++) {
int source, dest, weight;
scanf("%d %d %d", &source, &dest, &weight);
graph[source][dest] = weight;
graph[dest][source] = weight; // Assuming undirected graph
}
dijkstra(graph, 0, V);
return 0;
}
OUTPUT:
Program 5
Design and implement C Program to obtain the Topological ordering of vertices in a
given digraph.
#include <stdio.h>
#include <stdlib.h>
// Driver code
int main() {
int V, E;
printf("Enter the number of vertices: ");
scanf("%d", &V);
Graph* graph = createGraph(V);
printf("Enter the number of edges: ");
scanf("%d", &E);
printf("Enter the edges (source vertex, destination vertex):\n");
for (int i = 0, src, dest; i < E; i++) {
scanf("%d %d", &src, &dest);
addEdge(graph, src, dest);
}
topologicalSort(graph);
return 0;
}
Dept.of AI&ML,RRCE 2024- 2025 22
ADA LAB BCSL404
OUTPUT:
Program 6
6. Design and implement C Program to solve 0/1 Knapsack problem using
Dynamic Programming method.
#include <stdio.h>
// K[n][W] contains the maximum value that can be put in a knapsack of capacity W
return K[n][W];
}
int main() {
int val[100], wt[100]; // Arrays to store values and weights
Dept.of AI&ML,RRCE 2024- 2025 24
ADA LAB BCSL404
OUTPUT:
Program 7
7. Design and implement C Program to solve discrete Knapsack and
continuous Knapsack problems using greedy approximation method.
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, capacity, i;
printf("Enter the number of items: ");
scanf("%d", &n);
discreteKnapsack(items, n, capacity);
continuousKnapsack(items, n, capacity);
return 0;
}
OUTPUT:
Program 8
Design and implement C Program to find a subset of a given set S = {sl ,
s2, .... ,sn} of n positive integers whose sum is equal to a given positive
integer d.
#include <stdio.h>
#include <stdbool.h>
return 0;
}
OUTPUT:
Program 9
Design and implement C Program to sort a given set of n integer elements
using Selection Sort method and compute its time complexity. Run the
program for varied values of n> 5000 and record the time taken to sort. Plot
a graph of the time taken versus n. The elements can be read from a file or
can be generated using the random number generator.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Function to perform Selection Sort
void selectionSort(int arr[], int n) {
int i, j, minIndex, temp;
for (i = 0; i < n - 1; i++) {
minIndex = i;
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap the found minimum element with the first element
temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
int main() {
int n, i;
clock_t start, end;
double cpu_time_used;
scanf("%d", &n);
if (n < 5000) {
printf("Please enter a value of n greater than 5000.\n");
return 1;
}
int *arr = (int *)malloc(n * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
OUTPUT:
Program 10
while (1) {
do {
i++;
} while (arr[i] < pivot);
do {
j--;
} while (arr[j] > pivot);
if (i >= j)
return j;
}
}
int main() {
int n, i;
clock_t start, end;
double cpu_time_used;
if (n < 5000) {
printf("Please enter a value of n greater than 5000.\n");
return 1;
}
srand(time(NULL));
start = clock();
quickSort(arr, 0, n - 1);
end = clock();
free(arr);
return 0;
}
OUTPUT:
Program 11
Design and implement C Program to sort a given set of n integer elements
using Merge Sort method and compute its time complexity. Run the program
for varied values of n> 5000, and record the time taken to sort. Plot a graph
of the time taken versus n. The elements can be read from a file or can be
generated using the random number generator.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
j = 0;
k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
int m = l + (r - l) / 2;
int main() {
int n, i;
clock_t start, end;
double cpu_time_used;
if (n < 5000) {
printf("Please enter a value of n greater than 5000.\n");
return 1;
}
OUTPUT:
Program 12
Design and implement C/C++ Program for N Queen's problem using Backtracking.
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int board[20],count;
int main()
{
int n,i,j;
void queen(int row,int n);
for(i=1;i<=n;++i)
printf("\t%d",i);
for(i=1;i<=n;++i)
{
printf("\n\n%d",i);
for(j=1;j<=n;++j) //for nxn board
{
if(board[i]==j)
printf("\tQ"); //queen at i,j position
else
printf("\t-"); //empty slot
}
}
}
int column;
for(column=1;column<=n;++column)
{
if(place(row,column))
{
board[row]=column; //no conflicts so place queen
if(row==n) //dead end
print(n); //printing the board configuration
else //try queen with next position
queen(row+1,n);
}
}
}
OUTPUT: