Search results
Top results related to divide & conquer strategy
Top Answer
Answered Apr 26, 2020 · 1 votes
This is my python version code for the algorithm in its "closest larger right" version. Obviously, as you can see it is recursive. Recursion is really elegant but a bit tricky because few lines of code condense lots of concepts regarding to algorithms design and the own language they are coded. In my opinion 4 relevant moments are happening:
- 1) Recursive calls. Function is call to itself. During this step the list progresible slice into halves. Once the atomic unit is reached the base algorithm will be executed over them firstly (step 3). if not solution is reached greater list sizes will be involved in the calculation in further recursions.
- 2) Termination condition. Previous step is not run forever, it allows stop recursion and going to the next step (base algorithm). Now the code has len(arr) > 1: that means that the atomic unit will be pairs of numbers (or one of three in case of odd list). You can increase the number so that the recursive function will stay less time slicing the list and summarizing the results, but the counterpart is that in a parallelized environment "workers" will have to digest a bigger list.
- 3) Base algorithm. It makes the essential calculation. Whatever the size of the list, it returns the indexes of its elements to the right closest larger number
- 4) "Calculation saving". The base algorithm no need to calculated indexes on those numbers resolved in previous recursions. There is also a break to stop calculations once the number gets the index in the current recursion list.
Other algorithms models could be design, more efficient for sure. It occurs to me ones based on dictionaries or on different slicing strategies.
def closest_larger_right(arr):- len_total = len(arr) result = [None] * len_total- def recursive(arr, len_total, position=0):- # 2) Termination condition if len(arr) > 1:- mid = len(arr) // 2 left = arr[:mid] right = arr[mid:]- position_left = 0 + position position_right = len(left) + position- # 1) Recursive calls recursive(left, len_total, position_left) recursive(right, len_total, position_right)- # 3) Base algorithm for i in range(len(arr)-1): # 4) Calculation saving if result[i + position] is None: for j in range(i+1, len(arr), 1): if (arr[i] < arr[j]): result[i + position] = j + position break return result- return recursive(arr, len_total)-# output: [2, 2, 3, 7, 5, 7, 7, None]print(closest_larger_right([5, 2, 6, 8, 1, 4, 3, 9]))--
1/5
Top Answer
Answered Aug 26, 2015 · 4 votes
A divide and conquer approach would be to have a recursive function k-way-merge(), that gets a list of lists as input, and at each step:
- If you currently have 1 array, return it (stop clause)
- Otherwise, split the list of lists to two, and for each half - recursively invoke k-way-merge(). merge the two resulting lists.
The main aspect you need to change in your code is in:
int[] first_Half = new int[half]; int[] second_Half = new int[lists.length - half]; -
Here, you need first_half and second_half to be int[][], as they are actually list of lists.
In addition, in the last line:
return merge(merge(first_half),merge(second_half));-
Note that the outer merge() is different, it's not a recursive call - but a call to "regular" merge(), that merges two arrays into one (as the logic on how to do it is missing from the code, I assume you have such merge() implemented).
Other than that, the approach looks correct, but a bit inefficient, since you copy the int[][] object every iteration, rather than using indices to "remember where you are".
2/5
Top Answer
Answered Nov 16, 2011 · 5 votes
First,
//(2) Let b be the leftmost point of HB .for (int i=0; i<hullb.length; i++){ if (b < hullb[i][0]){ b = hullb[i][0]; }}for (int i=0; i<hullb.length; i++){ if (b > hullb[i][0]){ b = hullb[i][0]; }}
there seems to be a leftover copy-pasto.
Now, let us try to change "however I do not understand (3) or more precisely how to code it using the elements I have".
(a) While ab is not a lower tangent to HA do a = a - 1 (move a clockwise).
So you need a test whether a straight line is a lower tangent to HA. The geometrical situation here simplfies the test. Fundamental is of course the convexity of HA. The considered straight lines,L, always pass through a vertex of HA, say v, and a vertwx of HB, say b. Let the two neighbouring vertices be u and w, labelled so that u, v, w is a clockwise piece of the boundary. There are three possibilities, 1. u and w both lie above or on L, then, by convexity, L is a lower tangent to HA; 2. one of u and w lies above or on L, the other below; 3. both lie below L.
w lies below L if and only if the path bvw makes a left turn at v. u lies below L if and only if the path bvu makes a left turn at v.
You need a function to check whether a path ABC makes a left turn (by an angle less than 180°) at B. Your rightTurn has the gist, but the wrong parameters, because one of the points you want to test belongs to the other hull. And test that you got the signs right, it's easy to slip there.
So while either of the two neighbours of v lies below the straight line through b and v, replace v by its clockwise neighbour and check again. Take care of the array bounds when picking neighbours.
After a couple of steps you've found a straight line which is a lower tangent to HA, but in general not of HB.
(b) While ab is not a lower tangent to HB do b = b + 1 (move b counterclockwise).
This is analogous, but with a change of orientation. A point p on the boundary of HB lies below the straight line through a (a vertex of HA) and b (the considered vertex of HB) if and only if the path pba makes a left turn at b.
Picking counterclockwise neighbours on the boundary of HB until a lower tangent of HB is found rotates the line about the vertex a on the boundary of HA, so that that lower tangent of HB need not be a lower tangent of HA anymore (look at the picture you linked).
So back to 3 (a). That will rotate the line about b, possibly so much that it is no longer a lower tangent to HB. 3 (b) again. Iterate until you found a lower tangent to both.
aIndex = findRightmostA();bIndex = findLeftmostB();while(!(lowerTangentA (aIndex, bIndex) && lowerTangentB(aIndex, bIndex))){ aIndex = findLowerTangentA(aIndex, bIndex); bIndex = findLowerTangentB(aIndex, bIndex);}lowerTangentA(aIndex, bIndex){ ux = hulla[aIndex+1][0]; uy = hulla[aIndex+1][1]; // array bounds! ax = hulla[aIndex][0]; ay = hulla[aIndex][1]; wx = hulla[aIndex-1][0]; wy = hulla[aIndex-1][1]; bx = hullb[bIndex][0]; by = hullb[bIndex][1]; return !(leftTurn(bx,by,ax,ay,ux,uy) || leftTurn(bx,by,ax,ay,wx,wy));}
Finding the upper tangent to both hulls is analogous. It is worthwhile to try and abstract the tangent checks so that you can use the same function for all checks (lower/upper, HA/HB).
3/5
11.8K Divide and Conquer algorithm is a problem-solving strategy that involves breaking down a complex problem into smaller, more manageable parts, solving each part individually, and then combining the solutions to solve the original problem. It is a widely used algorithmic technique in computer science and mathematics.
Example: In the Merge Sort algorithm, the “Divide and Conquer” strategy is used to sort a list of elements. Below image illustrate the dividing and merging states to sort the array using Merge Sort.
<img alt="Divide-and-Conquer-banner">
Table of Content
- What is Divide and Conquer?
- Stages of Divide and Conquer
- Applications of Divide and Conquer
- Basics of Divide and Conquer
- Standard Algorithms on Divide and Conquer
- Binary Search based problems
- Practice problems on Divide and Conquer
What is Divide and Conquer Algorithm?
Divide and Conquer is a problem-solving technique that involves breaking a larger problem into subproblems, solving the subproblems independently and combining the solutions...
4/5
Top Answer
Answered Dec 23, 2019 · 0 votes
/* package codechef; // don't place package name! */-import java.util.*;import java.lang.*;import java.io.*;-/* Name of the class has to be "Main" only if the class is public. */class Codechef{ int bigA[][]; int bigB[][]; public Codechef (int bigA[][], int bigB[][]) { this.bigA = bigA; this.bigB = bigB; } public int[][] recursiveMatrixMultipy (int A[][], int B[][], int n) { int c[][] = new int[n][n]; if (n == 1) { c[0][0] = bigA[ A[0][0] ][ A[1][0] ] * bigB[ B[0][0] ][ B[1][0] ]; } else { int [][] a11 = new int[2][2]; int [][] a12 = new int[2][2]; int [][] a21 = new int[2][2]; int [][] a22 = new int[2][2]; int [][] b11 = new int[2][2]; int [][] b12 = new int[2][2]; int [][] b21 = new int[2][2]; int [][] b22 = new int[2][2]; partitionMatrix (A, a11, a12, a21, a22); partitionMatrix (B, b11, b12, b21, b22); int c11[][] = addMatrix(recursiveMatrixMultipy (a11, b11, n / 2), recursiveMatrixMultipy (a12, b21, n / 2) ); int c12[][] = addMatrix(recursiveMatrixMultipy (a11, b12, n / 2), recursiveMatrixMultipy (a12, b22, n / 2) ); int c21[][] = addMatrix(recursiveMatrixMultipy (a21, b11, n / 2), recursiveMatrixMultipy (a22, b21, n / 2) ); int c22[][] = addMatrix(recursiveMatrixMultipy (a21, b12, n / 2), recursiveMatrixMultipy (a22, b22, n / 2) ); c = merge (c11, c12, c21, c22); } return c; } public int[][] addMatrix (int A[][], int B[][]) { int n = A[0].length; int c[][] = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { c[i][j] = A[i][j] + B[i][j]; } } return c; } public void partitionMatrix (int T[][], int t11[][], int t12[][], int t21[][], int t22[][]) { int r_start = T[0][0]; int r_end = T[0][1]; int c_start = T[1][0]; int c_end = T[1][1]; int r_mid = r_start + (r_end - r_start) / 2; int c_mid = c_start + (c_end - c_start) / 2; t11[0][0] = r_start; t11[0][1] = r_mid; t11[1][0] = c_start; t11[1][1] = c_mid; t12[0][0] = r_start; t12[0][1] = r_mid; t12[1][0] = c_mid + 1; t12[1][1] = c_end; t21[0][0] = r_mid + 1; t21[0][1] = r_end; t21[1][0] = c_start; t21[1][1] = c_mid; t22[0][0] = r_mid + 1; t22[0][1] = r_end; t22[1][0] = c_mid + 1; t22[1][1] = c_end; } public int[][] merge (int c11[][], int c12[][], int c21[][], int c22[][]) { int n_C = c11[0].length; int n = 2 * n_C; int C[][] = new int[n][n]; for (int i = 0; i < n_C; i++) { for (int j = 0; j < n_C; j++) { C[i][j] = c11[i][j]; C[i][j + n_C] = c12[i][j]; C[i + n_C][j] = c21[i][j]; C[i + n_C][j + n_C] = c22[i][j]; } } return C; } public static void main (String[] args) throws java.lang.Exception { int matrix[][] = {{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4}}; int n = matrix[0].length; //checks if power of 2 if ((n > 0) && ( ( n & ( n - 1 ) ) == 0 ) ) { Codechef matrixDivAndConquer = new Codechef (matrix, matrix); int A[][] = {{0, n - 1},{0, n - 1}}; int B[][] = {{0, n - 1},{0, n - 1}}; int C[][] = matrixDivAndConquer.recursiveMatrixMultipy (A, B, n); System.out.println (Arrays.deepToString(C)); } }}
Well, my error was when i was partitioning my matrix into submatrixes.
===============================Strassen Algo below===========================
import java.util.*;import java.lang.*;import java.io.*;-class StrassenAlgo{ public int[][] recursiveMatrixMultipy (int A[][], int B[][], int a[][], int b[][], int n) { int c[][] = new int[n][n]; if (n == 1) { c[0][0] = A[ a[0][0] ][ a[1][0] ] * B[ b[0][0] ][ b[1][0] ]; } else { int [][] a11 = new int[2][2]; int [][] a12 = new int[2][2]; int [][] a21 = new int[2][2]; int [][] a22 = new int[2][2]; int [][] b11 = new int[2][2]; int [][] b12 = new int[2][2]; int [][] b21 = new int[2][2]; int [][] b22 = new int[2][2]; partitionMatrix (a, a11, a12, a21, a22); partitionMatrix (b, b11, b12, b21, b22); int s1[][] = substractMatrix (B, B, b12, b22); int s2[][] = addMatrix (A, A, a11, a12); int s3[][] = addMatrix (A, A, a21, a22); int s4[][] = substractMatrix (B, B, b21, b11); int s5[][] = addMatrix (A, A, a11, a22); int s6[][] = addMatrix (B, B, b11, b22); int s7[][] = substractMatrix (A, A, a12, a22); int s8[][] = addMatrix (B, B, b21, b22); int s9[][] = substractMatrix (A, A, a11, a21); int s10[][] = addMatrix (B, B, b11, b12); int ss[][] = {{0, s1.length - 1}, {0, s1.length - 1}}; int p1[][] = recursiveMatrixMultipy (A, s1, a11, ss, n / 2); int p2[][] = recursiveMatrixMultipy (s2, B, ss, b22, n / 2); int p3[][] = recursiveMatrixMultipy (s3, B, ss, b11, n / 2); int p4[][] = recursiveMatrixMultipy (A, s4, a22, ss, n / 2); int p5[][] = recursiveMatrixMultipy (s5, s6, ss, ss, n / 2); int p6[][] = recursiveMatrixMultipy (s7, s8, ss, ss, n / 2); int p7[][] = recursiveMatrixMultipy (s9, s10, ss, ss, n / 2 ); int pp[][] = {{0, p1.length - 1}, {0, p1.length - 1}}; // c11 = p5 + p4 - p2 + p6 int c11[][] = substractMatrix (addMatrix (p4, addMatrix (p5, p6, pp, pp), pp, pp), p2, pp, pp); // c12 = p1 + p2 int c12[][] = addMatrix (p1, p2, pp, pp); // c21 = p3 + p4 int c21[][] = addMatrix (p3, p4, pp, pp); // c22 = p5 + p1 - p3 - p7 int c22[][] = substractMatrix(addMatrix (p1, p5, pp, pp), addMatrix(p3, p7, pp, pp), pp, pp); c = merge (c11, c12, c21, c22); } return c; } public int[][] addMatrix (int A[][], int B[][], int a[][], int b[][]) { int n = a[0][1] - a[0][0] + 1; int c[][] = new int[n][n]; for (int i = a[0][0], u = b[0][0], x = 0; i <= a[0][1]; i++, u++, x++) { for (int j = a[1][0], v = b[1][0], y = 0; j <= a[1][1]; j++, v++, y++) { c[x][y] = A[i][j] + B[u][v]; } } return c; } public int[][] substractMatrix (int A[][], int B[][], int a[][], int b[][]) { int n = a[0][1] - a[0][0] + 1; int c[][] = new int[n][n]; for (int i = a[0][0], u = b[0][0], x = 0; i <= a[0][1]; i++, u++, x++) { for (int j = a[1][0], v = b[1][0], y = 0; j <= a[1][1]; j++, v++, y++) { c[x][y] = A[i][j] - B[u][v]; } } return c; } public void partitionMatrix (int T[][], int t11[][], int t12[][], int t21[][], int t22[][]) { int r_start = T[0][0]; int r_end = T[0][1]; int c_start = T[1][0]; int c_end = T[1][1]; int r_mid = r_start + (r_end - r_start) / 2; int c_mid = c_start + (c_end - c_start) / 2; t11[0][0] = r_start; t11[0][1] = r_mid; t11[1][0] = c_start; t11[1][1] = c_mid; t12[0][0] = r_start; t12[0][1] = r_mid; t12[1][0] = c_mid + 1; t12[1][1] = c_end; t21[0][0] = r_mid + 1; t21[0][1] = r_end; t21[1][0] = c_start; t21[1][1] = c_mid; t22[0][0] = r_mid + 1; t22[0][1] = r_end; t22[1][0] = c_mid + 1; t22[1][1] = c_end; } public int[][] merge (int c11[][], int c12[][], int c21[][], int c22[][]) { int n_C = c11[0].length; int n = 2 * n_C; int C[][] = new int[n][n]; for (int i = 0; i < n_C; i++) { for (int j = 0; j < n_C; j++) { C[i][j] = c11[i][j]; C[i][j + n_C] = c12[i][j]; C[i + n_C][j] = c21[i][j]; C[i + n_C][j + n_C] = c22[i][j]; } } return C; } public static void main (String[] args) throws java.lang.Exception { int matrix[][] = {{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4}}; int n = matrix[0].length; if ((n > 0) && ( ( n & ( n - 1 ) ) == 0 ) ) { StrassenAlgo matrixDivAndConquer = new StrassenAlgo (); int a[][] = {{0, n - 1},{0, n - 1}}; int b[][] = {{0, n - 1},{0, n - 1}}; int C[][] = matrixDivAndConquer.recursiveMatrixMultipy (matrix, matrix, a, b, n); System.out.println (Arrays.deepToString(C)); } }}
5/5
www.geeksforgeeks.org › divide-and-conquerDivide and Conquer Algorithm - GeeksforGeeks
www.geeksforgeeks.org › divide-and-conquerAug 23, 2024 · Divide and Conquer algorithm is a problem-solving strategy that involves breaking down a complex problem into smaller, more manageable parts, solving each part individually, and then combining the solutions to solve the original problem. It is a widely used algorithmic technique in computer science and mathematics.
People also ask
What is a divide and conquer strategy?
- Divide “Divide” is the first step of the divide and conquer strategy. As suggested by the name, in this step we divide the problem into smaller subproblems until the problem is small enough to be solved. At this step, sub-problems become smaller but still represent some part of the actual problem.
Divide and Conquer Algorithms | Baeldung on Computer Science
www.baeldung.com/cs/divide-and-conquer-strategyHow does divide and conquer algorithm work?
- Traditional algorithms are easily outperformed by the divide and conquer approach. Divide and conquer algorithm operates in three stages: Divide: Divide the problem recursively into smaller subproblems. Solve: Subproblems are solved independently. Combine: Combine subproblem solutions in order to deduce the answer to the original large problem.
Divide and Conquer Strategy for Problem Solving - CodeCrucks
codecrucks.com/divide-and-conquer/How does divide and conquer work?
- As previously mentioned, the divide and conquer strategy operates in three stages: Divide the problem recursively into smaller subproblems. Subproblems are solved independently. Combine subproblem solutions to arrive at the answer to the original large problem. The control abstraction for the divide and conquer (DC) strategy is as follows:
Divide and Conquer Strategy for Problem Solving - CodeCrucks
codecrucks.com/divide-and-conquer/What is a divide and conquer paradigm?
- The divide-and-conquer paradigm is often used to find an optimal solution of a problem. Its basic idea is to decompose a given problem into two or more similar, but simpler, subproblems, to solve them in turn, and to compose their solutions to solve the given problem. Problems of sufficient simplicity are solved directly.
Divide-and-conquer algorithm - Wikipedia
en.wikipedia.org/wiki/Divide-and-conquer_algorithmwww.programiz.com › dsa › divide-and-conquerDivide and Conquer Algorithm - Programiz
www.programiz.com › dsa › divide-and-conquerA divide and conquer algorithm is a strategy of solving a large problem by breaking the problem it into smaller sub-problems, solving the sub-problems and combining them to get the desired output. In this tutorial, you will understand the working of divide and conquer approach with an example.
www.geeksforgeeks.org › introduction-to-divide-andIntroduction to Divide and Conquer Algorithm - GeeksforGeeks
www.geeksforgeeks.org › introduction-to-divide-andJun 24, 2024 · Divide and Conquer algorithm is a problem-solving strategy that involves breaking down a complex problem into smaller, more manageable parts, solving each part individually, and then combining the solutions to solve the original problem.
en.wikipedia.org › wiki › Divide-and-conquer_algorithmDivide-and-conquer algorithm - Wikipedia
en.wikipedia.org › wiki › Divide-and-conquer_algorithmIn computer science, divide and conquer is an algorithm design paradigm. A divide-and-conquer algorithm recursively breaks down a problem into two or more sub-problems of the same or related type, until these become simple enough to be solved directly.
codecrucks.com › divide-and-conquerDivide and Conquer Strategy for Problem Solving - CodeCrucks
codecrucks.com › divide-and-conquerSep 18, 2021 · Divide and conquer algorithm operates in three stages: Divide: Divide the problem recursively into smaller subproblems. Solve: Subproblems are solved independently. Combine: Combine subproblem solutions in order to deduce the answer to the original large problem.
www.baeldung.com › cs › divide-and-conquer-strategyDivide and Conquer Algorithms | Baeldung on Computer Science
www.baeldung.com › cs › divide-and-conquer-strategyMar 18, 2024 · In the divide and conquer strategy, we solve a problem recursively by applying three steps at each level of the recursion: Divide, conquer, and combine. In this tutorial, we’re going to explore them in detail.
www.cs.csustan.edu › ~xliang › Courses2Chapter 4: Divide-and-Conquer - California State University ...
www.cs.csustan.edu › ~xliang › Courses2Divide instance of problem into two or more smaller instances. Solve smaller instances recursively. Obtain solution to original (larger) instance by combining these solutions. Divide-and-Conquer. Technique (cont.) a problem of size n. subproblem 1 of size n/2. solution to subproblem 1. solution to the original problem. subproblem 2 of size n/2.
Searches related to divide & conquer strategy