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 86 of 100

Solve the problem of finding the Optimal Cuts on a Convex Polygon using dynamic programming.?

πŸ“• Buy this interview preparation book: 100 Dynamic Programming questions & answers β€” PDF + EPUB for $5

The problem of finding the optimal cuts on a convex polygon can be solved using dynamic programming. The objective of this problem is to find a sequence of non-intersecting diagonals that partition the polygon into triangles of maximum total area.

The Optimal Cuts problem can be solved using the following steps:

1. Divide the polygon into smaller triangles:

- Let P be the convex polygon with n vertices.

- For each pair of vertices (i, j) such that 2 i < j n - 1, compute the area of the triangle formed by these vertices and add it to the corresponding entry in a 2D table dp[i][j].

- This can be done using the formula: area(P(i, j, k)) = 0.5 * |(x[j] - x[i]) * (y[k] - y[i]) - (y[j] - y[i]) * (x[k] - x[i])|, where x[i] and y[i] are the x and y coordinates of vertex i.

2. Solve the subproblem:

- Let opt(i, j) be the maximum total area of a triangulation of the polygon P[i, j].

- We want to find opt(1, n).

- For this, we need to consider all possible triangulations of P[1, n] and find the one with the maximum total area.

3. Build the solution:

- To build the solution, we need to store the vertices that form the diagonals that maximize the total area.

- Let sol(i, j) be the set of vertices that form the diagonals that maximize the total area of a triangulation of P[i, j].

- We want to find sol(1, n).

The dynamic programming approach is as follows:

Base cases:

- opt(i, i+2) = area(P(i, i+1, i+2)) for 1  i < n-2

Recursive case:

- opt(i, j) = max{opt(i, k) + opt(k, j) + area(P(i, j, k))} where i+2  k  j-1

where i+2 k j-1.

To build the solution, we need to store the vertices that form the diagonals that maximize the total area:

- sol(i, i+2) = {(i, i+1, i+2)} for 1  i < n-2
- sol(i, j) = { k  sol(i, k)  sol(k, j) } where opt(i,j) is the maximum and i+2 <= k <= j-1.

Here’s the Java code that implements this dynamic programming solution:

public class OptimalCuts {

    static double[] x, y;
    static double[][] dp;
    static List<Integer>[][] sol;

    public static void main(String[] args) {
        x = new double[]{0, 1, 2, 3, 2, 1, 0, -1, -2, -3, -2, -1};
        y = new double[]{0, -1, -2, -1, 0, 1, 2, 1, 0, -1, -2, -1};
        int n = x.length;

        dp = new double[n][n];
        sol = new ArrayList[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = i + 2; j < n; j++) {
                sol[i][j] = new ArrayList<>();
            }
        }

        for (int len = 3; len <= n; len++) {
            for (int i = 0; i + len - 1 < n; i++) {
                int j = i + len - 1;
                for (int k = i + 1; k < j; k++) {
                    double area = triangleArea(i, j, k);
                    if (dp[i][j] < dp[i][k] + dp[k][j] + area) {
                        dp[i][j] = dp[i][k] + dp[k][j] + area;
                        sol[i][j].clear();
                        sol[i][j].addAll(sol[i][k]);
                        sol[i][j].addAll(sol[k][j]);
                        sol[i][j].add(k);
                    }
                }
            }
        }

        System.out.println("Maximum Total Area: " + dp[0][n-1]);
        System.out.println("Optimal Cuts: " + sol[0][n-1]);
    }

    static double triangleArea(int i, int j, int k) {
        return 0.5 * Math.abs((x[j]-x[i]) * (y[k]-y[i]) - (y[j]-y[i]) * (x[k]-x[i]));
    }
}

In this example, we have a convex polygon with 12 vertices, represented by the β€˜xβ€˜ and β€˜yβ€˜ arrays. We compute the dynamic programming table β€˜dpβ€˜ and the solution table β€˜solβ€˜ using the algorithm described above. Finally, we print the maximum total area of a triangulation and the vertices that form the diagonals that maximize the total area.

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