Example #1
0
    def test_optional_capacity(self):
        # Test optional capacity parameter.
        G = nx.DiGraph()
        G.add_edge('x','a', spam = 3.0)
        G.add_edge('x','b', spam = 1.0)
        G.add_edge('a','c', spam = 3.0)
        G.add_edge('b','c', spam = 5.0)
        G.add_edge('b','d', spam = 4.0)
        G.add_edge('d','e', spam = 2.0)
        G.add_edge('c','y', spam = 2.0)
        G.add_edge('e','y', spam = 3.0)

        solnFlows = {'x': {'a': 2.0, 'b': 1.0},
                     'a': {'c': 2.0},
                     'b': {'c': 0, 'd': 1.0},
                     'c': {'y': 2.0},
                     'd': {'e': 1.0},
                     'e': {'y': 1.0},
                     'y': {}}
        solnValue = 3.0
        s = 'x'
        t = 'y'
        
        flowValue, flowDict = nx.ford_fulkerson(G, s, t, capacity = 'spam')
        assert_equal(flowValue, solnValue)
        assert_equal(flowDict, solnFlows)
        assert_equal(nx.min_cut(G, s, t, capacity = 'spam'), solnValue)
        assert_equal(nx.max_flow(G, s, t, capacity = 'spam'), solnValue)
        assert_equal(nx.ford_fulkerson_flow(G, s, t, capacity = 'spam'),
                     solnFlows)
Example #2
0
def compare_flows(G, s, t, solnFlows, solnValue):
    flowValue, flowDict = nx.ford_fulkerson(G, s, t)
    assert_equal(flowValue, solnValue)
    assert_equal(flowDict, solnFlows)
    assert_equal(nx.min_cut(G, s, t), solnValue)
    assert_equal(nx.max_flow(G, s, t), solnValue)
    assert_equal(nx.ford_fulkerson_flow(G, s, t), solnFlows)
Example #3
0
def compare_flows(G, s, t, solnFlows, solnValue):
    flowValue, flowDict = nx.ford_fulkerson(G, s, t)
    assert_equal(flowValue, solnValue)
    assert_equal(flowDict, solnFlows)
    assert_equal(nx.min_cut(G, s, t), solnValue)
    assert_equal(nx.max_flow(G, s, t), solnValue)
    assert_equal(nx.ford_fulkerson_flow(G, s, t), solnFlows)
def flow(n):
    Gs = nx.Graph()
    for v in n:
        Gs.add_edge('a',v,capacity=1)
        sets = G.node[v]['t']
        for s in sets:
            Gs.add_edge(v,s,capacity=1)
    for i in range(6):
        Gs.add_edge('b',i,capacity=1)
    return sum(nx.ford_fulkerson_flow(Gs,'a','b')['a'].values())
Example #5
0
def compare_flows(G, s, t, solnFlows, solnValue, capacity = 'capacity'):
    flowValue, flowDict = nx.ford_fulkerson(G, s, t, capacity)
    assert_equal(flowValue, solnValue)
    assert_equal(flowDict, solnFlows)
    flowValue, flowDict = nx.preflow_push(G, s, t, capacity)
    assert_equal(flowValue, solnValue)
    validate_flows(G, s, t, flowDict, solnValue, capacity)
    assert_equal(nx.min_cut(G, s, t, capacity), solnValue)
    assert_equal(nx.max_flow(G, s, t, capacity), solnValue)
    assert_equal(nx.ford_fulkerson_flow(G, s, t, capacity), solnFlows)
