logo

有向グラフ用の Hierholzer のアルゴリズム

有向オイラー グラフが与えられた場合、タスクは次の結果を出力することです。 オイラー回路 。オイラー回路は、グラフのすべてのエッジを 1 回だけ横断し、開始頂点で終了するパスです。

注記: 指定されたグラフにはオイラー回路が含まれています。

例:



入力 : 有向グラフ

有向グラフ用の Hierholzer のアルゴリズム' title=

出力: 0 3 4 0 2 1 0

前提条件:

  • 私たちは次のことについて議論しました 与えられたグラフがオイラーグラフであるかどうかを調べる問題 無向グラフの場合
  • Directed Grpag におけるオイラー回路の条件: (1) すべての頂点が単一の強く接続されたコンポーネントに属します。 (2) すべての頂点の入次数と出次数が同じです。無向グラフの場合は条件が異なることに注意してください (すべての頂点の次数が等しい)

アプローチ:

  1. 任意の開始頂点 v を選択し、その頂点から v に戻るまでエッジの軌跡をたどります。軌跡が別の頂点に入るとき、すべての頂点の入次数と出次数が同じでなければならないため、v 以外の頂点でスタックすることはできません。w から出る未使用のエッジが存在する必要があります。この方法で形成されたツアーは閉じたツアーですが、最初のグラフのすべての頂点とエッジをカバーしていない可能性があります。
  2. 現在のツアーに属しているが、隣接するエッジがツアーの一部ではない頂点 u が存在する限り、u から別のトレイルを開始し、未使用のエッジをたどって u に戻るまで、このようにして形成されたツアーを前のツアーに結合します。

図:

5 つのノードを持つ上記のグラフの例: adj = {{2 3} {0} {1} {4} {0}}。

  1. 頂点 0 から開始 :
    • 現在のパス: [0]
    • 回路: []
  2. 頂点0→3 :
    • 現在のパス: [0 3]
    • 回路: []
  3. 頂点 3 → 4 :
    • 現在のパス: [0 3 4]
    • 回路: []
  4. 頂点 4 → 0 :
    • 現在のパス: [0 3 4 0]
    • 回路: []
  5. 頂点 0 → 2 :
    • 現在のパス: [0 3 4 0 2]
    • 回路: []
  6. 頂点 2 → 1 :
    • 現在のパス: [0 3 4 0 2 1]
    • 回路: []
  7. 頂点 1 → 0 :
    • 現在のパス: [0 3 4 0 2 1 0]
    • 回路: []
  8. 頂点 0 にバックトラック :回路に0を追加します。
    • 現在のパス: [0 3 4 0 2 1]
    • 回路: [0]
  9. 頂点 1 にバックトラック :回路に1を加えます。
    • 現在のパス: [0 3 4 0 2]
    • 回路: [0 1]
  10. 頂点 2 に戻る :回路に2を加えます。
    • 現在のパス: [0 3 4 0]
    • 回路: [0 1 2]
  11. 頂点 0 にバックトラック :回路に0を追加します。
    • 現在のパス: [0 3 4]
    • 回路: [0 1 2 0]
  12. 頂点 4 に戻る : 回路に 4 を追加します。
    • 現在のパス: [0 3]
    • 回路: [0 1 2 0 4]
  13. 頂点 3 に戻る : 回路に 3 を追加します。
    • 現在のパス: [0]
    • 回路: [0 1 2 0 4 3]
  14. 頂点 0 にバックトラック :回路に0を追加します。
    • 現在のパス: []
    • 回路: [0 1 2 0 4 3 0]

上記のアプローチの実装を以下に示します。 

C++
// C++ program to print Eulerian circuit in given // directed graph using Hierholzer algorithm #include    using namespace std; // Function to print Eulerian circuit vector<int> printCircuit(vector<vector<int>> &adj) {  int n = adj.size();  if (n == 0)  return {};  // Maintain a stack to keep vertices  // We can start from any vertex here we start with 0  vector<int> currPath;  currPath.push_back(0);  // list to store final circuit  vector<int> circuit;  while (currPath.size() > 0) {  int currNode = currPath[currPath.size() - 1];  // If there's remaining edge in adjacency list  // of the current vertex  if (adj[currNode].size() > 0) {    // Find and remove the next vertex that is  // adjacent to the current vertex  int nextNode = adj[currNode].back();  adj[currNode].pop_back();  // Push the new vertex to the stack  currPath.push_back(nextNode);  }  // back-track to find remaining circuit  else {  // Remove the current vertex and  // put it in the circuit  circuit.push_back(currPath.back());  currPath.pop_back();  }  }  // reverse the result vector   reverse(circuit.begin() circuit.end());    return circuit; } int main() {  vector<vector<int>> adj = {{2 3} {0} {1} {4} {0}};  vector<int> ans = printCircuit(adj);    for (auto v: ans) cout << v << ' ';  cout << endl;  return 0; } 
Java
// Java program to print Eulerian circuit in given // directed graph using Hierholzer algorithm import java.util.*; class GfG {  // Function to print Eulerian circuit  static List<Integer> printCircuit(List<List<Integer>> adj) {  int n = adj.size();  if (n == 0)  return new ArrayList<>();  // Maintain a stack to keep vertices  // We can start from any vertex here we start with 0  List<Integer> currPath = new ArrayList<>();  currPath.add(0);  // list to store final circuit  List<Integer> circuit = new ArrayList<>();  while (currPath.size() > 0) {  int currNode = currPath.get(currPath.size() - 1);  // If there's remaining edge in adjacency list  // of the current vertex  if (adj.get(currNode).size() > 0) {  // Find and remove the next vertex that is  // adjacent to the current vertex  int nextNode = adj.get(currNode).get(adj.get(currNode).size() - 1);  adj.get(currNode).remove(adj.get(currNode).size() - 1);  // Push the new vertex to the stack  currPath.add(nextNode);  }  // back-track to find remaining circuit  else {  // Remove the current vertex and  // put it in the circuit  circuit.add(currPath.get(currPath.size() - 1));  currPath.remove(currPath.size() - 1);  }  }  // reverse the result vector  Collections.reverse(circuit);  return circuit;  }  public static void main(String[] args) {  List<List<Integer>> adj = new ArrayList<>();  adj.add(new ArrayList<>(Arrays.asList(2 3)));  adj.add(new ArrayList<>(Arrays.asList(0)));   adj.add(new ArrayList<>(Arrays.asList(1)));   adj.add(new ArrayList<>(Arrays.asList(4)));   adj.add(new ArrayList<>(Arrays.asList(0)));   List<Integer> ans = printCircuit(adj);  for (int v : ans) System.out.print(v + ' ');  System.out.println();  } } 
Python
# Python program to print Eulerian circuit in given # directed graph using Hierholzer algorithm # Function to print Eulerian circuit def printCircuit(adj): n = len(adj) if n == 0: return [] # Maintain a stack to keep vertices # We can start from any vertex here we start with 0 currPath = [0] # list to store final circuit circuit = [] while len(currPath) > 0: currNode = currPath[-1] # If there's remaining edge in adjacency list # of the current vertex if len(adj[currNode]) > 0: # Find and remove the next vertex that is # adjacent to the current vertex nextNode = adj[currNode].pop() # Push the new vertex to the stack currPath.append(nextNode) # back-track to find remaining circuit else: # Remove the current vertex and # put it in the circuit circuit.append(currPath.pop()) # reverse the result vector circuit.reverse() return circuit if __name__ == '__main__': adj = [[2 3] [0] [1] [4] [0]] ans = printCircuit(adj) for v in ans: print(v end=' ') print() 
C#
// C# program to print Eulerian circuit in given // directed graph using Hierholzer algorithm using System; using System.Collections.Generic; class GfG {    // Function to print Eulerian circuit  static List<int> printCircuit(List<List<int>> adj) {  int n = adj.Count;  if (n == 0)  return new List<int>();  // Maintain a stack to keep vertices  // We can start from any vertex here we start with 0  List<int> currPath = new List<int> { 0 };  // list to store final circuit  List<int> circuit = new List<int>();  while (currPath.Count > 0) {  int currNode = currPath[currPath.Count - 1];  // If there's remaining edge in adjacency list  // of the current vertex  if (adj[currNode].Count > 0) {  // Find and remove the next vertex that is  // adjacent to the current vertex  int nextNode = adj[currNode][adj[currNode].Count - 1];  adj[currNode].RemoveAt(adj[currNode].Count - 1);  // Push the new vertex to the stack  currPath.Add(nextNode);  }  // back-track to find remaining circuit  else {  // Remove the current vertex and  // put it in the circuit  circuit.Add(currPath[currPath.Count - 1]);  currPath.RemoveAt(currPath.Count - 1);  }  }  // reverse the result vector  circuit.Reverse();  return circuit;  }  static void Main(string[] args) {  List<List<int>> adj = new List<List<int>> {  new List<int> { 2 3 }  new List<int> { 0 }  new List<int> { 1 }  new List<int> { 4 }  new List<int> { 0 }  };  List<int> ans = printCircuit(adj);  foreach (int v in ans) {  Console.Write(v + ' ');  }  Console.WriteLine();  } } 
JavaScript
// JavaScript program to print Eulerian circuit in given // directed graph using Hierholzer algorithm // Function to print Eulerian circuit function printCircuit(adj) {  let n = adj.length;  if (n === 0)  return [];  // Maintain a stack to keep vertices  // We can start from any vertex here we start with 0  let currPath = [0];  // list to store final circuit  let circuit = [];  while (currPath.length > 0) {  let currNode = currPath[currPath.length - 1];  // If there's remaining edge in adjacency list  // of the current vertex  if (adj[currNode].length > 0) {  // Find and remove the next vertex that is  // adjacent to the current vertex  let nextNode = adj[currNode].pop();  // Push the new vertex to the stack  currPath.push(nextNode);  }  // back-track to find remaining circuit  else {  // Remove the current vertex and  // put it in the circuit  circuit.push(currPath.pop());  }  }  // reverse the result vector  circuit.reverse();  return circuit; } let adj = [[2 3] [0] [1] [4] [0]]; let ans = printCircuit(adj); for (let v of ans) {  console.log(v ' '); } 

出力
0 3 4 0 2 1 0 

時間計算量 :  O(V + E) ここで、V はグラフ内の頂点の数、E はエッジの数です。その理由は、アルゴリズムが深さ優先検索 (DFS) を実行し、各頂点と各エッジを 1 回だけ訪問するためです。したがって、各頂点の訪問には O(1) 時間がかかり、各エッジの横断には O(1) 時間がかかります。

空間の複雑さ : O(V + E) アルゴリズムでは、スタックを使用して現在のパスを保存し、リストを使用して最終回路を保存します。スタックの最大サイズは最悪でも V + E になる可能性があるため、空間の複雑さは O(V + E) になります。

クイズの作成