เส้นทางออยเลอร์เป็นเส้นทาง โดยที่เราสามารถเยี่ยมชมทุกขอบเพียงครั้งเดียว เราสามารถใช้จุดยอดเดียวกันได้หลายครั้ง ในกรณีนี้จะมีการพิจารณากราฟหนึ่งกราฟที่มีวงจรออยเลอร์ เนื่องจากมีเส้นทางออยเลอร์ด้วย
ในการตรวจสอบว่ากราฟกำกับมีเส้นทางออยเลอร์หรือไม่ เราต้องตรวจสอบเงื่อนไขเหล่านี้ -
- ต้องมีจุดยอดเดียว an โดยที่ (in-degree + 1 =out_degree)
- ต้องมีจุดยอดเดียว bn โดยที่ (in-degree =out_degree + 1)
- วางจุดยอดทั้งหมดที่มี (in-degree =out_degree) หากกรณีเหล่านี้ล้มเหลว กราฟจะไม่มีเส้นทางออยเลอร์

จุดยอด b มี (ในองศา 1, นอกองศา 2) จุดยอด c มี (ในองศา 2, นอกองศา 1) และสำหรับจุดยอดที่เหลือ a, d มี (ในองศา 2, นอกองศา 2), e มี (ในองศา 1, นอกองศา 1)
อินพุต
เมทริกซ์ที่อยู่ติดกันของกราฟ
| 0 | 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 0 | 0 |
| 0 | 0 | 0 | 1 | 0 |
| 0 | 1 | 0 | 0 | 1 |
| 1 | 0 | 0 | 0 | 0 |
ผลลัพธ์
พบเส้นทางออยเลอร์
อัลกอริทึม
สำรวจ (u เยี่ยมชม)
ป้อนข้อมูล โหนดเริ่มต้น u และโหนดที่เข้าชมเพื่อทำเครื่องหมายว่าโหนดใดถูกเยี่ยมชม
เอาต์พุตสำรวจจุดยอดที่เชื่อมต่อทั้งหมด
Begin mark u as visited for all vertex v, if it is adjacent with u, do if v is not visited, then traverse(v, visited) done End
isConnected(กราฟ)
Input :กราฟ
เอาต์พุต:เป็นจริงหากเชื่อมต่อกราฟ
Begin define visited array for all vertices u in the graph, do make all nodes unvisited traverse(u, visited) if any unvisited node is still remaining, then return false done return true End
hasEulerPath(กราฟ)
ใส่กราฟที่กำหนด
เอาต์พุต True เมื่อพบวงจรออยเลอร์หนึ่งวงจร
Begin an := 0 bn := 0 if isConnected() is false, then return false define list for inward and outward edge count for each node for all vertex i in the graph, do sum := 0 for all vertex j which are connected with i, do inward edges for vertex i increased increase sum done number of outward of vertex i is sum done if inward list and outward list are same, then return true for all vertex i in the vertex set V, do if inward[i] ≠ outward[i], then if inward[i] + 1 = outward[i], then an := an + 1 else if inward[i] = outward[i] + 1, then bn := bn + 1 done if an and bn both are 1, then return true otherwise return false End
โค้ดตัวอย่าง
#include<iostream>
#include<vector>
#define NODE 5
using namespace std;
int graph[NODE][NODE] = {{0, 0, 1, 1, 0},
{1, 0, 1, 0, 0},
{0, 0, 0, 1, 0},
{0, 1, 0, 0, 1},
{1, 0, 0, 0, 0}};
void traverse(int u, bool visited[]) {
visited[u] = true; //mark v as visited
for(int v = 0; v<NODE; v++) {
if(graph[u][v]) {
if(!visited[v])
traverse(v, visited);
}
}
}
bool isConnected() {
bool *vis = new bool[NODE];
//for all vertex u as start point, check whether all nodes are visible or not
for(int u; u < NODE; u++) {
for(int i = 0; i<NODE; i++)
vis[i] = false; //initialize as no node is visited
traverse(u, vis);
for(int i = 0; i<NODE; i++) {
if(!vis[i]) //if there is a node, not visited by traversal, graph is not connected
return false;
}
}
return true;
}
bool hasEulerPath() {
int an = 0, bn = 0;
if(isConnected() == false){ //when graph is not connected
return false;
}
vector<int> inward(NODE, 0), outward(NODE, 0);
for(int i = 0; i<NODE; i++) {
int sum = 0;
for(int j = 0; j<NODE; j++) {
if(graph[i][j]) {
inward[j]++; //increase inward edge for destination vertex
sum++; //how many outward edge
}
}
outward[i] = sum;
}
//check the condition for Euler paths
if(inward == outward) //when number inward edges and outward edges for each node is same
return true; //Euler Circuit, it has Euler path
for(int i = 0; i<NODE; i++) {
if(inward[i] != outward[i]) {
if((inward[i] + 1 == outward[i])) {
an++;
} else if((inward[i] == outward[i] + 1)) {
bn++;
}
}
}
if(an == 1 && bn == 1) { //if there is only an, and bn, then this has euler path
return true;
}
return false;
}
int main() {
if(hasEulerPath())
cout << "Euler Path Found.";
else
cout << "There is no Euler Circuit.";
} ผลลัพธ์
Euler Path Found.