-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
252 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#include<stdio.h> | ||
#define MAX 4 | ||
#define INF 9999 | ||
|
||
void calc( int a[MAX][MAX], int anext[MAX][MAX]) | ||
{ | ||
int i,j,k; | ||
|
||
for( k=0; k<MAX ; k++ ) | ||
{ | ||
for(i=0;i<MAX;i++) | ||
{ | ||
for(j=0;j<MAX;j++) | ||
{ | ||
if(i==k) | ||
anext[i][j]=a[i][j]; | ||
|
||
else | ||
{ | ||
if( a[i][k]+a[k][j] < a[i][j] ) | ||
anext[i][j] = a[i][k]+a[k][j]; | ||
else | ||
anext[i][j] = a[i][j]; | ||
} | ||
|
||
} | ||
} | ||
|
||
|
||
for(i=0;i<MAX;i++) | ||
for(j=0;j<MAX;j++) | ||
a[i][j]=anext[i][j]; | ||
|
||
} | ||
|
||
|
||
for(i=0;i<MAX;i++) | ||
{ | ||
for(j=0;j<MAX;j++) | ||
{ | ||
printf("%d ",a[i][j]); | ||
} | ||
printf("\n"); | ||
} | ||
} | ||
|
||
|
||
int main() | ||
{ | ||
|
||
int a[MAX][MAX], anext[MAX][MAX] ; | ||
int i,j; | ||
|
||
|
||
printf("Enter adjacency matrix Enter 0 for Infinity: \n"); | ||
for(i=0;i<MAX;i++) | ||
{ | ||
for(j=0;j<MAX;j++) | ||
{ | ||
scanf("%d",&a[i][j]); | ||
if(i!=j && a[i][j]==0) | ||
a[i][j]=INF; | ||
} | ||
} | ||
|
||
|
||
printf("\n"); | ||
calc( a, anext ); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include<stdio.h> | ||
#define MAX 4 | ||
#define INF 9999 | ||
|
||
void calc( int a[MAX][MAX], anext[MAX][MAX]) | ||
{ | ||
int node = 0; | ||
int i,j,k; | ||
|
||
for( k=0; k<MAX ; k++ ) | ||
{ | ||
for(i=0;i<MAX;i++) | ||
{ | ||
for(j=0;j<MAX;j++) | ||
{ | ||
if(i==k) | ||
anext[i][j]=a[i][j]; | ||
|
||
else | ||
{ | ||
if( a[i][k]+a[k][j] < a[i][j] ) | ||
anext[i][j] = a[i][k]+a[k][j]; | ||
else | ||
anext[i][j] = a[i][j]; | ||
} | ||
|
||
} | ||
} | ||
|
||
|
||
for(i=0;i<MAX;i++) | ||
for(j=0;j<MAX;j++) | ||
a[i][j]=anext[i][j]; | ||
|
||
} | ||
|
||
|
||
for(i=0;i<MAX;i++) | ||
{ | ||
printf("%c ",(char)(i+65)); | ||
printf("%d\n", dist[i]); | ||
} | ||
|
||
} | ||
|
||
|
||
int main() | ||
{ | ||
|
||
int a[MAX][MAX], anext[MAX][MAX] ; | ||
int i,j; | ||
|
||
|
||
printf("Enter adjacency matrix: \n"); | ||
for(i=0;i<MAX;i++) | ||
{ | ||
for(j=0;j<MAX;j++) | ||
{ | ||
scanf("%d",&a[i][j]); | ||
if(a[i][j]==0) | ||
a[i][j]=INF; | ||
} | ||
} | ||
|
||
|
||
printf("\n"); | ||
calc( a, anext ); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#include <stdio.h> | ||
#include <math.h> | ||
|
||
int n; | ||
int Colors; | ||
int C[100]; | ||
int G[100][100]; | ||
|
||
int place(int node, int color) | ||
{ | ||
int i; | ||
for(i=0;i<n;i++) | ||
{ | ||
if(G[node][i]!=0) | ||
{ | ||
if(C[i]==color) | ||
return 0; | ||
} | ||
} | ||
return 1; | ||
} | ||
|
||
int putColor( int node ) | ||
{ | ||
int i, flag; | ||
for(i=0;i<Colors;i++) | ||
{ | ||
if(place(node, i)) | ||
{ | ||
C[node] = i; | ||
|
||
if( node==(n-1) ) //Nth node | ||
return 1; | ||
|
||
flag = putColor(node+1); //Next node | ||
|
||
if(flag==1) | ||
return 1; | ||
} | ||
} | ||
|
||
if( i==Colors ) //Backtrack | ||
return 0; | ||
|
||
return 1; | ||
} | ||
|
||
int main() | ||
{ | ||
int i,j; | ||
n=6; | ||
Colors=3; | ||
|
||
printf("Enter adjacency matrix: \n"); | ||
for(i=0;i<n;i++) | ||
{ | ||
for(j=0;j<n;j++) | ||
{ | ||
scanf("%d",&G[i][j]); | ||
} | ||
} | ||
|
||
for(i=0;i<n;i++) | ||
C[i]=-1; | ||
|
||
putColor(0); | ||
|
||
printf("\nSolution: "); | ||
for(i=0;i<n;i++) | ||
printf("%d ",C[i]); | ||
|
||
return 0; | ||
} |