WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Dynamic Programming · Guru · question 99 of 100

Implement the solution to the Optimal Multistage Graph problem with Custom Cost Functions using dynamic programming.?

📕 Buy this interview preparation book: 100 Dynamic Programming questions & answers — PDF + EPUB for $5

The Optimal Multistage Graph problem is a classic optimization problem that seeks to find the shortest path through a directed graph with multiple stages, each with different costs. This problem can be solved using dynamic programming, which allows us to compute the optimal solutions for each stage and then use those solutions to compute the overall optimal solution.

Here is an implementation of the Optimal Multistage Graph problem with Custom Cost Functions using dynamic programming in Java:

public class OptimalMultistageGraph {
    private static final int INF = Integer.MAX_VALUE;

    public static void shortestPath(int[][] graph, int[] costs) {
        int n = graph.length;
        int[] dp = new int[n];
        int[] path = new int[n];

        for (int i = n - 2; i >= 0; i--) {
            dp[i] = INF;
            for (int j = i + 1; j < n; j++) {
                int cost = costs[i] + graph[i][j];
                if (cost < dp[i]) {
                    dp[i] = cost;
                    path[i] = j;
                }
            }
        }

        int current = 0;
        while (current != n - 1) {
            System.out.print(current + " -> ");
            current = path[current];
        }
        System.out.println(n - 1);
        System.out.println("Minimum cost: " + dp[0]);
    }
}

In this implementation, we use the bottom-up approach to solve the problem. We use an array ‘dp‘ to store the minimum cost of reaching each stage starting from the last stage, and an array ‘path‘ to keep track of the optimal path between stages.

The loop starts from the second-to-last stage and iterates backward to the first stage. For each stage ‘i‘, we iterate through all possible paths from ‘i‘ to subsequent stages ‘j‘, and compute the cost of taking that path which is the sum of the cost of reaching stage ‘i‘ and the cost of the edge from ‘i‘ to ‘j‘. We then update the minimum cost and optimal path if we find a smaller cost.

After iterating through all stages, we can print the optimal path and the minimum cost by following the ‘path‘ array from the first stage to the last stage.

Here’s an example usage:

int[][] graph = {{0, 2, 1, 3, INF, INF},
                 {INF, 0, INF, INF, 4, INF},
                 {INF, INF, 0, INF, 1, 2},
                 {INF, INF, INF, 0, INF, 3},
                 {INF, INF, INF, INF, 0, 1},
                 {INF, INF, INF, INF, INF, 0}};

int[] costs = {0, 2, 1, 2, 5, 0};

// Compute shortest path and print result
OptimalMultistageGraph.shortestPath(graph, costs);

This example represents a graph with six stages and custom costs. The ‘graph‘ matrix represents the edges between stages, where ‘INF‘ represents no edge. The ‘costs‘ array represents the cost of each stage. Running this program would output:

0 -> 2 -> 4 -> 5
Minimum cost: 4

This indicates that the optimal path goes from stage 0 to stage 2 (cost 2), then to stage 4 (cost 1), and finally to stage 5 (cost 1), with a total minimum cost of 4.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Dynamic Programming interview — then scores it.
📞 Practice Dynamic Programming — free 15 min
📕 Buy this interview preparation book: 100 Dynamic Programming questions & answers — PDF + EPUB for $5

All 100 Dynamic Programming questions · All topics