示例#1
0
    def maximum_acyclic_subgraph_helper_recurse(self, graph):
        '''
        Graph is an adjacency matrix, and is continuously changed.
        Calls make_cut to determine the cut, the computes the edges over each
        cut (from A to B and B to A, in the manner described above), removing
        edges in the graph and calling the helper function recursively on the
        new graphs.
        '''
        linearizing_agent = DAGSolver(graph)
        topo_sort = linearizing_agent.topological_sort()

        while topo_sort is None:
            cut_one, cut_two = self.make_cut(graph)
            edges_one_to_two = self.compute_edges_over_cut(graph, cut_one)
            edges_two_to_one = self.compute_edges_over_cut(graph, cut_two)

            if len(edges_one_to_two) >= len(edges_two_to_one):
                for vertex_one, vertex_two in edges_two_to_one:
                    graph[vertex_one][vertex_two] = 0
            else:
                for vertex_one, vertex_two in edges_one_to_two:
                    graph[vertex_one][vertex_two] = 0

            linearizing_agent = DAGSolver(graph)
            topo_sort = linearizing_agent.topological_sort()

        return topo_sort
示例#2
0
    def obtain_library_solution(self, scc_adj_matrix, force_ip=False, force_eades=False):
        num_vertices = len(scc_adj_matrix)
        library_graph = Graph().Adjacency(scc_adj_matrix)

        if not force_eades and (num_vertices < 10 or force_ip):
            removed_edges = library_graph.feedback_arc_set(method='ip')
        else:
            removed_edges = library_graph.feedback_arc_set(method='eades')

        library_graph.delete_edges(removed_edges)
        library_graph_adj_matrix = library_graph.get_adjacency()._get_data()
        library_graph_dag_solver = DAGSolver(library_graph_adj_matrix)
        solution = library_graph_dag_solver.topological_sort()

        return solution