Friday, November 30, 2012

WeakHashMap - Explained

http://docs.oracle.com/javase/6/docs/api/java/util/WeakHashMap.html


What is a WeakHashMap?
  • A WeakHashMap is a HashMap with Weak keys.
Weak Keys:
  • Objects with weak reference to actual objects (Keys)
Weak Reference
  • Weak reference doesn't restrict an object from being garbage collected.
When is an object available for garbage collection?
  • When an object doesn't have any reference (strong) from the program.

Weak references refer to the objects, but the presence of these reference doesn't effect the object's availability for garbage collection.

Once all the strong references to an object are gone, the object becomes available for garbage collection, irrespective of the presence/absence of Weak References to the object.


How does a WeakHashMap work?
1. Keys (weak reference to objects) & Values are inserted.
2. When the objects (keys) doesn't have any strong references, they are garbage collected.
3. Hence, the Keys in HashMap are invalid and hence the corresponding Key-Value pair is deleted.

The WeakHashMap is used for cacheing the objects, where the Entry(Key-Value) will be available only as long as the object itself is available.

Note: The WeakHashMap doesn't use equals() method, but instead uses == while comparing the keys. Since, == directly compares the object address, No two different objects can have the same address, Hence, once an object is deleted, no other object can equal to this object, (So, a new object cannot be constructed to replace the Deleted object). Hence, the keys are deleted.

Wednesday, November 28, 2012

Bayesian Networks

A Bayesian network, Bayes network, belief network, Bayes(ian) model or probabilistic directed acyclic graphical model is a probabilistic graphical model

It represents a set of random variables and their conditional dependencies via DAG
The Variables can be
1. Observable (or)
2. Latent (inferred)

Each node/variable in the network is associated with a probability function that takes as input a particular set of values for the parent variables and gives the probability of the variable represented by the node. For example, if the parents are m Boolean variables then the probability function could be represented by a table of 2^m entries, one entry for each of the 2^m possible combinations of its parents being true or false.


Parameter learning

In order to fully specify the Bayesian network and thus fully represent the joint probability distribution, it is necessary to specify for each node X the probability distribution for X conditional upon X's parents. The distribution of X conditional upon its parents may have any form. It is common to work with discrete or Gaussian distributions since that simplifies calculations.

An example Bayesian Network with parameters:

Saturday, November 24, 2012

LinkedHashMap

java.util.LinkedHashMap (link)

LinkedHashMap internally has the following

1. HashMap of Key-Value pairs
2. LinkedList of keys.

The HashMap guarantees the Constant look-up time, where as the LinkedList representation of keys internally will return the list of keys which are ordered according to the insertion order.

Note: The update of the key-value doesn't change the order of keys. Removal and insertions of keys will change the order of Keys.

Friday, November 16, 2012

K Nearest Neighbour Algorithm

K Nearest Neighbour (KNN Algorithm)

A technique to Classify the nodes into clusters.

  1. Identify the nodes.
  2. Calculate the distance between the nodes. (Cartesian, Jaccard or other Similarity Measure score)
  3. Now for each node do the following
    1. Select K nearest neighbours
    2. Draw and edge between current node and each of the K nearest nodes
  4. Finally identify the connected components in the resultant graph.

Note:
Vary the value of 'K' for a different Classification.