Compare commits
1 Commits
932a867720
...
2fe874e733
Author | SHA1 | Date |
---|---|---|
coolneng | 2fe874e733 |
|
@ -27,7 +27,8 @@ def get_furthest_element(element, data):
|
||||||
furthest_index = element_df["distance"].idxmax()
|
furthest_index = element_df["distance"].idxmax()
|
||||||
furthest_row = data.iloc[furthest_index]
|
furthest_row = data.iloc[furthest_index]
|
||||||
furthest_point = get_different_element(original=element, row=furthest_row)
|
furthest_point = get_different_element(original=element, row=furthest_row)
|
||||||
return {"point": furthest_point, "distance": furthest_row["distance"]}
|
furthest_element = {"point": furthest_point, "distance": furthest_row["distance"]}
|
||||||
|
return furthest_element, furthest_index
|
||||||
|
|
||||||
|
|
||||||
def greedy_algorithm(n, m, data):
|
def greedy_algorithm(n, m, data):
|
||||||
|
@ -36,10 +37,10 @@ def greedy_algorithm(n, m, data):
|
||||||
solutions = solutions.append(first_solution, ignore_index=True)
|
solutions = solutions.append(first_solution, ignore_index=True)
|
||||||
for _ in range(m):
|
for _ in range(m):
|
||||||
last_solution = solutions["point"].tail(n=1)
|
last_solution = solutions["point"].tail(n=1)
|
||||||
centroid = get_furthest_element(element=int(last_solution), data=data)
|
centroid, index = get_furthest_element(element=int(last_solution), data=data)
|
||||||
solutions = solutions.append(dict(centroid), ignore_index=True)
|
solutions = solutions.append(dict(centroid), ignore_index=True)
|
||||||
data = data.drop(centroid["point"], columns=["source", "destination"])
|
data = data.drop(index)
|
||||||
print(solutions)
|
return solutions
|
||||||
|
|
||||||
|
|
||||||
# NOTE In each step, switch to the element that gives the least amount
|
# NOTE In each step, switch to the element that gives the least amount
|
||||||
|
@ -56,7 +57,8 @@ def main():
|
||||||
if len(argv) != 2:
|
if len(argv) != 2:
|
||||||
usage(argv)
|
usage(argv)
|
||||||
n, m, data = parse_file(argv[1])
|
n, m, data = parse_file(argv[1])
|
||||||
greedy_algorithm(n, m, data)
|
solutions = greedy_algorithm(n, m, data)
|
||||||
|
print(solutions)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in New Issue