On the other hand the memory requirement for the basic trie is high. If there are n nodes, then there are n * (k + 1) pointers. The two variations that can be used are
Tuesday, 25 October 2011
Spell Suggestion Like Google / Microsoft Word
On the other hand the memory requirement for the basic trie is high. If there are n nodes, then there are n * (k + 1) pointers. The two variations that can be used are
Tuesday, 2 August 2011
Beginning Java Security on Android
One difference is that, while the default security provider for java on desktops is SunJCE the one on android is bouncycastle. So the default keystore type is BKS rather than JKS. This was not so obvious at first but, a few trials showed that, the keystore was BKS and you can also have PKCS12 keystore. The rest is all the same except that, if you create a PKCS12 keystore you need to store an entry as a PKCS12 type/stream otherwise you get an exception asking for a PKCS12 type.
Also, If the keystore path has a '.' anywhere in the path, storing the keystore throws exception. So if you stor your keystore at a location with your 'package folder' name for example 'com.myapp' it may result in exception. Removing the '.' character from the path name avoided this.
Monday, 1 August 2011
Encrypting Text Editor Crystal v 1.0 for Android
In the past few weeks I put together an android application which is similar to Crystal in my old blog post. This application allows the user to store text data on an android device by encrypting it. Crystal v 1.0 for android uses java security libraries available on android sdk. Crystal allows you to create a master password for your data files. Once you set your master password, you can write files using crystal and save them in encrypted form and read them later with your master password.
Crystal v 1.0 for android apk file is here . You will need a third party application to install it on your android phone. (Until I get my android market account set properly !)
Welcome Screen
More screen shots and how to use crystal is here
Current limitations
1) Your master password is immutable.
2) Crystal Selects the location for files on your device automatically.
3) AES is the only algorithm supported in the current version.
How it works
Crystal uses java security on android to create a master keystore on your device. This key store holds your key which is used to encrypt/decrypt your data files. The keystore has a password which is the master password. You set this password while using the application and it automatically generates a symmetric key and stores it in your keystore. The master password is never stored anywhere on your device. Your key is also safe as long as you keep your master password.
Saturday, 16 July 2011
Android User Interface Part 2
As in the previous post the steps are to
1) Define the layout in xml.
2) Define a class to represent the layout in code.
3) Inflate the layout in code.
4) Set it to an activity.
The xml layout.
-------------------
The difference is in the attributes that are specified on the xml layout that allows the buttons or any other view to take up enough space as we need it to.
The layout specifies a editview, a list view and two buttons. The two buttons have their width set to 0 dip. The table row which has the button has a collective weight sum of 2. Each of the buttons has a weight of 1. So they occupy half the width each.
The layout class
-------------------
This is similar to the class in the previous blog but, here it is sub-classed from tablelayout. The rest is same.
Using this in an activity
----------------------------
This is also the same as in previous post. Insatntiate a class of the layout just created with the activity as the context. Set it to the activity using 'setContentView' method. The method also shows how to use the arrayadapter to set values to a listview.
The advantage of using the xml layout is that, once it is defined and represented in a layout sub-class it can be used at multiple application activities on the run.
Android User Interface: Using Xml layout resources Part 1
In android Views are widgets or controls like textviews, buttons, lists and the like. Activity is what you see on the screen i.e your window or frame that is to be displayed. Layout is how you arrange / position your views on the screen. This is similar to java layouts.
Solution:
The first thing to do is to get your layout defined in an xml layout resource. This can easily be done by using the tools in eclispe for android development. Either you define the xml as shown below in hand or you can use the graphical editor.
In the xml file below, there is a textview that acts as a banner and a listview. The attributes of the views are mostly straight forward. We can get more control over the views using these attributes. For the time being we focus on getting this xml layout on the screen.
The second step is to represent layout in code using a class which is subclassed from android.widget.LinearLayout. We will use this class in code to build the layout and the views in it (inflate) at runtime.
(i) Define a class that extends from android.widget.LinearLayout
(ii) Define fields in the class for the textview and listview. For an instance of this layout class we will get the corresponding view objects into these.
(iii) Override the Constructor for the view, place a call to the super class constructor. Now we add the code to inflate the xml layout.
(iv) Inflating the layout is done using a system service in android called layout inflator service. Once we inflate the layout we get a reference to the textview and the listview of this layout by calling the findViewById method.
So far the layout is there to be used on an activity. To use this layout class in an activity,
(i) Declare this layout object in your activity.
(ii) In the onCreate method of this activity create the layout object using the current activity as context.
(iii) Set the layout using the 'setContentView' method.
You can set some values for the text view and the list view after this or in the layout class itself. Here an arrayadapter is used to set values to the listview.
The advantage of using the xml layout is that, you dont build the layout in code and keep you user interface decoupled from the code. When the layout inflator inflates the layout, it can determine the best by using the xml attributes.
** Note: Never forget to list your own new activities into the android manifest.
Sunday, 12 June 2011
Lejos
Thursday, 9 June 2011
Google.com Doodle Guitar 9 June 2011
Thursday, 2 June 2011
Spell checker design using scatter hash algorithm
The output behaviour:
Bit vector size invalid word accepted?
case a 500KB No.
case b 375KB No.
case c 250KB No.
case d 125KB Yes.
Final case: 93.75 KB Yes.
The first numbers on the bit vector memory feel not so good for a 58K word list. The mathematics that, describes the relation between the hash bit length (32 here), word list length and the probablity of an invalid word hashing correctly is described by McIlroy. This comes into play in the last two cases above. I did not prove it but, runs of the program point to that clearly. It can still be downsized but, the point was to try the scatter technique for the spell checker.
Observation: I did change the 32 values used in the hash functions which gave me a good reduction in bit vector size and lower error rate. The
final run used 32 consecutive values from the farther end of the proposed hash table size. Previously, I was using differences of 100 ie N - 100, N - 200, N -300, N -400 ...... N - (32 * 100). All that left me was with even values in the hash functions. This affected the accuracy of the hash I can say from the run of the program (i.e invalid words matched seeminlgy easy). But, when I used the consecutive values i.e N - 1, N -2....N - 32 I have introduced primes not purposefully. But, the urge to change the hash values was there. McIlroy says that, the use of n primes for the n hash functions is very good and he used it. A lot of reading on the web claimed that, the use of primes in hash function and their advantage was not that great. But, this small program with only a couple of primes seems to be faring better than with out them. Why 32? McIlroy used 27 I picked 32!. :)). This choice is to be based on the mathematical relation I pointed to above (refer McIlRoy paper).
The program when profiled in Netbeans takes 0.169ms to find if a word in the dictionary. A run of the program on this 573 word post gave optimized, Netbeans, McIlroy, atleast, probablity, seeminlgy as invalid. True. None of these were in the word list. "seeminlgy" is misspelt in this post! The checking code took 20.4 ms.
Friday, 20 May 2011
Designing a spell check or corrector. Round 1
Saturday, 14 May 2011
Serialization to the rescue
The Problem: To display visual warning to users on a PDA (MDA Compact 3 & HTC Qtek S9100) when they cross the speed limit. Simple enough. But, there are strings attached. You have to build the feature on an existing solution which displays a map on the screen and provides navigation, traffic info to the users. The exisitng solution is keeping track of the location via gps <= every second and you have to use that data. Now, the display should be like the speed signs on the streets. You need to provide audio alerts too. Things are not simplified by the fact that, a version of TomTom will also be running on the PDA. PDA has 200MHz processor, 64MB RAM. There is the previous version with still limited features that, the software is to work. How do we show the sign/live feed on the screen ? A sample sign
Thursday, 12 May 2011
Oho! algorithms
At huge N for N = 1248, the Juggler takes almost twice the time of the Reverser. 6.99 and 13.1.
Tuesday, 10 May 2011
Getting runtime from ~ 15 seconds to 4 seconds
The Problem: The anagram problem as discussed by Bentley. For a quick example 'rickets', 'sticker' and 'tickers' are anagrams. Using a permutations approach is defenitely unacceptable because of the time complexity. This brings us to the signature approach where we identify anagrams based on signatures. My program uses quick sort to build a signature. The partition code for the quick sort is that discussed by Bentley as I know from my college days the bugs and the confusion involved in coding the partition using the regular method.
The input to the code is a 58111 long list of english words.
Case 1: Using a swap function, quick sort and signature function. Output was dumped to the screen using 'System.out'. The total run time of the program was around 15 seconds. The quick sort took close to under 5 seconds. The screen dump took around 7-8 seconds !.
* I replaced the screen dump code with a write to a text file. So, I wrote all the anagrams (the hash map contents) to a text file. But, this was done using Buffered Writer. This saved 8 seconds leaving the whole code to around 6-7seconds.
This is a general approach I always used.
Case 2: I had a look at the time taken by the functions and noted the following. Notice the quicksort, partition, swap methods in the hot spots window. The invocation frquency for the simple swap is a place to tune. I replaced the 3 line swap code at 3 invocation points in the quick sort. So swap as a seperate function is gone.
Before the substitution was made partition code took colse to 2 seconds. After eliminating the method invocation, it takes 414 ms ! Quick sort now takes 2.3 seconds. But, overall time saved is 2 seconds! Now the runtime is 3.6 seconds compared to 5.5 before.
Case 3: Something I have taken for granted is Horners method in hashing a string. This is explained by Robert Lafore in his book. Hasing the strings took 120 ms.
Case 4: Since we can save time on method invocations, I believed that a non-recursive version of quick sort can have a serious effect on the running time. This was not the case. The self time of the quick sort function was more by 1 second. The program itself was a bit slow by 3-4 seconds. There are more instructions in the quicksort function so, the increase is 'explainable'. The stack which was used to bypass the recurssion was being called, for pop and push a huge number of times. The point is that, the non recursive version of quick sort did not yield anything better. On the contrary it is performing a bit worse (7-9 seconds for the whole program ! looking for the missing 3-4 seconds ).
Thursday, 27 January 2011
Crystal v1.0 An encryting text editor.
Some technical details.
1. It uses TripleDES key to encrypt your data onto the disk.
2. The data on the disk i.e. the encrypted binary is in base64 encoding.
3. Built out of SUN JAVA.
Features:
1. It performs the actions of a simple text editor well.
2. Provides encryption for the data. TripleDES with a strong key is good enough and is supported by SUN Java.
3. User provides password from which the encryption key is generated.
4. Since it is purely Java it can run on multiple platforms.
Limitations and Future features:
1. Handles only one file at version 1.0.
2. AES encryption on next version.
Installation:
3. Since it used TripleDES you will also need unlimited encryption strength policy files from SUN website here OR https://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/ViewProductDetail-Start?ProductRef=jce_policy-6-oth-JPR@CDS-CDS_Developer
Sunday, 16 January 2011
Bit Vector approach in Java
An example bit vector representing the numbers 8, 10, 2, 4, 7 is 0010100110100000 with the corresponding bit location set. (0 is inclusive in the list)
/**
*
* @author Hari
*/
public class BitVector
{
//the array of bytes.
byte[] bitvector;
//this is the maximum number we expect in the range.
int nMaxLimit;
//byte count needed.
int nByteCount;
// the zero mask.
byte byZero = 0x00;
//byte values used to set and retireve individual bits.
...................
...................
...................
...................
...................
// Generate the bytes used to set - reset bits in the sequence
private void genSet()
{
bySets[0] = 0x01;
for(int nIndex = 1; nIndex < 8; nIndex++)
{
bySets[nIndex] = (byte) (bySets[nIndex - 1] << 1);
}//for
}//
// Initialize all the bits to zero.
private void setAllToZero()
{
for(int nIndex = 0; nIndex< nByteCount; nIndex++)
{
bitvector[nIndex] &= byZero;
}//for
}//
//set the nth bit in the array.
public void setAt(int n) throws Exception
{
...................
...................
...................
...................
...................
locInVector = qt;
bitvector[locInVector] |= bySets[7 - rem];
}//
// check if n is in the vector. i.e by checking the nth bit in the byte array.
public boolean isPresent(int n) throws Exception
{
if(n > nMaxLimit || n < 0)
throw new Exception("Argument outside range of bit vector.");
int locInVector;
int qt = n / 8;
int rem = n % 8;
locInVector = qt;
if((bitvector[locInVector] & bySets[7 - rem])==0)
{
return false;
}
return true;
}//
...................
...................
...................
...................
...................
}//class
Wednesday, 12 January 2011
Alpha Rex Complete
Overall pretty impressed with what lego mindstorms came up with. Now remains using regular code rather than vpl.
The video is here
http://www.youtube.com/watch?v=9WuMRkO8lSk
Wednesday, 15 December 2010
Cloud Computing 2. Ground level
This did not help much though when it came to a complete non-tech person. So, I again use an example that was used in the seminar I attended. Consider your work place in an IT company, you may be availing a lot of services such as email and intranet sitting at your computer. If you are accessing a leave management system like I did some time ago, you could look at the back of your computer and see a network cable. Following the cable you end up on the socket on the wall and then you plough out the lines and still follow them to a switch then to a router and you finally find yourself in the server room of your company. Then, you follow the cables to the server that, was hosting the leave application software for everyone in that location. So you have pin pointed the fixed server/hardware, the room, the air conditioning, the people who maintain that (by this time you should have met them), saw the power supply, back-up and the like. Now you get the general idea. You, the user, located it feeling.
Now, if you ended up in huge warehouse with containers upon containers (holding the cable of course), and you see that, there is a lot of hardware with thousands of network connections going to as many stacks and you simply cannot pin point which one is your application server by yourself then, you are dealing with a cloud. Basically, you may have ended up with something like this.
http://www.youtube.com/watch?v=K3b5Ca6lzqE
There is a video of the Google Datacenter too.
Notice/see the mentioning of network, storage, cooling, power supply etc which you might have located in your server room too but, in a minute scale compared to this. So your data, application are on some server that is in this huge software + hardware infrastructure. There might be someone else’s data, application residing there too.
So did this explain the concept? It does not seem to be so very new outright but, the approach as we will see to utilizing such hardware software infrastructure and deriving advantages from it is new.
Cloud Computing - 1 The beginning
I have been reading up on a number of stuff the past couple of months and have consolidated stuff to put on my blog. One of the topics is cloud computing. This was rather vague in the sense that, if, I referred to any material couple of years back I used to get different ideas. But, a seminar I attended recently helped me to get some basic ideas to build upon.
What is there so much to understand about cloud computing? You may ask. Rather than being able to work with it in all its glory, you would be better off if, you can explain simply and clearly to an executive, preferably from a non-IT background, about the what is, how, why of cloud computing. Not agreeing to this? A lot of literatures just do this and only this. Why? Because as long as the execs don’t see the up-side, no computing is going to take off. So explaining the whole idea, selling cloud seems to be challenge I am facing now. So who started all this? A colleague some time ago asked how I would explain cloud computing to my grandma and expect her to figure out some advantage on her own. I am never going be able to explain to my grandma because, she is not around. But, the idea was still hanging around. After this, I just continued with my reading to understand cloud more and pursue some development too.
The rest of the stuff such as developing something to run on the cloud for an example, using the Google App Engine etc is left to books and technology literature directed at the tech people. I am going through these too and will put on some stuff as I progress. These will be totally technical and nothing in the direction of the para above.
The next few blogs would focus on specific aspects/points that, I got from my literature review which I did not come across in general.
Wednesday, 27 October 2010
Fun Fun Fun with Robotics
It took me a while to build it manually, and the VPL program was larger than most other examples.
It was all worth the effort. Why? There are a lot of reasons for a software engineer to be happy with this.
- First, not a single line of code has been written!! All the instructions are using the VPL that comes with the mindstorms studio. Yes, it does compile and all that stuff but, hey I did not have to type in the code. I was dragging 'logic blocks' to get the job done.
- Second, with this a software engineer can imagine something and he has to imagine not only the logic but, also dream something that is mechanically feasible.
- Third, you have to build it mechanically!!. Finally, something tangible compared to the programs and processes.
The walking mechanism is here in this video
http://www.youtube.com/watch?v=s9h9i2o8YnU
The touch sensors usage is very very impressive so is the technique used to walk. Stuff written on the package is true. 'Only three motors, 2 touch sensors, ultra sonic sensor, color sensor but a lot of possibilities'
Monday, 25 October 2010
Visual Programming Language - Lego Nxt2.0 Robotics Kit
I had a copy of MS Robotics Studio and played around with the studio. Missing was the sensors and motors i.e a robot kit.
Recently, I got a Nxt2.0 Kit and started doing some VPL programs and running it on the servo motors, sensors in the kit.
1) It is really nice to see and use the VPL studio that comes with the kit.
2) Even though there are only a limited number of sensors and motors, there is a lot of things that can be imagined and programmed.
3) Not to mention that, MSRS is compatible with this kit and we can code specifics in addition to the VPL capabilities.
When you read a books on Robotics, you see terms like actuators, sensors, services, concurrent execution etc and it was upto imagination to piece them together. But, with a kit like this it makes understanding things a lot more easier.
I tried out a few of the vehicles and really enjoyed building and programming with VPL. One of the sensors I really like is the Ultrasonic sensor that can be used to measure distance. The shooter robot was really interesting. The technique to shoot the colored balls is really impressive. (old pool game idea). My bot was ready and I gave it a try.
The video is here ---> http://www.youtube.com/watch?v=bhB_lYPPCto
It senses when some thing comes closer than one foot and fires. Simple. The villain was the only thing I can get my hands on before the batteries ran dry, a rolled up carpet.
Next would be Finishing the alpha rex humanoid and then moving to using custom code with this.
And yes, there are a lot of other robots and kits you can get your hands on these days. But, this is definitely a good place to start.

