Skip to content

Commit e0ab1d3

Browse files
authored
Add OptimalFileMerging algorithm (#5805)
1 parent 3a30e48 commit e0ab1d3

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@
328328
* [MergeIntervals](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/MergeIntervals.java)
329329
* [MinimizingLateness](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/MinimizingLateness.java)
330330
* [MinimumWaitingTime](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/MinimumWaitingTime.java)
331+
* [OptimalFileMerging](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/OptimalFileMerging.java)
331332
* [StockProfitCalculator](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/StockProfitCalculator.java)
332333
* io
333334
* [BufferedReader](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/io/BufferedReader.java)
@@ -956,6 +957,7 @@
956957
* [MergeIntervalsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/MergeIntervalsTest.java)
957958
* [MinimizingLatenessTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/MinimizingLatenessTest.java)
958959
* [MinimumWaitingTimeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/MinimumWaitingTimeTest.java)
960+
* [OptimalFileMergingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/OptimalFileMergingTest.java)
959961
* [StockProfitCalculatorTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/StockProfitCalculatorTest.java)
960962
* io
961963
* [BufferedReaderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/io/BufferedReaderTest.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.thealgorithms.greedyalgorithms;
2+
3+
import java.util.PriorityQueue;
4+
5+
/**
6+
* Class to solve the Optimal File Merging Problem.
7+
* The goal is to minimize the cost of merging files, where the cost of merging two files is the sum of their sizes.
8+
* The cost of merging all files is the sum of the costs of merging each pair of files.
9+
* Example:
10+
* files = [4, 3, 2, 6]
11+
* The minimum cost to merge all files is 29.
12+
* Steps:
13+
* 1. Merge files 2 and 3 (cost = 2 + 3 = 5). New files = [4, 5, 6]
14+
* 2. Merge files 4 and 5 (cost = 4 + 5 = 9). New files = [6, 9]
15+
* 3. Merge files 6 and 9 (cost = 6 + 9 = 15). New files = [15]
16+
* Total cost = 5 + 9 + 15 = 29
17+
*
18+
* @author Hardvan
19+
*/
20+
public final class OptimalFileMerging {
21+
private OptimalFileMerging() {
22+
}
23+
24+
/**
25+
* Calculates the minimum cost to merge all files.
26+
* Steps:
27+
* 1. Add all files to a min heap.
28+
* 2. Remove the two smallest files from the heap, merge them, and add the result back to the heap.
29+
* 3. Repeat step 2 until there is only one file left in the heap.
30+
* 4. The total cost is the sum of all the costs of merging the files.
31+
*
32+
* @param files array of file sizes
33+
* @return the minimum cost to merge the files
34+
*/
35+
public static int minMergeCost(int[] files) {
36+
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
37+
for (int file : files) {
38+
minHeap.add(file);
39+
}
40+
41+
int totalCost = 0;
42+
while (minHeap.size() > 1) {
43+
int first = minHeap.poll();
44+
int second = minHeap.poll();
45+
int cost = first + second;
46+
totalCost += cost;
47+
48+
minHeap.add(cost);
49+
}
50+
return totalCost;
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.thealgorithms.greedyalgorithms;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
9+
10+
public class OptimalFileMergingTest {
11+
12+
@ParameterizedTest
13+
@MethodSource("fileMergingProvider")
14+
public void testMinMergeCost(int[] files, int expected) {
15+
assertEquals(expected, OptimalFileMerging.minMergeCost(files));
16+
}
17+
18+
private static Stream<Arguments> fileMergingProvider() {
19+
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),
20+
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));
21+
}
22+
}

0 commit comments

Comments
 (0)