This repository has been archived by the owner on Nov 19, 2017. It is now read-only.
forked from GGurtner/YenKSP
-
Notifications
You must be signed in to change notification settings - Fork 1
/
algorithms.py
174 lines (147 loc) · 5.46 KB
/
algorithms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# algorithms.py
#
# Copyright 2012 Kevin R <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
from operator import itemgetter
from prioritydictionary import priorityDictionary
from graph import DiGraph
## @package YenKSP
# Computes K-Shortest Paths using Yen's Algorithm.
#
# Yen's algorithm computes single-source K-shortest loopless paths for a graph
# with non-negative edge cost. The algorithm was published by Jin Y. Yen in 1971
# and implores any shortest path algorithm to find the best path, then proceeds
# to find K-1 deviations of the best path.
## Computes K paths from a source to a sink in the supplied graph.
#
# @param graph A digraph of class Graph.
# @param start The source node of the graph.
# @param sink The sink node of the graph.
# @param K The amount of paths being computed.
#
# @retval [] Array of paths, where [0] is the shortest, [1] is the next
# shortest, and so on.
#
def ksp_yen(graph, node_start, node_end, max_k=2):
distances, previous = dijkstra(graph, node_start)
A = [{'cost': distances[node_end],
'path': path(previous, node_start, node_end)}]
B = []
if not A[0]['path']: return A
for k in range(1, max_k):
if k>1: # update distance dictionnary
distances=make_distances(graph, A[-1]['path'])
for i in range(0, len(A[-1]['path']) - 1):
node_spur = A[-1]['path'][i]
path_root = A[-1]['path'][:i+1]
edges_removed = []
for path_k in A:
curr_path = path_k['path']
if len(curr_path) > i and path_root == curr_path[:i+1]:
cost = graph.remove_edge(curr_path[i], curr_path[i+1])
if cost == -1:
continue
edges_removed.append([curr_path[i], curr_path[i+1], cost])
path_spur = dijkstra(graph, node_spur, node_end)
if path_spur['path']:
path_total = path_root[:-1] + path_spur['path']
dist_total = distances[node_spur] + path_spur['cost']
potential_k = {'cost': dist_total, 'path': path_total}
if not (potential_k in B):
B.append(potential_k)
for edge in edges_removed:
graph.add_edge(edge[0], edge[1], edge[2])
if len(B):
B = sorted(B, key=itemgetter('cost'))
A.append(B[0])
B.pop(0)
else:
break
return A
## Computes the shortest path from a source to a sink in the supplied graph.
#
# @param graph A digraph of class Graph.
# @param node_start The source node of the graph.
# @param node_end The sink node of the graph.
#
# @retval {} Dictionary of path and cost or if the node_end is not specified,
# the distances and previous lists are returned.
#
def dijkstra(graph, node_start, node_end=None):
distances = {}
previous = {}
Q = priorityDictionary()
for v in graph:
distances[v] = graph.INFINITY
previous[v] = graph.UNDEFINDED
Q[v] = graph.INFINITY
distances[node_start] = 0
Q[node_start] = 0
for v in Q:
if v == node_end: break
for u in graph[v]:
cost_vu = distances[v] + graph[v][u]
if cost_vu < distances[u]:
distances[u] = cost_vu
Q[u] = cost_vu
previous[u] = v
if node_end!=None:
return {'cost': distances[node_end],
'path': path(previous, node_start, node_end)}
else:
return (distances, previous)
## Finds a paths from a source to a sink using a supplied previous node list.
#
# @param previous A list of node predecessors.
# @param node_start The source node of the graph.
# @param node_end The sink node of the graph.
#
# @retval [] Array of nodes if a path is found, an empty list if no path is
# found from the source to sink.
#
def path(previous, node_start, node_end):
route = []
node_curr = node_end
while True:
route.append(node_curr)
if previous[node_curr] == node_start:
route.append(node_start)
break
elif previous[node_curr] == DiGraph.UNDEFINDED:
return []
node_curr = previous[node_curr]
route.reverse()
return route
## Returns the cumulative distance along the given path.
#
# @param graph A digraph of class Graph.
# @param path a list of nodes of the graph.
#
# @retval [] Array of cumulative cost.
#
def make_distances(graph, path):
distances = {path[0]:0.}
cost = 0.
for i in range(len(path)-1):
cost += graph[path[i]][path[i+1]]
distances[path[i+1]] = cost
return distances