Posts

N QUEENS

  #include <stdio.h> #include <math.h> // This array stores the column position of queens int queenPositions[20];  int solutionCount = 0; // To count the number of solutions // Function to print the board for one solution void printSolution(int n) {     printf("\nSolution %d:\n\n", ++solutionCount);     for (int row = 1; row <= n; row++) {         for (int col = 1; col <= n; col++) {             // Print 'Q' if a queen is placed, else print '-'             if (queenPositions[row] == col) {                 printf(" Q ");             } else {                 printf(" - ");             }         }         printf("\n");     }     printf("\n"); } // Function to check if...

Knapsack backtracking

 #include<stdio.h> void knapsack(int, int[], int[], int); void backtrack(int, int, int[], int[], int, int*, int*, int[]); int main() {     int capacity, n;     printf("Enter the number of items: ");     scanf("%d", &n);     int weights[n], profits[n];     printf("Enter weights of the items: ");     for (int i = 0; i < n; i++) {         scanf("%d", &weights[i]);     }     printf("Enter profits of the items: ");     for (int i = 0; i < n; i++) {         scanf("%d", &profits[i]);     }     printf("Enter the capacity of the knapsack: ");     scanf("%d", &capacity);     knapsack(n, weights, profits, capacity);     return 0; } void knapsack(int n, int weights[], int profits[], int capacity) {     int maxProfit = 0;     int currentWeight = 0, currentProfit = 0; ...

DFS LISTS

 #include <stdio.h> #include <stdlib.h> #define MAX 100 #define initial 1 #define visited 2 struct Node {     int vertex;     struct Node* next; }; struct Node* adjList[MAX];   int state[MAX];      int stack[MAX], top = -1;  void create_graph(int n, int gtype); void add_edge(int src, int dest, int gtype); void DF_Traversal(int n); void DFS(int v); void push(int vertex); int pop(); int isEmpty_stack(); int main() {     int n, gtype;          printf("Enter 1 for undirected graph and -1 for directed graph: ");     scanf("%d", &gtype);     printf("Enter the number of vertices: ");     scanf("%d", &n);          create_graph(n, gtype);          DF_Traversal(n);          return 0; } void create_graph(int n, int gtype) {     int max_edges, src, dest;      ...

DFS MATRIX

 #include <stdio.h> #include <stdlib.h> #define MAX 100 #define initial 1 #define visited 2 int adj[MAX][MAX];   int state[MAX];      int stack[MAX], top = -1;  void create_graph(int n, int gtype); void DF_Traversal(int n); void DFS(int v, int n); void push(int vertex); int pop(); int isEmpty_stack(); int main() {     int n, gtype;          printf("Enter 1 for undirected graph and -1 for directed graph: ");     scanf("%d", &gtype);     printf("Enter the number of vertices: ");     scanf("%d", &n);          create_graph(n, gtype);          DF_Traversal(n);          return 0; } void create_graph(int n, int gtype) {     int max_edges, src, dest;          if (gtype == 1)         max_edges = (n * (n - 1)) / 2;     else if (gtype == -1) ...

Knapsack problem

#include<stdio.h> void kanpsack(int ,int[],int[],int); int max(int,int); int main() {  int capacity,n;  printf("enter the number of items:");  scanf("%d",&n);  int weights[n],profits[n];  printf("enter weights the of items:");  for(int i=0;i<n;i++)  {   scanf("%d",&weights[i]);  }  printf("enter profits the of items:");  for(int i=0;i<n;i++)  {   scanf("%d",&profits[i]);  }  printf("enter the capacity of knapsack:");  scanf("%d",&capacity);  kanpsack(n,weights,profits,capacity);  return 0; } void kanpsack(int n,int weights[],int profits[],int capacity) {  int dp[n+1][capacity+1];  for(int i=0;i<=n;i++)  {   for(int w=0;w<=capacity;w++)   {    if(i==0||w==0)    {     dp[i][w]=0;    }    else if(weights[i-1]<=w)    {     dp[i][w] = max(profits[i - 1] + dp[i - 1][w - weig...

Job Sequencing With Deadlines

  #include<stdio.h> #define MAX 100   typedef struct {     int id, profit, deadline; } Job;   void sortJobs(Job jobs[], int n) {     for (int i = 0; i < n - 1; i++)         for (int j = 0; j < n - i - 1; j++)             if (jobs[j].profit < jobs[j + 1].profit) {                 Job temp = jobs[j];                 jobs[j] = jobs[j + 1];                 jobs[j + 1] = temp;             } } void jobSequencing(Job jobs[], int n) {     int slot[MAX] = {0}, profit = 0;       sortJobs(jobs, n);       printf("The seque...

Dijkstra's Algorithm Lists

 #include <stdio.h> #include <stdlib.h> #include <limits.h> #define MAX 100 #define INF INT_MAX typedef struct Edge {     int dest;     int weight;     struct Edge* next; } Edge; typedef struct Graph {     Edge* edges[MAX]; } Graph; int dist[MAX]; int pred[MAX]; int visited[MAX]; void create_graph(Graph* graph, int n, int gtype); void dijkstra(Graph* graph, int n, int source); void print_shortest_path(int source, int destination); int main() {     int n, gtype, source;     printf("Enter 1 for undirected graph and -1 for directed graph: ");     scanf("%d", &gtype);     printf("Enter the number of vertices: ");     scanf("%d", &n);          Graph graph = { {NULL} };     create_graph(&graph, n, gtype);          printf("Enter the source vertex: ");     scanf("%d", &source);     dijkstr...