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  






Friday, December 2, 2016

Tricks to extend the windows license



Try following steps to extends the windows license

·        1.  Run cmd (commad prompt)in admin mode




     Write a command :    slmgr –rearm


 


    3.   Restart 

Sunday, November 6, 2016

Some example of recursion in C programming language

Recursion:

1.     Write a program to find the factorial of number using recursive method.


Source code:
#include<stdio.h>
#include<conio.h>
int factorial(int n)
{
    if(n==0||n==1)
    return 1;
    else
    return n*factorial(n-1);
}
void main()
{        int n,x;
                 printf("Enter the number:");
                 scanf("%d",&n);
                 x=factorial(n);
                  printf("The factorial of %d=%d",n,x);
                  getch();
}

2.     Write a program to multiply two numbers  by recursive method.

Source code:
#include<stdio.h>
#include<conio.h>
int multiplication(int a,int b)
{
    if(b==1)
    return a;
    else
    return a+multiplication(a,b-1);
}
 void main()
 {     int a,b,x;
       clrscr();
       printf("Enter the first number to multiply::");
       scanf("%d",&a);
       printf("Enter the second number to multiply::");
       scanf("%d",&b);
       x=multiplication(a,b);
       printf("%d*%d=%d",a,b,x);
       getch();

 }

3.     Write a program to find the number one exponent to other(ab) using recursion

Source code:
#include<stdio.h>
#include<conio.h>
int power(int a,int b)
{     if(b==0)
      return 1;
      else
      return a*power(a,b-1);
}

void main()
  {   int a,b,x;
      printf("Enter the number a:") ;
      scanf("%d",&a);
      printf("Enter the number b:");
      scanf("%d",&b);
      x=power(a,b);
      printf("%d power %d=%d",a,b,x);
      getch();
  }

4.     Write a program to slove the tower of Hanoi problem using recursion.

Source code:
#include<stdio.h>
#include<conio.h>
void movedisk(int n,char s,char t,char a)
{
if(n==1)
  {
printf("\n move disk from %c to %c",s,t);
return;

  }
movedisk(n-1,s,a,t);
printf("\n move disk from %c to %c",s,t);



movedisk(n-1,a,t,s);
  }
  main()
  {
                  int n;
                  clrscr();
                  printf("How many disk\n:");
                  scanf("%d",&n);
                  movedisk(n,'s','t','a');
                  getch();
                  return 0;
  }



Friday, November 4, 2016

simple user interface using java swing

import javax.swing.*;

import org.omg.CORBA.PRIVATE_MEMBER;

import java.awt.ActiveEvent;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Event;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.geom.Point2D;

public class appframe extends JFrame{

private JLabel item1=new JLabel("Name");
private JLabel item2=new JLabel("Address");
private JTextField name;
private JTextField address;


private JButton okbutton;
private JButton cancelbutton;


private JPanel buttonpanel;
private JPanel textpanel;

public appframe()
{   //setting the title of the app
super("The title v 1.00");

setLayout(new FlowLayout());




//creating the button panel
buttonpanel=new JPanel();

//creating text fields

textpanel=new JPanel();

//creating text field
name=new JTextField(20);
address=new JTextField(25);


//creatin buttons
okbutton=new JButton("OK");
cancelbutton =new JButton("Cancel");
//adding textfield to textpanel
textpanel.add(item1);
textpanel.add(name);
textpanel.add(item2);
textpanel.add(address);




//adding the buttons to buttonpanel
buttonpanel.add(okbutton,BorderLayout.SOUTH);
buttonpanel.add(cancelbutton );
//adding button panel to frame

add(textpanel,BorderLayout.NORTH);
add(buttonpanel,BorderLayout.SOUTH);

thehandler handler = new thehandler();//action listener


okbutton.addActionListener(handler);
cancelbutton.addActionListener(handler);







}
private class thehandler implements ActionListener{


public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub



if(e.getActionCommand()=="OK"){
  buttonpanel.setBackground(Color.blue);

}
if(e.getActionCommand()=="Cancel"){
buttonpanel.setBackground(Color.RED);
}

}
}}



//
create a main method as follows
import javax.swing.JFrame;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;


public class test {

public static void main(String[] args) {
// TODO Auto-generated method stub
 appframe window1=new appframe();
window1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window1.setSize(640,600);


window1.setLocation(100,100);
window1.setVisible(true);
window1.setResizable(false);

}




}



Thursday, November 3, 2016

LISP(list processing)

1.     An overview on lisp(list processing) programming.

Introduction:

 Lisp, an acronym for list processing is a programming language that was designed for easy manipulation of data strings. In lisp all computation is expressed as a function if at least one object. Objects can be other function, data item (such as constants or variables), or data structure. Lisp ability to compute with symbolic expression rather than number makes it convent for AI application.

In, lisp statements are written in prefix notation (e.g. + 2 3). It execute the statement through read-eval-print cycle.

Loop
Read in an expression from the console;
Evaluate the expression;
Print the result of evaluation to the console;

End Loop



Everything in lisp is either an atom or a list. Atom is an operator which determines whether something passed to it is an atom or list. Atom are either constant or variable. List is linearly array of element.

Socket Programming in Java

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