Created
December 26, 2023 01:07
-
-
Save vhxs/b0ddaa7c7d88ec17efb5ef4744362dcc to your computer and use it in GitHub Desktop.
Testing edge order is all that matters to make MSTs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import networkx as nx | |
def random_graph() -> nx.Graph: | |
num_nodes = 20 | |
probability = 0.2 | |
return nx.erdos_renyi_graph(num_nodes, probability) | |
def add_weights(graph: nx.Graph): | |
running_weight = 0 | |
for edge in graph.edges: | |
running_weight += random.randint(0, 100) | |
graph[edge[0]][edge[1]]["weight"] = running_weight | |
def get_mst_edges(graph: nx.Graph): | |
tree = nx.minimum_spanning_tree(graph) | |
return tree.edges | |
if __name__ == '__main__': | |
for _ in range(10): | |
G = random_graph() | |
mst = None | |
for _ in range(10): | |
H = G.copy() | |
add_weights(H) | |
tree = get_mst_edges(H) | |
if mst is None: | |
mst = tree | |
elif mst != tree: | |
print("Edge weights matter!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment