Looking for algorithm finding euler path

后端 未结 5 750
终归单人心
终归单人心 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: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

提交回复
热议问题