SwiftGraph
SwiftGraph copied to clipboard
Comparison of `WeightedEdge` should take into consideration whether the edges are directed
Let's say I have two weighted and not directed edges, connecting the same vertices, as follows:
let edgeA = WeightedEdge<Int> = WeightedEdge(u: 0, v: 1, directed: false, weight: 10)
let edgeB = WeightedEdge<Int> = WeightedEdge(u: 1, v: 0, directed: false, weight: 10)
Currently we have so that edgeA == edgeB
returns false
.
Shouldn't the ==(_:_:)
operator take into consideration whether they're directed? Or am I missing something?
So in this particular scenario I would expect edgeA == edgeB
to return true
, as they are not directed.
In summary we would have:
let directedEdgeA = WeightedEdge<Int> = WeightedEdge(u: 0, v: 1, directed: false, weight: 10)
let directedEdgeB = WeightedEdge<Int> = WeightedEdge(u: 1, v: 0, directed: false, weight: 10)
directedEdgeA == directedEdgeB // returns `true`
let undirectedEdgeC = WeightedEdge<Int> = WeightedEdge(u: 0, v: 1, directed: true, weight: 10)
let undirectedEdgeD = WeightedEdge<Int> = WeightedEdge(u: 1, v: 0, directed: true, weight: 10)
undirectedEdgeC == undirectedEdgeD // returns `false`
If there are any worries about how changing the ==(_:_:)
implementation would affect other features, should we have a separate method to compare two edges?
Any thoughts on this?
It's a fair question. I don't know if this would break anyone's existing code. Is it causing a particular problem for you? Since I wouldn't really call it a bug, but more of a vagary we would make the change in a new point release not a bug fix release. i.e. 3.2 not 3.1.1
If the edges were not directed, we would then have to check if they were between the same two vertices, which would have a very slight performance cost if an algorithm was doing a ton of equality checks.
One could also argue, that maybe the data matters more than the graph theory in some circumstances. For example, imagine a program that was marking which vertex the edge was created from and to even though it allowed traversal in both directions.