Professional Documents
Culture Documents
TECHNOLOGY
Session 2023-24
Submitted to:
Mr. Narendra Kumar Rajput
(Assistant Professor)
Mission
M1: To impart high quality education in the science of computing.
M2: To prepare educated and skilled computer professionals.
M3: To create excellence by providing comprehensive knowledge of the
latest tools and technologies in the domain of computer science, so that
students strive to become leaders.
M4: To inculcate ethical values in students so that they understand their
responsibility towards the nation with focus on upliftment of all sections of
society.
M5: To facilitate establishment of research centers and encourage students
to solve complex technological problems.
Program Educational Objectives
PEO1: Make valuable contributions to design, development and
production in the practice of computer science and engineering in related
engineering areas or application areas, and at the interface of computers
and physical systems.
PSO2: Ability to design and develop software for web based and mobiles
androids under real world environment.
#include <stdio.h>
if (arr[mid] == target) {
return mid; // Target found
} else if (arr[mid] < target) {
return binarySearchRecursive(arr, mid + 1, high, target);
} else {
return binarySearchRecursive(arr, low, mid - 1, target);
}
} else {
return -1; // Target not found
}
}
// Linear Search
int linearSearch(int arr[], int size, int target) {
for (int i = 0; i < size; ++i) {
if (arr[i] == target) {
return i; // Target found
}
}
return -1; // Target not found
}
int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int size = sizeof(arr) / sizeof(arr[0]);
int targetBinary = 7;
int targetLinear = 3;
// Binary Search
int resultBinary = binarySearchRecursive(arr, 0, size - 1, targetBinary);
if (resultBinary != -1) {
printf("Binary Search: Element %d found at index %d\n", targetBinary,
resultBinary);
} else {
printf("Binary Search: Element %d not found\n", targetBinary);
}
// Linear Search
int resultLinear = linearSearch(arr, size, targetLinear);
if (resultLinear != -1) {
printf("Linear Search: Element %d found at index %d\n", targetLinear,
resultLinear);
} else {
printf("Linear Search: Element %d not found\n", targetLinear);
}
return 0;
}
Output:
if (largest != i) {
swap(&arr[i], &arr[largest]);
heapify(arr, size, largest);
}
}
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int size = sizeof(arr) / sizeof(arr[0]);
heapSort(arr, size);
return 0;
}
Output:
Original array: 12 11 13 5 6 7
Sorted array: 5 6 7 11 12 13
Exp 03 : Program for Merge Sort
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int size = sizeof(arr) / sizeof(arr[0]);
return 0;
}
Output:
Original array: 12 11 13 5 6 7
Sorted array: 5 6 7 11 12 13
Exp 04 : Program for Selection Sort
int main() {
int arr[] = {64, 25, 12, 22, 11};
int size = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, size);
return 0;
}
Output:
Original array: 64 25 12 22 11
Sorted array: 11 12 22 25 64
Exp 05 : Program for Insertion Sort
// Move elements of arr[0..i-1] that are greater than key to one position
ahead of their current position
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
int main() {
int arr[] = {12, 11, 13, 5, 6};
int size = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, size);
return 0;
}
Output:
Original array: 12 11 13 5 6
Sorted array: 5 6 11 12 13
Exp 06 : Program for Quick Sort.
return 0;
}
Output:
Original array: 10 7 8 9 1 5
Sorted array: 1 5 7 8 9 10
Exp 07 : Find Minimum Spanning Tree using Kruskal’s
Algorithm.
Algorithm: Below are the steps for finding MST using Kruskal’s algorithm:
The graph contains 9 vertices and 14 edges. So, the minimum spanning tree
formed will be having (9 – 1) = 8 edges.
After sorting:
Step 6: Pick edge 8-6. Since including this edge results in the cycle, discard it.
Pick edge 2-3: No cycle is formed, include it.
Step 7: Pick edge 7-8. Since including this edge results in the cycle, discard it.
Pick edge 0-7. No cycle is formed, include it.
Step 8: Pick edge 1-2. Since including this edge results in the cycle, discard it.
Pick edge 3-4. No cycle is formed, include it.
Note: Since the number of edges included in the MST equals to (V – 1), so the
algorithm stops here
Exp 08 : Find Minimum Cost Spanning Tree of a given
undirected graph using Prim’s algorithm.
Note: For determining a cycle, we can divide the vertices into two sets [one set
contains the vertices included in MST and the other contains the fringe vertices.]
Step 1: Firstly, we select an arbitrary vertex that acts as the starting vertex of the
Minimum Spanning Tree. Here we have selected vertex 0 as the starting vertex.
Step 2: All the edges connecting the incomplete MST and other vertices are the
edges {0, 1} and {0, 7}. Between these two the edge with minimum weight is {0,
1}. So include the edge and vertex 1 in the MST.
Step 3: The edges connecting the incomplete MST to other vertices are {0, 7},
{1, 7} and {1, 2}. Among these edges the minimum weight is 8 which is of the
edges {0, 7} and {1, 2}. Let us here include the edge {0, 7} and the vertex 7 in
the MST. [We could have also included edge {1, 2} and vertex 2 in the MST].
Step 4: The edges that connect the incomplete MST with the fringe vertices are
{1, 2}, {7, 6} and {7, 8}. Add the edge {7, 6} and the vertex 6 in the MST as it
has the least weight (i.e., 1).
Step 5: The connecting edges now are {7, 8}, {1, 2}, {6, 8} and {6, 5}. Include
edge {6, 5} and vertex 5 in the MST as the edge has the minimum weight (i.e., 2)
among them.
Step 6: Among the current connecting edges, the edge {5, 2} has the minimum
weight. So include that edge and the vertex 2 in the MST.
Step 7: The connecting edges between the incomplete MST and the other edges
are {2, 8}, {2, 3}, {5, 3} and {5, 4}. The edge with minimum weight is edge {2,
8} which has weight 2. So include this edge and the vertex 8 in the MST.
Step 8: See here that the edges {7, 8} and {2, 3} both have same weight which
are minimum. But 7 is already part of MST. So we will consider the edge {2, 3}
and include that edge and vertex 3 in the MST.
Step 9: Only the vertex 4 remains to be included. The minimum weighted edge
from the incomplete MST to 4 is {3, 4}.
The final structure of the MST is as follows and the weight of the edges of the
MST is (4 + 8 + 1 + 2 + 4 + 2 + 7 + 9) = 37.
Note: If we had selected the edge {1, 2} in the third step then the MST would
look like the following.
Exp 09 : Implement N Queen Problem using Backtracking
Problem : The idea is to place queens one by one in different columns, starting
from the leftmost column. When we place a queen in a column, we check for
clashes with already placed queens. In the current column, if we find a row for
which there is no clash, we mark this row and column as part of the solution. If
we do not find such a row due to clashes, then we backtrack and return false
Algorithm:
1. Create a set sptSet (shortest path tree set) that keeps track of vertices included
in the shortest path tree, i.e., whose minimum distance from the source is
calculated and finalized. Initially, this set is empty.
2. Assign a distance value to all vertices in the input graph. Initialize all distance
values as INFINITE.
Assign the distance value as 0 for the source vertex so that it is picked first.
Note: We use a boolean array sptSet[] to represent the set of vertices included in
SPT. If a value sptSet[v] is true, then vertex v is included in SPT, otherwise not.
Array dist[] is used to store the shortest distance values of all vertices.
Illustration :
To understand the Dijkstra’s Algorithm lets take a graph and find the shortest
path from source to all nodes.
The following subgraph shows vertices and their distance values, only the
vertices with finite distance values are shown. The vertices included in SPT are
shown in green colour.
Step 2:
1. Pick the vertex with minimum distance value and not already included in
SPT (not in sptSET). The vertex 1 is picked and added to sptSet.
2. So sptSet now becomes {0, 1}. Update the distance values of adjacent
vertices of 1.
3. The distance value of vertex 2 becomes 12.
Step 3:
1. Pick the vertex with minimum distance value and not already included in
SPT (not in sptSET). Vertex 7 is picked. So sptSet now becomes {0, 1, 7}.
2. Update the distance values of adjacent vertices of 7. The distance value of
vertex 6 and 8 becomes finite (15 and 9 respectively).
Step 4:
1. Pick the vertex with minimum distance value and not already included in
SPT (not in sptSET). Vertex 6 is picked. So sptSet now becomes {0, 1, 7, 6}.
2. Update the distance values of adjacent vertices of 6. The distance value of
vertex 5 and 8 are updated.
We repeat the above steps until sptSet includes all vertices of the given graph.
Finally, we get the following Shortest Path Tree (SPT).