Вы находитесь на странице: 1из 40

NAME:

DEPT:
YEAR:

SEM:

ROLL NO:
SUB:

1.BellamanFord
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
struct Edge{
int src, dest, weight;
};
struct Graph{
int V, E;
struct Edge* edge;
};
struct Graph* createGraph(int V, int E){
struct Graph* graph = (struct Graph*) malloc( sizeof(struct Graph) );
graph->V = V;
graph->E = E;
graph->edge = (struct Edge*) malloc( graph->E * sizeof( struct Edge ) );
return graph;
}
void printArr(int dist[], int n){
printf("Vertex Distance from Source\n");
for(int i = 0; i < n; ++i)
printf("%d \t\t %d\n", i, dist[i]);
}
void BellmanFord(struct Graph* graph, int src){
int V = graph->V;
int E = graph->E;
int dist[V];
for(int i = 0; i < V; i++)
dist[i] = INT_MAX;
dist[src] = 0;
2

for(int i = 1; i <= V-1; i++) {


for(int j = 0; j < E; j++) {
int u = graph->edge[j].src;
int v = graph->edge[j].dest;
int weight = graph->edge[j].weight;
if(dist[u] != INT_MAX && dist[u] + weight < dist[v])
dist[v] = dist[u] + weight;
}
}
for(int i = 0; i < E; i++) {
int u = graph->edge[i].src;
int v = graph->edge[i].dest;
int weight = graph->edge[i].weight;
if(dist[u] != INT_MAX && dist[u] + weight < dist[v])
printf("Graph contains negative weight cycle");
}
printArr(dist, V);
return;
}
int main(){
int V = 5;
int E = 8;
struct Graph* graph = createGraph(V, E);
graph->edge[0].src = 0;
graph->edge[0].dest = 1;
graph->edge[0].weight = -1;

graph->edge[1].src = 0;
graph->edge[1].dest = 2;
graph->edge[1].weight = 4;

graph->edge[2].src = 1;
graph->edge[2].dest = 2;
graph->edge[2].weight = 3;

graph->edge[3].src = 1;
graph->edge[3].dest = 3;
graph->edge[3].weight = 2;

graph->edge[4].src = 1;
graph->edge[4].dest = 4;
graph->edge[4].weight = 2;

graph->edge[5].src = 3;
graph->edge[5].dest = 2;
graph->edge[5].weight = 5;

graph->edge[6].src = 3;
graph->edge[6].dest = 1;
graph->edge[6].weight = 1;

graph->edge[7].src = 4;
graph->edge[7].dest = 3;
graph->edge[7].weight = -3;

BellmanFord(graph, 0);

return 0;
}

2.BinarySearch
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int a;
void input(){
printf("Enter the length of array : ");
scanf("%d",&a);
}
int main(){
input();
int arr[a];
int start,end,middle,i,item;
for(i=0; i<a; i++) {
printf("Enter element : ");
scanf("%d",&arr[i]);
}
printf("Enter the element to be searched : ");
scanf("%d",&item);
start = 0; end = a-1;
middle = (start+end)/2;
while(item != arr[middle] && start <= end) {
if(item > arr[middle])
start = middle+1;
else
end = middle-1;
middle = (start+end)/2;
} if(item == arr[middle])
printf("%d found at position %d \n",item,(middle+1));
if(start > end)
printf("%d not found in array\n",item);}
6

