当前位置: 首页>>代码示例>>Python>>正文


Python sorting.topological_sorting函数代码示例

本文整理汇总了Python中pygraph.algorithms.sorting.topological_sorting函数的典型用法代码示例。如果您正苦于以下问题:Python topological_sorting函数的具体用法?Python topological_sorting怎么用?Python topological_sorting使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了topological_sorting函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_topological_sort_on_very_deep_graph

 def test_topological_sort_on_very_deep_graph(self):
     gr = pygraph.classes.graph.graph()
     gr.add_nodes(list(range(0,20001)))
     for i in range(0,20000):
         gr.add_edge((i,i+1))
     recursionlimit = getrecursionlimit()
     topological_sorting(gr)
     assert getrecursionlimit() == recursionlimit
开发者ID:soeltjen,项目名称:python-graph,代码行数:8,代码来源:unittests-sorting.py

示例2: _invalidate_caches

	def _invalidate_caches(self):
		'invalidate the downstream caches of updated nodes'
		
		if len(self.updated) == 0:
			return
			
		# Sort the nodes in worklist and remove duplicates
		sg = topological_sorting(self.digraph) # sorted graph
		
		worklist = []
		# insert nodes into worklist in sorted order
		for node in sg:
			if node in self.updated:
				worklist.append(node)
		self.updated.clear()
		
		# iterate through worklist
		while worklist:
			node = worklist.pop() # one item at a time
			downstream = breadth_first_search(self.digraph, root=node)[1] # get all downstream labels
			for n in downstream:
				if n in worklist:
					# remove labels that will already be done
					worklist.remove(n)
				# remove cache entries
				self.cache[n] = None
开发者ID:Shootfast,项目名称:grafpy,代码行数:26,代码来源:dag.py

示例3: testDigraph

 def testDigraph(self):
     
     def has_parent(node, list):
         for each in list:
             if gr.has_edge(each, node):
                 return True
         return (ts == [])
         
     gr = pygraph.digraph()
     gr.add_nodes([0,1,2,3,4,5,6,7,8])
     gr.add_edge(0,1)
     gr.add_edge(0,2)
     gr.add_edge(1,3)
     gr.add_edge(1,4)
     gr.add_edge(2,5)
     gr.add_edge(2,6)
     gr.add_edge(3,7)
     gr.add_edge(8,0)
     gr.add_edge(7,5)
     gr.add_edge(3,0)
     gr.add_edge(4,3)
     gr.add_edge(2,7)
     gr.add_edge(6,0)
     ts = topological_sorting(gr)
     while (ts):
         x = ts.pop()
         assert has_parent(x, ts)
开发者ID:svn2github,项目名称:python-graph2,代码行数:27,代码来源:unittests-sorting.py

示例4: sort_nodes_topologically

def sort_nodes_topologically(graph, nodeLs):
    """
    Get a topological sort of a subset of the nodes of a graph
    
    @type  graph: graph_wrapper.GraphWrapper
    @param graph: a graph in which the nodes reside

    @type  nodeLs: list [node]
    @param nodeLs: a list of nodes from which to generate sorting. nodes must not be mutually accessive!
    
    @rtype:  list [node]
    @return: topological sort of the nodes
    """
    # uid_dic = dict([(node.uid,node) for node in nodeLs])
    # helperNodes = uid_dic.keys()
    helperGraph = graph.__class__(originalSentence="")  # TODO: efficiency - this is done this way to avoid circular dependency
    helperGraph.add_nodes(nodeLs)
    acc = accessibility(graph)
    
    for node1 in nodeLs:
            for node2 in acc[node1]:
                if node2 in nodeLs:
                    if node1.uid != node2.uid:  # TODO: efficiency 
                        helperGraph.add_edge((node1, node2))
    
    sorted_nodes = topological_sorting(helperGraph)
    return sorted_nodes
开发者ID:arueckle,项目名称:props,代码行数:27,代码来源:graph_utils.py

