Chu trình / mạch Euler là một đường dẫn; bằng cách đó chúng tôi có thể ghé thăm mọi cạnh chính xác một lần. Chúng ta có thể sử dụng các đỉnh giống nhau cho nhiều lần. Euler Circuit là một loại đường dẫn Euler đặc biệt. Khi đỉnh bắt đầu của đường Euler cũng được nối với đỉnh kết thúc của đường dẫn đó, thì nó được gọi là Mạch Euler.
Để kiểm tra xem một biểu đồ có phải là Eulerian hay không, chúng ta phải kiểm tra hai điều kiện -
-
Biểu đồ phải được kết nối.
-
Độ trong và độ của mỗi đỉnh phải giống nhau.
Đầu vào - Ma trận kề của đồ thị.
| 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 ra - Đã tìm thấy mạch Euler
Thuật toán
traverse (u, đã ghé thăm)
Đầu vào - Nút bắt đầu u và nút đã truy cập để đánh dấu nút nào được truy cập.
Đầu ra - Đi ngang qua tất cả các đỉnh được kết nối.
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 (đồ thị)
Đầu vào - Biểu đồ.
Đầu ra - Đúng nếu biểu đồ được kết nối.
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 (Đồ thị)
Đầu vào - Đồ thị đã cho.
Đầu ra - Đúng khi tìm thấy một mạch Euler.
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
Mã mẫu
#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.";
} Đầu ra
Euler Circuit Found.