0

I need to cover all the edges of a graph with the minimum number of paths. I read that the Eulerian path method is needed here. I tried to reproduce it, but it doesn't work correctly.

def split_graph(graph, n):
    stack = []
    path = []
    some_list = []

    for i in range(n):
        some_list .append(sum(graph[i]))

    cur = 0
    while (len(stack) > 0 or sum(graph[cur]) != 0):

        if (sum(graph[cur]) == 0):
            path.append(cur+1)
            cur = stack[-1]
            del stack[-1]

        else:
            for i in range(n):
                if (graph[cur][i] == 1):
                    stack.append(cur)
                    graph[cur][i] = 0
                    graph[i][cur] = 0
                    cur = i
    print(path)

if __name__ == '__main__':
    graph = [[0, 1, 0, 1, 0, 0],
              [1, 0, 1, 1, 0, 0],
              [0, 1, 0, 0, 0, 0],
              [1, 1, 0, 0, 0, 1],
              [0, 0, 0, 0, 0, 1],
              [0, 0, 0, 1, 1, 0]]
    n = len(graph)
    split_graph(graph, n)

Required output (There may be several options, this is one of them):

[6, 7, 4, 1, 2, 3]
[2,4]
[7, 5]

My output:

[3, 5, 6, 1, 4, 2]

How can this be fixed?

Lib
  • 29
  • 5
  • Welcome to Stack Overflow. We don't provide debugging services. The following references give advice on debugging your code. [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/), [Six Debugging Techniques for Python Programmers](https://medium.com/techtofreedom/six-debugging-techniques-for-python-programmers-cb25a4baaf4b) or [Ultimate Guide to Python Debugging](https://towardsdatascience.com/ultimate-guide-to-python-debugging-854dea731e1b) – itprorh66 Nov 12 '22 at 20:13
  • @itprorh66, Here the question is rather that I incorrectly used the algorithm itself (or maybe it shouldn’t be used here at all). Debugging won't help me here. – Lib Nov 13 '22 at 11:47
  • stating "but it doesn't work correctly." implies that there is a problem with the current code, thus a debugging issue. Questions about which algorithm is correct or best for a particular problem leads to opinion based answers which are not appropriate for stackoverflow. – itprorh66 Nov 13 '22 at 15:42

0 Answers0