The legendary ferrule.
Simple expression of algorithm ideas
For the European region, if you start from a node and go down at Will (you need to mark it when you walk through and don't want to come next time), it will inevitably end at this node (except for the Start Node, the degrees of other nodes are even, as long as they can be entered ). In this way, a circle is formed, but some sides may come back before they are passed. We will look forward from the End Node until we find the first intersection, and then start from this node to continue the above steps, we will certainly find a path back to this point, then we connect the two circles together. When you find all the circles, the search for the entire Euler loop is complete.
When looking for the Euler loop, you can choose any Start node. If there is a base level vertex to find the Euler's path, it is better to start from the base level vertex, and the above steps are still valid.
Written Expression of algorithm ideas
A basic idea for solving such a problem is to start from a node and find a ring path from this point and return to this point. Now, the ring has been created. This method ensures that each point is traversed.
If the edge of a vertex is not traversed, the vertex serves as the starting point. This edge serves as the starting edge and connects it to the current ring. In this way, all edges are traversed. In this way, the entire graph is connected together.
More formally, to find out the Euler's path, we need to find out the starting point cyclically. Follow these steps:
Take a start point and start the following steps
If the vertex is not connected, add the vertex to the path and return the result.
If the vertex has a connected vertex, a table of the connected vertex is listed and then traversed until the vertex is not connected. (Traverse a vertex and delete a vertex)
Process the current vertex, delete the edge connected to the vertex, repeat the above steps on its adjacent vertex, and add the current vertex to the path.
The following is the pseudo code:
# Circuit is a Global Array
Find_euler_circuit
Circuitpos = 0
Find_circuit (node 1)
# Nextnode and visited is a local Array
# The path will be found in reverse order
Find_circuit (node I)
If node I has no neighbors then
Circuit (circuitpos) = node I
Circuitpos = circuitpos + 1
Else
While (node I has neighbors)
Pick a Random Neighbor node J of node I
Delete_edges (node J, node I)
Find_circuit (node J)
Circuit (circuitpos) = node I
Circuitpos = circuitpos + 1
To find the Euler's path, simply find a node with an odd degree and call find_circuit.
A little code (PKU 2337 captured inside, please refer to the http://blog.csdn.net/logic_nut/archive/2009/08/22/4473174.aspx)
// Call the following function in the main function <br/> Euler (START,-1); // The second parameter is replaced by-1 because it starts directly from start, ignore the output. <Br/> // the Euler function is an iterative process using the legendary ring method. <br/> // npath is a global variable, record the number of existing edges <br/> // The edges are stored in the adj [] and are linked list structures implemented by arrays (just like many hashes, the data field of the first node is not used, and only the pointer is partially valid.) <br/> void Euler (INT cur, int edgen) // edgen edge selected on the edgen node currently reached by the cur, that is, the upper node reaches the cur through edgen <br/>{< br/> int I; <br/> while (adj [cur]. NXT! =-1) <br/>{< br/> I = adj [cur]. NXT; <br/> adj [cur]. NXT = adj [I]. NXT; // equivalent to deleting the used edge <br/> Euler (adj [I]. end, I); <br/>}< br/> path [npath ++] = edgen; // post-order record. To maintain the edge priority during search, reverse output <br/>}