-
Notifications
You must be signed in to change notification settings - Fork 183
Inconsistency about weights #1519
Comments
I would prefer if the adjacency matrix would not be the same as That the default weights return a matrix full of ones is a bit of a hack for performance - as for algorithms that take a weight parameter one usually just queries the entries of that matrix where there is an edge. There are certainly other ways to do that that would yield the same performance though. |
What about allowing So what about having an alternative type like |
OK, let's see if this helps clear things up:
Adjacency matrices simply tell you whether an edge exists between two given vertices. It has nothing to do with weights at all. Adjacency matrices do not have to be integers: you can tell
So if you want a matrix of Note that an adjacency matrix will ALWAYS be a sparse matrix, even if weights are defined (as in MetaGraphs) in another data structure (like a dict). Does this help? Edited to respond to @greimel - in LightGraphs, we NEVER reference fields of structs - there are always accessors. That is, you should NEVER use For
|
Thank you for your responses! My question was not how to use LightGraphs, but why it works the way it does. Why doesn't I think this is inconsistent with
|
For a consistent behavior in the ecosystem, these things would have to change
|
I think you are misunderstanding. Moreover, see my code snipped from the OP using LightGraphs, SimpleWeightedGraphs
begin
w_g = SimpleWeightedGraph(3)
add_edge!(w_g, 1, 2, 0.5)
add_edge!(w_g, 2, 3, 0.8)
add_edge!(w_g, 1, 3, 2.0)
end
adjacency_matrix(w_g)
# 3×3 SparseMatrixCSC{Float64, Int64} with 6 stored entries:
# ⋅ 0.5 2.0
# 0.5 ⋅ 0.8
# 2.0 0.8 ⋅ |
Ah, you're talking about
That is, we really should be doing a |
Very good! I am happy to prepare a PR. What about point two of
Do you agree that weights of a MetaGraph should be zero for unconnected nodes? |
I'm not convinced yet. Also - please include benchmarks in your PR: that is, you can do |
I take back what I said earlier about efficiency. What we should be doing for SWG is |
Ok, so let me give another try. Point 1: Simplicity -
|
I think there may be a misunderstanding about what "default weight" means: it means: IF the edge exists, AND no weights have been defined for the edge, THEN we use this value. It's not "Every entry in the adjacency matrix has this value UNLESS an edge is defined". The latter definition would result in dense matrices for all adjacency matrices, which is not desirable at all. Edited: but it's possible I'm misunderstanding, since I'm rushing to get out the door and haven't had coffee yet. Sorry. This discussion should probably wait until I'm back in the office (week after next). |
I guess we agree then that the current behavior is a bug. Here's the other part of the code snippet from the OP.
The weights are 1.0 for unconnected nodes. |
That sure looks like a bug. But I don't think I've ever used that constructor. You're familiar with Let's pick this up week after next if you don't mind :) I'm seriously late this morning. |
This is a really important discussion to have, though. Thanks for bringing it up. |
I think I understand Seth's point to be distinguishing the weights object from the weighted adjacency matrix. If you do collect(weights(mg)) .* adjacency_matrix(mg) you get the weighted adjacency matrix. A nonbreaking solution could be to introduce a weighted_adjacency_matrix(mg) function. |
I could live with that. But it would keep the inconsistencies between Either of the two changes I am proposing is breaking, unfortunately. But I would consider the current behavior as bugs in both cases. One could
Changing the behavior of |
I'm tempted to make |
Closing this out as it appears that the discussion has run its course. |
I'd appreciate if you could reopen this. I don't think the issue is solved. (It's just that I was busy with other things.) If you create the same graph, once as a Once I am bitten by this issue again, I'll make that PR (that I announced months ago, sorry!). |
Happy to reopen. I still think |
I am confused about how the LightGraphs ecosystem handles weighted graphs. I wonder why
adjacency_matrix == weights
forSimpleWeightedGraph
s, while forMetaGraph
sadjacency_matrix
is a matrix of {0,1}, even in the presence of weightsIs there a reason for this inconsistency?
I am not a mathematician, but an economist. My standard reference for graphs is
Social and Economic Networks by Matthew O. Jackson. And he makes no difference between adjacency matrix and weight matrix.
I am happy to contribute to get rid of this inconsistency, if that means to bring MetaGraphs closer to the behavior of SimpleWeightedGraphs.
Here is some code to show you what I mean
The text was updated successfully, but these errors were encountered: