Пример #1
0
def render_graph():
    global nodes_count
    global matrix
    i = 0
    g = nx.Graph()
    p = nx.Graph()
    colors = ["#00ffff", "#000000", "#0000ff", "#ff00ff", "#00ff00", "#800000", "#ff0000", "#ffff00"]
    for node in range(nodes_count):
        g.add_node(node)
        p.add_node(node)
    for element in matrix:
        if element == 1 and i // nodes_count != i % nodes_count:
            g.add_edge(i // nodes_count, i % nodes_count)
            p.add_edge(i // nodes_count, i % nodes_count)
        i += 1
    one_color = []
    node_color = ["b" for i in range(len(g.nodes))]
    j = 0
    while j <= len(g.nodes):
        one_color = list(maximum_independent_set(p))
        p.remove_nodes_from(one_color)
        # print(one_color)
        # print(p.nodes)
        for i in range(len(node_color)):
            if i in one_color:
                node_color[i] = colors[j]
        j += 1
        # print(node_color)
    nx.draw_planar(g, with_labels=True, node_color=node_color, alpha=0.8)
    plt.show()
Пример #2
0
def prune_related(kc, kc_thres):
    '''
    Find the minimal set of individuals to keep by removing relationships based on
    supplied threshold.

    Input:
    kc:         NumPy array, symmetric. Kinship coefficient matrix.
    kc_thres:   Any positive number. Treshold for which relationships to consider.

    Returns:
    List of indexes corresponding to individuals in kc matrix.
    '''

    assert kc.shape[0] == kc.shape[1], 'Input matrix "kc" must have equal number of columns and rows.'

    # Set diagonal to zero, ignoring kinship with self.
    kc[np.diag_indices_from(kc)] = 0

    # Construct adjacency matrix, specifying which individual pairs have kinship
    # above supplied threshold.
    adj = kc > kc_thres

    # Construct graph in NetworkX.
    graph = from_numpy_matrix(adj)

    # Find approximate maximal independent set.
    mis = maximum_independent_set(graph)

    return mis
def main():
    # filename = sys.argv(1)
    g = ds.getDirectedData(
        '/home/ankursarda/Projects/graph-analytics/networkx/data/sample_data4.adj'
    )
    res = mis.maximum_independent_set(g)
    print(res)
def getCliqueData(gm):
    node_list = list(gm.nodes)
    num_rows = len(list(it.combinations(node_list, 2))) * 2

    longest_max_indep_set = len(independent_set.maximum_independent_set(gm))
    max_cliques = list(clique.clique_removal(gm)[1]) * num_rows
    num_max_cliques = len(max_cliques)
    longest_max_clique = len(max_cliques[0])

    clique_data = {
        "Longest Maximum Independent Set": [longest_max_indep_set] * num_rows,
        "Number of Maximum Cliques": [num_max_cliques] * num_rows,
        "Longest Maximum Clique": [longest_max_clique] * num_rows
    }

    return clique_data
Пример #5
0
def render_graph():
    global nodes_count
    global matrix
    i = 0
    g = nx.Graph()
    for node in range(nodes_count):
        g.add_node(node)
    for element in matrix:
        if element == 1 and i // nodes_count != i % nodes_count:
            g.add_edge(i // nodes_count, i % nodes_count)
        i += 1
    ker = list(maximum_independent_set(g))
    node_color = ["b" for i in range(len(g.nodes))]
    for i in range(len(node_color)):
        if i in ker:
            node_color[i] = "r"
    print(ker)
    nx.draw(g, with_labels=True, node_color=node_color)
    plt.show()
Пример #6
0
print("c) Colors:",
      max(nx.greedy_color(G).values()) + 1, ", Z =", nx.greedy_color(G))
print()

# d) Minimum edge coloring
print("d) Colors:",
      max(nx.greedy_color(nx.line_graph(G)).values()) + 1, ", E =",
      nx.greedy_color(nx.line_graph(G)))
print()

# e) Maximum clique
print("e) Q =", clique.max_clique(G))
print()

# f) Maximum stable set
print("f) S =", independent_set.maximum_independent_set(G))
print()

# g) Maximum matching
print("g) M =", nx.max_weight_matching(G))
print()

# h) Minimum vertex cover
print("h) R =", vertex_cover.min_weighted_vertex_cover(G))
print()

# i) Minimum edge cover
print("i) F =", covering.min_edge_cover(G))
print()

# j) Shortest closed path (circuit) that visits every vertex
Пример #7
0
for (v1, v2) in (g2.edges()):  #массив цветов ребер
    val = nx.greedy_color(nx.line_graph(g2)).get((v1, v2))
    if val == None:
        val = nx.greedy_color(nx.line_graph(g2)).get((v2, v1))
    edges_color.append(val)
nx.draw_planar(g2, with_labels=True, edge_color=edges_color)
plt.savefig("edges_color.png")
plt.clf()

#e
print("Maximum clique")
print(clique.max_clique(g2))

#f
print("Maximum stable set")
print(independent_set.maximum_independent_set(g2))

#g
print("Maximum matching")
print(nx.max_weight_matching(g2, maxcardinality=True, weight=0))

#i
print("Minimum edge cover")
print(covering.min_edge_cover(g2))  #ищем минимальное реберное покрытие

#j
print("Hanging vertices")
print([u for v, u in g3.edges() if len(g3[u]) == 1])  #узнаем висячие вершины
g3.remove_nodes_from([u for v, u in g3.edges()
                      if len(g3[u]) == 1])  #убираем висячие ребра
print("The shortest closed path that visits every vertex")
Пример #8
0
def prune_graph_max_independent_set(G):
    s = maximum_independent_set(G)
    return G.subgraph(G.nodes() - s)