Exemple #1
0
def greedy_algorithm(G):
    CG = construct_cluster_graph(G)
    CG_copy = CG.copy()
    print "done constructing"
    valid_nodes = CG.nodes()
    selected_clusters = []
    pre_penalty = 0
    while len(valid_nodes) != 0:
        # node_selected = find_max_penalty(valid_nodes, CG)
        node_selected = find_min_weighted_degree(valid_nodes, CG)
        pre_penalty += CG.node[node_selected]['penalty']
        selected_clusters.append(node_selected)
        list_edges = CG.edges(node_selected, False)
        nodes_to_remove = set()
        nodes_to_remove.add(node_selected)
        for edge in list_edges:
            nodes_to_remove.add(edge[0])
            nodes_to_remove.add(edge[1])
        for node in nodes_to_remove:
            valid_nodes.remove(node)
            CG.remove_node(node)
    list_cycles = []
    for cluster in selected_clusters:
        list_cycles.append(CG_copy.node[cluster]['nodes'])
    penalty = find_total_penalty(G) - pre_penalty
    output_string = format_output_cycles(list_cycles)
    print(find_total_penalty(G), pre_penalty)
    return [output_string, penalty]
Exemple #2
0
def algorithm(graph):
    CG = construct_cluster_graph(graph)
    valid_nodes = CG.nodes()
    adults = []
    start = valid_nodes.pop()
    selected_nodes = [start]
    pre_penalty = 0
    cycles = []
    current_cycle = []
    while selected_nodes != []:
        node = selected_nodes.pop(0)
        edges = CG.edges(node, False)
        if node in current_cycle:
            cycles.append(current_cycle)
            for n in current_cycle:
                pre_penalty += n['penalty']
                valid_nodes.remove(n)
            current_cycle = []
        current_cycle.append(node)
        for inner, outer in edges:
            neighbor = CG.node[outer]
            child_cycles = []
            if neighbor['penalty'] == 2:
                child_cycles.append(neighbor)
                selected_nodes.append(neighbor)
            else:
                adults.append(neighbor)
        if child_cycles == []:
            for adult in adults:
                selected_nodes.append(adult)
        child_cycles = []
        adults = []
    penalty = find_total_penalty(G) - pre_penalty
    output_string = format_output_cycles(cycles)
    return [output_string, penalty]
Exemple #3
0
def algorithm(graph):
    CG = construct_cluster_graph(graph)
    valid_nodes = CG.nodes()
    adults = []
    start = valid_nodes.pop()
    selected_nodes = [start]
    pre_penalty = 0
    cycles = []
    current_cycle = []
    while selected_nodes != []:
        node = selected_nodes.pop(0)
        edges = CG.edges(node, False)
        if node in current_cycle:
            cycles.append(current_cycle)
            for n in current_cycle:
                pre_penalty += n['penalty']
                valid_nodes.remove(n)
            current_cycle = []
        current_cycle.append(node)
        for inner, outer in edges:
            neighbor = CG.node[outer]
            child_cycles = []
            if neighbor['penalty'] == 2:
                child_cycles.append(neighbor)
                selected_nodes.append(neighbor)
            else:
                adults.append(neighbor)
        if child_cycles == []:
            for adult in adults:
                selected_nodes.append(adult)
        child_cycles = []
        adults = []
    penalty = find_total_penalty(G) - pre_penalty
    output_string = format_output_cycles(cycles)
    return [output_string, penalty]
