# Multistage graph problem with forward approach and backward approach algorithms

In this article, we are going to learn about **Multistage graph problem with its solution** based on dynamic programming i.e. **forward approach and backward approach algorithms** for multistage graph.

Submitted by Shivangi Jain, on August 04, 2018

## Multistage graph problem

- The multistage graph problem is to find a minimum cost from a source to a sink.
- A multistage graph is a directed graph having a number of multiple stages, where stages element should be connected consecutively.
- In this multiple stage graph, there is a vertex whose in degree is
**0**that is known as the source. And the vertex with only one out degree is**0**is known as the destination vertex. - The one end of the multiple stage graphs is at
**i**thus the other reaching end is on**i+1**stage. - If we denote a graph
**G = (V, E)**in which the vertices are partitioned into**K >= 2**disjoints sets,**Vi**,**1 <= I <=K**. So that, if there is an edge**< u, v >**from**u**to**v**in E, the**u £Vi**and**v € v (i+1)**, for some**I**,**1 <= i <= K**. And sets**V1**and**Vk**are such that**|V1| = |Vk| = 1**.

### Algorithm for Forward Approach

1. F graph (graph G, int K, int n, int p[]) 2. { 3. Float cost [max size], int d [max size], r; 4. Cost [n] = 0.0 5. For (int j = n-1; j>= 1; j--) 6. { 7. Let r be a vertex such thatis an edge of G and C[j][r] + cost[r] is minimum; 8. Cost [j] = C[j][r] + Cost[r] 9. D [j] = r 10. } 11. P [1] = 1 , P[k] = n 12. For (j = 2 ; j <= K-1; j++) 13. P[j] = d[P(j-1)]; 14. }

**Input = input** is a **K** stage graph **G = (V, E)** with **n** vertices indexed in order of stages.

**E** is a set of an edge.

**C [i][j]** is the cost or weight of the edge **[i][j]**

### Algorithm for Backward Approach

1. Algorithm BGraph (G, K, n, p) 2. // some function as FGraph 3. { 4. B cost [1] = 0.0; 5. For j = 2 to n do 6. { 7. // compute b cost [j]. 8. Let r be such thatis an edge of 9. G and b cost [r] + c [r, j]; 10. D [j] = r; 11. } 12. // find a minimum cost path 13. P [1] = 1; p [k] = n; 14. For j = k-1 to 2 do p[j] = d [p (j+1)]; 15. }

If we consider **s** as the vertex in **V1** and **t** as the vertex in **Vk**. The vertex **s** is supposed as the source and vertex **t** as the sink. The cost of a path from **s** to **t** is the sum of the costs of the edges on the path.

Here, each set **Vi** defines a stage in the graph. Each path starts from stage 1 goes to stage 2 then to stage 3 and so on, because of constraints on **E**.

The minimum cost of **s** to **t** path is indicated by a dashed line. This method can be used for solving many problems such as allocating some resources to given number of projects with the intent to find the maximum profit.

Related Tutorials

- Introduction to Algorithms
- Introduction to Greedy Strategy in Algorithms
- Stability in sorting
- External Merge Sorting Algorithm
- Radix Sort and its Algorithm
- Bucket Sort Algorithm
- Bubble sort Algorithm, Flow Chart and C++ Code
- Insertion sort Algorithm, flowchart and C, C++ Code
- Merge Sort | One of the best sorting algorithms used for large inputs
- Binary Search in C, C++
- Randomized Binary Search
- Meta Binary Search | One-sided Binary Search
- Difference between Linear Search and Binary Search
- Binary Search in String
- Variants of Binary Search
- Sieve of Eratosthenes to find prime numbers
- Optimal Merge Pattern (Algorithm and Example)
- Given an array of n numbers, Check whether there is any duplicate or not
- Finding the missing number
- Find the number occurring an odd number of times
- Find the pair whose sum is closest to zero in minimum time complexity
- Find three elements in an array such that their sum is equal to given element K
- Bitonic Search Algorithm
- Check whether a number is Fibonacci or not
- Segregate even and odd numbers in minimum time complexity
- Find trailing zeros in factorial of a number
- Find Nearest Greatest Neighbours of each element in an array
- Interpolation search algorithm
- Floor and ceil of an element in an array using C++
- Two Elements whose sum is closest to zero
- Find a pair with a given difference
- Count number of occurrences (or frequency) in a sorted array
- Find a Fixed Point (Value equal to index) in a given array
- Find the maximum element in an array which is first increasing and then decreasing
- Dynamic Programming (Components, Applications and Elements)
- Algorithm for fractional knapsack problem
- Algorithm and procedure to solve a longest common subsequence problem
- Find the Nth Fibonacci number | C++
- Longest Common Subsequence using Dynamic programming (DP)
- Longest Increasing Subsequence using Dynamic programming (DP)
- Find the maximum sub-array sum using KADANE'S ALGORITHM
- Non-intersecting chords using Dynamic Programming (DP)
- Edit Distance using Dynamic Programming (DP)
- Finding Ugly Number using Dynamic Programming (DP)
- Egg dropping problem using Dynamic Programming (DP)
- Wild card matching problem using Dynamic programming (DP)
- Compute sum of digits in all numbers from 1 to N for a given N
- Minimum jumps required using Dynamic programming (DP)
- Graph coloring problem's solution using backtracking algorithm
- Breadth First Search (BFS) and Depth First Search (DFS) Algorithms
- Travelling Salesman Problem
- Kruskal's (P) and Prim's (K) Algorithms
- Floyd Warshall algorithm with its Pseudo Code

