jayaramprabhudurairaj a.k.a bicepjai

physics + math + coding + dancing + muscles = bicepjai

Bit Count: Parallel Counting – MIT HAKMEM

leave a comment

Find the number of set bits in a given integer.
Parallel Counting: MIT HAKMEM Count

HAKMEM (Hacks Memo) is a legendary collection of neat mathematical and programming hacks contributed mostly by people at MIT and some elsewhere. This source is from the MIT AI LABS and this brilliant piece of code orginally in assembly was probably conceived in the late 70’s.

int BitCount(unsigned int u)
{
unsigned int uCount;

uCount = u – ((u >> 1) & 033333333333) – ((u >> 2) & 011111111111);
return ((uCount + (uCount >> 3)) & 030707070707) % 63;
}

Lets take a look at the theory behind this idea.

Take a 32bit number n;
n = a31 * 231 + a30 * 230 +…..+ ak * 2k +….+ a1 * 2 + a0;

Here a0 through a31 are the values of bits (0 or 1) in a 32 bit number. Since the problem at hand is to count the number of set bits in the number, simply summing up these co-efficients would yeild the solution. (a0 + a1 +..+ a31 ).

How do we do this programmatically?

Take the original number n and store in the count variable.
count=n;

Shift the orignal number 1 bit to the right and subtract from the orignal.
count = n – (n >>1);

Now Shift the original number 2 bits to the right and subtract from count;
count = n – (n>>1) – (n>>2);

Keep doing this until you reach the end.
count = n – (n>>1) – (n>>2) – … -( n>>31);

Let analyze and see what count holds now.
n = a31 * 231 + a30 * 230 +…..+ ak * 2k +….+ a1 * 2 + a0;

n >> 1 = a31 * 230 + a30 * 229 +…..+ ak * 2k-1 +….+ a1;
n >> 2 = a31 * 229 + a30 * 228 +…..+ ak* 2k-2 +….+ a2

;
..
n >> k = a31 * 2(31-k) + a30 * 2(30-k) +…..+ ak * 2k;

..
n>>31 = a31;

