All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.io.github.alexandrepiveteau.graphs.DirectedNetworkBuilder.kt Maven / Gradle / Ivy

Go to download

Model directed, undirected, weighted and unweighted graphs and perform computations on them in Kotlin multiplatform.

There is a newer version: 0.6.1
Show newest version
package io.github.alexandrepiveteau.graphs

import io.github.alexandrepiveteau.graphs.internal.IntVector

/** A [DirectedNetworkBuilder] is a [DirectedGraphBuilder] which can also add arcs with a weight. */
public interface DirectedNetworkBuilder : NetworkBuilder, DirectedGraphBuilder {

  /**
   * Adds a new weighted arc to the graph.
   *
   * @param arc the arc to add.
   * @param weight the weight of the arc.
   */
  public fun addArc(arc: Arc, weight: Int = 0)

  override fun addArc(arc: Arc): Unit = addArc(arc, 0)
}

/** A [MutableListNetworkBuilder] for [DirectedNetworkBuilder]. */
@PublishedApi
internal open class MutableListDirectedNetworkBuilder(
    private val neighbors: MutableList,
    private val weights: MutableList,
) : MutableListNetworkBuilder(neighbors, weights), DirectedNetworkBuilder {
  override fun addArc(arc: Arc, weight: Int) {
    val (u, v) = arc
    checkLink(u, v)
    neighbors[u.index] += v.index
    weights[u.index] += weight
  }
}

/**
 * Builds a [DirectedNetwork] using the given [scope].
 *
 * @param scope the scope in which the graph is built.
 * @return the newly built graph.
 */
public inline fun buildDirectedNetwork(scope: DirectedNetworkBuilder.() -> Unit): DirectedNetwork {
  // TODO : Have a true / false parameter to choose if we want to use an adjacency matrix or not.
  // 1. Store the neighbors of each vertex in a vector.
  val neighbors = mutableListOf()
  val weights = mutableListOf()
  MutableListDirectedNetworkBuilder(neighbors, weights).apply(scope)
  // 2. Store the neighbors of each vertex in an array.
  val (n, w) = compactToVertexAndWeightsArray(neighbors, weights)
  return AdjacencyListDirectedNetwork(n, w)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy