def remove_cycle_edges_by_mfas(graph_file,nodetype = int):
	g = nx.read_edgelist(graph_file,create_using = nx.DiGraph(),nodetype = nodetype)
	scc_nodes,_,_,_ = scc_nodes_edges(g)
	degree_dict = get_nodes_degree_dict(g,scc_nodes)
	sccs = get_big_sccs(g)
	edges_to_be_removed = []
	from multiprocessing import Process, Queue
	import timeit
	t1 = timeit.default_timer()
	jobs = []
	q = Queue()
	for scc in sccs:
		p = Process(target = greedy_local_heuristic, args = ([scc],degree_dict,q))
		jobs.append(p)
		p.start()
	for p in jobs:
		p.join()
		edges_to_be_removed += list(q.get())
	t2 = timeit.default_timer()
	print("mfas time usage: %0.4f s" % (t2 - t1))
	#greedy_local_heuristic(sccs,degree_dict,edges_to_be_removed)
	edges_to_be_removed = list(set(edges_to_be_removed))
	g.remove_edges_from(edges_to_be_removed)
	edges_to_be_removed_file = graph_file[:len(graph_file)-6] + "_removed_by_mfas.edges"
	write_pairs_to_file(edges_to_be_removed,edges_to_be_removed_file)
	return edges_to_be_removed
def introduce_cycles_2_DAG(graph_file, num_extra_edges, path_length):

    if path_length <= 0:
        print("no constraints on path length")
    else:
        print("path length: %d" % path_length)

    g = nx.read_edgelist(graph_file, create_using=nx.DiGraph(), nodetype=int)
    extra_edges = introduce_cycles(g, num_extra_edges, path_length=path_length)

    extra_edges_file = graph_file[:len(graph_file) - 6] + "_extra_" + str(
        num_extra_edges) + "_path_len_" + str(path_length) + ".edges"
    graph_with_extra_edges_file = graph_file[:len(
        graph_file) - 6] + "_graph_w_extra_" + str(
            num_extra_edges) + "_path_len_" + str(path_length) + ".edges"

    print("extra edges saved in: %s" % extra_edges_file)
    print("graph with extra edges saved in: %s" % graph_with_extra_edges_file)
    from file_io import write_pairs_to_file

    write_pairs_to_file(extra_edges, extra_edges_file)
    write_pairs_to_file(extra_edges + list(g.edges()),
                        graph_with_extra_edges_file)

    return (extra_edges_file, graph_with_extra_edges_file)
示例#3
0
def dfs_remove_back_edges(graph_file):
    '''
	0: white, not visited 
	1: grey, being visited
	2: black, already visited
	'''

    g = nx.read_edgelist(graph_file, create_using=nx.DiGraph(), nodetype=int)
    nodes_color = {}
    edges_to_be_removed = []
    for node in g.nodes_iter():
        nodes_color[node] = 0

    nodes_order = list(g.nodes_iter())
    nodes_order = np.random.permutation(nodes_order)
    num_dfs = 0
    for node in nodes_order:

        if nodes_color[node] == 0:
            num_dfs += 1
            dfs_visit_recursively(g, node, nodes_color, edges_to_be_removed)

    #print("number of nodes to start dfs: %d" % num_dfs)
    #print("number of back edges: %d" % len(edges_to_be_removed))
    edges_to_be_removed_file = graph_file[:len(graph_file) -
                                          6] + "_removed_by_dfs.edges"
    print("edges to be removed, saved in file: %s" % edges_to_be_removed_file)
    from file_io import write_pairs_to_file
    write_pairs_to_file(edges_to_be_removed, edges_to_be_removed_file)
    return edges_to_be_removed
示例#4
0
def remove_cycle_edges_by_mfas(graph_file):
    g = nx.read_edgelist(graph_file, create_using=nx.DiGraph(), nodetype=int)
    scc_nodes, _, _, _ = scc_nodes_edges(g)
    degree_dict = get_nodes_degree_dict(g, scc_nodes)
    sccs = get_big_sccs(g)
    edges_to_be_removed = []
    import timeit
    t1 = timeit.default_timer()
    greedy_local_heuristic(sccs, degree_dict, edges_to_be_removed)
    t2 = timeit.default_timer()
    print("mfas time usage: %0.4f s" % (t2 - t1))
    edges_to_be_removed = list(set(edges_to_be_removed))
    g.remove_edges_from(edges_to_be_removed)
    edges_to_be_removed_file = graph_file[:len(graph_file) -
                                          6] + "_removed_by_mfas.edges"
    write_pairs_to_file(edges_to_be_removed, edges_to_be_removed_file)
    return edges_to_be_removed
