Given a Markov chain G, we have the find the probability of reaching the state F at time t = T if we start from state S at time t = 0.
A Markov chain is a random process consisting of various states and the probabilities of moving from one state to another. We can represent it using a directed graph where the nodes represent the states and the edges represent the probability of going from one node to another. It takes unit time to move from one node to another. The sum of the associated probabilities of the outgoing edges is one for every node.
Consider the given Markov Chain( G ) as shown in below image:Â Â
Examples:Â
Input : S = 1, F = 2, T = 1 Output : 0.23 We start at state 1 at t = 0, so there is a probability of 0.23 that we reach state 2 at t = 1. Input : S = 4, F = 2, T = 100 Output : 0.284992
We can use dynamic programming and depth-first search (DFS) to solve this problem, by taking the state and the time as the two DP variables. We can easily observe that the probability of going from state A to state B at time t is equal to the product of the probability of being at A at time t – 1 and the probability associated with the edge connecting A and B. Therefore the probability of being at B at time t is the sum of this quantity for all A adjacent to B.Â
Below is the implementation of the above approach:Â Â
C++
| // C++ implementation of the above approach#include <bits/stdc++.h>usingnamespacestd;  // Macro for vector of pair to store// each node with edge#define vp vector<pair<int, float> >  // Function to calculate the// probability of reaching F// at time T after starting// from SfloatfindProbability(vector<vp>& G, intN,                      intF, intS, intT){    // Declaring the DP table    vector<vector<float> > P(N + 1, vector<float>(T + 1, 0));      // Probability of being at S    // at t = 0 is 1.0    P[S][0] = 1.0;      // Filling the DP table    // in bottom-up manner    for(inti = 1; i <= T; ++i)        for(intj = 1; j <= N; ++j)            for(autok : G[j])                P[j][i] += k.second * P[k.first][i - 1];      returnP[F][T];}  // Driver codeintmain(){    // Adjacency list    vector<vp> G(7);      // Building the graph    // The edges have been stored in the row    // corresponding to their end-point    G[1] = vp({ { 2, 0.09 } });    G[2] = vp({ { 1, 0.23 }, { 6, 0.62 } });    G[3] = vp({ { 2, 0.06 } });    G[4] = vp({ { 1, 0.77 }, { 3, 0.63 } });    G[5] = vp({ { 4, 0.65 }, { 6, 0.38 } });    G[6] = vp({ { 2, 0.85 }, { 3, 0.37 }, { 4, 0.35 }, { 5, 1.0 } });      // N is the number of states    intN = 6;      intS = 4, F = 2, T = 100;      cout << "The probability of reaching "<< F         << " at time "<< T << " \nafter starting from "         << S << " is "<< findProbability(G, N, F, S, T);      return0;} | 
Java
| // Java implementation of the above approachimportjava.util.*;  classGFG{    staticclasspair     {        intfirst;        doublesecond;          publicpair(intfirst, doublesecond)        {            this.first = first;            this.second = second;        }    }      // Function to calculate the    // probability of reaching F    // at time T after starting    // from S    staticfloatfindProbability(Vector<pair>[] G,                         intN, intF, intS, intT)    {        // Declaring the DP table        float[][] P = newfloat[N + 1][T + 1];        Â        // Probability of being at S        // at t = 0 is 1.0        P[S][0] = (float) 1.0;          // Filling the DP table        // in bottom-up manner        for(inti = 1; i <= T; ++i)            for(intj = 1; j <= N; ++j)                for(pair k : G[j])                    P[j][i] += k.second * P[k.first][i - 1];          returnP[F][T];    }      // Driver code    publicstaticvoidmain(String[] args)    {        // Adjacency list        Vector<pair>[] G = newVector[7];        for(inti = 0; i < 7; i++)         {            G[i] = newVector<pair>();        }        Â        // Building the graph        // The edges have been stored in the row        // corresponding to their end-point        G[1].add(newpair(2, 0.09));        G[2].add(newpair(1, 0.23));        G[2].add(newpair(6, 0.62));        G[3].add(newpair(2, 0.06));        G[4].add(newpair(1, 0.77));        G[4].add(newpair(3, 0.63));        G[5].add(newpair(4, 0.65));        G[5].add(newpair(6, 0.38));        G[6].add(newpair(2, 0.85));        G[6].add(newpair(3, 0.37));        G[6].add(newpair(4, 0.35));        G[6].add(newpair(5, 1.0));          // N is the number of states        intN = 6;          intS = 4, F = 2, T = 100;          System.out.print("The probability of reaching "+ F +                " at time "+ T + " \nafter starting from "+                S + " is "                + findProbability(G, N, F, S, T));    }}  // This code is contributed by Rajput-Ji | 
Python3
| # Python3 implementation of the above approach Â# Macro for vector of pair to store# each node with edge# define vp vector<pair<int, float> > Â# Function to calculate the# probability of reaching F# at time T after starting# from SdeffindProbability(G, N, F, S, T):      # Declaring the DP table    P =[[0fori inrange(T +1)]             forj inrange(N +1)] Â    # Probability of being at S    # at t = 0 is 1.0    P[S][0] =1.0; Â    # Filling the DP table    # in bottom-up manner    fori inrange(1, T +1):        forj inrange(1, N +1):            fork inG[j]:                P[j][i] +=k[1] *P[k[0]][i -1]; Â    returnP[F][T]  # Driver codeif__name__=='__main__':      # Adjacency list    G =[0fori inrange(7)] Â    # Building the graph    # The edges have been stored in the row    # corresponding to their end-point    G[1] =[ [ 2, 0.09] ]    G[2] =[ [ 1, 0.23], [ 6, 0.62] ]    G[3] =[ [ 2, 0.06] ]    G[4] =[ [ 1, 0.77], [ 3, 0.63] ]    G[5] =[ [ 4, 0.65], [ 6, 0.38] ]    G[6] =[ [ 2, 0.85], [ 3, 0.37],              [ 4, 0.35], [ 5, 1.0] ] Â    # N is the number of states    N =6 Â    S =4    F =2    T =100    Â    print("The probability of reaching {} at "          "time {}\nafter starting from {} is {}".format(          F, T, S, findProbability(G, N, F, S, T))) Â# This code is contributed by rutvik_56 | 
C#
| // C# implementation of the above approachusingSystem;usingSystem.Collections.Generic;  classGFG{    classpair     {        publicintfirst;        publicdoublesecond;          publicpair(intfirst, doublesecond)        {            this.first = first;            this.second = second;        }    }      // Function to calculate the    // probability of reaching F    // at time T after starting    // from S    staticfloatfindProbability(List<pair>[] G,                         intN, intF, intS, intT)    {        // Declaring the DP table        float[,] P = newfloat[N + 1, T + 1];        Â        // Probability of being at S        // at t = 0 is 1.0        P[S, 0] = (float) 1.0;          // Filling the DP table        // in bottom-up manner        for(inti = 1; i <= T; ++i)            for(intj = 1; j <= N; ++j)                foreach(pair k inG[j])                    P[j, i] += (float)k.second *                                 P[k.first, i - 1];          returnP[F, T];    }      // Driver code    publicstaticvoidMain(String[] args)    {        // Adjacency list        List<pair>[] G = newList<pair>[7];        for(inti = 0; i < 7; i++)         {            G[i] = newList<pair>();        }        Â        // Building the graph        // The edges have been stored in the row        // corresponding to their end-point        G[1].Add(newpair(2, 0.09));        G[2].Add(newpair(1, 0.23));        G[2].Add(newpair(6, 0.62));        G[3].Add(newpair(2, 0.06));        G[4].Add(newpair(1, 0.77));        G[4].Add(newpair(3, 0.63));        G[5].Add(newpair(4, 0.65));        G[5].Add(newpair(6, 0.38));        G[6].Add(newpair(2, 0.85));        G[6].Add(newpair(3, 0.37));        G[6].Add(newpair(4, 0.35));        G[6].Add(newpair(5, 1.0));          // N is the number of states        intN = 6;          intS = 4, F = 2, T = 100;          Console.Write("The probability of reaching "+ F +                " at time "+ T + " \nafter starting from "+                S + " is "                + findProbability(G, N, F, S, T));    }}  // This code is contributed by 29AjayKumar | 
Javascript
| <script>  // Javascript implementation of the above approach  // Function to calculate the// probability of reaching F// at time T after starting// from SfunctionfindProbability(G, N, F, S, T){    // Declaring the DP table    varP = Array.from(Array(N+1), ()=> Array(T+1).fill(0))      // Probability of being at S    // at t = 0 is 1.0    P[S][0] = 1.0;      // Filling the DP table    // in bottom-up manner    for(vari = 1; i <= T; ++i)        for(varj = 1; j <= N; ++j)            G[j].forEach(k => {                Â                P[j][i] += k[1] * P[k[0]][i - 1];            });      returnP[F][T];}  // Driver code// Adjacency listvarG = Array(7);// Building the graph// The edges have been stored in the row// corresponding to their end-pointG[1] = [ [ 2, 0.09 ] ];G[2] = [ [ 1, 0.23 ], [ 6, 0.62 ] ];G[3] = [ [ 2, 0.06 ] ];G[4] = [ [ 1, 0.77 ], [ 3, 0.63 ] ];G[5] = [ [ 4, 0.65 ], [ 6, 0.38 ] ];G[6] = [ [ 2, 0.85 ], [ 3, 0.37 ], [ 4, 0.35 ], [ 5, 1.0 ] ];// N is the number of statesvarN = 6;varS = 4, F = 2, T = 100;document.write( "The probability of reaching "+ F     + " at time "+ T + " <br>after starting from "     + S + " is "+ findProbability(G, N, F, S, T).toFixed(8));    </script> | 
The probability of reaching 2 at time 100 after starting from 4 is 0.284992
Â
Complexity Analysis:
- Time complexity: O(N2 * T)Â
- Space complexity: O(N * T)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

 
                                    








