简体   繁体   中英

Input Automation to Shortest Path Algorithm (BFS Algorithm)

I am using BFS Algorithm to find the shortest path between the points which covers all the points and generates the shortest path. I am giving input(nearest neighbors) manually, and finding difficulty in automating the input to BFS Algorithm. Also please suggest if you know any algorithm which does a better job to generate the shortest path covering all the points.

Example: Points - [R59C36,R59C39,R59C52,R60C1,R60C20,R60C34,R62C2,R62C7,R63C8,R65C9,R66C11,R66C6,R67C11]

Input – Giving nearest neighbors to each point

graph = {
    'R59C36':['R59C39','R59C52','R60C34','R60C20','R60C1'],
    'R59C39':['R59C52','R60C34','R60C20','R60C1'],
    'R60C1':['R60C20','R62C2','R62C7'],
    'R60C20':['R60C34','R62C2','R62C7'],
    'R60C34':['R62C2','R62C7'],
    'R59C52':['R60C34'],
    'R62C2':['R62C7','R63C8'],
    'R62C7':['R63C8'],
    'R63C8':['R65C9'],
    'R65C9':['R66C6','R66C11'],
    'R66C6':['R66C11','R67C11'],
    'R66C11':['R67C11'],
    'R67C11':[]
}

Output - R59C36, R59C39, R59C52, R60C34, R60C20, R60C1, R62C2, R62C7, R63C8, R65C9, R66C6, R66C11, R67C11

graph = {
    'R59C36':['R59C39','R59C52','R60C34','R60C20','R60C1'],
    'R59C39':['R59C52','R60C34','R60C20','R60C1'],
    'R60C1':['R60C20','R62C2','R62C7'],
    'R60C20':['R60C34','R62C2','R62C7'],
    'R60C34':['R62C2','R62C7'],
    'R59C52':['R60C34'],
    'R62C2':['R62C7','R63C8'],
    'R62C7':['R63C8'],
    'R63C8':['R65C9'],
    'R65C9':['R66C6','R66C11'],
    'R66C6':['R66C11','R67C11'],
    'R66C11':['R67C11'],
    'R67C11':[]
}
points = ['R59C36','R59C39','R59C52','R60C1','R60C20','R60C34','R62C2','R62C7','R63C8','R65C9','R66C11','R66C6','R67C11']


output = []
for point in points:
    if point not in output:
        output.append(point)
    
    _points = graph[point]
    for _point in _points:
        if _point not in output:
            output.append(_point)

print(output)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM