Graphs
Build a Graph as an Adjacency List
Represent an undirected graph as a per-vertex list of neighbours. For every
edge (u, v), append v to adj[u] and u to adj[v]. Neighbour lists
keep insertion order so the graph is a stable, deterministic fixture for the
search lessons.
Algorithm
Basic Implementation
basic.scala
import scala.collection.mutable.{HashMap, ArrayBuffer}
object Main {
def main(args: Array[String]): Unit = {
val edges = List((1, 2), (1, 3), (2, 4), (3, 4), (4, 5), (5, 6))
val adj = HashMap.empty[Int, ArrayBuffer[Int]]
for ((u, v) <- edges) {
adj.getOrElseUpdate(u, ArrayBuffer.empty[Int]) += v
adj.getOrElseUpdate(v, ArrayBuffer.empty[Int]) += u
}
val parts = adj.keys.toList.sorted.map { v =>
s"$v: [" + adj(v).mkString(", ") + "]"
}
println(parts.mkString("{", ", ", "}"))
}
}
Complexity
- Build: O(V + E)
- Space: O(V + E)
Implementation notes
- Scala: a mutable
HashMapofArrayBuffers stores neighbours viagetOrElseUpdate; keys are sorted before printing. - The replay shows the adjacency list after each edge is added, matching the lesson spec.
adjacency list
Each edge adds two directed entries, one in each direction.