当前位置:网站首页>欧拉路/欧拉回路
欧拉路/欧拉回路
2022-07-28 04:37:00 【*﹏ℳ๓无情*】
欧拉路/欧拉回路简介
经过图中所有边恰好一次的通路称为欧拉通路或欧拉路。
经过图中所有边恰好一次的回路称为欧拉回路。
判别法
对于无向图G,G中存在欧拉回路当且仅当G中所有度非0的点是连通的且没有奇数度数的点。
对于无向图G,G中存在欧拉路当且仅当G中所有非0的点是连通的且G中恰有0或2个奇数度数的点(0个表示存在欧拉回路)。
对于有向图G,G中存在欧拉回路当且仅当G中所有度非0的点是强连通的且每个点的入度和出度相同。
对于有向图G,G中存在欧拉路当且仅当:
将G中所有有向边改为无向边后,G中所有度非0的点是连通的;
最多只有一个点的出度减去入度为1;
最多只有一个点的入度减去出度为1;
其他所有点的入度等于出度。
流程
先判断图中是否存在欧拉图(检查非0的点的连通性及度数)
然后采用dfs来构造求解欧拉路
令P为最终的路径序列,记路径起点为x(如果无向图全为偶数度数的点则随便找一个度数非0的点为x,否则找2个奇数点中任意一个为x;如果有向图所有点入度等于出度则随便找一个为x,否则找出度减入度为1的点为x);
dfs(node u):
遍历所有u连出去的边u→v,且u→v之前没有被访问过;
将边u→v打上标记(如果是无向图则v→u也要打上标记);
dfs(v);
将边u→v添加到路径序列P中;
dfs(x)结束后,将P中记录的边倒序输出,即为求得的欧拉路、
时间复杂度: O ( n + m ) O(n+m) O(n+m)(找起点 O ( n ) O(n) O(n),找欧拉路 O ( m ) O(m) O(m))
有向图(有连通性保证)
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:起点,y:出度比入度大1,z:出度不等于入度的点
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]);
}
无向图(不保证连通性)
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]);
}
边栏推荐
- [practice] use the web animations API to realize a clock with accurate timing
- [Sylar] framework -chapter11 socket module
- 重要的 SQL Server 函数 - 数字函数
- Advanced architects, 16 common principles of microservice design and Governance
- Odoo action analysis (action.client, action.act_window, action.server)
- [Sylar] framework -chapter14 tcpserver module
- 王爽汇编语言详细学习笔记三:寄存器(内存访问)
- MySQL:数据类型和运算符
- MySQL partition table transformation
- [Sylar] framework Chapter 22 auxiliary module
猜你喜欢

Use Baidu developer tool 4.0 to build a dedicated applet IDE
![[kinematics] simulation of orbital angular momentum based on MATLAB [including Matlab source code 1971]](/img/5e/dfe029490183ee74687606941ce98e.jpg)
[kinematics] simulation of orbital angular momentum based on MATLAB [including Matlab source code 1971]

How to select reliable securities analysts?
![[record of question brushing] 9. Number of palindromes](/img/01/74b0f40a29aaec487898a791cc8b79.png)
[record of question brushing] 9. Number of palindromes
![[coding and decoding] Huffman coding and decoding based on Matlab GUI [including Matlab source code 1976]](/img/af/27e3794f93166d8ecad9b42dafaf0f.png)
[coding and decoding] Huffman coding and decoding based on Matlab GUI [including Matlab source code 1976]

上班摸鱼打卡模拟器微信小程序源码

Select sorting method

Shanghai Telecom released public computing services and signed the action plan of "Joint Innovation Center for intelligent computing applications" with Huawei and other partners

Information system project manager (2022) - key content: Project Contract Management (13)

重要的 SQL Server 函数 - 其他函数
随机推荐
Information system project manager (2022) - key content: organization level project management, process management, project set management (18)
Jupyter notebook installation code prompt function
【sylar】框架篇-Chapter20-守护进程模块
Shanghai Telecom released public computing services and signed the action plan of "Joint Innovation Center for intelligent computing applications" with Huawei and other partners
Select sorting method
《KG-BERT: BERT for Knowledge Graph Completion》
网页源代码查看竟然有这么多方法!你都知道吗?
【sylar】框架篇-Chapter15-Stream 模块
20-Openwrt crond crontab
【实战】使用 Web Animations API 实现一个精确计时的时钟
[yolov5 practice 5] traffic sign recognition system based on yolov5 -yolov5 integration pyqt5
【sylar】框架篇-Chapter8-定时器模块
21 openwrt kernel module changed to.Ko automatic loading
NAT基本原理与私有IP
CMake使用基础汇总
[Sylar] framework -chapter12 bytearray module
[Sylar] framework -chapter11 socket module
Esp8266 WiFi module and mobile communication
Transformer landing | next vit realizes the real-time landing of industrial tensorrt, surpassing RESNET and cswin
[Sylar] framework -chapter7-io coordination scheduling module