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


Python Graph.clean方法代码示例

本文整理汇总了Python中Graph.clean方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.clean方法的具体用法?Python Graph.clean怎么用?Python Graph.clean使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Graph的用法示例。


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

示例1: __init__

# 需要导入模块: import Graph [as 别名]
# 或者: from Graph import clean [as 别名]
class Assembler:
    def __init__(self, filename, k, errorcorrect=False):

        #loads file
        reads = Loader.load(filename)
        #gets graph
        self.graph = Graph(reads, k, errorcorrect)
        self.k = k

    def make_superpath(self):
        """loops to make superpaths in graph"""
        # try without curls
        while self.superpath_helper():
            self.graph.clean()
        self.graph.clean()

    def superpath_helper(self):
        """Does actual merging"""
        for read in self.graph.readList:
            for i in range(len(read.edges)-1):
                #get preceding if possible
                if i > 0:
                    p = read.edges[i-1]
                else:
                    p = None
                #get x,y
                x = read.edges[i]
                y = read.edges[i+1]
                #check if edges can be merged
                if self.graph.is_mergeable(p,x,y):
                    #make sure merge worked
                    if self.graph.merge(x,y):
                        return True

        return False

    def is_eulerian(self):
        """Checks whether or not the graph is eulerian"""
        # count semi-balanced nodes (|indegree - outdegree| = 1)
        semis = 0
        for node in self.graph.nodeList:
            diff = abs(len(node.outgoing)-len(node.incoming))
            # if not balanced or semi-balanced, it is not euler
            if diff > 1:
                return False
            elif diff == 1:
                semis += 1
        # not eulerian if more than 2 semi-balanced nodes
        if semis > 2:
            return False

        return True

    def balance(self):
        """Connects semi-balanced nodes to eachother
            This function only works if the graph is eulerian!
            Returns False if failed. Returns True on success.
        """
        # find unbalanced nodes
        semis = []
        for node in self.graph.nodeList:
            diff = abs(len(node.outgoing)-len(node.incoming))
            if diff == 1:
                semis += [node]
        # can we do it?
        if len(semis) != 2:
            print("Too many unbalanced: %i. The graph is not eulerian.")%len(semis)
            return False
        # find balance
        if len(semis[0].incoming) > len(semis[0].outgoing): # e.g. needs an outgoing to balance
            # 0 -> 1
            self.graph.new_edge(semis[0], semis[1], semis[0].contents)
        else:
            # 1 -> 0
            self.graph.new_edge(semis[1], semis[0], semis[1].contents)
        # balance found
        return True


    def eulerian_path(self):
        """Constructs a eulerian
            path on the graph using
            Heirholzer's algorithm
        """
        # init
        currentPath = []
        finalPath = []
        # try to start on semi-balanced with less incoming
        edge = None
        for node in self.graph.nodeList:
            diff = abs(len(node.outEdges)-len(node.inEdges))
            if diff == 1 and len(node.inEdges) < len(node.outEdges):
                edge = self.graph.get_unvisited(node)
        # just pick first if failed
        if not edge: edge = self.graph.get_unvisited(self.graph.nodeList[0])
        # add all edges to stack in linear fashion
        while edge != None:
            edge.visited = True
            currentPath.append(edge)
            edge = self.graph.get_unvisited(edge.outNode) # next node/edge
#.........这里部分代码省略.........
开发者ID:tneely,项目名称:CS75Project,代码行数:103,代码来源:Assembler.py


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