Exemple #1
0
 def test_default_flow_function_karate_club_graph(self):
     G = nx.karate_club_graph()
     nx.set_edge_attributes(G, 1, "capacity")
     T = nx.gomory_hu_tree(G)
     assert nx.is_tree(T)
     for u, v in combinations(G, 2):
         cut_value, edge = self.minimum_edge_weight(T, u, v)
         assert nx.minimum_cut_value(G, u, v) == cut_value
Exemple #2
0
 def test_default_flow_function_karate_club_graph(self):
     G = nx.karate_club_graph()
     nx.set_edge_attributes(G, 1, 'capacity')
     T = nx.gomory_hu_tree(G)
     assert_true(nx.is_tree(T))
     for u, v in combinations(G, 2):
         cut_value, edge = self.minimum_edge_weight(T, u, v)
         assert_equal(nx.minimum_cut_value(G, u, v),
                      cut_value)
Exemple #3
0
 def test_davis_southern_women_graph(self):
     G = nx.davis_southern_women_graph()
     nx.set_edge_attributes(G, 1, 'capacity')
     for flow_func in flow_funcs:
         T = nx.gomory_hu_tree(G, flow_func=flow_func)
         assert_true(nx.is_tree(T))
         for u, v in combinations(G, 2):
             cut_value, edge = self.minimum_edge_weight(T, u, v)
             assert_equal(nx.minimum_cut_value(G, u, v), cut_value)
Exemple #4
0
 def test_florentine_families_graph(self):
     G = nx.florentine_families_graph()
     nx.set_edge_attributes(G, 1, 'capacity')
     for flow_func in flow_funcs:
         T = nx.gomory_hu_tree(G, flow_func=flow_func)
         assert nx.is_tree(T)
         for u, v in combinations(G, 2):
             cut_value, edge = self.minimum_edge_weight(T, u, v)
             assert (nx.minimum_cut_value(G, u, v) == cut_value)
Exemple #5
0
 def test_les_miserables_graph_cutset(self):
     G = nx.les_miserables_graph()
     nx.set_edge_attributes(G, 1, "capacity")
     for flow_func in flow_funcs:
         T = nx.gomory_hu_tree(G, flow_func=flow_func)
         assert nx.is_tree(T)
         for u, v in combinations(G, 2):
             cut_value, edge = self.minimum_edge_weight(T, u, v)
             assert nx.minimum_cut_value(G, u, v) == cut_value
Exemple #6
0
 def test_davis_southern_women_graph(self):
     G = nx.davis_southern_women_graph()
     nx.set_edge_attributes(G, 'capacity', 1)
     for flow_func in flow_funcs:
         T = nx.gomory_hu_tree(G, flow_func=flow_func)
         assert_true(nx.is_tree(T))
         for u, v in combinations(G, 2):
             cut_value, edge = self.minimum_edge_weight(T, u, v)
             assert_equal(nx.minimum_cut_value(G, u, v),
                          cut_value)
Exemple #7
0
 def test_karate_club_graph(self):
     G = nx.karate_club_graph()
     nx.set_edge_attributes(G, 'capacity', 1)
     for flow_func in flow_funcs:
         T = nx.gomory_hu_tree(G, flow_func=flow_func)
         assert_true(nx.is_tree(T))
         for u, v in combinations(G, 2):
             cut_value, edge = self.minimum_edge_weight(T, u, v)
             assert_equal(nx.minimum_cut_value(G, u, v),
                          cut_value)
Exemple #8
0
 def test_florentine_families_graph(self):
     G = nx.florentine_families_graph()
     nx.set_edge_attributes(G, 1, 'capacity')
     for flow_func in flow_funcs:
         T = nx.gomory_hu_tree(G, flow_func=flow_func)
         assert_true(nx.is_tree(T))
         for u, v in combinations(G, 2):
             cut_value, edge = self.minimum_edge_weight(T, u, v)
             assert_equal(nx.minimum_cut_value(G, u, v),
                          cut_value)
