Skip to content
This repository has been archived by the owner on Feb 3, 2018. It is now read-only.

Commit

Permalink
Fixed calculation of minimal spanning trees of graphs with negative-w…
Browse files Browse the repository at this point in the history
…eighted edges.
  • Loading branch information
pmatiello committed Dec 31, 2011
1 parent 22790de commit f36f755
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 15 deletions.
5 changes: 3 additions & 2 deletions Changelog
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A library for working with graphs in Python
CHANGELOG


Next Release [??? ??, ????]
Release 1.8.1 [??? ??, ????]

Enhancements:
Shortest-path now executes in O(n*log(n)) instead of O(n^2).
Expand All @@ -16,7 +16,8 @@ Fixes:
Linking, unlinking and relinking a hypernode to hyperedge now works as expected;
Graph comparison does not raise exceptions anymore;
Fixed undesired sharing of attribute lists;
Fixed reading of XML-stored graphs with edge attributes.
Fixed reading of XML-stored graphs with edge attributes;
Fixed calculation of minimal spanning trees of graphs with negative-weighted edges.


Release 1.8.0 [Oct 01, 2010]
Expand Down
8 changes: 4 additions & 4 deletions core/pygraph/algorithms/minmax.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def minimal_spanning_tree(graph, root=None):
# Algorithm loop
while (nroot is not None):
ledge = _lightest_edge(graph, visited)
if (ledge == (-1, -1)):
if (ledge == None):
if (root is not None):
break
nroot = _first_unvisited(graph, visited)
Expand Down Expand Up @@ -118,13 +118,13 @@ def _lightest_edge(graph, visited):
@rtype: tuple
@return: Lightest edge in graph going from a visited node to an unvisited one.
"""
lightest_edge = (-1, -1)
weight = -1
lightest_edge = None
weight = None
for each in visited:
for other in graph[each]:
if (other not in visited):
w = graph.edge_weight((each, other))
if (w < weight or weight < 0):
if (weight is None or w < weight or weight < 0):
lightest_edge = (each, other)
weight = w
return lightest_edge
Expand Down
9 changes: 0 additions & 9 deletions tests/unittests-minmax.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,6 @@ def test_minimal_spanning_tree_on_graph(self):
add_spanning_tree(gr2, mst_copy)
assert len(depth_first_search(gr2, root=0)[0]) < len_dfs

def test_minimal_spanning_tree_with_negative_weigths(self):
gr = graph()
gr.add_nodes((0,1,2,3))
gr.add_edge((0,1), wt=-0.4)
gr.add_edge((1,2), wt=-0.6)
gr.add_edge((2,0), wt=-0.5)
gr.add_edge((3,2), wt=-0.1)
gr.add_edge((3,1), wt=-0.2)
print minimal_spanning_tree(gr, 0)

# shortest path tests

Expand Down

0 comments on commit f36f755

Please sign in to comment.