Example #6
0
    def test_optional_capacity(self):
        # Test optional capacity parameter.
        G = nx.DiGraph()
        G.add_edge('x', 'a', spam=3.0)
        G.add_edge('x', 'b', spam=1.0)
        G.add_edge('a', 'c', spam=3.0)
        G.add_edge('b', 'c', spam=5.0)
        G.add_edge('b', 'd', spam=4.0)
        G.add_edge('d', 'e', spam=2.0)
        G.add_edge('c', 'y', spam=2.0)
        G.add_edge('e', 'y', spam=3.0)

        solnFlows = {
            'x': {
                'a': 2.0,
                'b': 1.0
            },
            'a': {
                'c': 2.0
            },
            'b': {
                'c': 0,
                'd': 1.0
            },
            'c': {
                'y': 2.0
            },
            'd': {
                'e': 1.0
            },
            'e': {
                'y': 1.0
            },
            'y': {}
        }
        solnValue = 3.0
        s = 'x'
        t = 'y'

        flowValue, flowDict = nx.ford_fulkerson(G, s, t, capacity='spam')
        assert_equal(flowValue, solnValue)
        assert_equal(flowDict, solnFlows)
        assert_equal(nx.min_cut(G, s, t, capacity='spam'), solnValue)
        assert_equal(nx.max_flow(G, s, t, capacity='spam'), solnValue)
        assert_equal(nx.ford_fulkerson_flow(G, s, t, capacity='spam'),
                     solnFlows)
Example #7
0
def compare_flows(G, s, t, solnFlows, solnValue, capacity = 'capacity'):
    flowValue, flowDict = nx.ford_fulkerson(G, s, t, capacity)
    assert_equal(flowValue, solnValue)
    assert_equal(flowDict, solnFlows)
    flowValue, flowDict = nx.preflow_push(G, s, t, capacity)
    assert_equal(flowValue, solnValue)
    validate_flows(G, s, t, flowDict, solnValue, capacity)
    flowValue, flowDict = nx.shortest_augmenting_path(G, s, t, capacity,
                                                      two_phase=False)
    assert_equal(flowValue, solnValue)
    validate_flows(G, s, t, flowDict, solnValue, capacity)
    flowValue, flowDict = nx.shortest_augmenting_path(G, s, t, capacity,
                                                      two_phase=True)
    assert_equal(flowValue, solnValue)
    validate_flows(G, s, t, flowDict, solnValue, capacity)
    assert_equal(nx.min_cut(G, s, t, capacity), solnValue)
    assert_equal(nx.max_flow(G, s, t, capacity), solnValue)
    assert_equal(nx.ford_fulkerson_flow(G, s, t, capacity), solnFlows)
    def solve_instance(self, instance):
        """
        Where the magic happens.
        This method should return the solution (as a string) of the given instance.
        """
        graph = self.construct_graph(instance)

        flow = nx.ford_fulkerson_flow(graph, instance.city_name, FINAL)

        max_speed_kmh = 0
        for node, flow_dict in flow.items():
            if FINAL in flow_dict:
                max_speed_kmh += flow_dict[FINAL]

        seconds_in_a_hour = 3600
        meters_per_car = 5

        max_speed_ms = max_speed_kmh * (1000/1) * (1/seconds_in_a_hour)
        cars_per_second = max_speed_ms * (1/meters_per_car)
        cars_per_hour = cars_per_second * seconds_in_a_hour

        return "{0} {1:.0f}".format(instance.city_name, cars_per_hour)
Example #9
0
def compare_flows(G, s, t, solnFlows, solnValue, capacity='capacity'):
    flowValue, flowDict = nx.ford_fulkerson(G, s, t, capacity)
    assert_equal(flowValue, solnValue)
    assert_equal(flowDict, solnFlows)
    flowValue, flowDict = nx.preflow_push(G, s, t, capacity)
    assert_equal(flowValue, solnValue)
    validate_flows(G, s, t, flowDict, solnValue, capacity)
    flowValue, flowDict = nx.shortest_augmenting_path(G,
                                                      s,
                                                      t,
                                                      capacity,
                                                      two_phase=False)
    assert_equal(flowValue, solnValue)
    validate_flows(G, s, t, flowDict, solnValue, capacity)
    flowValue, flowDict = nx.shortest_augmenting_path(G,
                                                      s,
                                                      t,
                                                      capacity,
                                                      two_phase=True)
    assert_equal(flowValue, solnValue)
    validate_flows(G, s, t, flowDict, solnValue, capacity)
    assert_equal(nx.min_cut(G, s, t, capacity), solnValue)
    assert_equal(nx.max_flow(G, s, t, capacity), solnValue)
    assert_equal(nx.ford_fulkerson_flow(G, s, t, capacity), solnFlows)