Exemple #9
0
 def test_wikipedia_example(self):
     # Example from https://en.wikipedia.org/wiki/Gomory%E2%80%93Hu_tree
     G = nx.Graph()
     G.add_weighted_edges_from((
         (0, 1, 1), (0, 2, 7), (1, 2, 1),
         (1, 3, 3), (1, 4, 2), (2, 4, 4),
         (3, 4, 1), (3, 5, 6), (4, 5, 2),
     ))
     for flow_func in flow_funcs:
         T = nx.gomory_hu_tree(G, capacity='weight', flow_func=flow_func)
         assert_true(nx.is_tree(T))
         for u, v in combinations(G, 2):
             cut_value, edge = self.minimum_edge_weight(T, u, v)
             assert_equal(nx.minimum_cut_value(G, u, v, capacity='weight'),
                          cut_value)
Exemple #10
0
 def test_wikipedia_example(self):
     # Example from https://en.wikipedia.org/wiki/Gomory%E2%80%93Hu_tree
     G = nx.Graph()
     G.add_weighted_edges_from((
         (0, 1, 1), (0, 2, 7), (1, 2, 1),
         (1, 3, 3), (1, 4, 2), (2, 4, 4),
         (3, 4, 1), (3, 5, 6), (4, 5, 2),
     ))
     for flow_func in flow_funcs:
         T = nx.gomory_hu_tree(G, capacity='weight', flow_func=flow_func)
         assert_true(nx.is_tree(T))
         for u, v in combinations(G, 2):
             cut_value, edge = self.minimum_edge_weight(T, u, v)
             assert_equal(nx.minimum_cut_value(G, u, v, capacity='weight'),
                          cut_value)
Exemple #11
0
                while G3T==G3S:
                    G3S = choice(list(G3.nodes()))
                    G3T = choice(list(G3.nodes()))
            # GRAFO 1
            g1_start_time = time.time()
            nx.maximum_flow(G1,G1S,G1T)
            g1_end_time = time.time() - g1_start_time
            totalTests.append(Test(0, 0, logarithmOrder, nx.density(G1), g1_end_time))

            g1_start_time = time.time()
            nx.maximum_flow_value(G1, G1S, G1T)
            g1_end_time = time.time() - g1_start_time
            totalTests.append(Test(0, 1, logarithmOrder, nx.density(G1), g1_end_time))

            g1_start_time = time.time()
            nx.minimum_cut_value(G1, G1S, G1T)
            g1_end_time = time.time() - g1_start_time
            totalTests.append(Test(0, 2, logarithmOrder, nx.density(G1), g1_end_time))

            # GRAFO 2
            g1_start_time = time.time()
            nx.maximum_flow(G2, G2S, G2T)
            g1_end_time = time.time() - g1_start_time
            totalTests.append(Test(1, 0, logarithmOrder, nx.density(G2), g1_end_time))

            g1_start_time = time.time()
            nx.maximum_flow_value(G2, G2S, G2T)
            g1_end_time = time.time() - g1_start_time
            totalTests.append(Test(1, 1, logarithmOrder, nx.density(G2), g1_end_time))

            g1_start_time = time.time()
Exemple #12
0
def print_flow(flow):
    for edge in G.edges():
        n1, n2 = edge
        print edge, flow[n1][n2]


print 'Flow value =', flow_value
print 'Flow ='
print_flow(maximum_flow)

# The maximum flow should equal the minimum cut.

# In[5]:

max_flow_value = nx.maximum_flow_value(G, 's', 't')
min_cut_value = nx.minimum_cut_value(G, 's', 't')

print max_flow_value == min_cut_value

# ## Flows with Demands
#
# Let's now record a demand value at each node (negative demand corresponds to supply at a node).

# In[6]:

# to add a property to a node, you should use G.node['s'] rather than
# G['s'] to reference the node.

G.node['s']['demand'] = -25
G.node['t']['demand'] = 20
G.node['u']['demand'] = 5
Exemple #13
0
nx.to_numpy_matrix(network1)

#%%
network1 = nx.DiGraph(a)
flowcost, flow = nx.maximum_flow(network1, _s=0, _t=3, capacity='weight')
flowcost

#%%
flow

