Graph | Eulerian path

In graph theory, a Eulerian trail (or Eulerian path) is a trail in a graph which visits every edge exactly once. Similarly, an Eulerian circuit or Eulerian cycle is an Eulerian trail which starts and ends on the same vertex. 

Hierholzer's algorithm 可以在O(E)的时间下给出解。

算法:

  • Choose any starting vertex v, and follow a trail of edges from that vertex until returning to v. It is not possible to get stuck at any vertex other than v, because the even degree of all vertices ensures that, when the trail enters another vertex w there must be an unused edge leaving w. The tour formed in this way is a closed tour, but may not cover all the vertices and edges of the initial graph.
  • As long as there exists a vertex u that belongs to the current tour but that has adjacent edges not part of the tour, start another trail from u, following unused edges until returning to u, and join the tour formed in this way to the previous tour.

当然,需要用到双向链表不断地把边删掉,如果最终所有边都被访问了,那么就可以退出了。但是如果找不到点继续扩展,而且边又还有的话,那么就说明给不出回路。

原文地址:https://www.cnblogs.com/linyx/p/4082227.html