-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add `ErdosRenyi graph model for tests
- Loading branch information
1 parent
cadf12a
commit ca27de4
Showing
3 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/commonTest/kotlin/io/github/alexandrepiveteau/graphs/builder/ErdosRenyi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} | ||
} | ||
} | ||
} |