Reputation: 1659
An adjacency list representation as a graph is shown here,
I am fairly new to Scala, and so this is fairly trivial I am assuming. I want to represent my graph in Scala as an adjacency list but edges should have a weight. So I was thinking having a tuple for each entry in the adjacency list for each vertex, where the tuple is the vertex and the edge cost. Hopefully this makes sense. I just want to know how to write this graph representation to a val.
For example:
A -> [(B, 1), (C, 1), (D, 1)]
B -> [(A, 1), (C, 2), (D, 2)]
C -> [(A, 1), (B, 2)]
D -> [(A, 1), (B, 2)]
How would I write this as a val in Scala? The index of the list can represent the letters of the nodes. Would this be a List[List[(String, Int)]]
? I find it hard in Scala to assign values to variables when I really want to specify the type of the variable.
Upvotes: 0
Views: 851
Reputation: 20285
How would I write this as a val in Scala?
val adjList = List(List(("b", 1), ("c", 1), ("d", 1)), List(...), List(...), List(...))
The types are inferred for your to be List[List[(String, Int)]]
. You can manually specify them if you wish but it is not required.
Upvotes: 0