Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@
* [Directedgraph](https://github.com/BrianLusina/KotlinGround/blob/master/datastructures/src/main/kotlin/com/kotlinground/datastructures/graphs/DirectedGraph.kt)
* [Edge](https://github.com/BrianLusina/KotlinGround/blob/master/datastructures/src/main/kotlin/com/kotlinground/datastructures/graphs/Edge.kt)
* [Graph](https://github.com/BrianLusina/KotlinGround/blob/master/datastructures/src/main/kotlin/com/kotlinground/datastructures/graphs/Graph.kt)
* [Graphnode](https://github.com/BrianLusina/KotlinGround/blob/master/datastructures/src/main/kotlin/com/kotlinground/datastructures/graphs/GraphNode.kt)
* [Vertex](https://github.com/BrianLusina/KotlinGround/blob/master/datastructures/src/main/kotlin/com/kotlinground/datastructures/graphs/Vertex.kt)
* Linkedlists
* Doubly
* [Doublylinkedlist](https://github.com/BrianLusina/KotlinGround/blob/master/datastructures/src/main/kotlin/com/kotlinground/datastructures/linkedlists/doubly/DoublyLinkedList.kt)
Expand Down Expand Up @@ -267,6 +267,8 @@
* [Mergeablestateflowimpltest](https://github.com/BrianLusina/KotlinGround/blob/master/datastructures/src/test/kotlin/com/kotlinground/datastructures/crdt/cvrdt/MergeableStateFlowImplTest.kt)
* [Mergeablevaluetest](https://github.com/BrianLusina/KotlinGround/blob/master/datastructures/src/test/kotlin/com/kotlinground/datastructures/crdt/cvrdt/MergeableValueTest.kt)
* [Utilstest](https://github.com/BrianLusina/KotlinGround/blob/master/datastructures/src/test/kotlin/com/kotlinground/datastructures/crdt/cvrdt/UtilsTest.kt)
* Graphs
* [Vertextest](https://github.com/BrianLusina/KotlinGround/blob/master/datastructures/src/test/kotlin/com/kotlinground/datastructures/graphs/VertexTest.kt)
* Linkedlists
* Doubly
* [Doublylinkedlisttest](https://github.com/BrianLusina/KotlinGround/blob/master/datastructures/src/test/kotlin/com/kotlinground/datastructures/linkedlists/doubly/DoublyLinkedListTest.kt)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.kotlinground.datastructures.graphs

class DirectedGraph : Graph<GraphNode>() {
class DirectedGraph<T> : Graph<Vertex<T>>() {

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.kotlinground.datastructures.graphs

abstract class Graph<Node> {
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.kotlinground.datastructures.graphs

import java.util.UUID

/**
* Vertex represents a node in a Graph
* @param data [T] Data contained in a vertex
* @param id [UUID] ID of the vertex
* @param properties [HashMap] Key-value pair of meta information or properties of a vertex
*/
data class Vertex<T>(
val data: T,
val id: String = UUID.randomUUID().toString(),
val adjacentVertices: HashMap<String, Vertex<T>> = hashMapOf(),
val properties: HashMap<String, Any> = hashMapOf(),
) {

/**
* Adds an adjacent vertex
* @param vertex [Vertex] vertex to add as a neighbour
*/
fun addAdjacentVertex(vertex: Vertex<T>) {
if (!adjacentVertices.containsKey(vertex.id.toString())) {
// add vertex to adjacent vertices if not available
adjacentVertices[vertex.id.toString()] = vertex

// add this as an adjacent vertex to this vertex
vertex.addAdjacentVertex(this)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.kotlinground.datastructures.graphs

import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
import java.util.*

class VertexTest {
@Test
fun `should add adjacent vertex for a vertex`() {
val vertexId = UUID.randomUUID().toString()
val vertex = Vertex(1, id = vertexId)

val otherVertexId = UUID.randomUUID().toString()
val otherVertex = Vertex(2, id = otherVertexId)

vertex.addAdjacentVertex(otherVertex)

val vertexNeighbour = vertex.adjacentVertices[otherVertexId]
assertNotNull(vertexNeighbour)
assertEquals(otherVertex, vertexNeighbour)

val otherVertexNeighbour = otherVertex.adjacentVertices[vertexId]
assertNotNull(otherVertexNeighbour)
assertEquals(vertex, otherVertexNeighbour)
}
}