Skip to content

Commit

Permalink
Add `ErdosRenyi graph model for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrepiveteau committed Apr 25, 2023
1 parent cadf12a commit ca27de4
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
5 changes: 4 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ kotlin {
sourceSets {
val commonMain by getting { dependencies { implementation(kotlin("stdlib-common")) } }
val commonTest by getting { dependencies { implementation(kotlin("test")) } }
all { languageSettings.optIn("kotlin.contracts.ExperimentalContracts") }
all {
languageSettings.optIn("kotlin.contracts.ExperimentalContracts")
languageSettings.optIn("kotlin.time.ExperimentalTime")
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package io.github.alexandrepiveteau.graphs.algorithms

import io.github.alexandrepiveteau.graphs.DirectedGraph
import io.github.alexandrepiveteau.graphs.VertexArray
import io.github.alexandrepiveteau.graphs.arcTo
import io.github.alexandrepiveteau.graphs.asIntArray
import io.github.alexandrepiveteau.graphs.builder.buildDirectedGraph
import io.github.alexandrepiveteau.graphs.builder.erdosRenyi
import io.github.alexandrepiveteau.graphs.util.Repeats
import kotlin.random.Random
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
Expand Down Expand Up @@ -125,4 +128,15 @@ class KosarajuTests {
assertNotEquals(scc[map[graph[2]]], scc[map[graph[4]]])
assertNotEquals(scc[map[graph[3]]], scc[map[graph[4]]])
}

@Test
fun randomGraphsHaveTopologicalSortAfterScc() {
val random = Random(42)
repeat(Repeats) {
val graph = DirectedGraph.erdosRenyi(n = 1_000, p = 0.1, random = random)
val (scc, map) = graph.stronglyConnectedComponentsKosaraju()
val order = scc.topologicalSort()
assertEquals(map.values().asIntArray().distinct().count(), order.size)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.github.alexandrepiveteau.graphs.builder

import io.github.alexandrepiveteau.graphs.*
import kotlin.random.Random

/**
* Creates a random [UndirectedGraph] with [n] vertices and an edge between each pair of vertices
* with probability [p].
*
* @param n the number of vertices in the graph.
* @param p the probability of an edge between two vertices.
* @param random the [Random] instance to use.
*/
fun UndirectedGraph.Companion.erdosRenyi(
n: Int,
p: Double,
random: Random = Random,
) = buildUndirectedGraph {
val vertices = VertexArray(n) { addVertex() }
for (i in 0 until n) {
for (j in i + 1 until n) {
if (random.nextDouble() < p) {
addEdge(vertices[i] edgeTo vertices[j])
}
}
}
}

/**
* Creates a random [DirectedGraph] with [n] vertices and an arc between each pair of vertices with
* probability [p].
*
* @param n the number of vertices in the graph.
* @param p the probability of an arc between two vertices.
* @param random the [Random] instance to use.
*/
fun DirectedGraph.Companion.erdosRenyi(
n: Int,
p: Double,
random: Random = Random,
) = buildDirectedGraph {
val vertices = VertexArray(n) { addVertex() }
for (i in 0 until n) {
for (j in 0 until n) {
if (random.nextDouble() < p) {
addArc(vertices[i] arcTo vertices[j])
}
}
}
}

0 comments on commit ca27de4

Please sign in to comment.