Looking for algorithm finding euler path

后端 未结 5 748
终归单人心
终归单人心 2021-02-05 13:45

I\'m looking for an algorithm to find an Euler path in a graph.

I\'ve seen a good one a couple of weeks ago but I can\'t find it now, I remember there was tagging edges

相关标签:
5条回答
  • 2021-02-05 14:10

    Hierholzer's algorithm is a better way to find Euler path in a directed graph.

    http://stones333.blogspot.com/2013/11/find-eulerian-path-in-directed-graph.html

    It has the code plus test cases.

    0 讨论(0)
  • 2021-02-05 14:14

    From Graph-Magics.com, for an undirected graph, this will give you the tour in reverse order, i.e. from the end vertex to the start vertex:

    1. Start with an empty stack and an empty circuit (eulerian path).
      • If all vertices have even degree: choose any of them. This will be the current vertex.
      • If there are exactly 2 vertices having an odd degree: choose one of them. This will be the current vertex.
      • Otherwise no Euler circuit or path exists.

    Repeat step 2 until the current vertex has no more neighbors and the stack is empty.

    1. If current vertex has no neighbors:
      • Add it to circuit,
      • Remove the last vertex from the stack and set it as the current one.

    Otherwise:

    • Add the vertex to the stack,
    • Take any of its neighbors, remove the edge between selected neighbor and that vertex, and set that neighbor as the current vertex.
    0 讨论(0)
  • 2021-02-05 14:27

    Soo this is my solution , i've used finally block to print out because i get exception "stack is empty" and i don't really have time to fix that. I hope this helps someone!

    public void EulerTour()
        {
            GetInput(); //gets the adjency matrix
    
            int start = NodeList[0]; //start from any vertex, i choose 0
            tempPath.Push(NodeList[0]); //temporary path - stack
            try
            {
                while (tempPath.Count != 0) 
                {
                    for (int i = 0; i < total; i++)
                    {
                        if (adjencyMatrix[start, i] == 'd')
                        {
                            tempPath.Push(NodeList[i]);
                            adjencyMatrix[start, i] = 'n';
                            adjencyMatrix[i, start] = 'n';
    
                            if (!hasNeighbour((int)tempPath.Peek())) //checks if current node has any neighbour
                            {
                                while (!hasNeigbour((int)tempPath.Peek()))
                                {
                                    finalPath.Push(tempPath.Pop());
    
                                }
                                start = (int)tempPath.Peek();
                            }
                            else
                            {
                                start = i;
                                break;
                            }
                        }
                    }
                }
            }
            catch
            {
                Console.WriteLine("Print: ");
            }
            finally
            {
                foreach (object o in finalPath)
                {
                    Console.Write(o.ToString() + "---->");
                }
            }     
            Console.ReadKey();            
        }
    
    0 讨论(0)
  • 2021-02-05 14:30

    I don't have code in any language, but I know algorithm, how to find and I'll write it here.

    Suppose we have graph with n nodes. We call degree of a node number of edges connected to this node. At first we should say that sum degrees of each node would be even according to "handshake problem".

    Now suppose we have some Euler path p starting from node s and ending to node f. Then for each node which is different from st and t, each time we enter that node, we should leave(if we don't leave, then we won't get to final f node), so for these nodes degree would be even, and for s and f, if they are different degree would be odd, because we left s at first time, and then after each enter, we left s node, so there would be 1 + 2*n degree where n is the number of times we visited s again. Same goes for f. So there should be 2 nodes with odd degree if there exist Euler path. And if there is Euler circle, then degree of each node should be even, and if this is the case, then it doesn't matter which node we choose as s, we will end circle in s also.

    Now problem is how to get Euler circle/path. Here we need to define "bridge" in graph. Bridge is an edge with specific property, if we delete that edge, then in the remaining graph number of components would increase by one. In other words, bridge is an edge which is the only connecting edge for some two components in the graph.

    Since we know what is bridge, we can define algorithm. If there exist exactly 2 nodes with even degree: 1) we start from one of them and move forward to next nodes by choosing edges connected to current node. 2) if we should choose edge between bridge and nonbridge, we should always choose nonebridge. 3) if there is node nonebridge edge left, only then we can choose any bridge.

    If there is no node with odd degree, then we can start from any node and follow to those 3 rules.

    That's the whole algorithm which always works. here are some useful links also.

    https://www.math.ku.edu/~jmartin/courses/math105-F11/Lectures/chapter5-part2.pdf http://www.graph-magics.com/articles/euler.php

    0 讨论(0)
  • 2021-02-05 14:32

    An euler path exists if a graph has exactly two vertices with odd degree.These are in fact the end points of the euler path.

    So you can find a vertex with odd degree and start traversing the graph with DFS:As you move along have an visited array for edges.Don't traverse an edge twice.

    If you there is no edge left from a vertex, check if all edges have been visited, if so you are done.

    To store the actual euler path, you could keep a predecessor array, which stores previous vertex in the path.

    0 讨论(0)
提交回复
热议问题