You are on page 1of 7

EX.

NO:10 DATE:

GRAPH COLORING USING BACKTRACKING


AIM:
To write a java program to implement graph coloring using backtracking.

PROBLEM DESCRIPTION:
In graph theory graph coloring is a special case of graph labeling it is an assignment of labels traditionally called "colors" to elements of a graph subject to certain constraints. In its simplest form, it is a way of coloring the vertices of a graph such that no two adjacent vertices share the same color; this is called a vertex coloring. imilarly, an edge coloring assigns a color to each edge so that no two adjacent edges share the same color, and a face coloring of a planar graph assigns a color to each face or region so that no two faces that share a boundary have the same color.

ALGORITHM:
!. tart the program and define the function.

". #reate a class coloring $. %et the number of vertices in the graph. &. 'nter one if there is an edge in the graph. (. )nd enter *ero if there is no edge in the graph. +. %et the adjacency matrix of the given values. ,. -erform all possible combinations that are given. .. /isplay all the combination. 0. 'nd of the program

CLASS DIAGRAM:

Graph Colori ! )ttributes int a1212 int x12 int n 3perations void read45 void mcoloring45 void nextvalue45

PROGRAM:
So"r#$ #o%$: import java.io.6; class gcoloring 7 int a12128new int1!921!92; int x128new int1!92; int m, n; void read45 7 /ataInput tream in8new /ataInput tream4 ystem.in5; try 7 ystem.out.println4"'nter number of vertices in the graph"5; n8Integer.parseInt4in.read:ine455; ystem.out.println4"'nter ! if there is an edge 3therwise 9"5; for4int i8!;i;8n;i<<5 for4int j8!;j;8n;j<<5 7 ystem.out.println4"between "<i<" and "<j5; a1i21j28Integer.parseInt4in.read:ine455; = = catch4'xception e57= ystem.out.println4"%iven adjacency matrix is "5;

for4int i8!;i;8n;i<<5 7 for4int j8!;j;8n;j<<5 ystem.out.print4a1i21j2<">t"5; ystem.out.println45; = for4int i8!;i;8n;i<<5 x1i289; for4int i8";i;n;i<<5 7 m8i; ystem.out.println4")ll possible combinations for m 8 "<i<" are "5; mcoloring4!5; = = void mcoloring4int k5 7 do 7 nextvalue4k5; if4x1k28895 break; if4k88n5 7 for4int i8!;i;8n;i<<5 ystem.out.print4x1i2<">t"5; ystem.out.println45; = else mcoloring4k<!5; =while4true5; = void nextvalue4int k5 7 int j; do 7 x1k284x1k2<!5?4m<!5; if4x1k28895 return; for4j8!;j;8n;j<<5 7 if44a1k21j288!5 @@ 4x1k288x1j255 break; = if4j88n<!5 return; =while4true5; =

= class %raphcoloring 7 public static void main4 tring args1 25throws I3'xception 7 gcoloring g8new gcoloring45; g.read45; = =

OUTPUT

CONCLUSION: Thus the java program to implement graph coloring using back tracking has been executed and output is verified successfully.

You might also like