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

เพื่อตรวจสอบว่ากราฟเป็น Eulerian หรือไม่ เราต้องตรวจสอบสองเงื่อนไข -
-
กราฟจะต้องเชื่อมต่อ
-
in-degree และ out-degree ของแต่ละจุดยอดต้องเท่ากัน
ป้อนข้อมูล − เมทริกซ์ที่อยู่ติดกันของกราฟ
| 0 | 1 | 0 | 0 | 0 |
| 0 | 0 | 1 | 0 | 0 |
| 0 | 0 | 0 | 1 | 1 |
| 1 | 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 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(กราฟ)
ป้อนข้อมูล − กราฟ
ผลผลิต − เป็นจริงหากเชื่อมต่อกราฟ
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 isEulerCircuit(กราฟ)
ป้อนข้อมูล − กราฟที่กำหนด
ผลผลิต − เป็นจริงเมื่อพบวงจรออยเลอร์หนึ่งวงจร
Begin 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 otherwise return false End
โค้ดตัวอย่าง
#include<iostream>
#include<vector>
#define NODE 5
using namespace std;
int graph[NODE][NODE] = {{0, 1, 0, 0, 0},
{0, 0, 1, 0, 0},
{0, 0, 0, 1, 1},
{1, 0, 0, 0, 0},
{0, 0, 1, 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 isEulerCircuit() {
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;
}
if(inward == outward) //when number inward edges and outward edges
for each node is same
return true;
return false;
}
int main() {
if(isEulerCircuit())
cout << "Euler Circuit Found.";
else
cout << "There is no Euler Circuit.";
} ผลลัพธ์
Euler Circuit Found.