Exemplo n.º 1
0
 def test_graph(self):
     G = nx.path_graph(4)
     S = {1, 2}
     T = set(G) - S
     size = nx.normalized_cut_size(G, S, T)
     # The cut looks like this: o-{-o--o-}-o
     expected = 2 * ((1 / 4) + (1 / 2))
     assert expected == size
     # Test with no input T
     assert expected == nx.normalized_cut_size(G, S)
Exemplo n.º 2
0
 def test_directed(self):
     G = nx.DiGraph([(0, 1), (1, 2), (2, 3)])
     S = {1, 2}
     T = set(G) - S
     size = nx.normalized_cut_size(G, S, T)
     # The cut looks like this: o-{->o-->o-}->o
     expected = 2 * ((1 / 2) + (1 / 1))
     assert expected == size
     # Test with no input T
     assert expected == nx.normalized_cut_size(G, S)
Exemplo n.º 3
0
def findMin(tempList, setA, setB, G):
    minCost = nx.normalized_cut_size(G, setA, setB) / 2
    ele = 0
    for i in tempList:
        temp1 = list(setA)
        temp2 = list(setB)
        temp1.remove(i)
        temp2.append(i)
        cur_cost = nx.normalized_cut_size(G, set(temp1), set(temp2)) / 2
        if cur_cost < minCost:
            ele = i
            minCost = cur_cost
    return ele, minCost
def calculate_component_wise_ncut(G, comm_n_dict):

    comp_graphs = list(nx.connected_component_subgraphs(G))
    comp_wise_conductance = {}
    comp_wise_ncut = {}
    #comp_wise_conductance = {i:{} for i in range(len(comp_graphs))}
    i = 0
    
    for comp_id in range(len(comp_graphs)):
        graph = comp_graphs[comp_id]
        if len(list(graph.nodes())) > 1:
            for comm in comm_n_dict:
                if set(comm_n_dict[comm]).issubset(set(graph.nodes())) and len(list((graph.nodes()))) > len(comm_n_dict[comm]) and len(comm_n_dict[comm]) > 1:
                    if comp_id not in comp_wise_conductance:
                        comp_wise_conductance[comp_id] = {}
                    comp_wise_conductance[comp_id][comm] = nx.normalized_cut_size(graph, comm_n_dict[comm])

    #print comp_wise_conductance
    comp_min_conductance = {}
    comp_num_communities = {}
    for comp_id in comp_wise_conductance:
        #print comp_wise_conductance[comp_id]
        if len(comp_wise_conductance[comp_id]) > 0:
            comp_min_conductance[comp_id] = sum(comp_wise_conductance[comp_id].values())/ len(comp_wise_conductance[comp_id])
            comp_num_communities[comp_id] = len(comp_wise_conductance[comp_id])
            #comp_min_conductance[comp_id] = sorted(comp_wise_conductance[comp_id].items(), key=lambda x: x[1])[0][1]
    
    #print comp_wise_conductance
    return sum(comp_min_conductance.values())/len(comp_min_conductance.values())
Exemplo n.º 5
0
def normalize_cost(set1, set2, G):
    curr_cost = nx.normalized_cut_size(G, set1, set2) / 2
    while True:
        listA = list(set1)
        listB = list(set2)

        eleA, costA = findMin(listA, set1, set2, G)
        eleB, costB = findMin(listB, set2, set1, G)

        if min(costA, costB) >= curr_cost:
            break
        else:
            if costA < costB:
                listA.remove(eleA)
                listB.append(eleA)
                set1 = set(listA)
                set2 = set(listB)
                curr_cost = costA
            else:
                listB.remove(eleB)
                listA.append(eleB)
                set1 = set(listA)
                set2 = set(listB)
                curr_cost = costB

        print(curr_cost)
    print(sorted(set1))
    print(sorted(set2))
    return set1, set2, curr_cost
Exemplo n.º 6
0
 def test_graph(self):
     G = nx.path_graph(4)
     S = {1, 2}
     T = set(G) - S
     size = nx.normalized_cut_size(G, S, T)
     # The cut looks like this: o-{-o--o-}-o
     expected = 2 * ((1 / 4) + (1 / 2))
     assert_equal(expected, size)
Exemplo n.º 7
0
 def test_directed(self):
     G = nx.DiGraph([(0, 1), (1, 2), (2, 3)])
     S = {1, 2}
     T = set(G) - S
     size = nx.normalized_cut_size(G, S, T)
     # The cut looks like this: o-{->o-->o-}->o
     expected = 2 * ((1 / 2) + (1 / 1))
     assert_equal(expected, size)
Exemplo n.º 8
0
 def test_graph(self):
     G = nx.path_graph(4)
     S = {1, 2}
     T = set(G) - S
     size = nx.normalized_cut_size(G, S, T)
     # The cut looks like this: o-{-o--o-}-o
     expected = 2 * ((1 / 4) + (1 / 2))
     assert_equal(expected, size)
Exemplo n.º 9
0
 def test_directed(self):
     G = nx.DiGraph([(0, 1), (1, 2), (2, 3)])
     S = set([1, 2])
     T = set(G) - S
     size = nx.normalized_cut_size(G, S, T)
     # The cut looks like this: o-{->o-->o-}->o
     expected = 2 * ((1 / 2) + (1 / 1))
     assert_equal(expected, size)
Exemplo n.º 10
0
def get_normalizedCut_value(graph, subGraph):
    return nx.normalized_cut_size(graph, subGraph)
Exemplo n.º 11
0
        newgraph = G.subgraph(c)

# In[72]:

nx.node_connected_component(G, list(res)[0])

# In[73]:

a = list(sorted(res))
length = len(a) // 2
set1 = set(a[:length])
set2 = set(a[length:])

# In[74]:

print(nx.normalized_cut_size(newgraph, set1, set2) / 2)

# In[75]:

# print(nx.cut_size(G,set1,set2))

# In[82]:

# problem 7
# set1, set2

#1. try move set1 to set2
#2. try move set2 to set1
#3  move the smallest if cost is lower

 def cut_normalised_cost(self, set_A, set_B):
     return nx.normalized_cut_size(self.G, set_A, set_B, weight='weight')