// Euclidean loop linear time Algorithm for undirected graphs
// By rappizit@yahoo.com.cn
// 2007-11-02
# Include <vector>
# Include <list>
# Include <stack>
# Include <algorithm>
# Include <iostream>
Using namespace STD;
# Define pause system ("pause ")
Typedef vector <int> VI;
Typedef list <int> Li;
Typedef vector <li> vli;
VI eulercircle (vli g)
{
Int n = G. Size ();
Int edge = 0;
For (INT I = 0; I <n; I ++)
{
Int degree = G [I]. Size ();
If (degree % 2 |! Degree)
{
Return VI (0 );
}
Edge + = degree;
}
VI path (EDGE/2 + 1 );
Stack <int> S;
Int p = 0, I = 0;
Do {
If (G [I]. Empty ())
{
Do {
S. Pop ();
Path [p ++] = I;
} While (! S. Empty () & (I = S. Top (), g [I]. Empty ()));
}
Else {
Int T = * (G [I]. Begin ());
G [I]. Erase (G [I]. Begin ());
G [T]. Erase (find (G [T]. Begin (), g [T]. End (), I ));
// You can delete an edge E (t, I) at a constant time using a bidirectional neighbor table with a cross link )?!
S. Push (t );
I = T;
}
} While (! S. Empty ());
Return path;
}
Void main ()
{
Int n, m;
Cin> N> m;
Vli g (N );
While (M --)
{
Int U, V;
Cin> U> V;
G [u]. push_back (V); // In order to cater to the examples in the book, each undirected edge is input twice.
}
VI Path = eulercircle (g );
If (path. Size ())
{
For (INT I = 0; I <path. Size (); I ++)
{
Cout <path [I] <"";
}
Cout <Endl;
}
}
Euler's loop of undirected graphs
It is stored in an adjacent table and belongs to the vector <list <int> type.
1. If the degree of a point is odd or zero, there is no Euler loop and an empty vector is returned.
2. Otherwise, it starts from the point labeled I = 0.
3. If I has no adjacent contacts, any adjacent points in the stack will pop up in the vector path. If the stack is not empty, I will be assigned a value to the top element of the stack.
Otherwise, take the first adjacent vertex t of I and delete its edge (delete both ends in G ). Press T on the stack. I is assigned to T.
4. If the stack is empty, the path is returned. Otherwise, go to 3.
The algorithm time complexity is O (e ).
Test data:
7 20
0 1
0 2
0 5
0 6
1 0
1 2
2 0
2 3
2 4
2 1
3 4
3 2
4 6
4 5
4 3
4 2
5 4
5 0
6 4
6 0
Test results:
0 6 4 2 3 4 5 0 2 1 0
Process diagram:
First, the program adds the edge0-1To the tour and removes it from the adjacency lists (in two places) (top left, lists at left). Second, it adds1-2To the tour in the same way (left, second from top). Next, it winds up back0But continues to do another cycle0-5-4-6-0, Winding up back0With no more edges incident0(Right, second from top). Then it pops the isolated vertices0And6From the stack4Is at the top and starts a tour from4(Right, third from top), which takes it3,2, And back4, Where upon it pops all the now-isolated vertices4,2,3, And so forth. The sequence of vertices popped from the stack defines the Euler Tour0-6-4-2-3-4-5-0-2-1-0Of the Whole graph.
Reference Source:
Algorithms in Java, Part 5 graph algorithms, Chapter 17.7/Java Algorithm (version 3rd, Volume 2nd)-graph algorithm, Chapter 17.7
PS: But I implemented it in C ++.