I have a list of X and Y points that are going to be imported into the program. I was wondering if it is possible to make a directory for how to connect them? Almost like a tree graph, but instead of using the edge it would use the x and y points.
The [0]
in the example will be the starting point and the numbers will be the points from the imported file.
For Example
----4------5
|
8------7-----6----[0]------1-------2-----3
|
9---
I found an algorithm called Breadth-first search
to be able to determine the best path if given a start and an end point. I know that algorithm is for searching the possible paths but not determining the paths. If given the points from the example above..
point X Y
0 0 0
1 1 0
2 2 0
3 3 0
4 1.5 0.5
5 2.5 0.5
6 -1 0
7 -2 0
8 -3 0
9 -2.5 -0.5
I would like the points above to produce a directory like..
graph = {
'0': ['1', '6'],
'1': ['2', '4'],
'2': ['3'],
'4': ['5'],
'6': ['7'],
'7': ['8','9']
}
I found a great example here for the Breadth-first Search, but it needs the directory structor already made. Any help or advice is appreciated.
Breadth-First Search.py
def bfs(graph, start, end):
queue = []
queue.append([start])
while queue:
path = queue.pop(0)
node = path[-1]
if node == end:
return path
for adjacent in graph.get(node, []):
new_path = list(path)
new_path.append(adjacent)
queue.append(new_path)
print(bfs(graph, '0', '5'))