- Backtracking (Types and Algorithms)
- 4 Queen's problem and solution using backtracking algorithm
- N Queen's problem and solution using backtracking algorithm
- Find the GCD (Greatest Common Divisor) of two numbers using EUCLID'S ALGORITHM
- Compute the value of A raise to the power B using Fast Exponentiation
- Implement First Come First Served (FCFS) CPU Scheduling Algorithm using C program
- Implementations of FCFS scheduling algorithm using C++
- Implementation of Shortest Job First (SJF) Non-Preemptive CPU scheduling algorithm using C++
- Implementation of Shortest Job First (SJF) Preemptive CPU scheduling algorithm using C++
- Implementation of Priority scheduling (Pre-emptive) algorithm using C++
- Implementation of Priority scheduling (Non Pre-emptive) algorithm using C++
- Implementation of Round Robin CPU Scheduling algorithm using C++
- Analysis of LRU page replacement algorithm and Belady's anomaly
- Branch and Bound
- Find the roots of a complex polynomial equation using Regula Falsi Method in C
- Sieve of Eratosthenes to find prime numbers
- Implementations of FCFS scheduling algorithm using C++
- Implementation of Shortest Job First (SJF) Non-Preemptive CPU scheduling algorithm using C++
- Implementation of Shortest Job First (SJF) Preemptive CPU scheduling algorithm using C++
- Implementation of Priority scheduling (Pre-emptive) algorithm using C++
- Divide and Conquer Paradigm (What it is, Its Applications, Pros and Cons)
- Implementation of Priority scheduling (Non Pre-emptive) algorithm using C++
- Implementation of Round Robin CPU Scheduling algorithm using C++
- Jump Search Implementation using C++
- Optimal Merge Pattern (Algorithm and Example)
- Introduction to Greedy Strategy in Algorithms
- Strassen's Matrix Multiplication in algorithms
- Huffman Coding (Algorithm, Example and Time complexity)
- Backtracking (Types and Algorithms)
- 4 Queen's problem and solution using backtracking algorithm
- N Queen's problem and solution using backtracking algorithm
- Graph coloring problem's solution using backtracking algorithm
- Tournament Tree and their properties
- Deterministic and Non Deterministic Algorithms
- Lower Bound Theory
- Non Recursive Tree Traversal Algorithm
- Line Drawing Algorithm
- Breadth First Search (BFS) and Depth First Search (DFS) Algorithms
- P and NP problems and solutions | Algorithms
- Travelling Salesman Problem
- 2 – 3 Trees Algorithm
- Kruskal's (P) and Prim's (K) Algorithms
- Algorithm for fractional knapsack problem
- Algorithm and procedure to solve a longest common subsequence problem
- Midpoint Circle Algorithm
- Multistage graph problem with forward approach and backward approach algorithms
- Floyd Warshall algorithm with its Pseudo Code
- Reliability design problem
- Removing consecutive duplicates from a string
- Fast Exponentiation using Bitmasking

Comments and Discussions!