-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathAdjacencyList.kt
65 lines (52 loc) · 1.96 KB
/
AdjacencyList.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class AdjacencyList<T: Comparable<T>>: Graphable<T> {
var adjacencyMap: MutableMap<Vertex<T>, MutableList<Edge<T>>> = mutableMapOf()
private fun addDirectedEdge(source: Vertex<T>, destination: Vertex<T>, weight: Double?) {
val edge = Edge(source, destination, weight)
adjacencyMap[source]?.add(edge)
}
private fun addUndirectedEdge(vertices: Pair<Vertex<T>, Vertex<T>>, weight: Double?) {
val (source, destination) = vertices
addDirectedEdge(source, destination, weight)
addDirectedEdge(destination, source, weight)
}
override fun createVertex(data: T): Vertex<T> {
val vertex = Vertex(data)
adjacencyMap[vertex] ?: run {
adjacencyMap[vertex] = mutableListOf()
}
return vertex
}
override fun add(type: EdgeType, source: Vertex<T>, destination: Vertex<T>, weight: Double?) = when(type) {
is Directed -> {
addDirectedEdge(source, destination, weight)
}
is Undirected -> {
addUndirectedEdge(Pair(source, destination), weight)
}
}
override fun weight(source: Vertex<T>, destination: Vertex<T>): Double? {
val edges = adjacencyMap[source]
edges?.let {
it.forEach { edge ->
if (edge.destination == destination) return edge.weight
}
}
return null
}
override fun edges(source: Vertex<T>): MutableList<Edge<T>>? = adjacencyMap[source]
override fun toString(): String {
var result = ""
for ((vertex, edges) in adjacencyMap) {
var edgeString = ""
for ((index, edge) in edges.withIndex()) {
if (index != edges.count() - 1) {
edgeString += "${edge.destination}, "
} else {
edgeString += "${edge.destination}"
}
}
result += "$vertex ---> [ $edgeString ] \n"
}
return result
}
}