Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is the output of course3/assignment1SchedulingAndMST/question3/output_random_33_1000.txt correct? #69

Open
ahmedmustahid opened this issue Jun 27, 2024 · 0 comments

Comments

@ahmedmustahid
Copy link

ahmedmustahid commented Jun 27, 2024

In order to verify, I have used python networkx to find the MST and then calculate the cost.
Using networkx the value of sum of edges of MST is -2355343 as opposed to -2357954 as provided.
The following is the code:

import networkx as nx
from typing import Dict, List, Tuple

def getNxGraph(fname: str):
    graph: List[Tuple[str, str, Dict[str, int]]] = []
    with open(fname, "r") as f:
        for line in f:
            if len(line.split()) < 3:
                continue
            v1, v2, weight = line.split()
            weight = int(weight)
            temp = (v1, v2, {"weight": weight})
            graph.append(temp)

    G = nx.Graph()
    for g in graph:
        e1, e2, wt = g
        G.add_edge(e1, e2, weight=wt["weight"])

    return G
  
  
  if __name__ == "__main__":
  
    fname = f"input_random_33_1000.txt"

    
    G = getNxGraph(fname)
    G = nx.minimum_spanning_tree(G)
    s = sorted(G.edges(data=True))
    mincost = sum(
        [e[2]["weight"] for e in s]
    )  # [('1', '2', {'weight': 1}), ('1', '3', {'weight': 4}), ('2', '4', {'weight': 2})]
    print(s)
    print(f"mincost {mincost}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant