Add OptimalFileMerging algorithm (#5805)

This commit is contained in:
Hardik Pawar
2024-10-26 11:35:27 +05:30
committed by GitHub
parent 3a30e48315
commit e0ab1d357c
3 changed files with 76 additions and 0 deletions

View File

@ -328,6 +328,7 @@
* [MergeIntervals](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/MergeIntervals.java)
* [MinimizingLateness](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/MinimizingLateness.java)
* [MinimumWaitingTime](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/MinimumWaitingTime.java)
* [OptimalFileMerging](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/OptimalFileMerging.java)
* [StockProfitCalculator](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/StockProfitCalculator.java)
* io
* [BufferedReader](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/io/BufferedReader.java)
@ -956,6 +957,7 @@
* [MergeIntervalsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/MergeIntervalsTest.java)
* [MinimizingLatenessTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/MinimizingLatenessTest.java)
* [MinimumWaitingTimeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/MinimumWaitingTimeTest.java)
* [OptimalFileMergingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/OptimalFileMergingTest.java)
* [StockProfitCalculatorTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/StockProfitCalculatorTest.java)
* io
* [BufferedReaderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/io/BufferedReaderTest.java)

View File

@ -0,0 +1,52 @@
package com.thealgorithms.greedyalgorithms;
import java.util.PriorityQueue;
/**
* Class to solve the Optimal File Merging Problem.
* The goal is to minimize the cost of merging files, where the cost of merging two files is the sum of their sizes.
* The cost of merging all files is the sum of the costs of merging each pair of files.
* Example:
* files = [4, 3, 2, 6]
* The minimum cost to merge all files is 29.
* Steps:
* 1. Merge files 2 and 3 (cost = 2 + 3 = 5). New files = [4, 5, 6]
* 2. Merge files 4 and 5 (cost = 4 + 5 = 9). New files = [6, 9]
* 3. Merge files 6 and 9 (cost = 6 + 9 = 15). New files = [15]
* Total cost = 5 + 9 + 15 = 29
*
* @author Hardvan
*/
public final class OptimalFileMerging {
private OptimalFileMerging() {
}
/**
* Calculates the minimum cost to merge all files.
* Steps:
* 1. Add all files to a min heap.
* 2. Remove the two smallest files from the heap, merge them, and add the result back to the heap.
* 3. Repeat step 2 until there is only one file left in the heap.
* 4. The total cost is the sum of all the costs of merging the files.
*
* @param files array of file sizes
* @return the minimum cost to merge the files
*/
public static int minMergeCost(int[] files) {
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
for (int file : files) {
minHeap.add(file);
}
int totalCost = 0;
while (minHeap.size() > 1) {
int first = minHeap.poll();
int second = minHeap.poll();
int cost = first + second;
totalCost += cost;
minHeap.add(cost);
}
return totalCost;
}
}

View File

@ -0,0 +1,22 @@
package com.thealgorithms.greedyalgorithms;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
public class OptimalFileMergingTest {
@ParameterizedTest
@MethodSource("fileMergingProvider")
public void testMinMergeCost(int[] files, int expected) {
assertEquals(expected, OptimalFileMerging.minMergeCost(files));
}
private static Stream<Arguments> fileMergingProvider() {
return Stream.of(Arguments.of(new int[] {4, 3, 2, 6}, 29), Arguments.of(new int[] {5}, 0), Arguments.of(new int[] {2, 2, 2}, 10), Arguments.of(new int[] {10, 5, 3, 2}, 35), Arguments.of(new int[] {1, 1, 1, 1}, 8), Arguments.of(new int[] {1, 2, 3, 4, 5}, 33),
Arguments.of(new int[] {1, 2, 3, 4, 5, 6}, 51), Arguments.of(new int[] {1, 2, 3, 4, 5, 6, 7}, 74), Arguments.of(new int[] {1, 2, 3, 4, 5, 6, 7, 8}, 102), Arguments.of(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9}, 135));
}
}