| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
class RecursionError( OverflowError, ValueError ): |
|---|
| 10 |
'''Unable to calculate result because of recursive structure''' |
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
def sort(nodes, routes, noRecursion=1): |
|---|
| 14 |
'''Passed a list of node IDs and a list of source,dest ID routes |
|---|
| 15 |
attempt to create a list of stages where each sub list |
|---|
| 16 |
is one stage in a process. |
|---|
| 17 |
''' |
|---|
| 18 |
children, parents = _buildChildrenLists(routes) |
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
stage = [] |
|---|
| 22 |
stages = [stage] |
|---|
| 23 |
taken = [] |
|---|
| 24 |
for node in nodes: |
|---|
| 25 |
if (not parents.get(node)): |
|---|
| 26 |
stage.append (node) |
|---|
| 27 |
if nodes and not stage: |
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
stage.append( nodes[0]) |
|---|
| 31 |
taken.extend( stage ) |
|---|
| 32 |
nodes = filter ( lambda x, l=stage: x not in l, nodes ) |
|---|
| 33 |
while nodes: |
|---|
| 34 |
previousStageChildren = [] |
|---|
| 35 |
nodelen = len(nodes) |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
for node in stage: |
|---|
| 39 |
for child in children.get (node, []): |
|---|
| 40 |
if child not in previousStageChildren and child not in taken: |
|---|
| 41 |
previousStageChildren.append(child) |
|---|
| 42 |
elif child in taken and noRecursion: |
|---|
| 43 |
raise RecursionError( (child, node) ) |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
stage = previousStageChildren |
|---|
| 47 |
removes = [] |
|---|
| 48 |
for current in stage: |
|---|
| 49 |
currentParents = parents.get( current, [] ) |
|---|
| 50 |
for parent in currentParents: |
|---|
| 51 |
if parent in stage and parent != current: |
|---|
| 52 |
|
|---|
| 53 |
if not current in parents.get(parent, []): |
|---|
| 54 |
|
|---|
| 55 |
removes.append( current ) |
|---|
| 56 |
for remove in removes: |
|---|
| 57 |
while remove in stage: |
|---|
| 58 |
stage.remove( remove ) |
|---|
| 59 |
stages.append( stage) |
|---|
| 60 |
taken.extend( stage ) |
|---|
| 61 |
nodes = filter ( lambda x, l=stage: x not in l, nodes ) |
|---|
| 62 |
if nodelen == len(nodes): |
|---|
| 63 |
if noRecursion: |
|---|
| 64 |
raise RecursionError( nodes ) |
|---|
| 65 |
else: |
|---|
| 66 |
stages.append( nodes[:] ) |
|---|
| 67 |
nodes = [] |
|---|
| 68 |
return stages |
|---|
| 69 |
|
|---|
| 70 |
def _buildChildrenLists (routes): |
|---|
| 71 |
childrenTable = {} |
|---|
| 72 |
parentTable = {} |
|---|
| 73 |
for sourceID,destinationID in routes: |
|---|
| 74 |
currentChildren = childrenTable.get( sourceID, []) |
|---|
| 75 |
currentParents = parentTable.get( destinationID, []) |
|---|
| 76 |
if not destinationID in currentChildren: |
|---|
| 77 |
currentChildren.append ( destinationID) |
|---|
| 78 |
if not sourceID in currentParents: |
|---|
| 79 |
currentParents.append ( sourceID) |
|---|
| 80 |
childrenTable[sourceID] = currentChildren |
|---|
| 81 |
parentTable[destinationID] = currentParents |
|---|
| 82 |
return childrenTable, parentTable |
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
def toposort (nodes, routes, noRecursion=1): |
|---|
| 86 |
'''Topological sort from Tim Peters, fairly efficient |
|---|
| 87 |
in comparison (it seems).''' |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
dependencies = {} |
|---|
| 91 |
inversedependencies = {} |
|---|
| 92 |
if not nodes: |
|---|
| 93 |
return [] |
|---|
| 94 |
if not routes: |
|---|
| 95 |
return [nodes] |
|---|
| 96 |
for node in nodes: |
|---|
| 97 |
dependencies[ node ] = (0, node) |
|---|
| 98 |
inversedependencies[ node ] = [] |
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
for depended, depends in routes: |
|---|
| 102 |
|
|---|
| 103 |
try: |
|---|
| 104 |
newdependencylevel, object = dependencies.get ( depends, (0, depends)) |
|---|
| 105 |
except TypeError: |
|---|
| 106 |
print depends |
|---|
| 107 |
raise |
|---|
| 108 |
dependencies[ depends ] = (newdependencylevel + 1, depends) |
|---|
| 109 |
|
|---|
| 110 |
newdependencylevel,object = dependencies.get ( depended, (0, depended) ) |
|---|
| 111 |
dependencies[ depended ] = (newdependencylevel, depended) |
|---|
| 112 |
|
|---|
| 113 |
dependencieslist = inversedependencies.get ( depended, []) |
|---|
| 114 |
dependencieslist.append (depends) |
|---|
| 115 |
inversedependencies[depended] = dependencieslist |
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
sortinglist = dependencies.values() |
|---|
| 120 |
sortinglist.sort () |
|---|
| 121 |
output = [] |
|---|
| 122 |
while sortinglist: |
|---|
| 123 |
deletelist = [] |
|---|
| 124 |
generation = [] |
|---|
| 125 |
output.append( generation) |
|---|
| 126 |
while sortinglist and sortinglist[0][0] == 0: |
|---|
| 127 |
number, object = sortinglist[0] |
|---|
| 128 |
generation.append ( object ) |
|---|
| 129 |
deletelist.append( object ) |
|---|
| 130 |
for inverse in inversedependencies.get(object, () ): |
|---|
| 131 |
try: |
|---|
| 132 |
oldcount, inverse = dependencies [ inverse] |
|---|
| 133 |
if oldcount > 0: |
|---|
| 134 |
|
|---|
| 135 |
dependencies [ inverse] = (oldcount-1, inverse) |
|---|
| 136 |
else: |
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
deletelist.append( inverse ) |
|---|
| 140 |
|
|---|
| 141 |
inversedependencies[object] = [] |
|---|
| 142 |
except KeyError: |
|---|
| 143 |
|
|---|
| 144 |
pass |
|---|
| 145 |
del sortinglist [0] |
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
if not deletelist: |
|---|
| 149 |
if noRecursion: |
|---|
| 150 |
raise RecursionError( sortinglist ) |
|---|
| 151 |
else: |
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
dependencies[sortinglist[0][1]] = (0,sortinglist[0][1]) |
|---|
| 156 |
|
|---|
| 157 |
for item in deletelist: |
|---|
| 158 |
try: |
|---|
| 159 |
del dependencies [ item ] |
|---|
| 160 |
except KeyError: |
|---|
| 161 |
pass |
|---|
| 162 |
|
|---|
| 163 |
sortinglist = dependencies.values() |
|---|
| 164 |
if not generation: |
|---|
| 165 |
output.remove( generation ) |
|---|
| 166 |
sortinglist.sort () |
|---|
| 167 |
return output |
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
if __name__ == "__main__": |
|---|
| 174 |
|
|---|
| 175 |
nodes = ['a', 'b', 'c', 'd', 'e', 'f'] |
|---|
| 176 |
route = [('a', 'b'), ('b', 'c'), ('b', 'd'), ('e','f')] |
|---|
| 177 |
|
|---|
| 178 |
for x in toposort( nodes, route): |
|---|
| 179 |
for a in x: |
|---|
| 180 |
print a |
|---|
| 181 |
|
|---|
| 182 |
raise SystemExit |
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
import pprint, traceback |
|---|
| 187 |
nodes= [ 0,1,2,3,4,5 ] |
|---|
| 188 |
testingValues = [ |
|---|
| 189 |
[ (0,1),(1,2),(2,3),(3,4),(4,5)], |
|---|
| 190 |
[ (0,1),(0,2),(1,2),(3,4),(4,5)], |
|---|
| 191 |
[ |
|---|
| 192 |
(0,1), |
|---|
| 193 |
(0,2), |
|---|
| 194 |
(0,2), |
|---|
| 195 |
(2,4), |
|---|
| 196 |
(2,5), |
|---|
| 197 |
(3,2), |
|---|
| 198 |
(0,3)], |
|---|
| 199 |
[ |
|---|
| 200 |
(0,1), |
|---|
| 201 |
(1,2), |
|---|
| 202 |
(2,0), |
|---|
| 203 |
(2,4), |
|---|
| 204 |
(2,5), |
|---|
| 205 |
(3,2), |
|---|
| 206 |
(0,3)], |
|---|
| 207 |
[ |
|---|
| 208 |
(0,1), |
|---|
| 209 |
(1,1), |
|---|
| 210 |
(1,1), |
|---|
| 211 |
(1,4), |
|---|
| 212 |
(1,5), |
|---|
| 213 |
(1,2), |
|---|
| 214 |
(3,1), |
|---|
| 215 |
(2,1), |
|---|
| 216 |
(2,0)], |
|---|
| 217 |
[ |
|---|
| 218 |
(0,1), |
|---|
| 219 |
(1,0), |
|---|
| 220 |
(0,2), |
|---|
| 221 |
(0,3), |
|---|
| 222 |
], |
|---|
| 223 |
[ |
|---|
| 224 |
(0,1), |
|---|
| 225 |
(1,0), |
|---|
| 226 |
(0,2), |
|---|
| 227 |
(3,1), |
|---|
| 228 |
], |
|---|
| 229 |
] |
|---|
| 230 |
print 'sort, no recursion allowed' |
|---|
| 231 |
for index in range(len(testingValues)): |
|---|
| 232 |
|
|---|
| 233 |
try: |
|---|
| 234 |
print ' ', sort( nodes, testingValues[index] ) |
|---|
| 235 |
except: |
|---|
| 236 |
print 'exception raised' |
|---|
| 237 |
print 'toposort, no recursion allowed' |
|---|
| 238 |
for index in range(len(testingValues)): |
|---|
| 239 |
|
|---|
| 240 |
try: |
|---|
| 241 |
print ' ', toposort( nodes, testingValues[index] ) |
|---|
| 242 |
except: |
|---|
| 243 |
print 'exception raised' |
|---|
| 244 |
print 'sort, recursion allowed' |
|---|
| 245 |
for index in range(len(testingValues)): |
|---|
| 246 |
|
|---|
| 247 |
try: |
|---|
| 248 |
print ' ', sort( nodes, testingValues[index],0 ) |
|---|
| 249 |
except: |
|---|
| 250 |
print 'exception raised' |
|---|
| 251 |
print 'toposort, recursion allowed' |
|---|
| 252 |
for index in range(len(testingValues)): |
|---|
| 253 |
|
|---|
| 254 |
try: |
|---|
| 255 |
print ' ', toposort( nodes, testingValues[index],0 ) |
|---|
| 256 |
except: |
|---|
| 257 |
print 'exception raised' |
|---|
| 258 |
|
|---|
| 259 |
|
|---|
| 260 |
|
|---|