Euler (back) road

Source: Internet
Author: User

If there is a stroke in a graph, the path of a stroke is called the Euler Road, and if you finally return to the starting point, then this path is called the Euler loop.

Here is a definition: singular point refers to the number of edges connected to this point has an odd number of points, conversely, an even number is called even point.

So how to find the Euler (back) path in a diagram, there are two theorems:

1. Conditions for the existence of the Euler road: the graph is connected, and there are only two singular points

2. Conditions for the presence of a Euler loop: the graph is connected and there are 0 singularities

Therefore, in order to find the Euler circuit, the depth-first traversal can be performed on any one point, and the Euclidean circuit is searched, then the depth-first traversal of a singularity is performed, and the time complexity is O (m+n).

Code:

1#include <iostream>2#include <cstring>3 using namespacestd;4 #defineMAXN 1015 intG[MAXN][MAXN],DU[MAXN],CIRCUIT[MAXN];6 intN,e,circuitpos,i,j,x,y,start;7 voidFind_circuit (inti)8 {9      for(j=1; j<=n;j++)Ten       if(g[i][j]==1) One       { Ag[i][j]=g[j][i]=0; - Find_circuit (j); -       } thecircuit[++circuitpos]=i; - } - intMain () - { +Memset (G,0,sizeof(g)); -Cin>>n>>e; +      for(i=1; i<=e;++i) A     { atscanf"%d%d",&x,&y); -g[x][y]=g[y][x]=1; -du[x]++;d u[y]++; -     } -start=1; -      for(i=1; i<=n;++i) in      if(du[i]%2==1) -start=i; tocircuitpos=0; + find_circuit (start); -      for(i=1; i<=circuitpos;++i) theprintf"%d", Circuit[i]); *}
View Code

Euler (back) road

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.