#%%
flow3 = nx.minimum_cut(network1, _s=0, _t=3, capacity='weight')
flow3

#%%
flow4 = nx.minimum_cut_value(network1, _s=0, _t=3, capacity='weight')
flow4

#%%
# minimum cost flow problem
import networkx as nx
G = nx.DiGraph()
G.add_node('vs', demand=-10)
G.add_node('vt', demand=10)
G.add_edge('vs', 'v1', weight=4, capacity=10)
G.add_edge('vs', 'v2', weight=1, capacity=8)
G.add_edge('v2', 'v1', weight=2, capacity=5)
G.add_edge('v2', 'v3', weight=3, capacity=10)
G.add_edge('v1', 'v3', weight=6, capacity=2)
G.add_edge('v3', 'vt', weight=2, capacity=4)
G.add_edge('v1', 'vt', weight=1, capacity=7)
Exemple #14
0
    for edge in G.edges():
        n1, n2 = edge
        print edge, flow[n1][n2]


print 'Flow value =', flow_value
print 'Flow ='
print_flow(maximum_flow)


# The maximum flow should equal the minimum cut.

# In[5]:

max_flow_value = nx.maximum_flow_value(G, 's', 't')
min_cut_value = nx.minimum_cut_value(G, 's', 't')

print max_flow_value == min_cut_value


# ## Flows with Demands
# 
# Let's now record a demand value at each node (negative demand corresponds to supply at a node).

# In[6]:

# to add a property to a node, you should use G.node['s'] rather than
# G['s'] to reference the node.

G.node['s']['demand'] = -25 
G.node['t']['demand'] = 20
    g.add_edge((X + 'a'), (X + 'b'), capacity=P)
    ListaVertices.append(X)
'''
Esse 'for' é para colocar os valores das arestas
de acordo com o numero de fios (W) dado na primeira entrada
'''
for x in range(W):
    ID_W = input(
        'Digite vertice inicial, vertice final e peso: '
    )  #Aresta e peso. [X,Y,P] X = ArestaInicial, Y = ArestaFinal, P = Peso
    T = ID_W.split(' ')
    X = T[0]  #Origem
    Y = T[1]  #Destino
    P = int(T[2])
    if X == '1':
        'Atribui vpeso para uma aresta - Vertice Inicial(Computador do chefe) Liga no inicial da aresta "Quebrada"(Representado pela letra "a")'
        g.add_edge(X, (Y + 'a'), capacity=P)  #'função da biblioteca'
    elif Y == str(M):
        'Atribui vpeso para uma aresta - Final do vertica "Quebrado"Representado pela letra "b") liga no Vertice final(Servidor)'
        g.add_edge((X + 'b'), Y, capacity=P)  #'função da biblioteca'
    else:

        g.add_edge((X + 'b'), (Y + 'a'), capacity=P)  #'função da biblioteca'

'imprime o grafo, usando a biblioteca'
printGraph(g)

Cut = nx.minimum_cut_value(g, '1', str(M))

print('Valor de Saída: ' + str(Cut))
Exemple #16
0
    
    if origen1==destino1 and origen2==destino2:
       buscar=True
    else:
       buscar=False
 
 
 
 
 t11i=time.time()
 min_cut=nx.minimum_cut(G,(origen1,origen2),(destino1,destino2),capacity='weight')
 t11f=time.time()
 
 
 t12i=time.time()
 cut_value = nx.minimum_cut_value(G, (origen1,origen2),(destino1,destino2),capacity='weight')            
 t12f=time.time()
 
 t13i=time.time()
 flow_value = nx.maximum_flow_value(G, (origen1,origen2),(destino1,destino2),capacity='weight')
 t13f=time.time()
 
 tcreacion1 = t1f-t1i
 
 ta1a1=t11f-t11i
 ta1a2=t12f-t12i
 ta1a3=t13f-t13i
 
 T11.append(tcreacion1 + ta1a1)
 T12.append(tcreacion1 + ta1a2)
 T13.append(tcreacion1 + ta1a3)