示例5: resolve_plugin_dependencies

    def resolve_plugin_dependencies(self):
        graph = digraph()
        problems = defaultdict(list)

        def check_plugin_dependencies(plugin_id):
            result = True

            def add_problem(problem_type, plugin_id, dependency):
                problems[plugin_id].append(problem_type(plugin_id, dependency))
                result = False

            for dependency in self.plugin_dependencies(plugin_id):
                if dependency.id not in self.manifests:
                    add_problem(MissingDependency, plugin_id, dependency)
                elif dependency.version:
                    if manifests[required_id].version not in dependency.version:
                        add_problem(IncorrectVersion, plugin_id, dependency)
                elif dependency.id not in graph:
                    if dependency.id in self.enabled_plugins:
                        add_problem(IndirectDependency, plugin_id, dependency)
                    else:
                        add_problem(DisabledDependency, plugin_id, dependency)

            return result

        def remove_dependents(plugin_id):
            for node in traversal(graph, plugin_id, 'pre'):
                for dependent in graph[node]:
                    edge = node, dependent
                    problems[dependent].append(IndirectDependency(dependent, 
                        graph.get_edge_properties(edge)['dependency']))
                graph.del_node(node)

        graph.add_nodes(self.enabled_plugins)
        for plugin_id in self.enabled_plugins:
            if check_plugin_dependencies(plugin_id):
                for dependency in self.plugin_dependencies(plugin_id):
                    edge = dependency.id, plugin_id
                    graph.add_edge(edge)
                    graph.set_edge_properties(edge, dependency=dependency)
            else:
                remove_dependents(plugin_id)

        transitive_deps = accessibility(graph)
        cycle_nodes = [
            node
            for node in graph
            if any(
                (node in transitive_deps[dependent])
                for dependent in transitive_deps[node]
                if dependent != node)]
        for node in cycle_nodes:
            problems[node].append(CyclicDependency(node))
            graph.del_node(node)

        self.dependency_graph = graph
        self._dependency_problems = problems
        self._load_order = topological_sorting(graph)
开发者ID:vstojkovic,项目名称:berk,代码行数:58,代码来源:plugins.py

示例6: allOff

 def allOff(self):
     '''Turn all servers off ungracefully
     '''
     nodeList = topological_sorting(self.graph)
     for node in nodeList:
         server = self.graph.node_attributes(node)
         for outlet in server.getOutlets():
             outlet.setState(False)
     return
开发者ID:Keeper-of-the-Keys,项目名称:Ockle,代码行数:9,代码来源:ServerNetwork.py

示例7: evaluate_dag

	def evaluate_dag(self):
		'Force every node to be evaluated'
		# First invalidate all changed caches
		self._invalidate_caches()
		
		# Then call update on every node
		sg = topological_sorting(self.digraph) # sorted graph
		for label in sg:
			self._update_node(label)
开发者ID:Shootfast,项目名称:grafpy,代码行数:9,代码来源:dag.py

示例8: getSortedNodeList

 def getSortedNodeList(self):
     '''
     returns a list of the nodes topologically sorted
     
     :return: a list of the nodes topologically sorted
     '''
     nodeList = topological_sorting(self.graph)
     servers=[]
     for node in nodeList:
         servers.append(self.graph.node_attributes(node))
     return servers
开发者ID:Keeper-of-the-Keys,项目名称:Ockle,代码行数:11,代码来源:ServerNetwork.py

示例9: order_seq_only

def order_seq_only(graph):
    """
    A topological sort is performed on the graph. All actions are
    enclosed into a Sequence ISE structure.
    """
    _prepare(graph)
    nodes = topological_sorting(graph.reverse())
    actions = []
    for node in nodes:
        actions.extend(_create_actions_from(node, graph, create_deps=False))

    return _create_final_xml_from(actions, SEQ)
开发者ID:pv-bull,项目名称:sequencer,代码行数:12,代码来源:algo.py

