We should first take 1, then take 2 and remove edge {1,2}
Then take 3 and remove edge {2,3},but now we cannot take vertex 1.(We should take vertex 4 now).
That's why in my algorithm, I add an array NearEnd to indicate a path that will result in the End vertex.
By the way, in my algorithm, I have not consider the situation that there's a vertex with odd degree, because when you say "Hui Lu", I think it means it the path should come back to the original vertex. In the fact, the algorithm is same if consider those graphs with two odd degree.
其实只要每次寻找序号最小的点,然后再到序输出就可以了。下面是USACO上面的分析。
Assuming you pick the lowest index vertex connected to each node, the Eulerian Path algorithm actually determines the path requested, although in the reverse direction. You must start the path determination at the lowest legal vertex for this to work.
for(i=1;i<=N;i++)NearEnd[i]=0;
curi=1;
do
{
Find smallest i that V[i] near V[curi] && NearEnd[i]==0.
If such i exist
{
Output(i);
Remove Edge(V[i],V[curi]);
if(curi==1&°ree(V[curi])==1||Degree(V[curi])==2)
{
if(curi==1||Exist V[k] near V[curi]&&NearEnd[k]==1)
{
NearEnd[curi]==1;
Find k that V[k] near V[i] and NearEnd[k]==0;//only one k.
while(Degree(V[k]==2)
{
nextk is the value that V[k] near V[nextk] && NearEnd[next]==0;//only one point
k=nextk;
}
}
}
else
{ //There must only those vertex with NearEnd[k]==1 left
while(Find i that V[i] near V[curi])
{
Output(i);
Remove Edge(V[i],V[curi]);
curi=i;
}
return;
}
}while(1);