You can quickly see that: (Hint: 2k – 2k-1 = 2k-1
count = n – (n>>1) – (n>>2) – … -( n>>31) =a31+ a30 +..+a0;
which is what we are looking for;

int BitCount(unsigned int u)
{
unsigned int uCount=u;
do
{
u=u>>1;
uCount -= u;
}
while(u);
}

This certainaly is an interesting way to solve this problem. But how do you make this brilliant? Run this in constant time with constant memory!!.

int BitCount(unsigned int u)
{
unsigned int uCount;

uCount = u – ((u >> 1) & 033333333333) – ((u >> 2) & 011111111111);
return ((uCount + (uCount >> 3)) & 030707070707) % 63;
}

For those of you who are still wondering whats going? Basically use the same idea, but instead of looping over the entire number, sum up the number in blocks of 3 (octal) and count them in parallel.

After this statement uCount = n – ((n >> 1) & 033333333333) – ((n >> 2) & 011111111111);
uCount has the sum of bits in each octal block spread out through itself.

So if you can a block of 3 bits

u = a2*22 + a1*2+ a0;
u>>1 = a2*2 + a1;
u>>2 = a2;

u – (u>>1) – (u>>2) is a2+a1+a0 which is the sum of bits in each block of 3 bits.

The nexe step is to grab all these and sum them up:

((uCount + (uCount >> 3)) will re-arrange them in blocks of 6 bits and sum them up in such a way the every other block of 3 has the sum of number of set bits in the original number plus the preceding block of 3. The only expection here is the first block of 3. The idea is not to spill over the bits to adjacent blocks while summing them up. How is that made possible. Well, the maximum number of set bits in a block of 3 is 3, in a block of 6 is 6. and 3 bits can represent upto 7. This way you make sure you dont spill the bits over. To mask out the junk while doing a uCount>>3. Do and AND with 030707070707. THe only expection is the first block as I just mentioned.

What does ((uCount + (uCount >> 3)) & 030707070707) hold now?
Its 2^0 * (2^6 – 1) * sum0 + 2^1 * (2^6 – 1) * sum1 + 2^2 * (2^6 – 1) * sum2 + 2^3 * (2^6 – 1) * sum3 + 2^4 * (2^6 – 1) * sum4 + 2^5 * (2^3 – 1) * sum5
where sum0 is the sum of number of set bits in every block of 6 bits starting from the ‘low’ position.
What we need is sum0 + sum1 + sum2 + sum3 + sum4 + sum5;
2^6-1 is 63. Clearly a modulo with 63 will get you what you want.

Remember, that this works only with 32 bits numbers, For 64-bit numbers (well I will wait for comments or do this in the next post).

Written by bicepjai

August 20th, 2012 at 11:43 pm

Posted in photography

Implementation of Ukkonen’s algorithm to build a prefix tree in O(n)

leave a comment

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
import java.util.*;
 
public class ss {
public static int stacktrack;
public char TERMINATORS_RANGE = '\ud800';
public static int count=0;
public static void dfsd(Node c){
if (c.isLeaf()){
//System.out.println("\nbasecase");
//count++;
return;
}
Node a;
System.out.println(c.sons.keySet());
Iterator it = c.sons.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
a = (Node)pairs.getValue();
for(int i=0;i<stacktrack;i++)System.out.print("\t");
System.out.println(stacktrack+" br>>>>>>> ="+count+"= "+pairs.getKey() + " = " + a.edgeStart + " : " + a.edgeEnd );
stacktrack++;
count++;
dfsd(c.sons.get(pairs.getKey()));
stacktrack--;
for(int i=0;i<stacktrack;i++)System.out.print("\t");
System.out.println(stacktrack+" bt<<<<<<< ="+count+"= "+pairs.getKey() + " = " + a.edgeStart + " : " + a.edgeEnd );
}
}
public static void main(String[] args) {
/*Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
*/
String s = "abbab";
SuffixTree t1 = new SuffixTree(s);
System.out.println(t1.nofnodes());
SuffixTree t2 = new SuffixTree(new String[]{"aab","aac"});
System.out.println(t2.nofnodes());
 
SuffixTree t3 = new SuffixTree();
t3.addString("aab");
t3.addString("aac");
System.out.println(t3.nofnodes());
dfsd(t3.root);
System.out.println(count)
}
}
 
 
/*
Ukkonen's algorithm for linear time construction of suffix trees.
*/
 
class Node {
Node parent, suffixLink;
int edgeStart, edgeEnd, parentDepth;
// The edge that reaches this node contains the substring s[edgeStart, edgeEnd]
TreeMap<Character, Node> sons;
 
public Node(){
parent = suffixLink = null;
edgeStart = edgeEnd = parentDepth = 0;
sons = new TreeMap<Character, Node>();
}
 
// Returns true if there is a path starting at root having length position + 1 that ends
// in the edge that reaches this node.
public boolean inEdge(int position){
return parentDepth <= position && position < depth();
}
 
public int edgeLength(){
return edgeEnd - edgeStart;
}
 
public int depth(){
return parentDepth + edgeLength();
}
 
void link(Node son, int start, int end, String s){
// Links the current node with the son. The edge will have substring s[start, end)
son.parent = this;
son.parentDepth = this.depth();
son.edgeStart = start;
son.edgeEnd = end;
sons.put(s.charAt(start),son);
}
 
public boolean isLeaf(){
return sons.size() == 0;
}
};
 
class SuffixTree {
ArrayList<Node> nodes;
Node root, needSuffix;
int currentNode;
int length;
char TERMINATORS_RANGE = '\ud800';
int termi=0;
String generalized;
 
public SuffixTree(String str) {
nodes = new ArrayList<Node>();
currentNode = 0;
str = str + (char)TERMINATORS_RANGE;
length = str.length();
root = newNode();
build(root, str);
}
 
public SuffixTree(String[] stra) {
nodes = new ArrayList<Node>();
currentNode = 0;
root = newNode();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < stra.length; i++) {
sb.append(stra[i]);
sb.append((char)(TERMINATORS_RANGE + i));
}
generalized = sb.toString();
length = generalized.length();
build(root, generalized);
}
 
public SuffixTree() {
nodes = new ArrayList<Node>();
currentNode = 0;
root = newNode();
}
void addString(String str){
str = str+ (char)(TERMINATORS_RANGE + termi);
termi++;
length = str.length();
build(root, str);
}
int nofnodes() {
return currentNode;
}
Node newNode(){
nodes.add(currentNode,new Node());
currentNode++;
return new Node();
}
 
Node walkDown(Node c, int j, int i, String str) {
int k = j + c.depth();
if (i - j + 1 > 0){
while (!c.inEdge(i - j)){
c = c.sons.get(str.charAt(k));
k += c.edgeLength();
}
}
return c;
}
 
void addSuffixLink(Node current){
if (needSuffix != null){
needSuffix.suffixLink = current;
}
needSuffix = null;
}
 
void build(Node root, String s) {
Node c = newNode();
needSuffix = null;
root.link(c, 0, length, s);
 
// Indicates if at the beginning of the phase we need to follow the suffix link of the current node
//and then walk down the tree using the skip and count trick.
boolean needWalk = true;
 
for (int i=0, j=1; i<length-1; ++i){
char nc = s.charAt(i+1);
while (j <= i + 1){
if (needWalk){
if (c.suffixLink == null && c.parent != null) c = c.parent;
c = (c.suffixLink == null ? root : c.suffixLink);
c = walkDown(c, j, i, s);
}
 
needWalk = true;
// Here c == the highest node below s[j...i] and we will add char s[i+1]
int m = i - j + 1; // Length of the string s[j..i].
if (m == c.depth()){
// String s[j...i] ends exactly at node c (explicit node).
addSuffixLink(c);
if (c.sons.containsKey(nc)){
c = c.sons.get(nc);
needWalk = false;
break;
}else{
Node leaf = newNode();
c.link(leaf, i+1, length, s);
}
}else{
// String s[j...i] ends at some place in the edge that reaches node c.
int where = c.edgeStart + m - c.parentDepth;
// The next character in the path after string s[j...i] is s[where]
if (s.charAt(where) == nc){ //Either rule 3 or rule 1
addSuffixLink(c);
if (!c.isLeaf() || j != c.edgeStart - c.parentDepth){
// Rule 3
needWalk = false;
break;
}
}else{
Node split = newNode();
c.parent.link(split, c.edgeStart, where, s);
split.link(c, where, c.edgeEnd, s);
split.link(newNode(), i+1, length, s);
addSuffixLink(split);
if (split.depth() == 1){
//The suffix link is the root because we remove the only character and end with an empty string.
split.suffixLink = root;
}else{
needSuffix = split;
}
c = split;
}
}
j++;
}
}
}
}