Exemple #17
0
def find_max_min_st_cut(G_):
    nodes = list(G_.nodes)
    val = nx.minimum_cut_value(G_, "s", "t", capacity="weight")
    return val
Exemple #18
0
def min_cut_value(play: dict):
    raise NotImplementedError
    DG = nx.DiGraph(big_dimat_df(play))
    res = nx.minimum_cut_value(DG)
Exemple #19
0
import networkx as nx

n = int(input())
A = list(map(int, input().split()))
G = nx.DiGraph()

source = "source"
sink = "sink"

for i in range(1, n + 1):
    if A[i - 1] <= 0:
        G.add_edge(source, i, capacity=-A[i - 1])
    else:
        G.add_edge(i, sink, capacity=A[i - 1])

for i in range(1, n // 2 + 1):
    for j in range(2 * i, n + 1, i):
        G.add_edge(i, j)

try:
    mincut = nx.minimum_cut_value(G, source, sink)
    res = sum(a for a in A if a > 0)
    print(res - mincut)

except nx.NetworkXError:
    print(0)
Exemple #20
0
import networkx as nx
import sys

def add_capacities(G):
    for i,j in G.edges_iter():
        G.edge[i][j]['capacity'] = 1

if __name__ == '__main__':
    if len(sys.argv) != 4:
        exit("Wrong number of arguments.")
    
    filename, src, dst = sys.argv[1:]
    G = nx.read_gml(filename)
    add_capacities(G)

    print nx.minimum_cut_value(G, int(src), int(dst))
Exemple #21
0
                                'Orden': len(G),'Densidad': G.size() / nx.complete_graph(l).size(),
                                'Tiempo': tiempoalgo})
            misdatos = misdatos.append(row)

            tiempoalgo = time()
            for a in range(5):
                m_cut = nx.minimum_cut(G, sources[nodes], sinks[nodes], capacity='weight')
            tiempoalgo = time() - tiempoalgo + tiempo

            row = pd.DataFrame({'Generador': ['Grafo completo'], 'Algoritmo': ['Corte mínimo'], 'Orden': len(G),
                                'Densidad': G.size() / nx.complete_graph(l).size(), 'Tiempo': tiempoalgo})
            misdatos = misdatos.append(row)

            tiempoalgo = time()
            for a in range(5):
                cut_value = nx.minimum_cut_value(G, sources[nodes], sinks[nodes], capacity='weight')
            tiempoalgo = time() - tiempoalgo + tiempo

            row = pd.DataFrame({'Generador': ['Grafo completo'], 'Algoritmo': ['Valor de corte mínimo'],
                                'Orden': len(G), 'Densidad': G.size() / nx.complete_graph(l).size(),
                                'Tiempo': tiempoalgo})
            misdatos = misdatos.append(row)

            #TERCER GENERADOR

            tiempo = time()
            H = nx.wheel_graph(l)
            w = norm.rvs(10.0, 0.5, nx.number_of_edges(H))
            mu, std = norm.fit(w)
            m = 0
            for u, v, d in H.edges(data=True):
Exemple #22
0
    global target
    if len(sys.argv) != 4:
        print "Usage: python <source_file> <path_of_input_data_file> <source_node> <sink_node> "
        sys.exit()
    else:
        inputpath = sys.argv[1]
        source = int(sys.argv[2])
        target = int(sys.argv[3])

if __name__ == "__main__":
    main(sys.argv)


#G=nx.read_gml("C:\Users\Justin\Documents\karate (1)\karate.gml")
G=nx.read_gml(inputpath)
for u in G.edges():
    G[u[0]][u[1]]['capacity']=1.0

maxflow1= nx.maximum_flow_value(G,source,target)
mincut1 = nx.minimum_cut_value(G,source,target)


#H=nx.read_gml("C:\Users\Justin\Downloads\power\power.gml")
#for u in H.edges():
#    H[u[0]][u[1]]['capacity']=1.0

#maxflow2 = nx.maximum_flow_value(H,2553,4458)
#mincut2 = nx.minimum_cut_value(H,2553,4458)

#print maxflow1, mincut1, maxflow2, mincut2
print maxflow1, mincut1