mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-28 06:55:02 +08:00
Add Topological Sorting Algorithm (#3060)
Co-authored-by: thanhtri122002 <thanhnt122002@122002> Co-authored-by: Andrii Siriak <siryaka@gmail.com> Co-authored-by: Anh Pham <62592224+anhpham197@users.noreply.github.com> Co-authored-by: SanOtaku <SanOtaku098@gmail.com> Co-authored-by: Raghav Taneja <97575679+RaghavTaneja@users.noreply.github.com> Co-authored-by: Andrii Siriak <siryaka@gmail.com> Co-authored-by: Hai Nguyen <88832724+ntquanghai@users.noreply.github.com> Co-authored-by: Utkarsh Yadav <Utkarshrock1510@gmail.com> Co-authored-by: Phạm Minh Hiếu <84634830+Ph1eu@users.noreply.github.com> Co-authored-by: nguyenviettrung-bi11276 <101244039+nguyenviettrung-bi11276@users.noreply.github.com> Co-authored-by: TaiNguyen2001 <102779475+TaiNguyen2001@users.noreply.github.com> Co-authored-by: de <88503943+HuyQHoang@users.noreply.github.com> Co-authored-by: thanhtri122002 <93241140+thanhtri122002@users.noreply.github.com> Co-authored-by: thanhtri122002 <thanhnt122002@122002>
This commit is contained in:
@ -0,0 +1,61 @@
|
||||
package com.thealgorithms.sorts;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import com.thealgorithms.sorts.TopologicalSort.Graph;
|
||||
import com.thealgorithms.sorts.TopologicalSort.BackEdgeException;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
class TopologicalSortTest {
|
||||
@Test
|
||||
void successTest() {
|
||||
/*
|
||||
* Professor Bumstead example DAG. Each directed edge means that garment u must be put on
|
||||
* before garment v.
|
||||
* */
|
||||
Graph graph = new Graph();
|
||||
graph.addEdge("shirt", "tie", "belt");
|
||||
graph.addEdge("tie", "jacket");
|
||||
graph.addEdge("belt", "jacket");
|
||||
graph.addEdge("watch", "");
|
||||
graph.addEdge("undershorts", "pants", "shoes");
|
||||
graph.addEdge("shoes", "");
|
||||
graph.addEdge("socks", "shoes");
|
||||
graph.addEdge("jacket","");
|
||||
graph.addEdge("pants", "belt", "shoes");
|
||||
LinkedList<String> expected = new LinkedList<>();
|
||||
expected.add("socks");
|
||||
expected.add("undershorts");
|
||||
expected.add("pants");
|
||||
expected.add("shoes");
|
||||
expected.add("watch");
|
||||
expected.add("shirt");
|
||||
expected.add("belt");
|
||||
expected.add("tie");
|
||||
expected.add("jacket");
|
||||
assertIterableEquals(expected, TopologicalSort.sort(graph));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void failureTest() {
|
||||
|
||||
/*
|
||||
* Graph example from Geeks For Geeks
|
||||
* https://www.geeksforgeeks.org/tree-back-edge-and-cross-edges-in-dfs-of-graph/
|
||||
* */
|
||||
Graph graph = new Graph();
|
||||
graph.addEdge("1", "2", "3", "8");
|
||||
graph.addEdge("2", "4");
|
||||
graph.addEdge("3", "5");
|
||||
graph.addEdge("4", "6");
|
||||
graph.addEdge("5", "4", "7", "8");
|
||||
graph.addEdge("6", "2");
|
||||
graph.addEdge("7", "");
|
||||
graph.addEdge("8", "");
|
||||
Exception exception = assertThrows(BackEdgeException.class, () -> TopologicalSort.sort(graph));
|
||||
String expected = "This graph contains a cycle. No linear ordering is possible. " +
|
||||
"Back edge: 6 -> 2";
|
||||
assertEquals(exception.getMessage(), expected);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user