Hiho Oralu Shi-----Fleury algorithm to find Euler path

Source: Internet
Author: User

Hiho Oralu Shi

Analysis:

Little ho: Just give me this simple puzzle!

Little hi: Really, no problem?

<10 minutes past >

Little ho: Ah ah ah ah ah! Can't do it!!! The number of dominoes is a lot of chaos.

Little hi: Hey, I knew you were going to have a problem.

Little ho: Little hi come help me!

Little hi: Okay, all right. Let's solve the problem together.

< little hi Think about >

Little hi: It turns out that ... Little Ho You look closely at this example:

Since the two connected numbers are always the same, we can write them only once, so this example could be written as: 3-2-4-3-5-1. 6 digits have exactly 5 gaps, and the numbers on both sides of each gap correspond exactly to a single domino.

If we treat each number as a point, each piece of dominoes is considered an edge. What do you think of it?

Small ho: In this case, it is:

To connect all the dominoes, that is, to walk all the edges once. Hey, this is not Euler road problem!

Little hi: Yes, the problem is actually a problem with the Euler road, but unlike the last time, this time we're going to find a way to get to the European path.

Little ho: So how do we find a way?

Little hi: Let's just borrow the last example.

Using our last method of proving Euler's path, we found 2 paths in this example:

L1:4-5-2-3-6-5
L2:2-4-1-2
Suppose we stack s, which records the order of the nodes each time we look for a path. When we find L1, the situation in stack S is:

S:4 5 2 3 6 5 [Top]
At this point we step out of the stack and delete the edges. When we go to Node 2 o'clock, we find that Node 2 is exactly the public node of L1 and L2. And L2 satisfies the other side after passing back to Node 2. If we take L2 in this place first, and then continue to walk L1 not just go through all sides.

And in the last proof we know that in addition to L1, other paths L2, L3 ... Must be satisfied with the starting point and the endpoint. So starting from any common node there must be a path back to this node.

Thus we get an algorithm:

Find a L1 path in the original image

Backtrack from the end of the L1, and then stack each point in turn. And check if there are any other edges that are not passing through the current point. If there is a current point as the starting point, find L2, and the L2 node is also used to repeat the stack record of the algorithm.

When the points in the L1 are all out of the stack, the algorithm ends.

Here we have another 3-story example:

In this example:

L1:1-2-6-5-1
L2:2-3-7-2
L3:3-4-8-3
In the first step we L1 the stack s, and we use an array path to record the order of our stacks:

S: [1 2 6 5 1]
Path:
And then out of the stack to Node 2 o'clock we found 2 other paths, so we added another path of 2:

s:1 [2 3 7 2]
Path:1 5 6
At this point the L2 has finished, and then starts popping the element until we find that 3 has another path, which is also pressed into the stack:

S:1 2 [3 4 8 3]
Path:1 5 6 2 7
The remaining elements are then popped up in turn:

S
Path:1 5 6 2 7 3 8 4 3 2 1
The path at this point is exactly the Euler path we need.

Little ho: It was ingenious to find Euler's way.

Little hi: And this algorithm has a very clever way to implement it. Because DFS itself is a process to stack out, so we directly use the nature of Dfs to implement the stack, the pseudo-code is as follows:

**dfs (U):
while (U exists without deleted Edge E (u,v))
Delete Edge E (u,v)
DFS (v)
End
Pathsize←pathsize + 1
path[pathsize]←u**

#include <iostream>#include <cstdio>#include <cstring>#include <string.h>#include <algorithm>#include <vector>using namespace STD;Const intN =1005;intN, m, flag, Top, SUM, du[n], ans[5005],MapN [N];voidDfsintx) {Ans[++top] = x; for(inti =1; I <= N; i++) {if(Map[x] [I] >=1)        {Map[x] [i]--;MapI            [x]--; DFS (i); Break; }    }}voidFleury (intX) {top =1; Ans[top] = x; while(Top >0)    {intK =0; for(inti =1; I <= N; i++)//Determine if it can be extended{if(Map[Ans[top]] [I] >=1)//If there is an edge starting from ans[top] then it is extensible.{k =1; Break;} }if(k = =0)//The point x no other side can go first (i.e. not extensible), then output it{printf("%d", Ans[top]);        top--; }Else if(k = =1)//If extensible, which route DFS can extend{top--;//This requires attentionDFS (ans[top+1]); }    }}intMain () { while(scanf("%d%d", &n, &m)! = EOF) {memset(Du,0,sizeof(du));memset(Map,0,sizeof(Map)); for(inti =1; I <= m; i++) {intx, y;scanf("%d%d", &x, &y);Map[x] [y]++;//record edge, because it is a graph so add two edges, there may be more than one edge between two points            Map[Y]            [x]++;            du[x]++;        du[y]++; } flag =1;//Flag flag start point. If you have an even number of degrees, start with 1 .sum =0; for(inti =1; I <= N; i++) {if(Du[i]%2==1) {sum++; flag = i;//If there are odd edges, start searching from odd side}        }if(Sum = =0|| sum = =2) Fleury (flag); }return 0;}

Hiho Oralu Shi-----Fleury algorithm to find Euler path

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.