Multistage graph problem with forward approach algorithm-programmingraja

 Aim: Multistage graph problem with forward approach algorithm.


Software used: Dev C++


Program coding:-

#include<iostream>

using namespace std; 


#define N 8 

#define INF INT_MAX  

intshortestDist(int graph[N][N]) { 

intdist[N]; 


dist[N-1] = 0;  

for (int i = N-2 ; i >= 0 ; i--) 

    { 

dist[i] = INF; 

for (int j = i ; j < N ; j++) 

        { 

if (graph[i][j] == INF) 

continue;

dist[i] = min(dist[i], graph[i][j] + 

dist[j]); 

        } 

    } 


returndist[0]; 

}

int main() 

int graph[N][N] = 

      {{INF, 1, 2, 5, INF, INF, INF, INF}, 

       {INF, INF, INF, INF, 4, 11, INF, INF}, 

       {INF, INF, INF, INF, 9, 5, 16, INF}, 

       {INF, INF, INF, INF, INF, INF, 2, INF}, 

       {INF, INF, INF, INF, INF, INF, INF, 18}, 

       {INF, INF, INF, INF, INF, INF, INF, 13}, 

       {INF, INF, INF, INF, INF, INF, INF, 2}}; 


cout<<shortestDist(graph); 

return 0; 

}

Output:-




Post a Comment

0 Comments