Written by bicepjai

August 20th, 2012 at 6:47 am

Posted in fun,puzzled-math,techie

Difference between Vector and ArrayList in java !!

leave a comment

java.util.Vector came along with the first version of java development kit (JDK). java.util.ArrayList was introduced in java version1.2, as part of java collections framework. As per java API, in Java 2 platform v1.2,vector has been retrofitted to implement List and vector also became a part of java collection framework.

All the methods of Vector is synchronized. But, the methods of ArrayList is not synchronized. All the new implementations of java collection framework is not synchronized.

Vector and ArrayList both uses Array internally as data structure. They are dynamically resizable. Difference is in the way they are internally resized. By default, Vector doubles the size of its array when its size is increased. But, ArrayList increases by half of its size when its size is increased.

Therefore as per Java API the only main difference is, Vector’s methods are synchronized and ArrayList’s methods are not synchronized.

Vector or ArrayList? Which is better to use in java?

In general, executing a ‘synchronized’ method results in expensive performance than a unsynchronized method. Keeping the difference in mind, using Vector will incur a performance hit than the ArrayList. But, when there is a certain need for thread-safe operation Vector needs to be used.

Is there an alternate available in java for Vector?
ArrayList can be synchronized using the java collections framework utility class and then ArrayList itself can be used in place of Vector.

When there is no need for synchronized operation and you still look for better performance ‘Array’ can be used instead of ArrayList. But the development is tedious, since it doesn’t provide user friendly methods.

When you use Vector or ArrayList, always initialize to the largest capacity that the java program will need. Since incrementing the size is a expensive operation

