【问题标题】:Python: euler circuit and euler pathPython:欧拉电路和欧拉路径
【发布时间】:2018-10-21 22:29:58
【问题描述】:

我在 Python 中有这段代码。用户写入图的邻接列表并获取图是否具有欧拉回路、欧拉路径或不是欧拉的信息。一切都很好,直到我最后写了这个: ln1 = [1, 2, 1, 6, 2, 3, 3, 4, 4, 5, 5, 6] 输出应该是:图有一个欧拉电路。 你能更正这段代码吗?不知道是什么问题

# Python program to check if a given graph is Eulerian or not
# Complexity : O(V+E)

from collections import defaultdict


# This class represents a undirected graph using adjacency list 
representation
class Graph:

    def __init__(self, vertices):
        self.V = vertices  # No. of vertices
        self.graph = defaultdict(list)  # default dictionary to store graph

    # function to add an edge to graph
    def addEdge(self, u, v):
        self.graph[u].append(v)
        self.graph[v].append(u)

    # A function used by isConnected
    def DFSUtil(self, v, visited):
        # Mark the current node as visited
        visited[v] = True

        # Recur for all the vertices adjacent to this vertex
        for i in self.graph[v]:
            if visited[i] == False:
                self.DFSUtil(i, visited)

    '''Method to check if all non-zero degree vertices are
    connected. It mainly does DFS traversal starting from 
    node with non-zero degree'''

    def isConnected(self):

        # Mark all the vertices as not visited
        visited = [False] * (self.V)

        #  Find a vertex with non-zero degree
        for i in range(self.V):
            if len(self.graph[i]) > 1:
                break

        # If there are no edges in the graph, return true
        if i == self.V - 1:
            return True

        # Start DFS traversal from a vertex with non-zero degree
        self.DFSUtil(i, visited)

        # Check if all non-zero degree vertices are visited
        for i in range(self.V):
            if visited[i] == False and len(self.graph[i]) > 0:
                return False

        return True

    '''The function returns one of the following values
       0 --> If grpah is not Eulerian
       1 --> If graph has an Euler path (Semi-Eulerian)
       2 --> If graph has an Euler Circuit (Eulerian)  '''

    def isEulerian(self):
        # Check if all non-zero degree vertices are connected
        if self.isConnected() == False:
            return 0
        else:
            # Count vertices with odd degree
            odd = 0
            for i in range(self.V):
                if len(self.graph[i]) % 2 != 0:
                    odd += 1

            '''If odd count is 2, then semi-eulerian.
            If odd count is 0, then eulerian
            If count is more than 2, then graph is not Eulerian
            Note that odd count can never be 1 for undirected graph'''
            if odd == 0:
                return 2
            elif odd == 2:
                return 1
            elif odd > 2:
                return 0

    # Function to run test cases
    def test(self):
        res = self.isEulerian()
        if res == 0:
            print "graph is not Eulerian"
        elif res == 1:
            print "graph has a Euler path"
        else:
            print "graph has a Euler cycle"





ln1 = [1, 2, 1, 6, 2, 3, 3, 4, 4, 5, 5, 6]
#ln1 = [0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 1, 2, 1, 4, 1, 5, 2, 3, 2, 4, 3, 5, 4, 5] #euler path
#ln1 = [1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 2, 3, 2, 5, 2, 6, 3, 4, 3, 5, 4, 6, 5, 6] #euler path
g1 = Graph(len(ln1)/2)
i = 0
j = 1
while i + 2 <= len(ln1) and j + 1 <= len(ln1):
    g1.addEdge(ln1[i], ln1[j])
    i += 2
    j += 2
g1.test()

【问题讨论】:

标签: python graph path euler-angles circuit


【解决方案1】:

您是否尝试将visited = [False] * (self.V) 替换为visited = [False] * int(self.V)

由于此问题不仅发生在这一行,您可能应该使用以下命令初始化您的类:

self.V = int(vertices)  # No. of vertices

恕我直言,顶点的数量无论如何都应该是整数。

但似乎还有很多问题。例如,在 if 子句之后的 isConnected 中有一个 return 语句,该子句取决于 i。但是i之前是在循环中使用的。所以这应该是有效的,如果它真的只取决于最后一个索引(在这种情况下似乎没问题)。

此外,您不会将更改保存到visited,因为它是一个方法变量而不是实例变量。所以visited 将保持设置为False

下一个问题:在DFSUtil

# Recur for all the vertices adjacent to this vertex
for i in self.graph[v]:
    if visited[i] == False:
        self.DFSUtil(i, visited)

self.graph[v]v=5 返回一个例如[0, 6] 的列表。但是索引 6 超出了长度为 6 的 visited 的范围。

【讨论】:

  • 我已尝试替换这些行,但没有任何反应。
  • 我不太明白你所说的“isConnected”和“visited”是什么意思。
【解决方案2】:

我修改了DFSUtil循环,把v改成了v+1

for i in self.graph[v+1]:
      if visited[i] == False:
      self.DFSUtil(i, visited)

现在程序启动了,但在输出中它说:“Graph is not Eulerian”。它当然应该说“图形具有欧拉电路”。

【讨论】:

  • 您应该逐步完成您的程序。如果我能看到对我迄今为止所做的任何事情的赞赏,例如赞成或接受我的回答,我总是愿意提供帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-17
  • 1970-01-01
  • 2017-08-27
  • 2022-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多