Posts

Showing posts from August, 2024

lab12

 #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);  printf(" - N Queens Problem Using Backtracking -");  printf("\n\nEnter number of Queens:");  scanf("%d",&n);  queen(1,n);  return 0;  }  //function for printing the solution  void print(int n)  {  int i,j;  printf("\n\nSolution %d:\n\n",++count);  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  }  }  }  /*funtion to check conflicts  If no conflict for desired postion returns 1 otherwise returns 0*/  int place(int row,int column)  {  int i;  for(i=1;i<=row-1;++i)  {...

lab11

 #include <stdio.h>  #include <stdlib.h>  #include <time.h>  // Merge two subarrays of arr[].  // First subarray is arr[l..m]  // Second subarray is arr[m+1..r]  void merge(int arr[], int l, int m, int r) {  int i, j, k;  int n1 = m - l + 1;  int n2 = r - m;  // Create temporary arrays  int L[n1], R[n2];  // Copy data to temporary arrays L[] and R[]  for (i = 0; i < n1; i++)  L[i] = arr[l + i];  for (j = 0; j < n2; j++)  R[j] = arr[m + 1 + j];  // Merge the temporary arrays back into arr[l..r]  i = 0;  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++;  }  // Copy the remaining elements of L[], if any  while (i < n1) {  arr[k] = L[i];  i++;  k++;  }  36  Department of CSE (CY),...

lab10

 #include <stdio.h>  #include <stdlib.h>  #include <time.h>  // Function to partition the array and return the pivot index  int partition(int arr[], int low, int high) {  int pivot = arr[high]; // Pivot element  int i = (low - 1);     // Index of smaller element  for (int j = low; j <= high - 1; j++) {  // If current element is smaller than or equal to pivot  if (arr[j] <= pivot) {  i++; // Increment index of smaller element  // Swap arr[i] and arr[j]  int temp = arr[i];  arr[i] = arr[j];  arr[j] = temp;  }  }  // Swap arr[i + 1] and arr[high] (or pivot)  int temp = arr[i + 1];  arr[i + 1] = arr[high];  arr[high] = temp;  return (i + 1);  }  // Function to implement Quick Sort  void quickSort(int arr[], int low, int high) {  if (low < high) {  // pi is partitioning index, arr[pi] is now at right place  int p...

lab9

 #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;  printf("Enter the number of elements (n): ");  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;  }  // Generate n random numbe...

lab8

 #include <stdio.h>  #include <stdbool.h>  #define MAX_SIZE 100  // Function to find subset with given sum  void subsetSum(int set[], int subset[], int n, int subSize, int total, int  nodeCount, int sum) {  if (total == sum) {  // Print the subset  printf("Subset found: { ");  for (int i = 0; i < subSize; i++) {  printf("%d ", subset[i]);  }  printf("}\n");  return;  } else {  // Check the sum of the remaining elements  for (int i = nodeCount; i < n; i++) {  subset[subSize] = set[i];  subsetSum(set, subset, n, subSize + 1, total + set[i], i + 1, sum);  }  }  }  int main() {  29  Department of CSE (CY), RNSIT          IV Semester          BCSL404 – ADA LABORATORY    Department of CSE (CY), RNSIT         30          int set[MAX_SIZE]; ...

lab7

 PROGRAM:  #include <stdio.h>  #include <stdlib.h>  // Structure to represent items  struct Item {  int value;  int weight;  double ratio; // Value-to-weight ratio for sorting  };  // Comparison function for sorting items based on ratio in descending order  int compare(const void *a, const void *b) {  struct Item *item1 = (struct Item *)a;  struct Item *item2 = (struct Item *)b;  double ratio1 = item1->ratio;  double ratio2 = item2->ratio;  if (ratio1 > ratio2) return -1;  else if (ratio1 < ratio2) return 1;  else return 0;  }  // Function to solve discrete Knapsack problem  void discreteKnapsack(struct Item items[], int n, int capacity) {  int i, j;  int dp[n + 1][capacity + 1];  // Initialize the DP table  for (i = 0; i <= n; i++) {  for (j = 0; j <= capacity; j++) {  if (i == 0 || j == 0)  dp[i][j] = 0;  else if...

lab6

 #include <stdio.h>  // Function to find maximum of two integers  int max(int a, int b) {  return (a > b) ? a : b;  }  // 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];  // Build table K[][] in bottom-up manner  for (i = 0; i <= n; i++) {  for (w = 0; w <= W; w++) {  if (i == 0 || w == 0)  K[i][w] = 0;  else if (wt[i - 1] <= w)  K[i][w] = max(val[i - 1] + K[i - 1][w - wt[i - 1]], K[i - 1][w]);  else  K[i][w] = K[i - 1][w];  }  }  // 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  int W, n; // Knapsack capacity and number of items  printf("Enter the number of items: ");  scanf("%d", &n);  printf("Enter the values and ...

lab5

 #include <stdio.h>  #include <stdlib.h>  #define MAX_VERTICES 100  // Structure to represent a graph  typedef struct {  int V;  int** adjMatrix;  } Graph;  // Function to create a new graph  Graph* createGraph(int V) {  Graph* graph = (Graph*)malloc(sizeof(Graph));  graph->V = V;  graph->adjMatrix = (int**)calloc(V, sizeof(int*));  for (int i = 0; i < V; i++) graph->adjMatrix[i] = (int*)calloc(V, sizeof(int));  return graph;  }  // Function to add an edge to the graph  void addEdge(Graph* graph, int src, int dest) {  graph->adjMatrix[src][dest] = 1;  }  // Function to perform topological sorting  void topologicalSort(Graph* graph) {  int V = graph->V, inDegree[MAX_VERTICES] = {0},  queue[MAX_VERTICES], front = 0, rear = -1;  for (int i = 0; i < V; i++)  for (int j = 0; j < V; j++)  if (graph->adjMatrix[i][j] == 1) ...

lab4

 #include <stdio.h>  #include <stdbool.h>  #include <limits.h>  #define MAX_VERTICES 10  // Maximum number of vertices  #define INF INT_MAX  // 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;  }  // A utility function to print the constructed distance array  void printSolution(int dist[], int V) {  printf("Vertex \t\t Distance from Source\n");  for (int i = 0; i < V; i++)  printf("%d \t\t %d\n", i, dist[i]);  }  // Dijkstra's algorithm for adjacency matrix representation of the graph  void dijkstra(int graph[MAX_VERTICES][MAX_VERTICES], int src, int V) {...

lab3

   #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;              printf("\n Enter the number of vertices:");       scanf("%d",&n);      ...

lab2

 #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 cons...

lab1

   #include <stdio.h>        #include <stdlib.h>                #define MAX_EDGES 1000                typedef struct Edge {            int src, dest, weight;        } Edge;                typedef struct Graph {            int V, E;            Edge edges[MAX_EDGES];        } Graph;                typedef struct Subset {            int parent, rank;        } Subset;                Graph* createGraph(int V, int E) {            Graph* graph = (Graph*) malloc(sizeof(Graph));            graph->V = V;    ...