Small example for demonstrating flow agumenting path (ford-fulkerson)
28 | algorithm.
29 | This example is taken from youtube lecture series on Advanced Operations
30 | Research by Prof. G.Srinivasan.
31 | Link to the video is:
32 | http://www.youtube.com/watch?v=J0wzih3_5Wo
33 | black: there is no flow on the arc
34 | red
35 | : the flow equals to the arc capacity
36 | green: there is positive flow on the arc, less then capacity.
37 |
38 |
39 | Expand source code
40 |
41 | '''
42 | Small example for demonstrating flow agumenting path (ford-fulkerson)
43 | algorithm.
44 | This example is taken from youtube lecture series on Advanced Operations
45 | Research by Prof. G.Srinivasan.
46 | Link to the video is:
47 | http://www.youtube.com/watch?v=J0wzih3_5Wo
48 |
49 | black: there is no flow on the arc
50 | red : the flow equals to the arc capacity
51 | green: there is positive flow on the arc, less then capacity.
52 | '''
53 | from __future__ import print_function
54 |
55 | try:
56 | from src.gimpy import Graph, DIRECTED_GRAPH
57 | except ImportError:
58 | from coinor.gimpy import Graph, DIRECTED_GRAPH
59 |
60 | if __name__=='__main__':
61 | g = Graph(type = DIRECTED_GRAPH, display = 'off')
62 | g.add_node(1,pos='"0,2!"', demand=4, label='(1, 4)')
63 |
64 | g.add_node(3,pos='"2,4!"', demand=0)
65 | g.add_node(2,pos='"2,0!"', demand=0)
66 | g.add_node(4,pos='"4,2!"', demand=0)
67 | g.add_node(6,pos='"6,4!"', demand=0)
68 |
69 | g.add_node(5,pos='"6,0!"', demand=0)
70 | g.add_node(7,pos='"8,2!"', demand=-4, label='(1, -4)')
71 | g.add_edge(1, 3, cost=0, capacity=2, label='(0, 2)')
72 | g.add_edge(1, 2, cost=0, capacity=2, label='(0, 2)')
73 | g.add_edge(3, 4, cost=1, capacity=1, label='(1, 1)')
74 | g.add_edge(3, 6, cost=2, capacity=2, label='(2, 2)')
75 | g.add_edge(2, 4, cost=3, capacity=1, label='(3, 1)')
76 | g.add_edge(2, 5, cost=5, capacity=1, label='(5, 1)')
77 | g.add_edge(4, 6, cost=1, capacity=3, label='(1, 3)')
78 | g.add_edge(4, 7, cost=4, capacity=2, label='(4, 2)')
79 | g.add_edge(4, 5, cost=1, capacity=1, label='(3, 1)')
80 | g.add_edge(6, 7, cost=0, capacity=2, label='(0, 2)')
81 | g.add_edge(5, 7, cost=0, capacity=2, label='(0, 2)')
82 | g.set_display_mode('off')
83 | g.display()
84 |
85 | # g.cycle_canceling('matplotlib')
86 | g.min_cost_flow(algo='cycle_canceling')
87 | # g.max_flow(1, 7)
88 |
89 | nl = list(int(n) for n in g.get_node_list())
90 | nl.sort()
91 | for n in nl:
92 | for m in nl:
93 | if g.check_edge(n, m):
94 | print(n, m, g.get_edge_attr(n, m, 'flow'))
95 |
96 |