3.Floyds
#include<stdio.h>
#define V 4
#define INF 99999
void printSolution(int dist[][V]);
void floydWarshell (int graph[][V])
{
int dist[V][V];
int i, j, k;
for(i = 0; i < V; i++)
for(j = 0; j < V; j++)
dist[i][j] = graph[i][j];
for(k = 0; k < V; k++)
{
for(i = 0; i < V; i++)
{
for(j = 0; j < V; j++)
{
if(dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
printSolution(dist);
}
void printSolution(int dist[][V])
{
printf("Following matrix shows the shortest distances"
" between every pair of vertices \n");
for(int i = 0; i < V; i++)
{
8

for(int j = 0; j < V; j++)


{
if(dist[i][j] == INF)
printf("%7s", "INF");
else
printf("%7d", dist[i][j]);
}
printf("\n");
}
}
int main()
{
int graph[V][V] = { {0, 5, INF, 10},
{INF, 0, 3, INF},
{INF, INF, 0, 1},
{INF, INF, INF, 0}
};
floydWarshell(graph);
return 0;
}

10

4.GraphColouring
#include<stdio.h>
#define V 4
void printSolution(int color[]);
int isSafe (int v, int graph[V][V], int color[], int c){
for(int i = 0; i < V; i++)
if( graph[v][i] && c == color[i])
{return 0;
}
return 1;
}
int graphColoringUtil( int graph[V][V], int m, int color[], int v){
if(v == V)
return 1;
for(int c = 1; c <= m; c++) {
if(isSafe(v, graph, color, c)) {
color[v] = c;
if(graphColoringUtil (graph, m, color, v+1) == 1)
return 1;
color[v] = 0;
}
}
return 0;
}
int graphColoring(int graph[V][V], int m){
int*color = new int[V];
for(int i = 0; i < V; i++)
color[i] = 0;
if(graphColoringUtil(graph, m, color, 0) == 0) {
printf("Solution does not exist");

11

return 0;
}
printSolution(color);
return 1;
}
void printSolution(int color[])
{
printf("Solution Exists:\n Following are the assigned colors \n");
for(int i = 0; i < V; i++)
printf(" %d ", color[i]);
printf("\n");
}
int main(){
int graph[V][V] = {{0, 1, 1, 1},
{1, 0, 1, 0},
{1, 1, 0, 1},
{1, 0, 1, 0}, };
int m = 3;
graphColoring (graph, m);
return 0;
}

12

13

5.Hamiltonian
#include<stdio.h>
#define V 5
void printSolution(int path[]);
int isSafe(int v, int graph[V][V], int path[], int pos){
if(graph [ path[pos-1] ][ v ] == 0)
return 0;
for(int i = 0; i < pos; i++)
if(path[i] == v)
return 0;
return 1;
}
int hamCycleUtil(int graph[V][V], int path[], int pos){
if(pos == V) {
if( graph[ path[pos-1] ][ path[0] ] == 1 )
return 1;
else
return 0;
}
for(int v = 1; v < V; v++) {
if(isSafe(v, graph, path, pos)) {
path[pos] = v;
if(hamCycleUtil (graph, path, pos+1) == 1)
return 1;
path[pos] = -1;
}
}
return 0;
}
int hamCycle(int graph[V][V]){
14

int*path = new int[V];


for(int i = 0; i < V; i++)
path[i] = -1;
path[0] = 0;
if( hamCycleUtil(graph, path, 1) == 0) {
printf("\nSolution does not exist");
return 0;
}
printSolution(path);
return 1;
}
void printSolution(int path[]){
printf("Solution Exists:"
" Following is one Hamiltonian Cycle \n");
for(int i = 0; i < V; i++)
printf(" %d ", path[i]);
printf(" %d ", path[0]);
printf("\n");}
int main(){
int graph1[V][V] = {{0, 1, 0, 1, 0},
{1, 0, 1, 1, 1},
{0, 1, 0, 0, 1},
{1, 1, 0, 0, 1},
{0, 1, 1, 1, 0}, };
hamCycle(graph1);
int graph2[V][V] = {{0, 1, 0, 1, 0},
{1, 0, 1, 1, 1},
{0, 1, 0, 0, 1},
{1, 1, 0, 0, 0},
{0, 1, 1, 0, 0}, };
hamCycle(graph2);
return 0;}
15

16

6.HeapSort
#include<stdio.h>
#include<conio.h>
void heapsort(int[],int);
void heapify(int[],int);
void adjust(int[],int);
int main(){
int n,i,a[50];
printf("\nEnter the limit:");
scanf("%d",&n);
printf("\nEnter the elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
heapsort(a,n);
printf("\nThe Sorted Elements Are:\n");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
printf("\n");
}
void heapsort(int a[],int n){
int i,t;
heapify(a,n);
for(i=n-1;i>0;i--) {
t = a[0];
a[0] = a[i];
a[i] = t;
adjust(a,i);
}
}
void heapify(int a[],int n){
int k,i,j,item;
17

for(k=1;k<n;k++) {
item = a[k];
i = k;
j = (i-1)/2;
while((i>0)&&(item>a[j])) {
a[i] = a[j];
i = j;
j = (i-1)/2;
}
a[i] = item;
}
}
void adjust(int a[],int n){
int i,j,item;
j = 0;
item = a[j];
i = 2*j+1;
while(i<=n-1) {
if(i+1 <= n-1) {
if(a[i] <a[i+1]){
i++;
if(item<a[i]){
a[j] = a[i];
j = i;
i = 2*j+1;
}
else
break;
}
a[j] = item;
}
}}
18

19

7.JobSequence with Deadline


#include<conio.h>
#include<stdio.h>
int n,i,j,k,t;
int check(int s[],int p) {
int ptr=0;
for(int i=0;i<n;i++) {
if(s[i]==p)
ptr++;
}
if(ptr==0)
return 1;
else
return 0;
}
int main(){
printf("enter the no of jobs

: ");

scanf("%d",&n);
int slot[n],profit,p[n],d[n];
for(i=0;i<n;i++) {
printf("\n enter the profit of job #%d

:",i+1);

scanf("%d",&p[i]);
printf("\n enter the deadline of job #%d :",i+1);
scanf("%d",&d[i]);
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(p[i]<p[j]){
t=p[i];
p[i]=p[j];
p[j]=t;
20

t=d[i];
d[i]=d[j];
d[j]=t;
}
}
for(i=0;i<n;i++)
slot[i]=0;
}
for(i=0;i<n;i++) {
for(j=d[i];j>0;j--)
{
if(check(slot,j)==1)
{slot[i]=j;
break;
}
}
printf("\n\n INDEX PROFIT DEADLINE SLOT ALLOTTED ");
for(i=0;i<n;i++) {
if(slot[i]>0)
printf("\n\n %d

%d

%d

[%d - %d]", i+1,p[i],d[i],(slot[i]-1),slot[i]);

%d

%d

REJECTED", i+1,p[i],d[i]);

else
printf("\n\n %d
}
return 0;
getch();
}
}

21

22

8.KnapSack
#include<stdio.h>
#include<conio.h>
int max(int a, int b){
return(a>b)?a:b;
}
int knapsack(int W, int wt[], int val[], int n){
if(n ==0 || W == 0)
return 0;
if(wt[n-1] > W)
return knapsack(W, wt,val,n-1);
else
return max(val[n-1]+knapsack(W-wt[n-1],wt,val,n-1), knapsack(W,wt,val,n-1));
}
int main(){
int val[] = {60,100,120};
int wt[] = {10,20,30};
int W = 50;
int n = sizeof(val)/sizeof(val[0]);
printf("%d", knapsack(W, wt, val, n));
return 0;
}

23

24

9.Kruskal
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int i,j,k,a,b,u,v,n,ne=1;
int min,mincost=0,cost[9][9],parent[9];
int find(int);
int uni(int,int);
int main(){
printf("\n\n\tImplementation of Kruskal's algorithm\n\n");
printf("\nEnter the no. of vertices\n");
scanf("%d",&n);
printf("\nEnter the cost adjacency matrix\n");
for(i=1;i<=n;i++) {
for(j=1;j<=n;j++) {
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
}
printf("\nThe edges of Minimum Cost Spanning Tree are\n\n");
while(ne<n) {
for(i=1,min=999;i<=n;i++) {
for(j=1;j<=n;j++) {
if(cost[i][j]<min) {
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
25

u=find(u);
v=find(v);
if(uni(u,v)) {
printf("\n%d edge (%d,%d) =%d\n",ne++,a,b,min);
mincost +=min;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n\tMinimum cost = %d\n",mincost);
getch();
}
int find(int i){
while(parent[i])
i=parent[i];
return i;
}
int uni(int i,int j){
if(i!=j) {
parent[j]=i;
return 1;
}
return 0;
}

26

27

10.MatrixMultiplication
#include<stdio.h>
#include<limits.h>
#include<conio.h>
int MatrixChainOrder(int p[], int i, int j){
if(i == j)
return 0;
int k;
int min = INT_MAX;
int count;
for(k = i; k <j; k++){
count = MatrixChainOrder(p, i, k) +
MatrixChainOrder(p, k+1, j) +
p[i-1]*p[k]*p[j];

if(count < min)


min = count;
}
return min;
}
int main(){
int arr[] = {1, 2, 3, 4, 3};
int n = sizeof(arr)/sizeof(arr[0]);
printf("Minimum number of multiplications is %d ",
MatrixChainOrder(arr, 1, n-1));
getchar();
return 0;
}

28

29

11.MergeSort
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
#define MAX_ARY 10
void merge_sort(int x[], int end, int start);
int main(void){
int ary[MAX_ARY];
int j = 0;
printf("\n\nEnter the elements to be sorted: \n");
for(j=0;j<MAX_ARY;j++)
scanf("%d",&ary[j]);
printf("Before :");
for(j = 0; j < MAX_ARY; j++)
printf(" %d", ary[j]);
printf("\n");
merge_sort(ary, 0, MAX_ARY - 1);
printf("After Merge Sort :");
for(j = 0; j < MAX_ARY; j++)
printf(" %d", ary[j]);
printf("\n");
getch();
}
void merge_sort(int x[], int end, int start){
int j = 0;
const int size = start - end + 1;
int mid = 0;
int mrg1 = 0;
int mrg2 = 0;
int executing[MAX_ARY];
if(end == start)
30

return;
mid = (end + start) / 2;
merge_sort(x, end, mid);
merge_sort(x, mid + 1, start);
for(j = 0; j < size; j++)
executing[j] = x[end + j];
mrg1 = 0;
mrg2 = mid - end + 1;
for(j = 0; j < size; j++) {
if(mrg2 <= start - end){
if(mrg1 <= mid - end){
if(executing[mrg1] > executing[mrg2])
x[j + end] = executing[mrg2++];
else
x[j + end] = executing[mrg1++];
}
else
x[j + end] = executing[mrg2++];
}
else
x[j + end] = executing[mrg1++];
}
}

31

32

12.N-Queen Problem
#include<stdio.h>
#include<math.h>
#include<conio.h>
int abs(int);
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;
for (i = 0; i< n; i++)
if (a[row][i] == 'Q') {
return (i);
break;
}
}
int feasible(int row, int col) {
int i, tcol;
for (i = 0; i< n; i++) {
tcol = getmarkedcol(i);
if (col == tcol || abs(row - i)== abs(col - tcol))
return 0;
}
33

return 1;
}
void nqueen(int row) {
int i, j;
if (row < n) {
for (i = 0; i< n; i++) {
if (feasible(row, i)) {
a[row][i] = 'Q';
nqueen(row + 1);
a[row][i] = '.';
}
}
} else {
printf("\nThe solution is:- ");
printmatrix();
}
}
int main() {
int i, j;
printf("\nEnter the no. of queens:- ");
scanf("%d", &n);
for (i = 0; i< n; i++)
for (j = 0; j < n; j++)
a[i][j] = '.';
nqueen(0);
return (0);
}
int abs(int x){
if (x<0)
x=x*-1;
return x;
}
34

35

13.Prims
#include<stdio.h>
#include<conio.h>
int a,b,u,v,n,i,j,ne=1;
int visited[10]={0},min,mincost=0,cost[10][10];
int main(){
printf("\n Enter the number of nodes:");
scanf("%d",&n);
printf("\n Enter the adjacency matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++) {
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999; }
visited[1]=1;
printf("\n");
while(ne<n) {
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]<min)
if(visited[i]!=0) {
min=cost[i][j];
a=u=i; b=v=j; }
if(visited[u]==0 || visited[v]==0) {
printf("\n Edge %d:(%d %d) cost:%d",ne++,a,b,min);
mincost+=min;
visited[b]=1; }
cost[a][b]=cost[b][a]=999; }
printf("\n Minimun cost=%d",mincost);
getch();
return 0;}
36

37

14.QuickSort
#include<stdio.h>
#include<conio.h>
int partition(int[],int,int);
void swap (int a[], int left, int right){
int temp;
temp=a[left];
a[left]=a[right];
a[right]=temp;
}
void quicksort( int a[], int low, int high ){
int pivot;
if ( high > low ) {
pivot = partition( a, low, high );
quicksort( a, low, pivot-1 );
quicksort( a, pivot+1, high );
}
}
int partition( int a[], int low, int high ){
int left, right;
int pivot_item;
int pivot = left = low;
pivot_item = a[low];
right = high;
while ( left < right ) {
while( a[left] <= pivot_item )
left++;
while( a[right] > pivot_item )
right--;
if ( left < right )
swap(a,left,right);
38

}
a[low] = a[right];
a[right] = pivot_item;
return right;
}
void printarray(int a[], int n){
int i;
for (i=0; i<n; i++)
printf(" %d ", a[i]);
printf("\n");
}
int main(){
int a[50], i, n;
printf("\nEnter no. of elements: ");
scanf("%d", &n);
printf("\nEnter the elements: \n");
for (i=0; i<n; i++)
scanf ("%d", &a[i]);
printf("\nUnsorted elements: \n");
printarray(a,n);
quicksort(a,0,n-1);
printf("\nSorted elements: \n");
printarray(a,n);
getch();
}

39

40

Вам также может понравиться