def remove_cycle_edges_by_mfas(graph_file):
	g = nx.read_edgelist(graph_file,create_using = nx.DiGraph(),nodetype = int)
	from remove_self_loops import remove_self_loops_from_graph
	self_loops = remove_self_loops_from_graph(g)

	scc_nodes,_,_,_ = scc_nodes_edges(g)
	degree_dict = get_nodes_degree_dict(g,scc_nodes)
	sccs = get_big_sccs(g)
	if len(sccs) == 0:
		print("After removal of self loop edgs: %s" % nx.is_directed_acyclic_graph(g))
		return self_loops
	edges_to_be_removed = []
	import timeit
	t1 = timeit.default_timer()
	greedy_local_heuristic(sccs,degree_dict,edges_to_be_removed)
	t2 = timeit.default_timer()
	print("mfas time usage: %0.4f s" % (t2 - t1))
	edges_to_be_removed = list(set(edges_to_be_removed))
	g.remove_edges_from(edges_to_be_removed)
	edges_to_be_removed += self_loops
	edges_to_be_removed_file = graph_file[:len(graph_file)-6] + "_removed_by_mfas.edges"
	write_pairs_to_file(edges_to_be_removed,edges_to_be_removed_file)
	return edges_to_be_removed
示例#6
0
def breaking_cycles_by_hierarchy_performance(graph_file,
                                             gt_file,
                                             players_score_name,
                                             nodetype=int):

    from measures import report_performance
    if players_score_name != "ensembling":
        players_score_dict = computing_hierarchy(graph_file,
                                                 players_score_name,
                                                 nodetype=nodetype)
        e1, e2, e3, e4 = remove_cycle_edges_by_hierarchy(graph_file,
                                                         players_score_dict,
                                                         players_score_name,
                                                         nodetype=nodetype)

        if players_score_name == "pagerank":
            report_performance(gt_file, e1, "PR")
            return

        if players_score_name == "socialagony":
            note = "SA_"
        elif players_score_name == "trueskill":
            note = "TS_"

        report_performance(gt_file, e1, note + "G")
        report_performance(gt_file, e2, note + "F")
        report_performance(gt_file, e3, note + "B")
        report_performance(gt_file, e4, note + "Voting")
    else:
        players_score_dict = computing_hierarchy(graph_file,
                                                 "socialagony",
                                                 nodetype=nodetype)
        e1, e2, e3, e4 = remove_cycle_edges_by_hierarchy(graph_file,
                                                         players_score_dict,
                                                         "socialagony",
                                                         nodetype=nodetype)
        report_performance(gt_file, e1, "SA_G")
        write_pairs_to_file(
            e1, graph_file[:len(graph_file) - 6] + "_removed_by_SA-G.edges")
        report_performance(gt_file, e2, "SA_F")
        write_pairs_to_file(
            e2, graph_file[:len(graph_file) - 6] + "_removed_by_SA-F.edges")
        report_performance(gt_file, e3, "SA_B")
        write_pairs_to_file(
            e3, graph_file[:len(graph_file) - 6] + "_removed_by_SA-B.edges")
        report_performance(gt_file, e4, "SA_Voting")
        write_pairs_to_file(
            e4,
            graph_file[:len(graph_file) - 6] + "_removed_by_SA-Voting.edges")

        players_score_dict = computing_hierarchy(graph_file,
                                                 "trueskill",
                                                 nodetype=nodetype)
        e5, e6, e7, e8 = remove_cycle_edges_by_hierarchy(graph_file,
                                                         players_score_dict,
                                                         "trueskill",
                                                         nodetype=nodetype)
        report_performance(gt_file, e5, "TS_G")
        write_pairs_to_file(
            e5, graph_file[:len(graph_file) - 6] + "_removed_by_TS-G.edges")
        report_performance(gt_file, e6, "TS_F")
        write_pairs_to_file(
            e6, graph_file[:len(graph_file) - 6] + "_removed_by_TS-F.edges")
        report_performance(gt_file, e7, "TS_B")
        write_pairs_to_file(
            e7, graph_file[:len(graph_file) - 6] + "_removed_by_TS-B.edges")
        report_performance(gt_file, e8, "TS_Voting")
        write_pairs_to_file(
            e7,
            graph_file[:len(graph_file) - 6] + "_removed_by_TS-Voting.edges")

        e9 = remove_cycle_edges_by_voting(
            graph_file,
            [set(e1), set(e2),
             set(e3), set(e5),
             set(e6), set(e7)],
            nodetype=nodetype)
        report_performance(gt_file, e9, "H_Voting")
        write_pairs_to_file(
            e9,
            graph_file[:len(graph_file) - 6] + "_removed_by_H-Voting.edges")
示例#7
0
        help=
        "thresold d to control path length (<=0: no constraints on path length, otherwise less than this threshold)"
    )

    args = parser.parse_args()

    n = args.num_nodes
    m = args.num_edges
    k = args.num_extra_edges
    l = args.path_length

    if not os.path.exists(args.dir):
        os.makedirs(args.dir)

    graph_file = args.dir + "gnm_" + str(n) + "_" + str(m) + ".edges"

    # generate random DAG
    from generate_random_dag import gnm_random_graph
    g = gnm_random_graph(n, m)
    from file_io import write_pairs_to_file
    write_pairs_to_file(g.edges(), graph_file)

    # introduce cycles to this DAG
    from introduce_cycles_to_DAG import introduce_cycles_2_DAG
    extra_edges_file, graph_with_extra_edges_file = introduce_cycles_2_DAG(
        graph_file, k, l)

    # remove cycle edges
    from break_cycles import break_cycles
    break_cycles(graph_with_extra_edges_file, extra_edges_file)