141 lines
5.0 KiB
Kotlin
141 lines
5.0 KiB
Kotlin
/*
|
|
* MIT License
|
|
*
|
|
* Copyright (c) 2019 norangebit
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*
|
|
*/
|
|
|
|
package it.norangeb.algorithms.graph.operations
|
|
|
|
import it.norangeb.algorithms.graph.DiGraph
|
|
import org.amshove.kluent.`should be equal to`
|
|
import org.spekframework.spek2.Spek
|
|
import org.spekframework.spek2.style.gherkin.Feature
|
|
|
|
object StronglyConnectedComponentsTest : Spek({
|
|
Feature("compute strongly connected components") {
|
|
Scenario("compute strongly connected components on a graph") {
|
|
val graph by memoized { DiGraph() }
|
|
|
|
Given("a graph with five scc") {
|
|
graph.addEdge(0, 1)
|
|
graph.addEdge(0, 5)
|
|
graph.addEdge(2, 0)
|
|
graph.addEdge(2, 3)
|
|
graph.addEdge(3, 2)
|
|
graph.addEdge(3, 5)
|
|
graph.addEdge(4, 2)
|
|
graph.addEdge(4, 3)
|
|
graph.addEdge(5, 4)
|
|
graph.addEdge(6, 0)
|
|
graph.addEdge(6, 4)
|
|
graph.addEdge(6, 8)
|
|
graph.addEdge(6, 9)
|
|
graph.addEdge(7, 6)
|
|
graph.addEdge(7, 9)
|
|
graph.addEdge(8, 6)
|
|
graph.addEdge(9, 10)
|
|
graph.addEdge(9, 11)
|
|
graph.addEdge(10, 12)
|
|
graph.addEdge(11, 4)
|
|
graph.addEdge(11, 12)
|
|
graph.addEdge(12, 9)
|
|
}
|
|
|
|
lateinit var result: StronglyConnectedComponents
|
|
|
|
When("compute the scc") {
|
|
result = StronglyConnectedComponents(graph)
|
|
}
|
|
|
|
Then("components should be 5") {
|
|
result.count() `should be equal to` 5
|
|
}
|
|
|
|
Then("connected on 0, 1 should be false") {
|
|
result.stronglyConnected(0, 1) `should be equal to` false
|
|
}
|
|
|
|
Then("connected on 3, 4 should be true") {
|
|
result.stronglyConnected(3, 4) `should be equal to` true
|
|
}
|
|
|
|
Then("connected on 4, 3 should be true") {
|
|
result.stronglyConnected(4, 3) `should be equal to` true
|
|
}
|
|
|
|
Then("connected on 9, 12 should be true") {
|
|
result.stronglyConnected(9, 12) `should be equal to` true
|
|
}
|
|
|
|
Then("connected on 9, 8 should be false") {
|
|
result.stronglyConnected(9, 8) `should be equal to` false
|
|
}
|
|
|
|
Then("connected on 1, 1 should be true") {
|
|
result.stronglyConnected(1, 1) `should be equal to` true
|
|
}
|
|
|
|
Then("connected on 7, 6 should be false") {
|
|
result.stronglyConnected(7, 6) `should be equal to` false
|
|
}
|
|
}
|
|
|
|
Scenario("compute scc on dag") {
|
|
val dag by memoized { DiGraph() }
|
|
|
|
Given("a dag") {
|
|
dag.addEdge(0, 1)
|
|
dag.addEdge(0, 3)
|
|
dag.addEdge(3, 1)
|
|
dag.addEdge(2, 0)
|
|
dag.addEdge(2, 1)
|
|
dag.addEdge(2, 3)
|
|
}
|
|
|
|
lateinit var result: StronglyConnectedComponents
|
|
|
|
When("compute the scc") {
|
|
result = StronglyConnectedComponents(dag)
|
|
}
|
|
|
|
Then("count should be equal to number of vertexes") {
|
|
result.count() `should be equal to` dag.vertexNumber()
|
|
}
|
|
|
|
Then("every vertex is sc only with it self") {
|
|
(0 until dag.vertexNumber())
|
|
.forEach { from ->
|
|
(0 until dag.vertexNumber())
|
|
.forEach { to ->
|
|
when (from) {
|
|
to ->
|
|
result
|
|
.stronglyConnected(from, to) `should be equal to` true
|
|
else -> result
|
|
.stronglyConnected(from, to) `should be equal to` false
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}) |