Daa Lab Manual
Daa Lab Manual
a LINEAR SEARCH
#include <stdio.h>
// Function to perform linear search
void linearSearch(int arr[], int size, int target, int result[], int
*resultSize) {
*resultSize = 0; // Initialize the resultSize to 0
int main() {
int size, target;
// Input the size of the array
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
// Input the elements of the array
printf("Enter the elements of the array:\n");
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
// Input the target value to search
printf("Enter the target value to search: ");
scanf("%d", &target);
// Declare an array to store matching indices
int result[size];
int resultSize;
// Perform linear search
linearSearch(arr, size, target, result, &resultSize);
// Print the matching indices
if (resultSize > 0)
{
printf("Matching indices for target %d: ", target);
for (int i = 0; i < resultSize; i++) {
printf("%d ", result[i]);
}
printf("\n");
} else {
printf("No matching indices found for target %d.\n", target);
}
return 0;
}
EXPERIMENT 1.B BINARY SEARCH
#include <stdio.h>
// Function to validate if the array is sorted in ascending order
int validateSortedArray(int arr[], int size) {
for (int i = 0; i < size - 1; ++i) {
if (arr[i] > arr[i + 1]) {
return 0; // Not sorted
}
}
return 1; // Sorted
}
// Function to perform binary search on a sorted list of integers
int binarySearch(int arr[], int size, int target) {
int low = 0;
int high = size - 1;
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// Function to merge two sorted halves of an array
void merge(int arr[], int left, int mid, int right)
{
int i, j, k;
int n1 = mid - left + 1;
int n2 = right - mid;
int main()
{
int n;
int arr[n];
printf("Enter the elements:\n");
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
return 0;
}
EX. NO.3 DFS
#include <stdio.h>
#include <stdbool.h>
int main()
{
int vertices, edges, start, end;
int adjacencyMatrix[MAX_VERTICES][MAX_VERTICES];
initializeGraph(vertices, adjacencyMatrix);
if (isConnected(vertices, adjacencyMatrix))
{
printf("The graph is connected.\n");
}
else
{
printf("The graph is not connected.\n");
}
return 0;
}
Ex. No. 4 BFS
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTICES 100
int queue[MAX_VERTICES];
int front = -1, rear = -1;
// Function to enqueue an element into the queue
void enqueue(int data) {
// Check for queue overflow
if (rear == MAX_VERTICES - 1) {
printf("Queue Overflow\n");
return;
}
// If queue is empty, set front to 0
if (front == -1) {
front = 0;
}
// Enqueue the data
queue[++rear] = data;
}
// Function to dequeue an element from the queue
int dequeue() {
// Check for queue underflow
if (front == -1 || front > rear) {
printf("Queue Underflow\n");
return -1;
}
// Dequeue the element and return
return queue[front++];
}
// Function to check if the queue is empty
int isEmpty()
{
return front == -1 || front > rear;
}
// Breadth-First Search traversal function
void bfs(int graph[MAX_VERTICES][MAX_VERTICES], int numVertices, int startVertex, int visited[]) {
// Mark the starting vertex as visited and enqueue it
visited[startVertex] = 1;
enqueue(startVertex);
// Perform BFS traversal
while (!isEmpty()) {
int currentVertex = dequeue();
printf("%d ", currentVertex);
// Visit adjacent vertices
for (int i = 0; i < numVertices; i++) {
if (graph[currentVertex][i] == 1 && !visited[i]) {
visited[i] = 1;
enqueue(i);
}
}
}
}
int main() {
int numVertices, graph[MAX_VERTICES][MAX_VERTICES], visited[MAX_VERTICES];
// Input the number of vertices
printf("Enter the number of vertices: ");
scanf("%d", &numVertices);
// Input the adjacency matrix
printf("Enter the adjacency matrix:\n");
for (int i = 0; i < numVertices; i++) {
for (int j = 0; j < numVertices; j++) {
scanf("%d", &graph[i][j]);
}
}
//Only for faculty reference, do not share the code with students
#include <stdio.h>
// Function to compute transitive closure using Warshall's algorithm
void transitiveClosure(int graph[][100], int n)
{
int i, j, k;
// Adding vertices individually
for (k = 0; k < n; k++)
{
// Fixing source vertices one by one
for (i = 0; i < n; i++)
{
// Fixing destination vertices one by one
for (j = 0; j < n; j++)
{
// If vertex k is on a path from i to j,
// then mark it as reachable
if (graph[i][k] && graph[k][j])
graph[i][j] = 1;
}
}
}
return 0;
}
EX.NO.6 FLOYD’S ALGORITHM
#include <stdio.h>
#define V 4
int dist[V][V];
int i, j, k;
dist[i][j] = graph[i][j];
printSolution(dist);
printf ("The following matrix shows the shortest distances between every pair of vertices:\n");
if (dist[i][j] == INF)
printf("%7s", "INF");
else
printf("%7d", dist[i][j]);
printf("\n");
}
int main() {
{8, 0, 2, INF},
};
floydWarshall(graph);
return 0;
#include<stdio.h>
return (a > b) ? a : b;
int i, w;
if (i == 0 || w == 0)
K[i][w] = 0;
else
return K[n][W];
// Main function
int main()
int W = 5;
printf("Maximum value that can be obtained is %d\n", knapsack(W, wt, val, n));
return 0;
#include <stdio.h>
#include <stdlib.h>
struct Edge {
};
int parent;
int rank;
};
// Function prototypes
int main() {
int V, E;
KruskalMST(edges, V, E);
free(edges);
return 0;
return subsets[i].parent;
subsets[xroot].parent = yroot;
subsets[yroot].parent = xroot;
else {
subsets[yroot].parent = xroot;
subsets[xroot].rank++;
subsets[v].parent = v;
subsets[v].rank = 0;
if (x != y) {
result[e++] = next_edge;
Union(subsets, x, y);
free(result);
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define V 9
return min_index;
int dist[V];
int sptSet[V];
dist[src] = 0;
sptSet[u] = 1;
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v])
printSolution(dist, V);
int main()
{0, 8, 0, 7, 0, 4, 0, 0, 2},
{0, 0, 0, 0, 0, 2, 0, 1, 6},
{8, 11, 0, 0, 0, 0, 1, 0, 7},
{0, 0, 2, 0, 0, 0, 6, 7, 0}};
dijkstra(graph, 0);
return 0;