commonMain.io.github.alexandrepiveteau.graphs.UndirectedGraph.traversals.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-graphs Show documentation
Show all versions of kotlin-graphs Show documentation
Model directed, undirected, weighted and unweighted graphs and perform computations on them in Kotlin multiplatform.
package io.github.alexandrepiveteau.graphs
import kotlin.contracts.contract
/**
* Performs the given [action] on each edge in this graph.
*
* ## Asymptotic complexity
* - **Time complexity**: O(|E|), where |E| is the number of edges in this graph.
* - **Space complexity**: O(1).
*/
public inline fun UndirectedGraph.forEachEdge(action: (Edge) -> Unit) {
contract { callsInPlace(action) }
forEachVertex { u -> forEachNeighbor(u) { v -> if (u.index <= v.index) action(u edgeTo v) } }
}