当前位置:网站首页>Euler road / Euler circuit
Euler road / Euler circuit
2022-07-28 04:42:00 【*Ruthless*】
List of articles
Aurora road / Introduction to Euler circuit
The path that passes through all edges of the graph exactly once is called Euler path or Euler road .
The circuit that passes through all edges of the graph exactly once is called Euler circuit .
Discrimination
For undirected graphs G,G Eulerian circuit exists in if and only if G All degrees of non 0 Points of are connected and have no odd degrees .
For undirected graphs G,G There is Euler road in if and only if G Central Africa 0 The points of are connected and G There happens to be 0 or 2 Points of odd degrees (0 One indicates the existence of Euler circuit ).
For digraphs G,G Eulerian circuit exists in if and only if G All degrees of non 0 The points of are strongly connected and the in degree and out degree of each point are the same .
For digraphs G,G There is Euler road in if and only if :
take G After changing all directed edges to undirected edges ,G All degrees of non 0 The points of are connected ;
At most, the output minus the input of one point is 1;
At most, the input minus the output of one point is 1;
The in degree of all other points is equal to the out degree .
technological process
First, judge whether there is Eulerian graph in the graph ( Check non 0 Connectivity and degree of points of )
Then use dfs To construct and solve Euler's road
Make P For the final path sequence , Remember that the starting point of the path is x( If the undirected graph is all points with even degrees, then find any degree that is not 0 The key point is x, Or find 2 Any one of the odd points is x; If all the in degrees of a digraph are equal to the out degrees, choose any one as x, Otherwise, find out that the degree minus the degree is 1 The key point is x);
dfs(node u):
Traverse all of u Connect to the outside side u→v, And u→v Have not been visited before ;
Put edge u→v Mark ( If it is an undirected graph v→u Also mark );
dfs(v);
Put edge u→v Add to path sequence P in ;
dfs(x) After the end , take P Side reverse output recorded in , That is, the obtained Euler road 、
Time complexity : O ( n + m ) O(n+m) O(n+m)( Find a starting point O ( n ) O(n) O(n), Find Oula Road O ( m ) O(m) O(m))
Directed graph ( Connectivity is guaranteed )
vector<int> edge[N];
int n, m, l, f[N], ind[N], outd[N], c[M];
inline void dfs(int x)
{
while (f[x] < outd[x])
{
int y = edge[x][f[x]];
f[x]++;
dfs(y);
c[++l] = y;
}
}
inline void Euler()
{
int x = 0, y = 0, z = 0; // x: The starting point ,y: Out degree is bigger than in degree 1,z: The point where the out degree is not equal to the in degree
for (int i = 1; i <= n; i++)
{
if (ind[i] + 1 == outd[i])
x = i, ++y;
if (ind[i] != outd[i])
++z;
}
if (!((y == 1 && z == 2) || !z))
{
printf("No\n");
return;
}
if (!x)
for (int i = 1; i <= n; i++)
if (ind[i])
x = i;
memset(f, 0, sizeof f);
l = 0;
dfs(x);
c[++l] = x;
if (l == m + 1)
printf("Yes\n");
else
{
printf("No\n");
return;
}
for (int i = l; i; --i)
printf("%d ", c[i]);
}
Undirected graph ( Connectivity is not guaranteed )
struct Node
{
int y, idx;
Node(int _y, int _idx)
{
y = _y;
idx = _idx;
}
};
vector<Node> edge[N];
int n, m, l, cnt = 1, f[N], d[N], c[M];
bool b[2 * M];
inline void dfs(int x)
{
while (f[x] < d[x])
{
int y = edge[x][f[x]].y, idx = edge[x][f[x]].idx;
if (!b[idx])
{
f[x]++;
b[idx] = b[idx ^ 1] = true;
dfs(y);
c[++l] = y;
}
else
f[x]++;
}
}
inline void Euler()
{
int x = 0, y = 0;
for (int i = 1; i <= n; i++)
if (d[i] & 1)
x = i, ++y;
if (y && y != 2)
{
printf("No\n");
return;
}
if (!x)
for (int i = 1; i <= n; i++)
if (d[i])
x = i;
memset(f, 0, sizeof f);
memset(b, false, sizeof b);
l = 0;
dfs(x);
c[++l] = x;
if (l != m + 1)
{
printf("No\n");
return;
}
printf("Yes\n");
for (int i = l; i; --i)
printf("%d ", c[i]);
}
边栏推荐
- [Sylar] framework -chapter11 socket module
- Full resolution of the use of go native plug-ins
- Depth traversal and breadth traversal of tree structure in JS
- [Sylar] framework -chapter14 tcpserver module
- transform: failed to synchronize: cudaErrorAssert: device-side assert triggered
- 重要的 SQL Server 函数 - 其他函数
- Strlen introduction, and the difference between sizeof
- 字符串0123456789abcdef,子串(非空且非同串本身)的个数是多少【杭州多测师】【杭州多测师_王sir】...
- [Sylar] framework -chapter20- daemon module
- Object locking in relational database transactions
猜你喜欢

Important SQL server functions - numeric functions

Important SQL server functions - other functions
![[practice] use the web animations API to realize a clock with accurate timing](/img/cd/9b9ab27ea6a9909725371eaa809b9f.jpg)
[practice] use the web animations API to realize a clock with accurate timing

Zhejiang University and other recent review papers on deep learning new drug design

01-Node-Express系统框架搭建(express-generator)

C语言初阶——循环语句(while,for,do while)

How to upgrade a pair of 12.2 RAC(primary) and a pair of 12.2 RAC(dataguard) to 19c

Render the data obtained from the database to the table in elementui

could only be written to 0 of the 1 minReplication nodes. There are 0 datanode(s) running and 0 node

Introduction to this pointer
随机推荐
High number_ Chapter 4__ Curvilinear integral_ Exercise solution
物联网工业串口转WiFi模块 无线路由WiFi模块的选型
阿里巴巴面试题【杭州多测师】【杭州多测师_王sir】
Strlen introduction, and the difference between sizeof
Web渗透之域名(子域名)收集方法
登录之后右上角改变 进入登录状态
将数据库拿到的数据渲染到elementUI 中的table中去
【Oracle】083错题集
[Oracle] 083 wrong question set
RuntimeError: stack expects each tensor to be equal size, but got [8] at entry 0 and [2] at entry 2
Niuke, convert string to integer
王爽汇编语言详细学习笔记三:寄存器(内存访问)
Sort - cardinal sort
Important SQL server functions - other functions
C语言初阶——循环语句(while,for,do while)
How to upgrade a pair of 12.2 RAC(primary) and a pair of 12.2 RAC(dataguard) to 19c
网络安全基本知识——密码(一)
Domain name (subdomain name) collection method of Web penetration
23 openwrt switch VLAN configuration
(克隆虚拟机步骤)