示例10: process

 def process(self):
     """ Process image processing flow for IPFGraph """
     
     # Invalidate all input ports in __blocks
     ((iport.invalidate() for iport in block.input_ports.values()) 
                          for block in self.__blocks.values())
     
     graph = self._make_flow_graph()
     
     # Apply topological sorting and execute processing blocks in
     # topological order 
     sorted_graph = topological_sorting(graph)
     for node in sorted_graph:
         node().process()
开发者ID:anton-golubkov,项目名称:Garland,代码行数:14,代码来源:ipfgraph.py

示例11: topo_up_down

 def topo_up_down(graph):
     """
     Yield the nodes of the graph, starting with the leaf-most
     (latest topological) node, running up to the root-most
     (earliest topological) node, and pushing back down to the leaves,
     excepting the leaf-most node.
     
     Undefined behavior if the graph is not a DAG.
     
     graph: A->B->C
     yields: (C, B, A, B)
     """
     tsort = topological_sorting(graph)
     for node in reversed(tsort):
         yield node
     for node in tsort[1 : -1]:
         yield node
开发者ID:Jay-Zen,项目名称:probabilistic-graphical-models,代码行数:17,代码来源:pgm.py

示例12: topo_sort_work

    def topo_sort_work(self):
        my_work = self.work

        work_dict = {}
        for w in my_work:
            work_dict[w.work_id] = w

        graph = digraph()
        graph.add_nodes(my_work)

        for w in my_work:
            for p in w.prereqs:
                if not work_dict.has_key(p):
                    continue
                if work_dict[p]:
                    graph.add_edge((work_dict[p], w))
        self.work = topological_sorting(graph)
        return
开发者ID:rjose,项目名称:dovetail,代码行数:18,代码来源:project.py

示例13: test_topological_sorting_on_tree

    def test_topological_sorting_on_tree(self):
        gr = testlib.new_graph()
        st, pre, post = depth_first_search(gr)
        tree = pygraph.classes.digraph.digraph()

        
        for each in st:
            if st[each]:
                if (each not in tree.nodes()):
                    tree.add_node(each)
                if (st[each] not in tree.nodes()):
                    tree.add_node(st[each])
                tree.add_edge((st[each], each))
        
        ts = topological_sorting(tree)
        for each in ts:
            if (st[each]):
                assert ts.index(each) > ts.index(st[each])
开发者ID:soeltjen,项目名称:python-graph,代码行数:18,代码来源:unittests-sorting.py

示例14: find_connected_resources

def find_connected_resources(resource, dependency_graph=None):
    """
    Collects all resources connected to the given resource and returns a
    dictionary mapping member resource classes to new collections containing
    the members found.
    """
    # Build a resource_graph.
    resource_graph = \
                build_resource_graph(resource,
                                     dependency_graph=dependency_graph)
    entity_map = OrderedDict()
    for mb in topological_sorting(resource_graph):
        mb_cls = get_member_class(mb)
        ents = entity_map.get(mb_cls)
        if ents is None:
            ents = []
            entity_map[mb_cls] = ents
        ents.append(mb.get_entity())
    return entity_map
开发者ID:b8va,项目名称:everest,代码行数:19,代码来源:storing.py

示例15: test_topological_sorting_on_digraph

 def test_topological_sorting_on_digraph(self):
     
     def is_ordered(node, list):
         # Has parent on list
         for each in list:
             if gr.has_edge((each, node)):
                 return True
         # Has no possible ancestors on list
         st, pre, post = depth_first_search(gr, node)
         for each in list:
             if (each in st):
                 return False
         return True
         
     gr = testlib.new_digraph()
     ts = topological_sorting(gr)
     
     while (ts):
         x = ts.pop()
         assert is_ordered(x, ts)
开发者ID:soeltjen,项目名称:python-graph,代码行数:20,代码来源:unittests-sorting.py


注:本文中的pygraph.algorithms.sorting.topological_sorting函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。