1

If a directed Edge is implemented something like:

class EdgeImpl(origin: Node, dest: Node) {
    def from = origin
    def to = dest
  }

then which is the difference for implementing an undirected Edge while when we create a new Edge we also have to say in both cases: new EdgeImpl(node1, node2)? I do not get the difference in implementation :(

Edit

I was analyzing, more concretely, this example

4

1 回答 1

2

边缘的实现没有真正的区别,在这两种情况下,只需要指定两个连接的节点。

当您想要实现边缘的含义需要解释的其他东西时,差异会弹出。例如,如果您有一个方法areConnected(a: Node, b: Node): Boolean,那么它的实现将遍历边列表,并且如果在有向图中,if 将返回 true from == a && to == b(from == a && to == b) || from == b && to == a)无向版本将改为评估。

该示例有点令人费解,并没有说明为什么确实需要所描述的功能,但请考虑例如如何创建 WeightedDirectedGraph,其中每条边还包含连接节点之间的权重或距离。

于 2015-05-18T19:11:20.627 回答