Exemple #4
0
def greedy_algorithm(G):
	CG = construct_cluster_graph(G)
	CG_copy = CG.copy()
	print "done constructing"
	valid_nodes = CG.nodes()
	selected_clusters = []
	pre_penalty = 0
	# cg_penalty = 0
	while len(valid_nodes) != 0:
		# node_selected = find_max_penalty(valid_nodes, CG)
		node_selected = find_min_weighted_degree(valid_nodes, CG)
		# node_selected = find_min_degree(valid_nodes, CG)
		# node_selected = find_random(valid_nodes, CG)
		pre_penalty += CG_copy.node[node_selected]['penalty']
		selected_clusters.append(node_selected)
		list_edges = CG.edges(node_selected, False)
		nodes_to_remove = set()
		nodes_to_remove.add(node_selected)
		# print len(list_edges)
		for edge in list_edges:
			assert (edge[0] == node_selected)
			nodes_to_remove.add(edge[1])
		for node in nodes_to_remove:
			valid_nodes.remove(node)
			# cg_penalty += CG_copy.node[node]['penalty']
			CG.remove_node(node)
	list_cycles = []
	for cluster in selected_clusters:
		list_cycles.append(CG_copy.node[cluster]['nodes'])
	penalty = find_total_penalty(G) - pre_penalty
	output_string = format_output_cycles(list_cycles)
	# print(find_total_penalty(G), pre_penalty, cg_penalty, penalty)
	print [output_string, penalty]
	return [output_string, penalty]
Exemple #5
0
def random_algorithm(input_graph):
    G = input_graph.copy()
    cycle_list = []
    pre_penalty = 0
    while G.nodes():
        try:
            edge_cycle = nx.find_cycle(G)
            vertex_cycle = []
            for edge in edge_cycle:
                vertex_cycle.append(edge[0])
                pre_penalty += G.node[edge[0]]['penalty']
                G.remove_node(edge[0])
            cycle_list.append(vertex_cycle)
        except:
            print("No cycle found")
            break
    penalty = find_total_penalty(input_graph) - pre_penalty
    formatted_cycle_list = format_output_cycles(cycle_list)
    return [formatted_cycle_list, penalty]
Exemple #6
0
def random_algorithm(input_graph):
	G = input_graph.copy()
	cycle_list = []
	pre_penalty = 0
	while G.nodes():
		try:
			edge_cycle = nx.find_cycle(G)
			vertex_cycle = []
			for edge in edge_cycle:
				vertex_cycle.append(edge[0])
				pre_penalty += G.node[edge[0]]['penalty']
				G.remove_node(edge[0])
			cycle_list.append(vertex_cycle)
		except:
			print("No cycle found")
			break	
	penalty = find_total_penalty(input_graph) - pre_penalty
	formatted_cycle_list = format_output_cycles(cycle_list)
	return [formatted_cycle_list, penalty]
def random_algorithm_3(input_graph):
    G = input_graph.copy()
    cycle_list = []
    pre_penalty = 0
    random_result = build_randomized_graph(input_graph)
    randG = random_result[0]
    nodes_dict = random_result[1]
    while randG.nodes():
        source_node = randG.nodes()[0]
        cycle = find_cycle(randG, source_node)
        if cycle == []:
            randG.remove_node(source_node)
        else:
            real_cycle = []
            for node in cycle:
                real_cycle.append(nodes_dict[node])
            cycle_list.append(real_cycle)
            for node in cycle:
                real_node = nodes_dict[node]
                pre_penalty += G.node[real_node]['penalty']
                randG.remove_node(node)
    penalty = find_total_penalty(input_graph) - pre_penalty
    formatted_cycle_list = format_output_cycles(cycle_list)
    return [formatted_cycle_list, penalty]
def random_algorithm_4(input_graph):
	G = input_graph.copy()
	cycle_list = []
	pre_penalty = 0
	random_result = build_randomized_graph(input_graph)
	randG = random_result[0]
	nodes_dict = random_result[1]
	while randG.nodes():
		source_node = randG.nodes()[0]
		cycle = find_random_length_cycle(randG, source_node)
		if cycle == []:
			randG.remove_node(source_node)
		else:
			real_cycle = []
			for node in cycle:
				real_cycle.append(nodes_dict[node])
			cycle_list.append(real_cycle)
			for node in cycle:
				real_node = nodes_dict[node]
				pre_penalty += G.node[real_node]['penalty']
				randG.remove_node(node)
	penalty = find_total_penalty(input_graph) - pre_penalty
	formatted_cycle_list = format_output_cycles(cycle_list)
	return [formatted_cycle_list, penalty]
Exemple #9
0
def test_format_output_cycles():
	output_list = [[0,1,2], [3,4,5], [6,7,8]]
	print(format_output_cycles(output_list))
Exemple #10
0
def test_format_output_cycles():
    output_list = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
    print(format_output_cycles(output_list))