MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / distinct_weight

Method distinct_weight

graphs/minimum_spanning_tree_boruvka.py:35–55  ·  view source on GitHub ↗

For Boruvks's algorithm the weights should be distinct Converts the weights to be distinct

(self)

Source from the content-addressed store, hash-verified

33 self.adjacency[tail][head] = weight
34
35 def distinct_weight(self):
36 """
37 For Boruvks's algorithm the weights should be distinct
38 Converts the weights to be distinct
39
40 """
41 edges = self.get_edges()
42 for edge in edges:
43 head, tail, weight = edge
44 edges.remove((tail, head, weight))
45 for i in range(len(edges)):
46 edges[i] = list(edges[i])
47
48 edges.sort(key=lambda e: e[2])
49 for i in range(len(edges) - 1):
50 if edges[i][2] >= edges[i + 1][2]:
51 edges[i + 1][2] = edges[i][2] + 1
52 for edge in edges:
53 head, tail, weight = edge
54 self.adjacency[head][tail] = weight
55 self.adjacency[tail][head] = weight
56
57 def __str__(self):
58 """

Callers

nothing calls this directly

Calls 3

get_edgesMethod · 0.95
sortMethod · 0.80
removeMethod · 0.45

Tested by

no test coverage detected