当前位置:   article > 正文

网络流算法Dinic的Python实现_dinic python

dinic python

在上一篇我们提到了网络流算法Push-relabel,那是90年代提出的算法,算是比较新的,而现在要说的Dinic算法则是由以色列人Dinitz在冷战时期,即60-70年代提出的算法变种而来的,其算法复杂度为O(mn^2)。

Dinic算法主要思想也是基于FF算法的,改进的地方也是减少寻找增广路径的迭代次数。此处Dinitz大师引用了一个非常聪明的数据结构,Layer Network,分层网络,该结构是由BFS tree启发得到的,它跟BFS tree的区别在于,BFS tree只保存到每一层的一条边,这样就导致了利用BFS tree一次只能发现一条增广路径,而分层网络保存了到每一层的所有边,但层内的边不保存。

介绍完数据结构,开始讲算法的步骤了,1)从网络的剩余图中利用BFS宽度优先遍历技术生成分层网络。2)在分层网络中不断调用DFS生成增广路径,直到s不可到达t,这一步体现了Dinic算法贪心的特性。3)max_flow+=这次生成的所有增广路径的flow,重新生成剩余图,转1)。

源代码如下:

采用递归实现BFS和DFS,效率不高。

  1. __author__ = 'xanxus'
  2. nodeNum, edgeNum = 0, 0
  3. arcs = []
  4. class Arc(object):
  5. def __init__(self):
  6. self.src = -1
  7. self.dst = -1
  8. self.cap = -1
  9. class Layer(object):
  10. def __init__(self):
  11. self.nodeSet = set()
  12. self.arcList = []
  13. s, t = -1, -1
  14. with open('demo.dimacs') as f:
  15. for line in f.readlines():
  16. line = line.strip()
  17. if line.startswith('p'):
  18. tokens = line.split(' ')
  19. nodeNum = int(tokens[2])
  20. edgeNum = tokens[3]
  21. if line.startswith('n'):
  22. tokens = line.split(' ')
  23. if tokens[2] == 's':
  24. s = int(tokens[1])
  25. if tokens[2] == 't':
  26. t = int(tokens[1])
  27. if line.startswith('a'):
  28. tokens = line.split(' ')
  29. arc = Arc()
  30. arc.src = int(tokens[1])
  31. arc.dst = int(tokens[2])
  32. arc.cap = int(tokens[3])
  33. arcs.append(arc)
  34. nodes = [-1] * nodeNum
  35. for i in range(s, t + 1):
  36. nodes[i - s] = i
  37. adjacent_matrix = [[0 for i in range(nodeNum)] for j in range(nodeNum)]
  38. for arc in arcs:
  39. adjacent_matrix[arc.src - s][arc.dst - s] = arc.cap
  40. def getLayerNetwork(current, ln, augment_set):
  41. if t - s in ln[current].nodeSet:
  42. return
  43. for i in ln[current].nodeSet:
  44. augment_set.add(i)
  45. has_augment = False
  46. for j in range(len(adjacent_matrix)):
  47. if adjacent_matrix[i][j] != 0:
  48. if len(ln) == current + 1:
  49. ln.append(Layer())
  50. if j not in augment_set and j not in ln[current].nodeSet:
  51. has_augment = True
  52. ln[current + 1].nodeSet.add(j)
  53. arc = Arc()
  54. arc.src, arc.dst, arc.cap = i, j, adjacent_matrix[i][j]
  55. ln[current].arcList.append(arc)
  56. if not has_augment and (i != t - s or i != 0):
  57. augment_set.remove(i)
  58. filter(lambda x: x == i, ln[current].nodeSet)
  59. newArcList = []
  60. for arc in ln[current - 1].arcList:
  61. if arc.dst != i:
  62. newArcList.append(arc)
  63. ln[current - 1].arcList = newArcList
  64. if len(ln) == current + 1:
  65. return
  66. getLayerNetwork(current + 1, ln, augment_set)
  67. def get_path(layerNetwork, src, current, path):
  68. for arc in layerNetwork[current].arcList:
  69. if arc.src == src and arc.cap != 0:
  70. path.append(arc)
  71. get_path(layerNetwork, arc.dst, current + 1, path)
  72. return
  73. def find_blocking_flow(layerNetwork):
  74. sum_flow = 0
  75. while (True):
  76. path = []
  77. get_path(layerNetwork, 0, 0, path)
  78. if path[-1].dst != t - s:
  79. break
  80. else:
  81. bottleneck = min([arc.cap for arc in path])
  82. for arc in path:
  83. arc.cap -= bottleneck
  84. sum_flow += bottleneck
  85. return sum_flow
  86. max_flow = 0
  87. while (True):
  88. layerNetwork = []
  89. firstLayer = Layer()
  90. firstLayer.nodeSet.add(0)
  91. layerNetwork.append(firstLayer)
  92. augment_set = set()
  93. augment_set.add(0)
  94. getLayerNetwork(0, layerNetwork, augment_set)
  95. if t - s not in layerNetwork[-1].nodeSet:
  96. break
  97. current_flow = find_blocking_flow(layerNetwork)
  98. if current_flow == 0:
  99. break
  100. else:
  101. max_flow += current_flow
  102. # add the backward arcs
  103. for layer in layerNetwork:
  104. for arc in layer.arcList:
  105. adjacent_matrix[arc.dst][arc.src] += adjacent_matrix[arc.src][arc.dst] - arc.cap
  106. adjacent_matrix[arc.src][arc.dst] = arc.cap
  107. for arc in arcs:
  108. print 'f %d %d %d' % (arc.src, arc.dst, arc.cap - adjacent_matrix[arc.src - s][arc.dst - s])


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/815557
推荐阅读
相关标签
  

闽ICP备14008679号