码迷,mamicode.com
首页 > 其他好文 > 详细

Graph | Eulerian path

时间:2014-11-07 20:38:26      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:style   io   ar   os   sp   for   on   art   bs   

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.

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

Graph | Eulerian path

标签:style   io   ar   os   sp   for   on   art   bs   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!