-
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.
This commit includes the following changes: - Update the codebase to Kotlin 1.9.20 - Split `Graph` into `VertexSet` and `Successors` interfaces - Update the existing algorithms to work on generic types - Introduce `Directed` and `Undirected` interfaces - Add random walk and weighted random walk algorithms
- Loading branch information
1 parent
c1c1b56
commit fc75380
Showing
36 changed files
with
646 additions
and
293 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
8 changes: 8 additions & 0 deletions
8
src/commonMain/kotlin/io/github/alexandrepiveteau/graphs/Directed.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,8 @@ | ||
package io.github.alexandrepiveteau.graphs | ||
|
||
/** A marker interface for [Graph]s that are directed. */ | ||
public interface Directed { | ||
|
||
/** Returns true if the given [arc] is contained in this [Directed] graph, and false otherwise. */ | ||
public operator fun contains(arc: Arc): Boolean | ||
} |
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
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
38 changes: 38 additions & 0 deletions
38
src/commonMain/kotlin/io/github/alexandrepiveteau/graphs/Predecessors.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,38 @@ | ||
package io.github.alexandrepiveteau.graphs | ||
|
||
/** | ||
* [Predecessors] are a [VertexSet] where each [Vertex] has a list of neighbors, which can be | ||
* accessed by their index. | ||
*/ | ||
public interface Predecessors : VertexSet { | ||
|
||
/** | ||
* Returns the number of edges entering the [Vertex] at the given index in this [Predecessors]. If | ||
* the vertex is not contained in this [Predecessors], an [IndexOutOfBoundsException] is thrown. | ||
*/ | ||
public fun predecessorsSize(index: Int): Int | ||
|
||
/** | ||
* Returns the number of edges entering the given [vertex] in this [Predecessors]. If the vertex | ||
* is not contained in this [Predecessors], a [NoSuchVertexException] is thrown. | ||
*/ | ||
public fun predecessorsSize(vertex: Vertex): Int = predecessorsSize(index(vertex)) | ||
|
||
/** | ||
* Returns the [Vertex] at the given [neighborIndex] index in the list of neighbors of the | ||
* [Vertex] at the given [index] in this [Predecessors]. If the vertex is not contained in this | ||
* [Predecessors], an [IndexOutOfBoundsException] is thrown. | ||
*/ | ||
public fun predecessor(index: Int, neighborIndex: Int): Vertex | ||
|
||
/** | ||
* Returns the [Vertex] at the given [neighborIndex] index in the list of neighbors of the given | ||
* [vertex] in this [Predecessors]. If the vertex is not contained in this [Predecessors], a | ||
* [NoSuchVertexException], and if the neighbor index is not contained in the list of neighbors of | ||
* the vertex, an [IndexOutOfBoundsException] is thrown. | ||
*/ | ||
public fun predecessor( | ||
vertex: Vertex, | ||
neighborIndex: Int, | ||
): Vertex = predecessor(index(vertex), neighborIndex) | ||
} |
56 changes: 56 additions & 0 deletions
56
src/commonMain/kotlin/io/github/alexandrepiveteau/graphs/Successors.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,56 @@ | ||
package io.github.alexandrepiveteau.graphs | ||
|
||
/** | ||
* [Successors] are a [VertexSet] where each [Vertex] has a list of neighbors, which can be accessed | ||
* by their index. | ||
*/ | ||
public interface Successors : VertexSet { | ||
|
||
/** | ||
* Returns the number of edges leaving the [Vertex] at the given index in this [Successors]. If | ||
* the vertex is not contained in this [Successors], an [IndexOutOfBoundsException] is thrown. | ||
*/ | ||
public fun successorsSize(index: Int): Int | ||
|
||
/** | ||
* Returns the number of edges leaving the given [vertex] in this [Successors]. If the vertex is | ||
* not contained in this [Successors], a [NoSuchVertexException] is thrown. | ||
*/ | ||
public fun successorsSize(vertex: Vertex): Int = successorsSize(index(vertex)) | ||
|
||
/** | ||
* Returns the index of the vertex at the given [neighborIndex] index in the list of neighbors of | ||
* the [Vertex] in this [Successors]. If the vertex is not contained in this [Successors], an | ||
* [IndexOutOfBoundsException] is thrown. | ||
*/ | ||
public fun successorIndex(index: Int, neighborIndex: Int): Int | ||
|
||
/** | ||
* Returns the index of the vertex at the given [neighborIndex] index in the list of neighbors of | ||
* the [Vertex] in this [Successors]. If the vertex is not contained in this [Successors], a | ||
* [NoSuchVertexException], and if the neighbor index is not contained in the list of neighbors of | ||
* the vertex, an [IndexOutOfBoundsException] is thrown. | ||
*/ | ||
public fun successorIndex( | ||
vertex: Vertex, | ||
neighborIndex: Int, | ||
): Int = successorIndex(index(vertex), neighborIndex) | ||
|
||
/** | ||
* Returns the [Vertex] at the given [neighborIndex] index in the list of neighbors of the | ||
* [Vertex] at the given [index] in this [Successors]. If the vertex is not contained in this | ||
* [Successors], an [IndexOutOfBoundsException] is thrown. | ||
*/ | ||
public fun successorVertex(index: Int, neighborIndex: Int): Vertex | ||
|
||
/** | ||
* Returns the [Vertex] at the given [neighborIndex] index in the list of neighbors of the given | ||
* [vertex] in this [Successors]. If the vertex is not contained in this [Successors], a | ||
* [NoSuchVertexException], and if the neighbor index is not contained in the list of neighbors of | ||
* the vertex, an [IndexOutOfBoundsException] is thrown. | ||
*/ | ||
public fun successorVertex( | ||
vertex: Vertex, | ||
neighborIndex: Int, | ||
): Vertex = successorVertex(index(vertex), neighborIndex) | ||
} |
42 changes: 42 additions & 0 deletions
42
src/commonMain/kotlin/io/github/alexandrepiveteau/graphs/SuccessorsWeight.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,42 @@ | ||
package io.github.alexandrepiveteau.graphs | ||
|
||
/** | ||
* [SuccessorsWeight] are a [Successors] where each link has a weight, which can be accessed by | ||
* their index. | ||
*/ | ||
public interface SuccessorsWeight : Successors { | ||
|
||
/** | ||
* Returns the weight of the edge between the [Vertex] at [index] and the [Vertex] at | ||
* [neighborIndex]. | ||
* | ||
* @throws IndexOutOfBoundsException if [index] or [neighborIndex] are not valid indices. | ||
*/ | ||
public fun successorWeight(index: Int, neighborIndex: Int): Int | ||
|
||
/** | ||
* Returns the weight of the edge between the [Vertex] at [index] and its [neighbor]. | ||
* | ||
* @throws IndexOutOfBoundsException if [index] is not a valid index. | ||
* @throws NoSuchVertexException if [neighbor] is not a valid vertex. | ||
*/ | ||
public fun successorWeight(index: Int, neighbor: Vertex): Int | ||
|
||
/** | ||
* Returns the weight of the edge between the [vertex] and its [neighbor]. | ||
* | ||
* @throws NoSuchVertexException if [vertex] or [neighbor] are not valid vertices. | ||
* @throws NoSuchEdgeException if there is no edge between [vertex] and [neighbor]. | ||
*/ | ||
public fun successorWeight(vertex: Vertex, neighbor: Vertex): Int = | ||
successorWeight(index(vertex), neighbor) | ||
|
||
/** | ||
* Returns the weight of the edge between the [vertex] and the [Vertex] at [neighborIndex]. | ||
* | ||
* @throws NoSuchVertexException if [vertex] is not a valid vertex. | ||
* @throws IndexOutOfBoundsException if [neighborIndex] is not a valid index. | ||
*/ | ||
public fun successorWeight(vertex: Vertex, neighborIndex: Int): Int = | ||
successorWeight(index(vertex), neighborIndex) | ||
} |
8 changes: 8 additions & 0 deletions
8
src/commonMain/kotlin/io/github/alexandrepiveteau/graphs/Undirected.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,8 @@ | ||
package io.github.alexandrepiveteau.graphs | ||
|
||
/** A marker interface for [Graph]s that are undirected. */ | ||
public interface Undirected { | ||
|
||
/** Returns true if the given [edge] is contained in this [Undirected] graph, and false otherwise. */ | ||
public operator fun contains(edge: Edge): Boolean | ||
} |
Oops, something went wrong.