Saturday, March 25, 2017

Graph coloring problem java implementation



Problem:

The problem is, given m colors, find a way of coloring the vertices of a graph such that no two adjacent vertices are colored using same color.

the given graphs:




java program


package graphcolor;


import java.util.ArrayList;

public class Graph {
class Node {
public int vertex;
public  int vcolor;
public ArrayList<Node> pointer =new ArrayList<Node>();
public Node( int v){
this.vertex=v;
}
}



Node[] n=new Node[7];



public Graph(){
for(int i=0;i<n.length;i++){
n[i]=new Node(i+1);
}
this.addEdge(n[0],n[1]);
this.addEdge(n[0],n[2]);
this.addEdge(n[1],n[3]);
this.addEdge(n[2],n[3]);
this.addEdge(n[2],n[4]);
this.addEdge(n[2],n[5]);
this.addEdge(n[3],n[4]);
this.addEdge(n[4],n[5]);
this.addEdge(n[6],n[6]);

}

public void addEdge(Node A, Node B){
A.pointer.add(B);
B.pointer.add(A);

}

public void colorGraph(){
for (int i=0;i<n.length;i++){
//method to finding the unused color
n[i].vcolor=this.findunusedcolor(n[i]);
}
}

public int findunusedcolor(Node n){
int unusedcolor = 0;
int usedcolor[]=new int[10];
for(int j=0;j<n.pointer.size();j++){
int c=n.pointer.get(j).vcolor;
usedcolor[j]=c;

}
int [] color=new int[3];
for(int i=0;i<color.length;i++){
color[i]=i;
}
boolean check;
for(int i=0;i<color.length;i++){
check=true;
for(int j=0;j<usedcolor.length;j++){
if(color[i]==usedcolor[j]){
check=false;
}
}
if(check){
unusedcolor=color[i];
}
}
return unusedcolor;
}

public void showGraph(){
for (int i=0;i<n.length;i++){
System.out.println("node"+ n[i].vertex+"   color"+n[i].vcolor );
}
}

}
***********************************************
package graphcolor;

public class GraphTest {

public static void main(String[] args) {
// TODO Auto-generated method stub
Graph g=new Graph();
g.colorGraph();
System.out.println("The Problem's solution is:");
g.showGraph();

}

}


Output:

****************************
The Problem's solution is:
node1   color2
node2   color1
node3   color1
node4   color2
node5   color0
node6   color2
node7   color2

Friday, March 24, 2017

Simple library management database

The MySQL link is here for complete database click here


The libtary management system ddfhksdahfksa <>



Java implementation of 4 Queen Problem by setting the one default queen initially

Problem: 
             The 4-Queens Problem consists in placing four queens on a 4 x 4 chessboard so that no two queens can capture each other. That is, no two queens are allowed to be placed on the same row, the same column or the same diagonal.



// queen test class

package fourqueen;

public class QueenTest {

public static void main(String[] args) {
// TODO Auto-generated method stub

Queen q=new Queen();
q.setQueen();
System.out.println("The Output is:");
q.showQueen();

}

}




//queen class

package fourqueen;

public class Queen {
final public int n=4;
public int a[][]=new int [n][n];

//constructor for queen
public Queen(){

for (int i=0;i<n;i++){
for  (int j=0;j<n;j++){
a[i][j]=0;
}
}
a[0][2]=1;//setting the initial queen



}

//function to check  the position is right for queen or not 
public boolean canPlaceQueen(int row,int col){
int i,j;

//checking left elements
for(i=0;i<col;i++){
if(a[row][i]==1){

return false;
}


}

//checking right elements
for( i=col;i<n;i++){

if(a[row][i]==1){

return false;
}



}


//checking upper element
for ( i=0;i<row;i++){

if(a[i][col]==1){

return false;
}

}
//checking lower element

for ( i=row;i<n;i++){
if(a[i][col]==1){
return false;
}
}
//checking left upper diagonal element 
for( i=row, j=col;i>=0&&j>=0;i--,j--){
if(a[i][j]==1){
return false;

}
}
//checking right upper diagonal element
for( i=row, j=col;i>=0&&j<n;i--,j++){
if(a[i][j]==1){
return false;

}

}

//checking left lower diagonal element
for( i=row, j=col;i<n&&j>=0;i++,j--){
if(a[i][j]==1){
return false;

}

}

//checking right lower diagonal element
for( i=row, j=col;i<n&&j<n;i++,j++){
if(a[i][j]==1){
return false;
}

}




return true;



}


public void setQueen(){

int i,j;


//checking from second row as first queen is place in first row
for(i=1;i<n;i++){

for(j=0;j<n;j++){


if(canPlaceQueen(i,j)){

a[i][j]=1;
}

}
}

}
//function to print queen
public void showQueen(){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(a[i][j]==1){
System.out.print("Q  ");
}
else
System.out.print(a[i][j]+"  ");

}
System.out.println("");
}


}










}

Output:

The Output is:
0  0  Q  0  
Q  0  0  0  
0  0  0  Q  
0  Q  0  0  






Socket Programming in Java

Source Code: client Side: package learning; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event....