หากต้องการทราบเกี่ยวกับออยเลอร์เซอร์กิต เรามีแนวคิดเกี่ยวกับเส้นทางออยเลอร์ เส้นทางออยเลอร์เป็นเส้นทาง โดยที่เราสามารถเยี่ยมชมทุกโหนดได้เพียงครั้งเดียว เราสามารถใช้ขอบเดียวกันได้หลายครั้ง วงจรออยเลอร์เป็นเส้นทางออยเลอร์ชนิดพิเศษ เมื่อจุดยอดเริ่มต้นของเส้นทางออยเลอร์เชื่อมโยงกับจุดยอดสิ้นสุดของเส้นทางนั้นด้วย
ในการตรวจจับวงจร เราต้องปฏิบัติตามเงื่อนไขเหล่านี้:
- ต้องเชื่อมต่อกราฟ
- เมื่อไม่มีจุดยอดของกราฟที่ไม่บอกทิศทางมีดีกรีเป็นคี่ ก็จะเป็นวงจรออยเลอร์
อินพุต

ผลลัพธ์
กราฟมีวงจรออยเลอร์
อัลกอริทึม
สำรวจ (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
มีEulerianCircuit(กราฟ)
ใส่กราฟที่กำหนด
เอาต์พุตส่งคืน 0 เมื่อไม่มีวงจรออยเลอร์ และส่งคืน 1 เมื่อมีวงจรออยเลอร์..
Begin if isConnected() is false, then return false define list of degree for each node oddDegree := 0 for all vertex i in the graph, do for all vertex j which are connected with i, do increase degree done if degree of vertex i is odd, then increase oddDegree done if oddDegree is 0, then return 1 else return 0 End
โค้ดตัวอย่าง
#include<iostream>
#include<vector>
#define NODE 5
using namespace std;
/*int graph[NODE][NODE] = {{0, 1, 1, 1, 0},
{1, 0, 1, 0, 0},
{1, 1, 0, 0, 0},
{1, 0, 0, 0, 1},
{0, 0, 0, 1, 0}};*/ //No Euler circuit, but euler path is present
int graph[NODE][NODE] = {{0, 1, 1, 1, 1},
{1, 0, 1, 0, 0},
{1, 1, 0, 0, 0},
{1, 0, 0, 0, 1},
{1, 0, 0, 1, 0}}; //uncomment to check Euler Circuit as well as path
/*int graph[NODE][NODE] = {{0, 1, 1, 1, 0},
{1, 0, 1, 1, 0},
{1, 1, 0, 0, 0},
{1, 1, 0, 0, 1},
{0, 0, 0, 1, 0}};*/ //Uncomment to check Non Eulerian Graph
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;
}
int hasEulerianCircuit() {
if(isConnected() == false) //when graph is not connected
return 0;
vector<int> degree(NODE, 0);
int oddDegree = 0;
for(int i = 0; i<NODE; i++) {
for(int j = 0; j<NODE; j++) {
if(graph[i][j])
degree[i]++; //increase degree, when connected edge found
}
if(degree[i] % 2 != 0) //when degree of vertices are odd
oddDegree++; //count odd degree vertices
}
if(oddDegree == 0) { //when oddDegree is 0, it is Euler circuit
return 1;
}
return 0;
}
int main() {
if(hasEulerianCircuit()) {
cout << "The graph has Eulerian Circuit." << endl;
} else {
cout << "The graph has No Eulerian Circuit." << endl;
}
} ผลลัพธ์
The graph has Eulerian Circuit.