Example #10
0
def mergePatch(texture, seamMap, seamMapInfo, initMap, sourceMap, patch, x, y):

    # Compute the overlap region
    overlapMask = initMap[x:x + patch.shape[0], y:y + patch.shape[1]]

    # If there is no merge to do, update the texture and initMap
    # and stop the function's execution
    if overlapMask.sum() == 0:
        texture[x:x + patch.shape[0], y:y + patch.shape[1]] = patch
        initMap[x:x + patch.shape[0], y:y + patch.shape[1]] = 1
        return 1

    # Generate the graph for max_flow min_cut algorithm
    g = networkx.Graph()

    for row in range(x, x + patch.shape[0]):
        for col in range(y, y + patch.shape[1]):

            # Check if the pixel is part of the overlap region
            if initMap[row, col] == 1:

                # Compute information on the pixel's neighbors
                topNeighInTexture = row > 0
                downNeighInTexture = row < texture.shape[0] - 1
                leftNeighInTexture = col > 0
                rightNeighInTexture = col < texture.shape[1] - 1

                topNeighInPatch = row > x
                downNeighInPatch = row < x + patch.shape[0] - 1
                leftNeighInPatch = col > y
                rightNeighInPatch = col < y + patch.shape[1] - 1

                topNeighInOverlap = (topNeighInTexture and topNeighInPatch
                                     and initMap[row - 1, col])
                downNeighInOverlap = (downNeighInTexture and downNeighInPatch
                                      and initMap[row + 1, col])
                leftNeighInOverlap = (leftNeighInTexture and leftNeighInPatch
                                      and initMap[row, col - 1])
                rightNeighInOverlap = (rightNeighInTexture
                                       and rightNeighInPatch
                                       and initMap[row, col + 1])

                # The texture is under construction
                if initMap.sum() < initMap.size:

                    # Check if the pixel should be connected to the source
                    # node
                    connectToA = ((row == x and row != 0)
                                  or (col == y and col != 0))

                    if connectToA:
                        add_edge(g,
                                 'A',
                                 nameFromRowCol(row, col),
                                 capacity=999999.0)

                    # Check if the pixel should be connected to the target
                    # node
                    if (not connectToA and
                        (not rightNeighInOverlap or not downNeighInOverlap)):
                        add_edge(g,
                                 'B',
                                 nameFromRowCol(row, col),
                                 capacity=999999.0)

                # The texture has been fully constructed, it is under
                # improvement
                else:

                    # Check if the pixel should be connected to the source node
                    connectToA = (row == x or row == x + patch.shape[0] - 1
                                  or col == y or col == y + patch.shape[1] - 1)

                    if connectToA:
                        add_edge(g,
                                 'A',
                                 nameFromRowCol(row, col),
                                 capacity=999999.0)

                # Connect the pixel to its right neighbor, if applicable
                if rightNeighInOverlap == 1:

                    # If there is already a seam between the pixel and it's
                    # right neighbor, add a special "seam node" to the graph.
                    # This node will be connected to the current pixel's node,
                    # its right neighbor's node as well as the 'B' node. Else,
                    # simply add an edge between the pixel and its neighbor.

                    if (seamMapInfo[row * 2, col * 2 + 1] != None):

                        seamValue = seamMap[row * 2, col * 2 + 1]
                        seamInfo = seamMapInfo[row * 2, col * 2 + 1]
                        seamNodeName = ("seam " + nameFromRowCol(row, col) +
                                        " " + nameFromRowCol(row, col + 1))

                        add_edge(g, seamNodeName, 'B', capacity=seamValue)

                        add_edge(g,
                                 seamNodeName,
                                 nameFromRowCol(row, col),
                                 capacity=m(seamInfo[0], patch[row - x,
                                                               col - y],
                                            seamInfo[2], patch[row - x,
                                                               col + 1 - y]))

                        add_edge(g,
                                 seamNodeName,
                                 nameFromRowCol(row, col + 1),
                                 capacity=m(seamInfo[1], patch[row - x,
                                                               col - y],
                                            seamInfo[3], patch[row - x,
                                                               col + 1 - y]))

                    else:

                        add_edge(g,
                                 nameFromRowCol(row, col),
                                 nameFromRowCol(row, col + 1),
                                 capacity=m(texture[row, col], patch[row - x,
                                                                     col - y],
                                            texture[row, col + 1],
                                            patch[row - x, col + 1 - y]))
                    """
                    
                    add_edge(g,nameFromRowCol(row, col),
                               nameFromRowCol(row, col + 1),
                               capacity=m(texture[row, col],
                                          patch[row - x, col - y],
                                          texture[row, col + 1],
                                          patch[row - x, col + 1 - y]))
                    """

                # Connect the pixel to its bottom neighbor, if applicable
                if downNeighInOverlap == 1:

                    # If there is already a seam between the pixel and it's
                    # bottom neighbor, add a special "seam node" to the graph.
                    # This node will be connected to the current pixel's node,
                    # its bottom neighbor's node as well as the 'B' node.
                    # Else, simply add an edge between the pixel and its
                    # neighbor.

                    if (seamMapInfo[row * 2 + 1, col * 2] != None):

                        seamValue = seamMap[row * 2 + 1, col * 2]
                        seamInfo = seamMapInfo[row * 2 + 1, col * 2]
                        seamNodeName = ("seam " + nameFromRowCol(row, col) +
                                        " " + nameFromRowCol(row + 1, col))

                        add_edge(g, seamNodeName, 'B', capacity=seamValue)

                        add_edge(g,
                                 seamNodeName,
                                 nameFromRowCol(row, col),
                                 capacity=m(seamInfo[0], patch[row - x,
                                                               col - y],
                                            seamInfo[2], patch[row + 1 - x,
                                                               col - y]))

                        add_edge(g,
                                 seamNodeName,
                                 nameFromRowCol(row + 1, col),
                                 capacity=m(seamInfo[1], patch[row - x,
                                                               col - y],
                                            seamInfo[3], patch[row + 1 - x,
                                                               col - y]))

                    else:

                        add_edge(g,
                                 nameFromRowCol(row, col),
                                 nameFromRowCol(row + 1, col),
                                 capacity=m(texture[row, col], patch[row - x,
                                                                     col - y],
                                            texture[row + 1, col],
                                            patch[row + 1 - x, col - y]))
                    """
                    
                    
                    add_edge(g,nameFromRowCol(row, col),
                               nameFromRowCol(row + 1, col),
                               capacity=m(texture[row, col],
                                          patch[row - x, col - y],
                                          texture[row + 1, col],
                                          patch[row + 1 - x, col - y]))
                    """

    if 'B' in g:

        # Compute max-flow min-cut
        try:
            flow = networkx.ford_fulkerson_flow(g, 'A', 'B')
        except:
            import pdb
            pdb.set_trace()

        # Compute the auxiliary graph and populate it with edges from the original
        # graph whose capacity are NOT saturated by the flow computed in the
        # max-flow algorithm.
        gAux = networkx.Graph()

        for edge in g.edges(data=True):
            if (edge[2]['capacity'] - flow[edge[0]][edge[1]] >= 1e-10):
                gAux.add_edge(edge[0], edge[1])

        # Partition the auxiliary graph according to which nodes are connected to
        # the source node and which are connected to the target node.
        if 'A' in gAux:
            sourcePartition = set(
                networkx.single_source_shortest_path(gAux, 'A'))
        else:
            sourcePartition = set()

        if 'B' in gAux:
            targetPartition = set(
                networkx.single_source_shortest_path(gAux, 'B'))
        else:
            targetPartition = set()

    else:
        sourcePartition = set(networkx.single_source_shortest_path(g, 'A'))
        targetPartition = set()

    # Ensure that the graph has been correctly separated in two
    # distinct partitions
    assert (sourcePartition - targetPartition == sourcePartition)

    # Update the maps
    sourceColor = numpy.random.randint(0, 255, 3)
    oldPatch = numpy.copy(texture[x:x + patch.shape[0], y:y + patch.shape[1]])
    for row in range(x, x + patch.shape[0]):
        for col in range(y, y + patch.shape[1]):
            if initMap[row, col] == 0:
                sourceMap[row, col] = sourceColor
                texture[row, col] = patch[row - x][col - y]
            else:
                if nameFromRowCol(row, col) in targetPartition:
                    # This pixel receives it's color from the new patch
                    sourceMap[row, col] = sourceColor
                    texture[row, col] = patch[row - x][col - y]

                    # Update the seam between this pixel and its top neighbor
                    if nameFromRowCol(row - 1, col) in targetPartition:
                        seamMap[row * 2 - 1, col * 2] = 0.0
                        seamMapInfo[row * 2 - 1, col * 2] = None
                    else:

                        if row > x and initMap[row - 1, col]:
                            seamMap[row * 2 - 1, col * 2] = m(
                                oldPatch[row - 1 - x,
                                         col - y], patch[row - 1 - x, col - y],
                                oldPatch[row - x, col - y], patch[row - x,
                                                                  col - y])

                            seamMapInfo[row * 2 - 1,
                                        col * 2] = (oldPatch[row - 1 - x,
                                                             col - y],
                                                    patch[row - 1 - x,
                                                          col - y],
                                                    oldPatch[row - x, col - y],
                                                    patch[row - x, col - y])

                    # Update the seam between this pixel and its bottom
                    # neighbor
                    if nameFromRowCol(row + 1, col) in targetPartition:
                        seamMap[row * 2 + 1, col * 2] = 0.0
                        seamMapInfo[row * 2 + 1, col * 2] = None
                    else:

                        if row < x + patch.shape[0] - 1 and initMap[row + 1,
                                                                    col]:
                            seamMap[row * 2 + 1, col * 2] = m(
                                oldPatch[row - x, col - y], patch[row - x,
                                                                  col - y],
                                oldPatch[row + 1 - x,
                                         col - y], patch[row + 1 - x, col - y])

                            seamMapInfo[row * 2 + 1,
                                        col * 2] = (oldPatch[row - x, col - y],
                                                    patch[row - x, col - y],
                                                    oldPatch[row + 1 - x,
                                                             col - y],
                                                    patch[row + 1 - x,
                                                          col - y])

                    # Update the seam between this pixel and its left neighbor
                    if nameFromRowCol(row, col - 1) in targetPartition:
                        seamMap[row * 2, col * 2 - 1] = 0.0
                        seamMapInfo[row * 2, col * 2 - 1] = None
                    else:
                        if col > y and initMap[row, col - 1]:
                            seamMap[row * 2, col * 2 - 1] = m(
                                oldPatch[row - x, col - 1 - y],
                                patch[row - x, col - 1 - y],
                                oldPatch[row - x, col - y], patch[row - x,
                                                                  col - y])

                            seamMapInfo[row * 2,
                                        col * 2 - 1] = (oldPatch[row - x,
                                                                 col - 1 - y],
                                                        patch[row - x,
                                                              col - 1 - y],
                                                        oldPatch[row - x,
                                                                 col - y],
                                                        patch[row - x,
                                                              col - y])

                    # Update the seam between this pixel and its right neighbor
                    if nameFromRowCol(row, col + 1) in targetPartition:
                        seamMap[row * 2, col * 2 + 1] = 0.0
                        seamMapInfo[row * 2, col * 2 + 1] = None
                    else:
                        if (col < y + patch.shape[1] - 1
                                and initMap[row, col + 1]):

                            seamMap[row * 2, col * 2 + 1] = m(
                                patch[row - x, col - y],
                                oldPatch[row - x, col - y], patch[row - x,
                                                                  col + 1 - y],
                                oldPatch[row - x, col + 1 - y])

                            seamMapInfo[row * 2,
                                        col * 2 + 1] = (patch[row - x,
                                                              col - y],
                                                        oldPatch[row - x,
                                                                 col - y],
                                                        patch[row - x,
                                                              col + 1 - y],
                                                        oldPatch[row - x,
                                                                 col + 1 - y])

            initMap[row, col] = 1

    return 1
