Find probability of selecting element from kth column after N iterations | C # implementation of the above approach ; Function to calculate probability ; declare dp [ ] [ ] and sum [ ] ; precalculate the first row ; calculate the probability for each element and update dp table ; return result ; Driver code
using System ; class GFG { static int n = 4 ; static int m = 4 ; static float calcProbability ( int [ , ] M , int k ) { float [ , ] dp = new float [ m , n ] ; float [ ] sum = new float [ n ] ; for ( int j = 0 ; j < n ; j ++ ) { dp [ 0 , j ] = M [ 0 , j ] ; sum [ 0 ] = sum [ 0 ] + dp [ 0 , j ] ; } for ( int i = 1 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { dp [ i , j ] += dp [ i - 1 , j ] / sum [ i - 1 ] + M [ i , j ] ; sum [ i ] += dp [ i , j ] ; } } return dp [ n - 1 , k - 1 ] / sum [ n - 1 ] ; } public static void Main ( ) { int [ , ] M = { { 1 , 1 , 0 , 3 } , { 2 , 3 , 2 , 3 } , { 9 , 3 , 0 , 2 } , { 2 , 3 , 2 , 2 } } ; int k = 3 ; Console . Write ( calcProbability ( M , k ) ) ; } }