Итак, я написал это для достижения этого
// Example graph 1
val z = Graph(1~>2, 2~>3, 2~>4, 3~>4, 5~>4)
// Example graph 2
val z1 = Graph(1~>2, 2~>3, 2~>4, 3~>4, 4~>6, 6~>7, 5~>4)
def getAllPaths(g: z1.NodeT, paths: List[z1.NodeT]): List[Any] = {
val directs = g.diSuccessors.toList
if (directs.length == 0) {
// No more direct successor left, return the array itself
paths
} else if (directs.length == 1) {
// Node with single direction, simply returns itself
if (paths.length == 0) {
// appends g itself and its direct successor for the first iteration
getAllPaths(directs(0), paths :+ g :+ directs(0))
} else {
// Appends only the direct successor
getAllPaths(directs(0), paths :+ directs(0))
}
} else {
directs.map(d => {
getAllPaths(d, paths :+ d)
})
}
}
val accum = List[z1.NodeT]()
println(getAllPaths(z1.get(1), accum))
// Results in: List(List(1, 2, 3, 4, 6, 7), List(1, 2, 4, 6, 7))
Оставьте это здесь на всякий случай, если кто-то заинтересован в решении той же проблемы!
Также, пожалуйста, помогите мненапиши более элегантно ;) .. Я все еще новичок в Scala
Последующие вопросы:
- Как мне ссылаться на тип NodeT без прохождения через переменнуюкак в примере выше, который обращается к нему через
z1.NodeT
?