Example #11
0
def mergePatch(texture, seamMap, seamMapInfo, initMap, sourceMap, patch,
               x, y):

    # Compute the overlap region
    overlapMask = initMap[x:x + patch.shape[0], y:y + patch.shape[1]]

    # If there is no merge to do, update the texture and initMap
    # and stop the function's execution
    if overlapMask.sum() == 0:
        texture[x: x + patch.shape[0], y: y + patch.shape[1]] = patch
        initMap[x: x + patch.shape[0], y: y + patch.shape[1]] = 1
        return 1

    # Generate the graph for max_flow min_cut algorithm
    g = networkx.Graph()

    for row in range(x, x + patch.shape[0]):
        for col in range(y, y + patch.shape[1]):

            # Check if the pixel is part of the overlap region
            if initMap[row, col] == 1:
                
                # Compute information on the pixel's neighbors
                topNeighInTexture = row > 0
                downNeighInTexture = row < texture.shape[0] - 1
                leftNeighInTexture = col > 0
                rightNeighInTexture = col < texture.shape[1] - 1
                        
                topNeighInPatch = row > x
                downNeighInPatch = row < x + patch.shape[0] - 1
                leftNeighInPatch = col > y
                rightNeighInPatch = col < y + patch.shape[1] - 1
                    
                topNeighInOverlap = (topNeighInTexture and
                                     topNeighInPatch and
                                     initMap[row - 1, col])
                downNeighInOverlap = (downNeighInTexture and 
                                      downNeighInPatch and
                                      initMap[row + 1, col])
                leftNeighInOverlap = (leftNeighInTexture and 
                                      leftNeighInPatch and
                                      initMap[row, col - 1])
                rightNeighInOverlap = (rightNeighInTexture and 
                                       rightNeighInPatch and
                                       initMap[row, col + 1])

                 # The texture is under construction
                if initMap.sum() < initMap.size:          

                    # Check if the pixel should be connected to the source
                    # node
                    connectToA = ((row == x and row != 0) or
                                  (col == y and col != 0))
                    
                    if connectToA:
                        add_edge(g, 'A', nameFromRowCol(row, col),
                                 capacity=999999.0)

                    # Check if the pixel should be connected to the target
                    # node
                    if (not connectToA and 
                        (not rightNeighInOverlap or not downNeighInOverlap)):
                        add_edge(g, 'B', nameFromRowCol(row, col),
                                 capacity=999999.0)

                # The texture has been fully constructed, it is under
                # improvement
                else:

                    # Check if the pixel should be connected to the source node
                    connectToA = (row == x or row == x + patch.shape[0] - 1 or
                                  col == y or col == y + patch.shape[1] - 1)
                    
                    if connectToA:
                        add_edge(g, 'A', nameFromRowCol(row, col),
                                 capacity=999999.0)
                                 
                # Connect the pixel to its right neighbor, if applicable
                if rightNeighInOverlap == 1:

                    # If there is already a seam between the pixel and it's
                    # right neighbor, add a special "seam node" to the graph.
                    # This node will be connected to the current pixel's node,
                    # its right neighbor's node as well as the 'B' node. Else,
                    # simply add an edge between the pixel and its neighbor.


                    if(seamMapInfo[row * 2, col * 2 + 1] != None):

                        seamValue = seamMap[row * 2, col * 2 + 1]
                        seamInfo = seamMapInfo[row * 2, col * 2 + 1]
                        seamNodeName = ("seam " + nameFromRowCol(row, col) + 
                                        " " + nameFromRowCol(row, col + 1))
                           
                        add_edge(g, seamNodeName, 'B', capacity=seamValue)
                        
                        add_edge(g,seamNodeName, nameFromRowCol(row, col),
                                   capacity=m(seamInfo[0], 
                                              patch[row - x, col - y],
                                              seamInfo[2],
                                              patch[row - x, col + 1 - y]))
                        
                        add_edge(g,seamNodeName, nameFromRowCol(row, col + 1),
                                   capacity=m(seamInfo[1], 
                                              patch[row - x, col - y],
                                              seamInfo[3],
                                              patch[row - x, col + 1 - y]))
                            
                    else:
                    
                        add_edge(g,nameFromRowCol(row, col),
                                   nameFromRowCol(row, col + 1),
                                   capacity=m(texture[row, col],
                                              patch[row - x, col - y],
                                              texture[row, col + 1],
                                              patch[row - x, col + 1 - y]))
                    
                    """
                    
                    add_edge(g,nameFromRowCol(row, col),
                               nameFromRowCol(row, col + 1),
                               capacity=m(texture[row, col],
                                          patch[row - x, col - y],
                                          texture[row, col + 1],
                                          patch[row - x, col + 1 - y]))
                    """
                                          

                # Connect the pixel to its bottom neighbor, if applicable
                if downNeighInOverlap == 1:
                    
                    # If there is already a seam between the pixel and it's
                    # bottom neighbor, add a special "seam node" to the graph.
                    # This node will be connected to the current pixel's node,
                    # its bottom neighbor's node as well as the 'B' node. 
                    # Else, simply add an edge between the pixel and its 
                    # neighbor.
                    
                    
                    if(seamMapInfo[row * 2 + 1, col * 2] != None):
                        
                        seamValue = seamMap[row * 2 + 1, col * 2]
                        seamInfo = seamMapInfo[row * 2 + 1, col * 2]
                        seamNodeName = ("seam " + nameFromRowCol(row, col) + 
                                        " " + nameFromRowCol(row + 1, col))
                           
                        add_edge(g, seamNodeName, 'B', capacity=seamValue)
                        
                        add_edge(g, seamNodeName, nameFromRowCol(row, col),
                                   capacity=m(seamInfo[0], 
                                              patch[row - x, col - y],
                                              seamInfo[2],
                                              patch[row + 1 - x, col - y]))
                        
                        add_edge(g, seamNodeName, nameFromRowCol(row + 1, col),
                                   capacity=m(seamInfo[1], 
                                              patch[row - x, col - y],
                                              seamInfo[3],
                                              patch[row + 1 - x, col - y]))
                            
                    else:
                    
                        add_edge(g, nameFromRowCol(row, col),
                                   nameFromRowCol(row + 1, col),
                                   capacity=m(texture[row, col],
                                              patch[row - x, col - y],
                                              texture[row + 1, col],
                                              patch[row + 1 - x, col - y]))
                    
                    """
                    
                    
                    add_edge(g,nameFromRowCol(row, col),
                               nameFromRowCol(row + 1, col),
                               capacity=m(texture[row, col],
                                          patch[row - x, col - y],
                                          texture[row + 1, col],
                                          patch[row + 1 - x, col - y]))
                    """

    if 'B' in g:

        # Compute max-flow min-cut
        try:
            flow = networkx.ford_fulkerson_flow(g, 'A', 'B')
        except:
            import pdb
            pdb.set_trace()

        # Compute the auxiliary graph and populate it with edges from the original
        # graph whose capacity are NOT saturated by the flow computed in the
        # max-flow algorithm.
        gAux = networkx.Graph()

        for edge in g.edges(data=True):                
            if (edge[2]['capacity'] - flow[edge[0]][edge[1]] >= 1e-10):
                gAux.add_edge(edge[0], edge[1])

        # Partition the auxiliary graph according to which nodes are connected to
        # the source node and which are connected to the target node.
        if 'A' in gAux:
            sourcePartition = set(networkx.single_source_shortest_path(gAux, 'A'))
        else:
            sourcePartition = set()
            
        if 'B' in gAux:
            targetPartition = set(networkx.single_source_shortest_path(gAux, 'B'))
        else:
            targetPartition = set() 
            
    else:
        sourcePartition = set(networkx.single_source_shortest_path(g, 'A'))
        targetPartition = set()

    # Ensure that the graph has been correctly separated in two
    # distinct partitions   
    assert(sourcePartition - targetPartition == sourcePartition)

    # Update the maps
    sourceColor = numpy.random.randint(0, 255, 3)
    oldPatch = numpy.copy(texture[x:x + patch.shape[0], y:y + patch.shape[1]])
    for row in range(x, x + patch.shape[0]):
        for col in range(y, y + patch.shape[1]):
            if initMap[row, col] == 0:
                sourceMap[row, col] = sourceColor
                texture[row, col] = patch[row - x][col - y]
            else:
                if nameFromRowCol(row, col) in targetPartition:
                    # This pixel receives it's color from the new patch
                    sourceMap[row, col] = sourceColor
                    texture[row, col] = patch[row - x][col - y]

                    # Update the seam between this pixel and its top neighbor
                    if nameFromRowCol(row - 1, col) in targetPartition:
                        seamMap[row * 2 - 1, col * 2] = 0.0
                        seamMapInfo[row * 2 - 1, col * 2] = None
                    else:

                        if row > x and initMap[row - 1, col]:
                            seamMap[row * 2 - 1, col * 2] = m(oldPatch[row - 1 - x, col - y],
                                                              patch[row - 1 - x,col - y],
                                                              oldPatch[row - x,col - y],
                                                              patch[row - x,col - y])

                            seamMapInfo[row * 2 - 1, col * 2] = (oldPatch[row - 1 - x,col - y],
                                                                 patch[row - 1 - x,col - y],
                                                                 oldPatch[row - x,col - y],
                                                                 patch[row - x,col - y])

                    # Update the seam between this pixel and its bottom
                    # neighbor
                    if nameFromRowCol(row + 1, col) in targetPartition:
                        seamMap[row * 2 + 1, col * 2] = 0.0
                        seamMapInfo[row * 2 + 1, col * 2] = None
                    else:

                        if row < x + patch.shape[0] - 1 and initMap[row+1, col]:
                            seamMap[row*2+1, col*2] = m(oldPatch[row-x,col-y],
                                                        patch[row-x,col-y],
                                                        oldPatch[row+1-x,col-y],
                                                        patch[row+1-x,col-y])

                            seamMapInfo[row*2+1, col*2] = (oldPatch[row-x,col-y],
                                                           patch[row-x,col-y],
                                                           oldPatch[row+1-x,col-y],
                                                           patch[row+1-x,col-y])

                    # Update the seam between this pixel and its left neighbor
                    if nameFromRowCol(row, col - 1) in targetPartition:
                        seamMap[row * 2, col * 2 - 1] = 0.0
                        seamMapInfo[row * 2, col * 2 - 1] = None
                    else:
                        if col > y and initMap[row, col - 1]:
                            seamMap[row*2, col*2-1] = m(oldPatch[row-x,col-1-y],
                                                        patch[row-x,col-1-y],
                                                        oldPatch[row-x,col-y],
                                                        patch[row-x,col-y])

                            seamMapInfo[row*2, col*2-1] = (oldPatch[row-x,col-1-y],
                                                           patch[row-x,col-1-y],
                                                           oldPatch[row-x,col-y],
                                                           patch[row-x,col-y])

                    # Update the seam between this pixel and its right neighbor
                    if nameFromRowCol(row, col + 1) in targetPartition:
                        seamMap[row * 2, col * 2 + 1] = 0.0
                        seamMapInfo[row * 2, col * 2 + 1] = None
                    else:
                        if (col < y + patch.shape[1] - 1 and
                            initMap[row, col + 1]):

                            seamMap[row*2, col*2+1] = m(patch[row-x,col-y],
                                                        oldPatch[row-x,col-y],
                                                        patch[row-x,col+1-y],
                                                        oldPatch[row-x,col+1-y])

                            seamMapInfo[row*2, col*2+1] = (patch[row-x,col-y],
                                                           oldPatch[row-x,col-y],
                                                           patch[row-x,col+1-y],
                                                           oldPatch[row-x,col+1-y])

            initMap[row, col] = 1

    return 1