等价多路径Dijkstra在python中的算法

时间:2017-03-24 22:29:49

标签: python algorithm dijkstra

使用这里提出的python配方:

http://code.activestate.com/recipes/119466-dijkstras-algorithm-for-shortest-paths/

http://code.activestate.com/recipes/117228-priority-dictionary/

并使用以下图表作为输入:

(function () {
'use strict';

// This is the array the isAircraftDueForTireChange function expects. 
// Assume the following object was loaded via an API as JSON.
// This object may contain some unrealistic or duplicate data; It is only intended for testing.
window.CAMP = {
    aircraftData: [
        {
            // Should require maintenance
            id: 1,
            lastTireChange: new Date('9/1/2016'),
            manufacturer: 'FooPlane',
            landings: [
                new Date('9/1/2015'),
                new Date('9/5/2015'),
                new Date('9/12/2016'),
                new Date('9/12/2015'),
                new Date('1/1/2016'),
                new Date('10/1/2015'),
                new Date('10/23/2015'),
                new Date('9/12/2016'),
                new Date('10/28/2015'),
                new Date('11/15/2015'),
                new Date('12/22/2015'),
                new Date('12/22/2015'),
                new Date('12/27/2015'),
                new Date('1/7/2016'),
                new Date('2/1/2016'),
                new Date('2/1/2016'),
                new Date('2/1/2016'),
                new Date('2/1/2016'),
                new Date('9/23/2016'),
                new Date('2/1/2016'),
                new Date('9/23/2016'),
                new Date('9/12/2016'),
                new Date('9/1/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/23/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/23/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/23/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/12/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/3/2016'),
                new Date('9/3/2016'),
                new Date('9/3/2016'),
                new Date('9/3/2016'),
                new Date('9/3/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/23/2016'),
                new Date('9/23/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/3/2016'),
                new Date('9/3/2016'),
                new Date('9/3/2016'),
                new Date('9/23/2016'),
                new Date('9/3/2016'),
                new Date('9/3/2016'),
                new Date('9/23/2016'),
                new Date('9/23/2016'),
                new Date('9/23/2016'),
                new Date('9/23/2016'),
                new Date('9/23/2016'),
                new Date('9/3/2016'),
                new Date('9/12/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/23/2016'),
                new Date('9/23/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/12/2016'),
                new Date('9/12/2016'),
                new Date('9/12/2016'),
                new Date('9/2/2016'),
                new Date('9/23/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/23/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/12/2016'),
                new Date('9/23/2016'),
                new Date('9/12/2016'),
                new Date('9/12/2016'),
                new Date('9/23/2016'),
                new Date('9/12/2016'),
                new Date('9/12/2016'),
                new Date('9/23/2016'),
                new Date('9/12/2016'),
                new Date('9/12/2016'),
                new Date('9/12/2016'),
                new Date('9/23/2016'),
                new Date('9/23/2016'),
                new Date('9/12/2016'),
                new Date('9/12/2016'),
                new Date('9/12/2016'),
                new Date('9/12/2016'),
                new Date('9/23/2016'),
                new Date('9/12/2016'),
                new Date('9/12/2016'),
                new Date('9/23/2016'),
                new Date('9/12/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016')
            ]
        },
        {
            // Should not require maintenance
            id: 2,
            lastTireChange: new Date('10/29/2016'),
            manufacturer: 'FooPlane',
            landings: [
                new Date('9/1/2015'),
                new Date('9/5/2015'),
                new Date('9/12/2016'),
                new Date('9/12/2015'),
                new Date('1/1/2016'),
                new Date('10/1/2015'),
                new Date('10/23/2015'),
                new Date('9/12/2016'),
                new Date('10/28/2015'),
                new Date('11/15/2015'),
                new Date('12/22/2015'),
                new Date('12/22/2015'),
                new Date('12/27/2015'),
                new Date('1/7/2016'),
                new Date('2/1/2016'),
                new Date('2/1/2016'),
                new Date('2/1/2016'),
                new Date('2/1/2016'),
                new Date('9/23/2016'),
                new Date('2/1/2016'),
                new Date('9/23/2016'),
                new Date('9/12/2016'),
                new Date('9/1/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/23/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/23/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/23/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/12/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/3/2016'),
                new Date('9/3/2016'),
                new Date('9/3/2016'),
                new Date('9/3/2016'),
                new Date('9/3/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/23/2016'),
                new Date('9/23/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/3/2016'),
                new Date('9/3/2016'),
                new Date('9/3/2016'),
                new Date('9/23/2016'),
                new Date('9/3/2016'),
                new Date('9/3/2016'),
                new Date('9/23/2016'),
                new Date('9/23/2016'),
                new Date('9/23/2016'),
                new Date('9/23/2016'),
                new Date('9/23/2016'),
                new Date('9/3/2016'),
                new Date('9/12/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/23/2016'),
                new Date('9/23/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/12/2016'),
                new Date('9/12/2016'),
                new Date('9/12/2016'),
                new Date('9/2/2016'),
                new Date('9/23/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/23/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/12/2016'),
                new Date('9/23/2016'),
                new Date('9/12/2016'),
                new Date('9/12/2016'),
                new Date('9/23/2016'),
                new Date('9/12/2016'),
                new Date('9/12/2016'),
                new Date('9/23/2016'),
                new Date('9/12/2016'),
                new Date('9/12/2016'),
                new Date('9/12/2016'),
                new Date('9/23/2016'),
                new Date('9/23/2016'),
                new Date('9/12/2016'),
                new Date('9/12/2016'),
                new Date('9/12/2016'),
                new Date('9/12/2016'),
                new Date('9/23/2016'),
                new Date('9/12/2016'),
                new Date('9/12/2016'),
                new Date('9/23/2016'),
                new Date('9/12/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016')
            ]
        },
        {
            // Should require maintenance
            id: 3,
            lastTireChange: new Date('9/1/2016'),
            manufacturer: 'BarPlane',
            landings: [
                new Date('9/1/2015'),
                new Date('9/5/2015'),
                new Date('9/12/2016'),
                new Date('9/12/2015'),
                new Date('1/1/2016'),
                new Date('10/1/2015'),
                new Date('10/23/2015'),
                new Date('9/12/2016'),
                new Date('10/28/2015'),
                new Date('11/15/2015'),
                new Date('12/22/2015'),
                new Date('12/22/2015'),
                new Date('12/27/2015'),
                new Date('1/7/2016'),
                new Date('2/1/2016'),
                new Date('2/1/2016'),
                new Date('2/1/2016'),
                new Date('2/1/2016'),
                new Date('9/23/2016'),
                new Date('2/1/2016'),
                new Date('9/23/2016'),
                new Date('9/12/2016'),
                new Date('9/1/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/23/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/23/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/23/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/12/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/3/2016'),
                new Date('9/3/2016'),
                new Date('9/3/2016'),
                new Date('9/3/2016'),
                new Date('9/3/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/23/2016'),
                new Date('9/23/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/3/2016'),
                new Date('9/3/2016'),
                new Date('9/3/2016'),
                new Date('9/23/2016'),
                new Date('9/3/2016'),
                new Date('9/3/2016'),
                new Date('9/23/2016'),
                new Date('9/23/2016'),
                new Date('9/23/2016'),
                new Date('9/23/2016'),
                new Date('9/23/2016'),
                new Date('9/3/2016'),
                new Date('9/12/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/23/2016'),
                new Date('9/23/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/12/2016'),
                new Date('9/12/2016'),
                new Date('9/12/2016'),
                new Date('9/2/2016'),
                new Date('9/23/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/23/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),







                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016')
            ]
        },
        {
            // Should not require maintenance
            id: 4,
            lastTireChange: new Date('10/29/2016'),
            manufacturer: 'BarPlane',
            landings: [
                new Date('9/1/2015'),
                new Date('9/5/2015'),
                new Date('9/12/2016'),
                new Date('9/12/2015'),
                new Date('1/1/2016'),
                new Date('10/1/2015'),
                new Date('10/23/2015'),
                new Date('9/12/2016'),
                new Date('10/28/2015'),
                new Date('11/15/2015'),
                new Date('12/22/2015'),
                new Date('12/22/2015'),
                new Date('12/27/2015'),
                new Date('1/7/2016'),
                new Date('2/1/2016'),
                new Date('2/1/2016'),
                new Date('2/1/2016'),
                new Date('2/1/2016'),
                new Date('9/23/2016'),
                new Date('2/1/2016'),
                new Date('9/23/2016'),
                new Date('9/12/2016'),
                new Date('9/1/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/23/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/23/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/23/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/12/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/3/2016'),
                new Date('9/3/2016'),
                new Date('9/3/2016'),
                new Date('9/3/2016'),
                new Date('9/3/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),


                new Date('9/23/2016'),
                new Date('9/23/2016'),
                new Date('9/23/2016'),
                new Date('9/23/2016'),
                new Date('9/23/2016'),
                new Date('9/3/2016'),
                new Date('9/12/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/23/2016'),
                new Date('9/23/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/2/2016'),
                new Date('9/12/2016'),
                new Date('9/12/2016'),
                new Date('9/12/2016'),
                new Date('9/2/2016'),
                new Date('9/23/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/23/2016'),
                new Date('9/5/2016'),
                new Date('9/5/2016'),
                new Date('9/12/2016'),
                new Date('9/23/2016'),
                new Date('9/12/2016'),
                new Date('9/12/2016'),
                new Date('9/23/2016'),
                new Date('9/12/2016'),
                new Date('9/12/2016'),
                new Date('9/23/2016'),
                new Date('9/12/2016'),
                new Date('9/12/2016'),
                new Date('9/12/2016'),
                new Date('9/23/2016'),
                new Date('9/23/2016'),
                new Date('9/12/2016'),
                new Date('9/12/2016'),
                new Date('9/12/2016'),
                new Date('9/12/2016'),
                new Date('9/23/2016'),
                new Date('9/12/2016'),
                new Date('9/12/2016'),
                new Date('9/23/2016'),
                new Date('9/12/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/1/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016')
            ]
        },
        {
            // Should require maintenance
            id: 5,
            lastTireChange: new Date('9/1/2016'),
            manufacturer: 'BazPlane',
            landings: [
                new Date('9/1/2015'),
                new Date('9/5/2015'),
                new Date('9/12/2016'),
                new Date('9/12/2015'),
                new Date('1/1/2016'),

                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016'),
                new Date('10/2/2016')
            ]
        },
        {
            // Should not require maintenance
            id: 6,
            lastTireChange: new Date('10/29/2016'),
            manufacturer: 'BazPlane',
            landings: [
                new Date('9/1/2015'),
                new Date('9/5/2015'),
                new Date('9/12/2016'),
                new Date('9/12/2015'),
                new Date('1/1/2016'),
                new Date('10/1/2015'),
                new Date('10/23/2015'),
                new Date('9/12/2016'),
                new Date('10/28/2015'),
                new Date('11/15/2015'),
                new Date('12/22/2015'),




            ]
        },
    ]
};
 })();

我正试图获得R1和R4之间的所有最短路径。但是,我只获得一条最短路径(R1-R2-R4),而不是(R1-R3-R4)。我需要模拟ECMP(比如OSPF)。我需要的是函数shortestPath返回所有最短路径(即[[R1-R2-R4],[R1-R3-R4]])在等价多路径的情况下(如图2上面)和只有最短路径的情况单路径的例如:

 graph_2  = {
    'R1':{'R2':5,'R3':5},
    'R2':{'R1':5,'R4':5},
    'R3':{'R1':5,'R4':5},
    'R4':{'R2':5,'R3':5},
}

我修改了Dijkstra函数中的代码:

  graph_3  = {
    'R1':{'R2':5,'R3':5},
    'R2':{'R1':5,'R4':5},
    'R3':{'R1':5,'R4':10},
    'R4':{'R2':5,'R3':5},
}

我收到以下输出和错误:

from priodict import priorityDictionary

graph_2  = {

'R1':{'R2':5,'R3':5},
'R2':{'R1':5,'R4':5},
'R3':{'R1':5,'R4':5},
'R4':{'R2':5,'R3':5},
}



def Dijkstra(G,start,end=None):

    D = {}  # dictionary of final distances
    P = {}  # dictionary of predecessors
    Q = priorityDictionary()   # est.dist. of non-final vert.
    Q[start] = 0
    for v in Q:
        D[v] = Q[v]
        if v == end: break

        for w in G[v]:
            vwLength = D[v] + G[v][w]

            if w in D:
                if vwLength < D[w]:
                    raise ValueError, \
            elif w not in Q or vwLength < Q[w]:
                Q[w] = vwLength
                P[w] = [v]
            elif  w not in Q or vwLength == Q[w]: <---adding this part
                Q[w] = vwLength
                P[w] += [v]

    return (D,P)

def shortestPath(G,start,end):
    D,P = Dijkstra(G,start,end)
    print D,P
    Path = []
    while 1:
        Path.append(end)
        print end
        if end == start: break
        end = P[end]
    Path.reverse()
    return Path

print shortestPath(graph_2,'R1','R4')

我想使用graph_2获得的是:

[['R1','R2','R4'],['R1','R3','R4']]

并使用graph_3:

[['R1','R2','R4']]

如果我按原样执行代码,即没有任何代码修改,我得到以下结果,无论我使用graph_2还是graph_3:

[['R1','R2','R4']]

即使是最短的路径,即使有多条路径。

我知道列表不能成为字典中的关键字,但说实话我坚持这一点,所以任何帮助都非常受欢迎

2 个答案:

答案 0 :(得分:0)

<强>分析

感谢您提供可运行的代码。您还没有解释您的设计,但功能问题足够清楚: P 负责返回当前路径上任何给定节点的前任。由于路径是严格线性的,因此只能有一个前驱。 shortestPath 中的 while 循环取决于此。

但是,您返回的 P 列出了当前路径中 R4 两个前辈,您立即尝试索引 P 同时使用它们。你做不到;你必须单独处理每条路径。

<强>解

简单地修改代码就等于为你做功课;即不酷。既然你已经这么远了,我建议你改变 shortestPath 中的循环来处理多个路径。首先要考虑返回值的每个,然后一次一个:

while True:
    for node in end:
        # continue with single-path code

编码注释

是的,我将循环条件从 1 更改为 True 。 您使用的食谱似乎是由翻译自 C 或其他早期第3代语言的人编写的,而不会学习当前的编码改进。单字母变量名,大写字母等是当今程序员不合标准的习惯。 (我自己必须重新学习一些。)

要了解您可以采用的方法,请参阅PEP-8

答案 1 :(得分:0)

最后,我想我得到了我想要的东西:)

这是最终的代码

json

以及每个图表的输出:

from priodict import priorityDictionary
from collections import defaultdict
from collections import OrderedDict
import sys
graph_2  = {

'R1':{'R2':5,'R3':5},
'R2':{'R1':5,'R4':5},
'R3':{'R1':5,'R4':5},
'R4':{'R2':5,'R3':5,'R5':5},
'R5':{'R4':5}

}

graph_3 = {

'192.168.255.1':{'192.168.255.2':10,'192.168.255.3':10},
'192.168.255.2':{'192.168.255.1':10,'192.168.255.3':50},
'192.168.255.3':{'192.168.255.1':20,'192.168.255.2':50},
'192.168.255.4':{'192.168.255.3':20},
'192.168.255.3':{'192.168.255.4':20}
}

graph_4 = {
'R1':{'R2':5},
'R2':{'R1':5},
'R2':{'R3':5},
'R3':{'R2':5},
'R3':{'R4':5},
'R4':{'R3':5}
}

graph_5 = {

'A':{'B':5,'C':10,'E':2},
'B':{'A':5,'C':5},
'C':{'B':5,'A':10,'E':8,'D':4},
'D':{'C':4,'E':5},
'E':{'A':2,'D':5,'C':8}

}

def Dijkstra(g,start,end=None):

    d = {}  # dictionary of final distances
    p = {}  # dictionary of predecessors
    q = priorityDictionary()   # est.dist. of non-final vert.
    q[start] = 0
    for v in q:
        d[v] = q[v]
        if v == end: break      
        for w in g[v]:
            vwLength = d[v] + g[v][w]
            if w in d:
                if vwLength < d[w]:
                    raise ValueError
            elif w not in q or vwLength < q[w]:
                q[w] = vwLength
                p[w] = [v]
            elif  w not in q or vwLength == q[w]:
                q[w] = vwLength
                p[w] += [v] 
    return (d,p)        

def shortestPath(g,start,end):

    d,p = Dijkstra(g,start,end)
    path = [[end]]
    while True:
        if len(p[end]) > 1:
            path.append(p[end])
            for node in p[end]:
                if node != start:
                    if ''.join(p[end]) == start: break
                    end = node
        path.append(p[end])
        if ''.join(p[end]) == start: break
        for node in p[end]:
            if node == start: break
            end = node  
    return path[::-1]


print shortestPath(graph_2,'R1','R5')
print shortestPath(graph_3,'192.168.255.1','192.168.255.2')
print shortestPath(graph_4,'R1','R4')
print shortestPath(graph_5,'A','C')