Given this function
def makeAdjacencyList(edgeList: Seq[(String, String)]): Map[String, Seq[String]] = {
val emptyMap = Map[String, Seq[String]]().withDefaultValue(Seq[String]())
edgeList.foldLeft(emptyMap) {
case (acc, curr) => {
acc + (curr._1 -> (acc(curr._1) :+ curr._2))
acc + (curr._2 -> (acc(curr._2) :+ curr._1))
}
}
}
It takes in a edgeList of an undirected graph, for example:
Vector((S1,E1), (S1,D1), (S2,E2), (S2,D1), (S3,E1), (S3,D2))
The function however gives an output of
[E1 -> [S1, S3], D1 -> [S1, S2], E2 -> [S2], D2 -> [S3]]
but I was expecting an output of
[S1 -> [E1, D1], S2 -> [E2, D1], S3 -> [E1], E1 -> [S1, S3], D1 -> [S1, S2], E2 -> [S2], D2 -> [S3]]
It seems like the first line of the foldLeft (acc + (curr._1 -> (acc(curr._1) :+ curr._2))) is being ignored and I am not quite sure why.