Teodorico Levoff - 4 months ago 67

Scala Question

An adjacency list representation as a graph is shown here,

https://www.khanacademy.org/computing/computer-science/algorithms/graph-representation/a/representing-graphs.

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)]]`

Answer

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.