mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-12-19 07:00:35 +08:00
Enhance docs, add more tests in Kosaraju (#5966)
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
package com.thealgorithms.datastructures.graphs;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -9,14 +9,13 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
public class KosarajuTest {
|
||||
|
||||
private Kosaraju kosaraju = new Kosaraju();
|
||||
private final Kosaraju kosaraju = new Kosaraju();
|
||||
|
||||
@Test
|
||||
public void findStronglyConnectedComps() {
|
||||
// Create a adjacency list of graph
|
||||
var n = 8;
|
||||
var adjList = new ArrayList<List<Integer>>(n);
|
||||
|
||||
public void testFindStronglyConnectedComponents() {
|
||||
// Create a graph using adjacency list
|
||||
int n = 8;
|
||||
List<List<Integer>> adjList = new ArrayList<>(n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
adjList.add(new ArrayList<>());
|
||||
}
|
||||
@@ -36,24 +35,24 @@ public class KosarajuTest {
|
||||
List<List<Integer>> expectedResult = new ArrayList<>();
|
||||
/*
|
||||
Expected result:
|
||||
0, 1, 2
|
||||
3
|
||||
5, 4, 6
|
||||
7
|
||||
{0, 1, 2}
|
||||
{3}
|
||||
{5, 4, 6}
|
||||
{7}
|
||||
*/
|
||||
expectedResult.add(Arrays.asList(1, 2, 0));
|
||||
expectedResult.add(Arrays.asList(3));
|
||||
expectedResult.add(List.of(3));
|
||||
expectedResult.add(Arrays.asList(5, 6, 4));
|
||||
expectedResult.add(Arrays.asList(7));
|
||||
assertTrue(expectedResult.equals(actualResult));
|
||||
expectedResult.add(List.of(7));
|
||||
|
||||
assertEquals(expectedResult, actualResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findStronglyConnectedCompsShouldGetSingleNodes() {
|
||||
// Create a adjacency list of graph
|
||||
var n = 8;
|
||||
var adjList = new ArrayList<List<Integer>>(n);
|
||||
|
||||
public void testFindSingleNodeSCC() {
|
||||
// Create a simple graph using adjacency list
|
||||
int n = 8;
|
||||
List<List<Integer>> adjList = new ArrayList<>(n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
adjList.add(new ArrayList<>());
|
||||
}
|
||||
@@ -71,9 +70,42 @@ public class KosarajuTest {
|
||||
List<List<Integer>> expectedResult = new ArrayList<>();
|
||||
/*
|
||||
Expected result:
|
||||
0, 1, 2, 3, 4, 5, 6, 7
|
||||
{0, 1, 2, 3, 4, 5, 6, 7}
|
||||
*/
|
||||
expectedResult.add(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 0));
|
||||
assertTrue(expectedResult.equals(actualResult));
|
||||
|
||||
assertEquals(expectedResult, actualResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDisconnectedGraph() {
|
||||
// Create a disconnected graph (two separate components)
|
||||
int n = 5;
|
||||
List<List<Integer>> adjList = new ArrayList<>(n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
adjList.add(new ArrayList<>());
|
||||
}
|
||||
|
||||
// Add edges for first component
|
||||
adjList.get(0).add(1);
|
||||
adjList.get(1).add(2);
|
||||
adjList.get(2).add(0);
|
||||
|
||||
// Add edges for second component
|
||||
adjList.get(3).add(4);
|
||||
adjList.get(4).add(3);
|
||||
|
||||
List<List<Integer>> actualResult = kosaraju.kosaraju(n, adjList);
|
||||
|
||||
List<List<Integer>> expectedResult = new ArrayList<>();
|
||||
/*
|
||||
Expected result:
|
||||
{0, 1, 2}
|
||||
{3, 4}
|
||||
*/
|
||||
expectedResult.add(Arrays.asList(4, 3));
|
||||
expectedResult.add(Arrays.asList(1, 2, 0));
|
||||
|
||||
assertEquals(expectedResult, actualResult);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user