-
Notifications
You must be signed in to change notification settings - Fork 8
/
Graph.kt
372 lines (315 loc) · 17.5 KB
/
Graph.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package ai.hypergraph.kaliningraph.types
import ai.hypergraph.kaliningraph.*
import ai.hypergraph.kaliningraph.cache.LRUCache
import ai.hypergraph.kaliningraph.graphs.*
import ai.hypergraph.kaliningraph.parsing.Σᐩ
import ai.hypergraph.kaliningraph.tensor.*
import ai.hypergraph.kaliningraph.theory.wl
import kotlin.js.JsName
import kotlin.math.sqrt
import kotlin.properties.ReadOnlyProperty
import kotlin.random.Random
// Provides caching and inheritable constructors for reified parameters <G, E, V>
// Interfaces are our only option because we need multiple inheritance
@Suppress("FunctionName", "UNCHECKED_CAST")
interface IGF<G, E, V> where G: IGraph<G, E, V>, E: IEdge<G, E, V>, V: IVertex<G, E, V> {
@JsName("G0") val G: (vertices: Set<V>) -> G // Graph constructor
@JsName("E0") val E: (s: V, t: V) -> E // Edge constructor
@JsName("V0") val V: (old: V, edgeMap: (V) -> Set<E>) -> V // Vertex constructor
fun V(out: Set<V>): V = TODO("Must override me if you want a fresh vertex")
val deepHashCode: Int
@JsName("G1") fun G() = G(setOf())
@JsName("G2") fun G(vararg graphs: G): G = G(graphs.toList())
@JsName("G3") fun G(vararg vertices: V): G = G(vertices.map { it.graph })
@JsName("G4") fun G(list: List<Any>): G = when {
list.isEmpty() -> setOf()
list allAre G() -> list.fold(G()) { it, acc -> it + acc as G }
list allAre list.first() -> list.map { it as V }.toSet()
else -> throw Exception("Unsupported constructor: G(${list.joinToString(",") { it::class.simpleName!! }})")
}.let { G(it) }
}
typealias AdjList<V> = List<V2<V>>
interface IGraph<G, E, V>: IGF<G, E, V>, Set<V>, Encodable
/*
* TODO: Which primary interface should we expect graphs to fulfill?
*
* 1. a set Set<V>
* - Pros: Simple, has precedent cf. https://github.com/maxitg/SetReplace/
* - Cons: Finite, no consistency constraints on edges
* 2. a [partial] function E ⊆ V×V / (V) -> Set<V>
* - Pros: Mathematically analogous, can represent infinite graphs
* - Cons: Disallowed on JS, see https://discuss.kotlinlang.org/t/extending-function-in-class/15176
* 3. a [multi]map Map<V, Set<V>>
* - Pros: Computationally efficient representation, graph[v] <=> graph(v)
* - Cons: Finite, incompatible with Set<V> perspective
* 4. a semiring, see https://en.wikipedia.org/wiki/Semiring#Definition
* - Pros: Useful for describing many algebraic path problems
* - Cons: Esoteric API / unsuitable as an abstract interface
*
* Algebraic perspective : https://github.com/snowleopard/alga-paper/releases/download/final/algebraic-graphs.pdf
* : https://arxiv.org/pdf/1909.04881.pdf
* Type-family perspective : https://www.cs.cornell.edu/~ross/publications/shapes/shapes-pldi14-tr.pdf#page=3
* : https://www.cs.cornell.edu/andru/papers/familia/familia.pdf#page=8
* Inductive perspective : https://web.engr.oregonstate.edu/~erwig/papers/InductiveGraphs_JFP01.pdf
* : https://doi.org/10.1145/258949.258955
* : https://www.cs.utexas.edu/~wcook/Drafts/2012/graphs.pdf
* Semiring perspective : http://stedolan.net/research/semirings.pdf
* : https://doi.org/10.1007/978-0-387-75450-5
* : https://doi.org/10.2200/S00245ED1V01Y201001CNT003
*/
where G: IGraph<G, E, V>, E: IEdge<G, E, V>, V: IVertex<G, E, V> {
val vertices: Set<V>
// TODO: Move the following ceremony into named tensor
//-------
operator fun get(cond: (V) -> Boolean): Set<V> = vertices.filter(cond)
val index: VIndex<G, E, V> get() = VIndex(vertices)
operator fun get(vertexIdx: Int): V = index[vertexIdx]
class VIndex<G: IGraph<G, E, V>, E : IEdge<G, E, V>, V : IVertex<G, E, V>>(val set: Set<V>) {
val array: List<V> = set.toList()
val map: Map<V, Int> = array.mapIndexed { index, a -> a to index }.toMap()
// operator fun get(it: IVertex<G, E, V>): Int? = map[it]
operator fun get(it: Int): V = array[it]
operator fun get(v: V): Int = map[v] ?: -1
}
// operator fun SpsMat.get(n0: V, n1: V) = this[index[n0]!!, index[n1]!!]
// operator fun SpsMat.set(n0: V, n1: V, value: Double) {
// this[index[n0]!!, index[n1]!!] = value
// }
//-------
// Implements graph merge. For all vertices in common, merge their neighbors.
// TODO: Figure out how to implement this operator "correctly"
// https://github.com/snowleopard/alga-paper/releases/download/final/algebraic-graphs.pdf
operator fun plus(that: G): G =
G((this - that) + (this join that) + (that - this))
operator fun minus(graph: G): G = G(vertices - graph.vertices)
infix fun join(that: G): Set<V> =
(vertices intersect that.vertices).sortedBy { it.id }.toSet()
.zip((that.vertices intersect vertices).sortedBy { it.id }.toSet())
.map { (left, right) -> V(left) { left.outgoing + right.outgoing } }
.toSet()
// TODO: Reimplement using matrix transpose
fun reversed(): G =
(vertices.associateWith { setOf<E>() } +
vertices.flatMap { src ->
src.outgoing.map { edge -> edge.target to E(edge.target, src) }
}.groupBy({ it.first }, { it.second }).mapValues { (_, v) -> v.toSet() })
.map { (k, v) -> V(k) { v } }.toSet().let { G(it) }
fun isomorphicTo(that: G): Boolean =
this.size == that.size &&
this.edges.size == that.edges.size &&
this.encode().contentEquals(that.encode())
fun vwise(lf: IGraph<G, E, V>.(V, V) -> Double): DoubleMatrix =
DoubleMatrix(size) { i, j ->
(this[i] cc this[j]).let { (v, n) ->
if (n in v.neighbors) lf(v, n) else 0.0
}
}
fun reachSequence(from: Set<V>, ADJ: BooleanMatrix = A_AUG, terminateOnFixpoint: Boolean = false): Sequence<Set<V>> =
sequence {
var B = BooleanMatrix(vertices.size, 1, vertices.map { it in from })
while (true) {
// Check if fixpoint reached
val OLD_B = B
B = ADJ * B
val toYield = B.data.mapIndexed { i, b -> if (b) index[i] else null }.filterNotNull().toSet()
val same = B == OLD_B
if (same && terminateOnFixpoint) break
else if(same) while(true) { yield(toYield) }
else yield(toYield)
}
}
fun reachability(from: Set<V>, steps: Int): Set<V> =
(A_AUG.pow(steps - 1) * BooleanMatrix(vertices.size, 1, vertices.map { it in from }).also { println("v: ${it.shape()}") }).data
.mapIndexed { i, b -> if (b) index[i] else null }.filterNotNull().toSet()
fun transitiveClosure(vtxs: Set<V>): Set<V> =
// edges.filter { it.source in vtxs }.map { it.target }
// TODO: Why does this work but the previous line does not?!
(edgList.filter { it.first in vtxs }.map { it.second.target }.toSet() - vtxs)
.let { if (it.isEmpty()) vtxs else transitiveClosure(vtxs + it) }
fun randomWalk(r: Random = Random.Default) = RandomWalk(r, this as G)
fun asString() =
edgList.map { "${it.first} -> ${it.second.target}" }.formatAsGrid().toString()
fun toDot(highlight: Set<V> = setOf()): String {
fun String.htmlify() =
replace("<", "<").replace(">", ">")
return """
strict digraph {
graph ["concentrate"="false","rankdir"="LR","bgcolor"="transparent","margin"="0.0","compound"="true","nslimit"="20"]
${
vertices.joinToString("\n") {
""""${it.id.htmlify()}" ["shape"="Mrecord","color"="black","fontcolor"="black","fontname"="JetBrains Mono","fontsize"="15","penwidth"="2.0"${if(it in highlight)""","fillcolor"=lightgray,"style"=filled""" else ""}]""" }
}
${edgList.joinToString("\n") { (v, e) ->
val (src, tgt) = v.id.htmlify() to e.target.id.htmlify()
""""$src" -> "$tgt" ["color"="${ if (v is LGVertex && v.occupied) "red" else "black" }","fontname"="JetBrains Mono","arrowhead"="normal","penwidth"="2.0","label"="${(e as? LabeledEdge)?.label ?: ""}"]""" }
}
}
""".trimIndent()
}
}
val <G: IGraph<G, E, V>, E: IEdge<G, E, V>, V: IVertex<G, E, V>> IGraph<G, E, V>.D: DoubleMatrix by cache { DoubleMatrix(size) { i, j -> if (i == j) this[i].neighbors.size.toDouble() else 0.0 } }
// Adjacency matrix
val <G: IGraph<G, E, V>, E: IEdge<G, E, V>, V: IVertex<G, E, V>> IGraph<G, E, V>.A: BooleanMatrix by cache { BooleanMatrix(size) { i, j -> this[j] in this[i].neighbors } }
val <G: IGraph<G, E, V>, E: IEdge<G, E, V>, V: IVertex<G, E, V>> IGraph<G, E, V>.A_AUG: BooleanMatrix by cache { A + A.transpose + BooleanMatrix.one(size) }
// Symmetric normalized adjacency
val <G: IGraph<G, E, V>, E: IEdge<G, E, V>, V: IVertex<G, E, V>> IGraph<G, E, V>.ASYMNORM: DoubleMatrix by cache { vwise { v, n -> 1.0 / sqrt(v.outdegree.toDouble() * n.outdegree.toDouble()) } }
// Graph Laplacian matrix
val <G: IGraph<G, E, V>, E: IEdge<G, E, V>, V: IVertex<G, E, V>> IGraph<G, E, V>.L: DoubleMatrix by cache { D - A }
val <G: IGraph<G, E, V>, E: IEdge<G, E, V>, V: IVertex<G, E, V>> IGraph<G, E, V>.I: DoubleMatrix by cache { DoubleMatrix(size, size, ::kroneckerDelta) }
// Symmetric normalized Laplacian
val <G: IGraph<G, E, V>, E: IEdge<G, E, V>, V: IVertex<G, E, V>> IGraph<G, E, V>.LSYMNORM: DoubleMatrix by cache { I - ASYMNORM }
val <G: IGraph<G, E, V>, E: IEdge<G, E, V>, V: IVertex<G, E, V>> IGraph<G, E, V>.ENCODED: DoubleMatrix by cache { vertices.map { it.encode() }.toTypedArray().toDoubleMatrix() }
// TODO: Implement APSP distance matrix using algebraic Floyd-Warshall
// https://doi.org/10.1137/1.9780898719918.ch5
// All pairs shortest path
val <G: IGraph<G, E, V>, E: IEdge<G, E, V>, V: IVertex<G, E, V>> IGraph<G, E, V>.APSP: Map<Pair<V, V>, Int> by cache {
val dist = mutableMapOf<Pair<V, V>, Int>()
for ((u, v) in vertices * vertices) {
dist[v to u] = if (v == u) 0 else Int.MAX_VALUE
}
for (e in adjList) { dist[e.first to e.second] = 1 }
while (true) {
var done = true
for ((k, i, j) in vertices * vertices * vertices) {
if (dist[i to k]!! < Int.MAX_VALUE && dist[k to j]!! < Int.MAX_VALUE) {
val newDist = dist[i to k]!! + dist[k to j]!!
if (newDist < dist[i to j]!!) { dist[i to j] = newDist; done = false }
}
}
if (done) break
}
dist
}
val <G: IGraph<G, E, V>, E: IEdge<G, E, V>, V: IVertex<G, E, V>> IGraph<G, E, V>.degMap: Map<V, Int> by cache { vertices.associateWith { it.neighbors.size } }
val <G: IGraph<G, E, V>, E: IEdge<G, E, V>, V: IVertex<G, E, V>> IGraph<G, E, V>.edges: Set<E> by cache { edgMap.values.flatten().toSet() }
val <G: IGraph<G, E, V>, E: IEdge<G, E, V>, V: IVertex<G, E, V>> IGraph<G, E, V>.edgList: List<Π2<V, E>> by cache { vertices.flatMap { s -> s.outgoing.map { s to it } } }
val <G: IGraph<G, E, V>, E: IEdge<G, E, V>, V: IVertex<G, E, V>> IGraph<G, E, V>.adjList: AdjList<V> by cache { edgList.map { (v, e) -> v cc e.target } }
val <G: IGraph<G, E, V>, E: IEdge<G, E, V>, V: IVertex<G, E, V>> IGraph<G, E, V>.edgMap: Map<V, Set<E>> by cache { vertices.associateWith { it.outgoing } }
val <G: IGraph<G, E, V>, E: IEdge<G, E, V>, V: IVertex<G, E, V>> IGraph<G, E, V>.histogram: Map<V, Int> by cache { associateWith { it.neighbors.size } }
val cache = LRUCache<String, Any>()
object PlatformVars { var PLATFORM_CALLER_STACKTRACE_DEPTH: Int = 3 }
// This is somewhat of a hack and may break depending on the platform.
// We do this because Kotlin Common has poor reflection capabilities.
fun getCaller() = Throwable().stackTraceToString()
.lines()[PlatformVars.PLATFORM_CALLER_STACKTRACE_DEPTH].hashCode()
// Lazily evaluates and caches result for later use, until cache expiry,
// after which said value will be reevaluated and cached if it is needed
// again. If you believe there may be a bug here, it is really important
// to first check hashCode() / deepHashCode - we expect it to be unique!
// We use this to materialize properties that are expensive to compute,
// and that we expect to be used multiple times once computed.
// The advantage of using the cache { ... } pattern versus lazy { ... }
// is that it allows us to do the following:
// typealias TQ = List<String>
// val TQ.hello by cache { "Hello" }
// val TQ.world by cache { hello + " world" }
// Whereas this is not possible with lazy { ... }:
// typealias TQ = List<String>
// val TQ.hello by lazy { "Hello" }
// val TQ.world by lazy { hello + " world" } // Fails
// It also allows us to add persistent properties to interfaces, see:
// https://stackoverflow.com/questions/43476811/can-a-kotlin-interface-cache-a-value/71632459#71632459
fun <T, Y> cache(caller: Int = getCaller(), fn: Y.() -> T) =
ReadOnlyProperty<Y, T> { y, _ ->
val id = if (y is IGF<*, *, *>) y.deepHashCode else y.hashCode()
val csg = "$id$caller"
// val csg = "${y!!::class.simpleName}${id}$caller"
(cache.getOrPut(csg) { y.fn() as Any } as T)
// .also { println("$id :: $caller :: $it") }
}
class RandomWalk<G, E, V>(
val rand: Random = Random.Default,
val graph: G,
val head: V = graph.random()
): Sequence<RandomWalk<G, E, V>>
where G: IGraph<G, E, V>,
E: IEdge<G, E, V>,
V: IVertex<G, E, V> {
val tail by lazy {
RandomWalk(
graph = graph,
head = graph.edgMap[head]!!.random(rand).target,
rand = rand
)
}
override fun toString() = head.toString()
override fun iterator() = generateSequence(this) { it.tail }.iterator()
}
interface IEdge<G, E, V> : IGF<G, E, V>
where G: IGraph<G, E, V>, E: IEdge<G, E, V>, V: IVertex<G, E, V> {
val source: V
val target: V
operator fun component1() = source
operator fun component2() = target
}
val <G: IGraph<G, E, V>, E: IEdge<G, E, V>, V: IVertex<G, E, V>> IEdge<G, E, V>.graph: G by cache { target.graph }
// TODO: Make this a "view" of the container graph
interface IVertex<G, E, V> : IGF<G, E, V>, Encodable
where G : IGraph<G, E, V>, E : IEdge<G, E, V>, V : IVertex<G, E, V> {
val id: String // TODO: Need to think about this more carefully
val edgeMap: (V) -> Set<E> // Make a self-loop by passing this
// tailrec prohibited on open members? may be possible with deep recursion
// https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-deep-recursive-function/
fun neighbors(k: Int = 0, vertices: Set<V> = neighbors + this as V): Set<V> =
if (k == 0 || vertices.neighbors() == vertices) vertices
else neighbors(k - 1, vertices + vertices.neighbors() + this as V)
// Removes all edges pointing outside the set
private fun Set<V>.closure(): Set<V> =
map { v -> V(this@IVertex as V) { v.outgoing.filter { it.target in this }.toSet() } }.toSet()
private fun Set<V>.neighbors(): Set<V> = flatMap { it.neighbors() }.toSet()
fun neighborhood(): G = G(neighbors(0).closure())
override fun encode(): DoubleArray
}
val <G: IGraph<G, E, V>, E: IEdge<G, E, V>, V: IVertex<G, E, V>> IVertex<G, E, V>.graph: G by cache { G(neighbors(-1)) }
val <G: IGraph<G, E, V>, E: IEdge<G, E, V>, V: IVertex<G, E, V>> IVertex<G, E, V>.incoming: Set<E> by cache { graph.reversed().edgMap[this] ?: emptySet() }
val <G: IGraph<G, E, V>, E: IEdge<G, E, V>, V: IVertex<G, E, V>> IVertex<G, E, V>.outgoing: Set<E> by cache { edgeMap(this as V).toSet() }
val <G: IGraph<G, E, V>, E: IEdge<G, E, V>, V: IVertex<G, E, V>> IVertex<G, E, V>.neighbors: Set<V> by cache { outgoing.map { it.target }.toSet() }
val <G: IGraph<G, E, V>, E: IEdge<G, E, V>, V: IVertex<G, E, V>> IVertex<G, E, V>.outdegree: Int get() = neighbors.size
abstract class AGF<G, E, V> : IGF<G, E, V>
where G : IGraph<G, E, V>, E : IEdge<G, E, V>, V : IVertex<G, E, V> {
override val deepHashCode: Int = Random.nextInt()
override fun hashCode() = deepHashCode
}
abstract class Graph<G, E, V>(override val vertices: Set<V> = setOf()) :
AGF<G, E, V>(), IGraph<G, E, V>, Set<V> by vertices
where G : Graph<G, E, V>, E : Edge<G, E, V>, V : Vertex<G, E, V> {
override fun equals(other: Any?) =
super.equals(other) || (other as? G)?.isomorphicTo(this as G) ?: false
override fun encode() =
if (isEmpty()) DoubleArray(10) { 0.0 }
else wl().values.sorted().map { it.toDouble() }.toDoubleArray()
// https://web.engr.oregonstate.edu/~erwig/papers/InductiveGraphs_JFP01.pdf#page=6
override fun toString() = asString()
}
abstract class Edge<G, E, V>(override val source: V, override val target: V) :
AGF<G, E, V>(), IEdge<G, E, V>
where G : Graph<G, E, V>, E : Edge<G, E, V>, V : Vertex<G, E, V> {
override fun equals(other: Any?) = (other as? E)?.let { hashCode() == other.hashCode() } ?: false
override fun hashCode(): Int = source.hashCode() + target.hashCode()
override fun toString() = "$source→$target"
}
abstract class Vertex<G, E, V>(override val id: String) :
AGF<G, E, V>(), IVertex<G, E, V>
where G : Graph<G, E, V>, E : Edge<G, E, V>, V : Vertex<G, E, V> {
val hash by lazy { id.hashCode() }
override fun equals(other: Any?) = (other as? Vertex<*, *, *>)?.let { id == it.id } ?: false
override fun encode() = id.vectorize()
override fun hashCode() = hash
override fun toString() = id
}
interface Encodable { fun encode(): DoubleArray }
// https://github.com/amodeus-science/amod
abstract class TMap: IGraph<TMap, TRoad, TCity>
abstract class TRoad: IEdge<TMap, TRoad, TCity>
abstract class TCity: IVertex<TMap, TRoad, TCity>
interface SGF<G, E, V> where
G: SGraph<G, E, V>, E: SEdge<G, E, V>, V: SVertex<G, E, V> { /*...*/ }
interface SGraph<G, E, V>: SGF<G, E, V> where
G: SGraph<G, E, V>, E: SEdge<G, E, V>, V: SVertex<G, E, V> { /*...*/ }
interface SEdge<G, E, V>: SGF<G, E, V> where
G: SGraph<G, E, V>, E: SEdge<G, E, V>, V: SVertex<G, E, V> { /*...*/ }
interface SVertex<G, E, V>: SGF<G, E, V> where
G: SGraph<G, E, V>, E: SEdge<G, E, V>, V: SVertex<G, E, V> { /*...*/ }
class SMap: SGraph<SMap, SRoad, SCity> { /*...*/ }
class SRoad: SEdge<SMap, SRoad, SCity> { /*...*/ }
class SCity: SVertex<SMap, SRoad, SCity> { /*...*/ }