Saturday, May 18, 2013

NQueen Problem using backtracking.

File: Location.h
#ifndef nQueen_Location_h
#define nQueen_Location_h

const int UNDEFINED = -1;

class Location {
private:
    int row;
    int col;
public:
    Location();
    Location( int row, int col );
    void setLocation( int row, int col );
    void setLocation( Location* location );
    int getRow();
    int getCol();
};
#endif

File: Location.cpp
#include "Location.h"

Location::Location(){
    row = UNDEFINED;
    col = UNDEFINED;
};

Location::Location( int _row, int _col ){
    row = _row;
    col = _col;
};

void Location::setLocation( int _row, int _col ) {
    row = _row;
    col = _col;
};
void Location::setLocation( Location* location ) {
    row = location->getRow();
    col = location->getCol();
};

int Location::getCol() {
    return col;
};

int Location::getRow(){
    return row;
};

File: Queen.h
#ifndef nQueen_Queen_h
#define nQueen_Queen_h

#include "Location.h"
#include <stdlib.h>

class Queen {
private:
    Location* location;
    
public:
    Queen();
    Queen( int row, int col );
    bool canAttack( int row, int col );
    int getRow();
    int getCol();
    Location* getLocation();
    void setLocation(int row, int col);
    void resetLocation();
    ~Queen();   
};

#endif

File: Queen.cpp
#include "Queen.h"
Queen::Queen() {
    Queen( UNDEFINED, UNDEFINED );
};

Queen::Queen( int row, int col ){
    location = new Location( row, col );
};

bool Queen::canAttack( int row, int col ){
    int myRow = getRow();
    int myCol = getCol();
    if ( myCol == UNDEFINED || myRow == UNDEFINED ) {
        return false;
    }
    if ( myRow == row || myCol == col ) {
        return true;
    }
    /* Compute the all 4 diagonal */
    int rowDiff = abs ( row - myRow );
    int colDiff = abs ( col - myCol );
    if ( rowDiff == colDiff ) {
        return true;
    }
    return false;
};

Location* Queen::getLocation() {
    return location;
};

void Queen::setLocation( int _row, int _col ){
    
    if ( location == nullptr ) {
        location = new Location( _row, _col);
        return;
    }
    location->setLocation(_row, _col );
   
};

void Queen::resetLocation() {
    setLocation(UNDEFINED, UNDEFINED);
}

int Queen::getRow() {
    return location->getRow();
}

int Queen::getCol() {
    return location->getCol();
}

Queen::~Queen(){
    delete location;
}

File: Board.h
#ifndef nQueen_Board_h
#define nQueen_Board_h

#include "Queen.h"

class Board{
private:
    Queen* queens;
    int boardSize;
    int numOfSoln;
    bool _solve( int row, Queen* queen );
    bool isCellSafe( int row, int col );
public:
    Board( int boardSize);
    void print();
    void solve();

};
#endif

File: Board.cpp

#include "Board.h"
#include<iostream>
using namespace std;

bool Board::_solve( int row, Queen* queen ){
    
    if ( row == boardSize ) {
        print();
        return true;
    }
    
    Queen* subQueen = queen;
    for ( int col = 0; col < boardSize; col ++ ) {
        if ( isCellSafe(row, col ) ) {
            subQueen->setLocation( row, col );
            _solve(row + 1, subQueen + 1 );
            subQueen->resetLocation();
        }
    }
    return false;
    
};

bool Board::isCellSafe( int row, int col ) {
    Queen* tmpQueen = queens;
    for ( int i = 0 ; i < boardSize; i ++ ) {
        if ( tmpQueen->canAttack( row, col ) ) {
            return false;
        }
        tmpQueen++;
    }
    return true;
};

Board::Board( int _boardSize) {
    
    boardSize = _boardSize;
    queens = new Queen[boardSize];
    for( int i = 0 ; i < boardSize; i++ ) {
        Queen* tmpQueen = new Queen( UNDEFINED, UNDEFINED );
        queens[i] = *tmpQueen;
    }
};

void Board::print(){
    Queen* rowQueen = queens;
    numOfSoln++;
    cout << "Printing solution: " << numOfSoln << "\n";
    for( int rownum = 0 ; rownum < boardSize; rownum++ ) {
        cout << rownum << ", " << rowQueen->getCol() << endl;
        rowQueen++;
    }
};

void Board::solve() {
    if ( _solve(0, queens ) ){
        print();
        return;
    }
    if ( numOfSoln == 0 ) {
        cout << "Could not find a solution" << endl;
    }   
};

File: main.cpp
#include<iostream>

#include "Board.h"

int main(int argc, const char * argv[]) {
    
    if ( argc !=  2 ) {
        fprintf(stderr, "Please provide board size.");
        return 1;
    }
    int size = atoi( argv[1]);
    Board* board = new Board( size);
    board->solve();
    delete board;
    return 0;
}
If something is not write please let me know.

