added code to handle sparse probability matrix