Monday, January 31, 2011

0/1-Kanpsack Algorithm



Suppose you have been ordered to bring some elements from a place where there are m boxes full of precious elements.
( each box contains elements of only one type. ) . Each element has a weight w_i and a value p_i , where i denotes the box number.

You are told  all the parameters ( weight and value ) of the boxes in advance . But you can not carry more than n units of weight  with your knapsack .Because your boss has given this to you. Now the problem is to find out   what is the highest amount of profit you can make with the knapsack you have. ( The more you make profit, the more your boss become impressed.)

Now take the pencil  and paper and figure out what you should do . Consider one box at a time. Say the box number is k .(Assume that there is no box whose number is higher than k ) You can make two things with the k^{th} box,  

Case 1: Pick an element from the box . It will cause your knapsack size to be reduced by w_k , adding p_k to your total amount of profit.

Case 2: Do not pick an element from the box. That will cause the number of boxes you are considering , reduced by one that is k-1. ( Be careful , when you are not taking an element from on box , you are discarding the box from your consideration. In future you will not be able to take any element from that box )

let profit( i , j) denote the profit you can make when you have i sized knapsack and j boxes. Then according to the first one of the cases above , you have 

                    profit( i-w_j , j  )+p_j  , 

that is your knapsack size is shorten by w_j and you have added p_j to the total amount of profit possible by the reduced knapsack.

Other case says the following ,

                               profit( i , j - 1)

that is , you are assuming that you have only j-1 boxes .

The problem is a maximization problem. So you have to take the maximum of the two situation . Then the solution becomes ,

profit(i,j) = max \left\{ profit(i - w_j , i) + p_j \\ profit( i , j - 1)

and your destination is to find profit(n,m). Here Knapsack size means how much of weight it can hold. 


Thursday, January 20, 2011

Insert blob files into Oracle using Java




In this article  I will discuss how to insert a image into database table using PreparedStatement interface of java. All other blob files can be handled in same way. You will need ojdbc6.jar as the oracle driver. If you try to use ojdbc14.jar you may get the following exception :

Exception in thread "main" java.lang.AbstractMethodError:
 Theoracle.jdbc.driver.T4CPreparedStatement.setBinaryStream(ILjava/io/InputStream;)V


Now let's create a database table using the following sql command.

create table imagetable ( imageid numeric(5) , image blob);

Now use the following java code to insert a image in the table.

Class.forName("oracle.jdbc.driver.OracleDriver"); 
Connection connect = DriverManager.getConnection  
                       ("jdbc:oracle:thin:@IP_ADDRESS:PORT_NUMBER/SERVICE_NAME" ,"username","password"); 
String filename = "Your filename here" ; 
InputStream input = new FileInputStream(filename); 
PreparedStatement pstmt = connect.prepareStatement("insert into imagetable values(?,?)"); 
pstmt.setBinaryStream(2, input); 
pstmt.setInt(1, 1); 
pstmt.execute();


Also change the connection parameters according to your's.