I have already coded Prim's algorithm in python, but this takes as inputs a weighted graph with nodes and edges, which is not what i have.
How do I convert the given co-ordinates in to a graph such that the program can accept the inputs, and i get a meaningful answer?
This is where the concept of Classes shall prove useful.
Create a Coordinate class and store your nodes and edges as instances of that class. Add a __sub__ method to the Coordinate class so as to get the Weights (Distances or whatever you want weights to be.) between two Coordinates.
Example :-
class Coordinate:
def __init__(self, x, y):
self.x = x
self.y = y
def __sub__(self, other):
return (self.x - other.x)**2 + (self.y - other.y)**2
# No need to square root to compare distances.
Now you can easily create an Adjacency Map/List as per your convenience and also find the corresponding weights and use your algorithm.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.