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
| object GraphDemo { var edgeSet: Set[String] = Set[String]() var nodeMap: scala.collection.mutable.Map[String, Seq[String]] = scala.collection.mutable.Map[String, Seq[String]]()
def generate(list: List[String]): Unit = { list.map(x => { val kv = x.split("->") var value: Seq[String] = nodeMap.getOrElse(kv(0), Seq[String]()) value = value :+ kv(1) nodeMap.put(kv(0),value) }) }
def dis(root: String, path: String): Unit = { val nodes = nodeMap.getOrElse(root, Seq[String]()) nodes.foreach(x => { val temp = path + x dis(x, temp) }) if(nodes.isEmpty){ println(path) } }
def main(args: Array[String]): Unit = { val list = List("a->b", "c->d", "c->e", "b->c", "e->f") generate(list) println(nodeMap) dis("a", "a") } }
|