Learn Programming C | C++ | Java | Android | Tips and Tricks

C Programs : Algorithms

Program to implement knapsack problem using greedy method

//Program to implement knapsack problem using greedy method What actually Problem Says ? Given a set of items, each with a weight and a value. Determine the number of each item to include in a collection so that the total weight is less than a given limit and the total value is as large as [...]

C Progran to Implement N Queen’s Problem using Backtracking

Program : C Progran to Implement N Queen’s Problem using Backtracking #include<stdio.h> #include<conio.h> #include<math.h> char a[10][10]; int n; void printmatrix() { int i,j; printf("\n"); for(i=0;i < n;i++) { for(j=0;j < n;j++) printf("%c\t",a[i][j]); printf("\n\n"); } } //------------------------------------------ int getmarkedcol(int row) { int i,j; for(i=0;i < n;i++) if(a[row][i]=='Q') { return(i); break; } } //------------------------------------------ int feasible(int row, [...]

C Program to implement prims algorithm using greedy method

C Program to implement prims algorithm using greedy method #include<stdio.h> #include<conio.h> int n, cost[10][10]; void prim() { int i,j,k,l,x,nr[10],temp,min_cost=0,tree[10][3]; /* For first smallest edge */ temp=cost[0][0]; for(i=0;i< n;i++) { for(j=0;j< n;j++) { if(temp>cost[i][j]) { temp=cost[i][j]; k=i; l=j; } } } /* Now we have fist smallest edge in graph */ tree[0][0]=k; tree[0][1]=l; tree[0][2]=temp; min_cost=temp; /* [...]