Saturday, February 2, 2013

Password Less SSH

SSH is linux utility to logon to a remote machine for executing command on it.
Every time you ssh to any machine you need to provide username/password to securely login on that machine.

Once a username/password is authenticated, you are logged on that machine and can execute the commands on the remote machine. But to successfully execute a password on remote machine without human intervention, you need to setup SSH such that it don't prompt for password.
E.g.: Hadoop applications requires that Master machine should be able to login to all the slaves machine in cluster password less SSH.

 Linux SSH utility provides a easy way to achieve the password less SSH.
Create  SSH Keys
     ssh-keygen -t rsa  -f ~/.ssh/id_rsa -P ''
       The options are
         -t rsa: Specifies the type of key to create
         -f Specifies the filename of the key file.
         -P Passpharse. Note: we are using empty passphrase.

Copy the public key on the remote machine.
    scp ~/.ssh/id_rsa.pub user@remote.machine:/tmp/id_rsa_.pub_test

Log on to the remote.machine.

Append the content of the file to .ssh/authtozed_keys.
     cat /tmp/id_rsa_.pub_test > ~/.ssh/authorized_keys

Ensure following on the remote machine.
   -  ~/.ssh has permission set to 700
   -  ~/.ssh/authorized_keys has permission set to 640

Alternatively you could use ssh-copy-id command to copy the public key on the remote machine. ssh-copy-id ensures that the correct permissions are set on the remote machine.
  > ssh-copy-id user@remote.machine

Try to SSH on the remote machine, SSH should not ask for the passphase and you should be logged on the remote server.

Reference:
    ssh-keygen linux man page.
    ssh-copy-id linux man page.

Saturday, April 28, 2012

Bitwise Operations Part I


This is the first artical on Bitwise Operation, and today I will discuss how to multiple numbers using bitwise operation.

Before we dwell in the multiplication lets do some quick overview of bitwise left shift operations. Left shift operator is represented by ‘<<’.

Whenever we left shift a number by 1 we move each bit present at position x to position x+1, and append 0 at the position zero (Also, known as LSB.)
Left shift example:

E.g.:
Left shift 13 by 1.
13 is represented as 1101
13 << 1 = 11010

Similarly we want to left shift something by 5 digits, each bit is moved 5 positions to left and last 5 LSB bits are replaced with zero.
13 << 5= 110100000
For multiplication we only need to know about left shift so lets get started.

Example 1: Multiple 13 and 2 To multiply any number represented in binary we need to left shift the number by 1. So, if we want to multiply 13 by 2 we just need to perform 1 operation.
 i.e. 13 << 1
The above operation produces: 11010, which is equivalent to 26 ( 13*2 = 26 )in decimal.
Example 2:  Calculate 170* 2
Binary of 170 is 10101010
Left shift 10101010 < 1 = 101010100 ( 340 in decimal )
Conclusion: Left shift a number by 1 to multiply a number by 2.

The above method can be extended multiply a number by 4,8,16,32 and so on… So, if you want to multiple a number  ‘x’ by 4, multiply the number x with 2, 2 times.
I.e.:
x<<1      // to get x*2 and again
x<<1      //to get x*2*2
/*  The above expression is equivalent to x<<2. */

In general, if we want to multiple a number ‘x’ with another number which can be represented as 2^n, we can get the result by doing x<<n. ( Left shit x by n ) Multiplying x with y, where x  > 1 and y > 1 In binary each bit can take 2 values 0 or 1 and each bit correspond to value which can be determined as follows: If the bit at position n is set, it means that that set bit add 2^n to the total value of the number.

E.g.:
=  1     0     0     1     0      0     0     1 

= 2^7   2^6   2^5   2^4   2^3   2^2   2^1  2^0

= 128   0     0     16    0     0     0    1

= 128 + 16 + 1

= 145

So if we want to multiple any number ‘x’ with 145, how do we calculate the result using bitwise operation.
To demonstrate the working we will multiply 11 with 145 (Result should be 1595.)
In the binary representation of 145, bits at position 7, 4, 0  (0 based positioning. )

Calculation:
= 11 * 145

= 11 * (2^7 + 2^4 + 2^0)

= 1011 * (2^7 + 2^4 + 2^0)              // 11 in binary is 1011

= 1011 * 2^7  + 1011 * 2^4 + 1011 * 2^0 

= 1011 <<7 + 1011 << 4 + 1011 << 0      

= 10110000000 + 10110000 + 1011   

= 11000111011                           // 1595 in binary

= 1595

So to get the result we need to represent the number in terms of power of two.  And then use the bitwise operation and addition to get the result of a complicated multiplication.