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 consistently strive for Academic Excellence to
To provide an open opportunity to young become a leading Institution in the field of
generation for evolving their core Engineering, Management and Research to produce
competencies helping them to build their competent and ethically sound manpower for the
career as Global professionals to be an benefit of Industry, Society, Nation and the Global
Autonomous Institution by achieving Environment.
excellence in the field of higher education.
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.
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
ADA BCSL4
Dept.of 2024- 3
ADA BCSL4
13 Internals
Course objectives:
1. To design and implement various algorithms in C/C++ programming using suitable development
tools to address different computational challenges.
2. To apply diverse design strategies for effective problem-solving.
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)
3. Make use of suitable integrated development tools to develop programs.
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
Dept.of 2024- 4
ADA BCSL4
Dept.of 2024- 5
ADA BCSL4
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>
Dept.of 2024- 6
ADA BCSL4
subsets[yroot].parent = xroot;
subsets[xroot].rank++;
}
}
Dept.of 2024- 7
ADA BCSL4
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:
Dept.of 2024- 9
ADA BCSL4
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
Dept.of 2024- 1
ADA BCSL4
Dept.of 2024- 1
ADA BCSL4
// 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);
Dept.of 2024- 1
ADA BCSL4
// 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:
Dept.of 2024- 1
ADA BCSL4
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;
Dept.of 2024- 1
ADA BCSL4
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:
Dept.of 2024- 1
ADA BCSL4
Dept.of 2024- 1
ADA BCSL4
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
Dept.of 2024- 1
ADA BCSL4
<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() {
Dept.of 2024- 1
ADA BCSL4
int p[10][10] = { 0 }, n, e, u, v, i, j;
Dept.of 2024- 1
ADA BCSL4
OUTPUT:
gedit 3b.c
gcc 3b.c
./a.out
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
Dept.of 2024- 2
ADA BCSL4
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;
}
Dept.of 2024- 2
ADA BCSL4
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
}
Dept.of 2024- 2
ADA BCSL4
dijkstra(graph, 0, V);
return 0;
}
OUTPUT:
Dept.of 2024- 2
ADA BCSL4
Program 5
Design and implement C Program to obtain the Topological ordering of vertices in a
given digraph.
#include <stdio.h>
#include
<stdlib.h>
// Structure to represent a
graph typedef struct {
int V;
int** adjMatrix;
} Graph;
Dept.of 2024- 2
ADA BCSL4
Dept.of 2024- 2
ADA BCSL4
// 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 2024- 2
ADA BCSL4
Dept.of 2024- 2
ADA BCSL4
OUTPUT:
Dept.of 2024- 2
ADA BCSL4
Program 6
6. Design and implement C Program to solve 0/1 Knapsack problem
using Dynamic Programming method.
#include <stdio.h>
}
// Function to solve 0/1 Knapsack problem
int knapsack(int W, int wt[], int val[], int n)
{
int i, w;
int K[n + 1][W + 1];
// K[n][W] contains the maximum value that can be put in a knapsack of capacity W
return K[n][W];
}
int main() {
Dept.of 2024- 3
ADA BCSL4
Dept.of 2024- 3
ADA BCSL4
OUTPUT:
Dept.of 2024- 3
ADA BCSL4
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>
Dept.of 2024- 3
ADA BCSL4
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:
Dept.of 2024- 3
ADA BCSL4
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>
Dept.of 2024- 3
ADA BCSL4
return 0;
}
OUTPUT:
Dept.of 2024- 3
ADA BCSL4
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;
}
Dept.of 2024- 3
ADA BCSL4
OUTPUT:
Dept.of 2024- 4
ADA BCSL4
Program 10
while (1) {
do {
i++;
} while (arr[i] < pivot);
do {
j--;
} while (arr[j] > pivot);
if (i >= j)
return j;
Dept.of 2024- 4
ADA BCSL4
}
}
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));
Dept.of 2024- 4
ADA BCSL4
start = clock();
quickSort(arr, 0, n -
1); end = clock();
free(arr);
return 0;
}
OUTPUT:
Dept.of 2024- 4
ADA BCSL4
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>
Dept.of 2024- 4
ADA BCSL4
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+
+;
}
Dept.of 2024- 4
ADA BCSL4
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;
}
Dept.of 2024- 4
ADA BCSL4
Dept.of 2024- 4
ADA BCSL4
OUTPUT:
Dept.of 2024- 4
ADA BCSL4
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)
{
Dept.of 2024- 4
ADA BCSL4
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
}
}
}
Dept.of 2024- 5
ADA BCSL4
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:
Dept.of 2024- 5