简体   繁体   English

使用 json 文件查找最短路径

[英]Using json file to find shortest path

I have a json file with a list of locations and miles.我有一个 json 文件,其中包含位置和里程列表。 Also have created a function for dijkstra algorithm that takes in 3 parameters.还为接受 3 个参数的 dijkstra 算法创建了 function。 I have loaded my json file but what three parameters do I use for start end and graph parameters in my function to show the shortest path?我已经加载了我的 json 文件,但是我在 function 中使用哪三个参数来显示最短路径?

Json file of place and distance Json 位置和距离文件

{
    "Western Governors University 4001 South 700 East, Salt Lake City, UT 84107": {
        "International Peace Gardens 1060 Dalton Ave S": 7.2,
        "Sugar House Park 1330 2100 S": 3.8,
        "Taylorsville-Bennion Heritage City Gov Off 1488 4800 S": 11.0,
        "Salt Lake City Division of Health Services 177 W Price Ave": 2.2,
        "South Salt Lake Public Works 195 W Oakland Ave": 3.5,
        "Salt Lake City Streets and Sanitation 2010 W 500 S": 10.9,
        "Deker Lake 2300 Parkway Blvd": 8.6,
        "Salt Lake City Ottinger Hall 233 Canyon Rd": 7.6,
        "Columbus Library 2530 S 500 E": 2.8,
        "Taylorsville City Hall 2600 Taylorsville Blvd": 6.4,
        "South Salt Lake Police 2835 Main St": 3.2,
        "Council Hall 300 State St": 7.6,
        "Redwood Park 3060 Lester St": 5.2,
        "Salt Lake County Mental Health 3148 S 1100 W": 4.4,
        "Salt Lake County/United Police Dept 3365 S 900 W": 3.66112816434,
        "West Valley Prosecutor 3575 W Valley Central Sta bus Loop": 7.6,
        "Housing Auth. of Salt Lake County 3595 Main St": 2.0,
        "Utah DMV Administrative Office 380 W 2880 S": 3.6,
        "Third District Juvenile Court 410 S State St": 6.5,
        "Cottonwood Regional Softball Complex 4300 S 1300 E": 1.9,
        "Holiday City Office 4580 S 2300 E": 3.4,
        "Murray City Museum 5025 State St": 2.4,
        "Valley Regional Softball Complex 5100 South 2700 West": 6.4,
        "City Center of Rock Springs 5383 South 900 East #104": 2.4,
        "Rice Terrace Pavilion Park 600 E 900 South": 5.0,
        "Wheeler Historic Farm 6351 South 900 East": 3.6
    },
    "International Peace Gardens 1060 Dalton Ave S": {
        "Western Governors University 4001 South 700 East, Salt Lake City, UT 84107": 7.2,
        "Sugar House Park 1330 2100 S": 7.1,
        "Taylorsville-Bennion Heritage City Gov Off 1488 4800 S": 6.4,
        "Salt Lake City Division of Health Services 177 W Price Ave": 6.0,
        "South Salt Lake Public Works 195 W Oakland Ave": 4.8,
        "Salt Lake City Streets and Sanitation 2010 W 500 S": 1.6,
        "Deker Lake 2300 Parkway Blvd": 2.8,
        "Salt Lake City Ottinger Hall 233 Canyon Rd": 4.8,
        "Columbus Library 2530 S 500 E": 6.3,
        "Taylorsville City Hall 2600 Taylorsville Blvd": 7.3,
        "South Salt Lake Police 2835 Main St": 5.3,
        "Council Hall 300 State St": 4.8,
        "Redwood Park 3060 Lester St": 3.0,
        "Salt Lake County Mental Health 3148 S 1100 W": 4.6,
        "Salt Lake County/United Police Dept 3365 S 900 W": 4.5,
        "West Valley Prosecutor 3575 W Valley Central Sta bus Loop": 7.4,
        "Housing Auth. of Salt Lake County 3595 Main St": 6.0,
        "Utah DMV Administrative Office 380 W 2880 S": 5.0,
        "Third District Juvenile Court 410 S State St": 4.8,
        "Cottonwood Regional Softball Complex 4300 S 1300 E": 9.5,
        "Holiday City Office 4580 S 2300 E": 10.9,
        "Murray City Museum 5025 State St": 8.3,
        "Valley Regional Softball Complex 5100 South 2700 West": 6.9,
        "City Center of Rock Springs 5383 South 900 East #104": 10.0,
        "Rice Terrace Pavilion Park 600 E 900 South": 4.4,
        "Wheeler Historic Farm 6351 South 900 East": 13.0
    },
    "Sugar House Park 1330 2100 S": {
        "Western Governors University 4001 South 700 East, Salt Lake City, UT 84107": 3.8,
        "International Peace Gardens 1060 Dalton Ave S": 7.1,
        "Taylorsville-Bennion Heritage City Gov Off 1488 4800 S": 9.2,
        "Salt Lake City Division of Health Services 177 W Price Ave": 4.4,
        "South Salt Lake Public Works 195 W Oakland Ave": 2.8,
        "Salt Lake City Streets and Sanitation 2010 W 500 S": 8.6,
        "Deker Lake 2300 Parkway Blvd": 6.3,
        "Salt Lake City Ottinger Hall 233 Canyon Rd": 5.3,
        "Columbus Library 2530 S 500 E": 1.6,
        "Taylorsville City Hall 2600 Taylorsville Blvd": 10.4,
        "South Salt Lake Police 2835 Main St": 3.0,
        "Council Hall 300 State St": 5.3,
        "Redwood Park 3060 Lester St": 6.5,
        "Salt Lake County Mental Health 3148 S 1100 W": 5.6,
        "Salt Lake County/United Police Dept 3365 S 900 W": 5.8,
        "West Valley Prosecutor 3575 W Valley Central Sta bus Loop": 5.7,
        "Housing Auth. of Salt Lake County 3595 Main St": 4.1,
        "Utah DMV Administrative Office 380 W 2880 S": 3.6,
        "Third District Juvenile Court 410 S State St": 4.3,
        "Cottonwood Regional Softball Complex 4300 S 1300 E": 3.3,
        "Holiday City Office 4580 S 2300 E": 5.0,
        "Murray City Museum 5025 State St": 6.1,
        "Valley Regional Softball Complex 5100 South 2700 West": 9.7,
        "City Center of Rock Springs 5383 South 900 East #104": 6.1,
        "Rice Terrace Pavilion Park 600 E 900 South": 2.8,
        "Wheeler Historic Farm 6351 South 900 East": 7.4
    }

Algorithm function for shortest path最短路径算法 function

def dijkstrasShortestDistance(start, end, graph):
    visited = chainingHashMap()

    paths = list()
    paths.append(([start], 0))
    while len(paths) > 0:
        nowPath = paths.pop()
        finalItem = nowPath[0][-1]
        hasVisited = visited.get(finalItem)[0]
        if not hasVisited:
            visited.insert(finalItem, True)
        else:
            continue
        if finalItem == end:
            return nowPath
        nextTuple = graph.get(finalItem)
        if not nextTuple[0]:
            continue
        nextNodes = nextTuple[1]
        for nextNodes in nextNodes:
            newPath = list(nowPath[0])
            newPath.append(nextNodes[0])
            newDistance = nowPath[1]
            newDistance += nextNodes[1]
            paths.append((newPath, newDistance))
        paths = sorted(paths, key=lambda tuple_item: tuple_item[0])
    return (list(), 0)

Main section to fun program有趣节目的主要部分

distance = load_json(os.path.join(mainDirectory, 'distanceTable.json'))
print(dijkstrasShortestDistance(not sure how to get info from json into here)

Each location is a graph node.每个位置都是一个图节点。 Each destination indicates an edge;每个目的地表示一条边; the following distance is an edge value.下面的距离是一个边缘值。 For starters, you have:首先,您有:

"Western Governors University 4001 South 700 East, Salt Lake City, UT 84107": {
    "International Peace Gardens 1060 Dalton Ave S": 7.2,
    "Sugar House Park 1330 2100 S": 3.8,
    ...

This involves three nodes, one for each location;这涉及三个节点,每个位置一个; let's call them WGU, IPG, SHP.我们称它们为 WGU、IPG、SHP。 You also have two edges with values (weight or cost):您还有两个带有值(重量或成本)的边:

src  dst   cost
WGU  IPG   7.2
WGU  SHP   3.8

You have to iterate through your input to build the graph.您必须遍历输入来构建图形。 Since you have all of the input conveniently in a dict, I expect that you can continue from here.由于您可以方便地将所有输入放在一个 dict 中,我希望您可以从这里继续。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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