Code Monkey home page Code Monkey logo

Comments (3)

HanekawaSimp avatar HanekawaSimp commented on June 7, 2024

Yes they are. I tried doing the questions on my own but was struggling a lot. Decided to look up for solutions to see how they work and found this. I have been trying to make this work but these are obsolete and do not run or even if they do, the solutions are wrong.

from coursera-stanford-graph-search-shortest-paths-and-data-structures.

HanekawaSimp avatar HanekawaSimp commented on June 7, 2024

heres the code that works if anyone is still looking for the answers

import time

file_path = "clustering1.txt"

with open(file_path, "r") as file:
num_nodes = int(file.readline())
clusters = {x: [x] for x in range(1, num_nodes + 1)}
edges = []
for line in file:
node1, node2, cost = map(int, line.strip().split())
edges.append((node1, node2, cost))
sorted_edges = sorted(edges, key=lambda x: x[2])

k = 4

def get_key_by_value(d, k):
for leader, nodes in d.items():
if k in nodes:
return leader

def update_clusters(c, a, d):
c[a].extend(c[d])
del c[d]

iteration = 0

start_time = time.time()

for edge in sorted_edges:
node1, node2, cost = edge
key1 = get_key_by_value(clusters, node1)
key2 = get_key_by_value(clusters, node2)
if key1 == key2:
continue
elif len(clusters[key1]) >= len(clusters[key2]):
update_clusters(clusters, key1, key2)
else:
update_clusters(clusters, key2, key1)
if len(clusters) == k:
break

max_spacing = None

for edge in sorted_edges:
node1, node2, cost = edge
key1 = get_key_by_value(clusters, node1)
key2 = get_key_by_value(clusters, node2)
if key1 != key2:
max_spacing = cost
break

end_time = time.time()
execution_time = end_time - start_time

print("Execution time: {:.6f} seconds".format(execution_time))
print("Execution time: {:.6f} minutes".format(execution_time / 60))
print("Execution time: {:.6f} hours".format(execution_time / 3600))
print("Maximum spacing of a 4-clustering: ", max_spacing)

from coursera-stanford-graph-search-shortest-paths-and-data-structures.

HanekawaSimp avatar HanekawaSimp commented on June 7, 2024

i was able to do it

from coursera-stanford-graph-search-shortest-paths-and-data-structures.

Related Issues (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.