Written by bicepjai

August 15th, 2012 at 2:50 am

Posted in fun,techie

swap variables without 3rd

leave a comment

class swap {
        public static void main(String[] args) {
                Scanner in = new Scanner(System.in);
                System.out.println("num1: ");
                int num1 = in.nextInt();
                System.out.println("num2: ");
                int num2 = in.nextInt();
                num1 = num1 + num2;
                num2 = num1 - num2;
                num1 = num1 - num2;
                System.out.println("aftr swap,num1= "+num1+"& num2= "+ num2);
        }
}

Written by bicepjai

August 9th, 2012 at 7:52 am

Posted in puzzled-math,techie

Missing Number – Bit Manipulation

leave a comment

Question: I came across in “Cracking the Coding Interview 4th edition” 5.7.

An array A[1...n] contains all the integers from 0 to n except one. In this problem, we cannot access an entire integer in A with a single operation. The elements of A are represented in binary, and the only operation we can use to access them is “fetch the jth bit of A[i ]“, which takes constant time. Find the missing integer in O(n) time.

This was confusing and online materials didnt seem to cover all the conditions. so i decided to make up the lost part in explanation. Solution depends upon the following factors.

  • (Total number of elements + 1) in the array is even or odd
  • The starting element’s respective position is 0 or 1

you can workout an example for the  LSB which would apply for rest of the bits

if (n+1 is even)
if( current_array[0]‘s LSB = 0 or current_array[0]‘s LSB = 1 ) )
if (nof_zeros > nof_ones) missing bit is 1
else  missing bit is 0

else [ if (n+1 is odd) ]
if ( current_array[0]‘s LSB = 0 ) )
if (nof_zeros > nof_ones) missing bit is 1
else  missing bit is 0
else [ if ( current_array[0]‘s LSB = 1 ) ) ]
if (nof_zeros – nof_ones == 0 or 2) missing bit is 1
else  missing bit is 0

You can remove all the other elements having current bit 0 or 1 and do the above check for the other bits,
giving us solution in  O(n)

workout an example with octal numbers and everything will be clear !

Written by bicepjai

August 1st, 2012 at 10:07 am

Posted in fun,puzzled-math,techie

Confucius inspired me !

leave a comment

“Choose a job you love, and you will never have to work a day in your life.”
― Confucius

Written by bicepjai

July 27th, 2012 at 10:46 pm

Posted in fun,puzzled-math,techie

Happy People Dancing on Planet Earth

leave a comment

What are these humans doing? Dancing. Many humans on Earth exhibit periods of happiness, and one method of displaying happiness is dancing. Happiness and dancing transcend political boundaries and occur in practically every human society. Above, Matt Harding traveled through many nations on Earth, planned on dancing, and filmed the result. The above video, the latest in a seriesof similar videos, is perhaps a dramatic example that humans from all over planet Earth feel a common bond as part of a single speciesHappiness is frequently contagious — few people are able to watch the above video without smiling.


source

Written by bicepjai

July 10th, 2012 at 8:07 pm

Posted in photography

python itertools for combinatorics is splendid !

leave a comment

Combinatoric generators in python itertools

Iterator Arguments Results
product() p, q, … [repeat=1] cartesian product, equivalent to a nested for-loop
permutations() p[, r] r-length tuples, all possible orderings, no repeated elements
combinations() p, r r-length tuples, in sorted order, no repeated elements
combinations_with_replacement() p, r r-length tuples, in sorted order, with repeated elements

Written by bicepjai

July 3rd, 2012 at 7:24 am

Posted in puzzled-math

bash IFS variable in scripting

leave a comment

This variable is used for splitting a string with delimiters. Sample code
By default the IFS=” sets the delimiter to newline
while IFS='' read -ra ADDR; do
      for i in "${ADDR[@]}"; do
          # process "$i"
      done
 done <<< "$IN"

Written by bicepjai

June 19th, 2012 at 8:20 pm

Posted in techie

find out gnome version in opensuse using command line

leave a comment

use the following command

gnome-session –version

Written by bicepjai

June 6th, 2012 at 10:32 pm

Posted in techie