Javatpoint Logo
Javatpoint Logo

Fleury's Algorithm for printing Eulerian Path or Circuit in C++

Fl?ury's Algorithm is one of the most used methods for solving Eul?rian Paths and Circuits in a graph. It offers a systematic way of traversing through the edges of a graph while each edge is visited exactly once. An Eul?rian path ?ff?rs ?ll ?dg?s whereas ?n Eul?rian circuit i? ? cl??? loop, both representing the completeness of coverage of the graph's edges. However, these paths and circuits can only be identified under certain conditions inherent in the graph.

The Fl?ury's Algorithm performs a syst?matic s?arch of th? graph to find th? Eul?rian path or cycle. It then proceeds to traverse edges, ensuring each edge is visited only once while preserving graph connectivity. When multiple choices are available, the Algorithm will intelligently choose those edges that do not act as bridges, thus preventing the graph from fragmenting into disconnected components.

Fl?ury's Algorithm for Eul?rian Paths and Circuits in Undirected Graph:

This is the simple version of Fl?ury's Algorithm. Fl?ury's Algorithm is cited to discov?r Eul?ri?n paths and circuits in undir?ct?d graphs. It ensures that every edge is travelled exactly once in Eulerian circuits and twice in Eulerian paths. The algorithm starts with selecting the initial vertex and then selects edges with the least alternatives, avoiding bridges to prevent premature disconnection.

Repeating this process until all edges are traversed, the Algorithm develops a path or circuit that covers every edge exactly once. Such simplicity and efficien?y make Fl?ury's Algorithm a useful tool for the study of Eul?rian characteristics of undir?ct?d graphs.

Program:

Output:

Eul?rian Path or Circuit: Graph has no Eul?rian path or circuit.

Explanation:

The provided C++ code implements Fl?ury's Algorithm to find Eul?rian Paths or Circuits in an undir?ct?d graph. This Algorithm is designed to travel through every edge of the graph only once, forming an Eulerian Path (if there are exactly two vertices with an odd degree) or an Eulerian Circuit (if all vertices have an even degree).

  • Graph R?pr?s?ntation:

The Algorithm starts by representing the graph using an adjacency list. The adjacency list is a data structure where each vertex keeps a list of its neighbouring vertices. In C++, the code uses a Graph class that encapsulates this structure. The constructor sets the number of vertices, adding edges to the graph using the addEdge method.

  • Eul?rian Path and Circuit Conditions:

Before diving into the Algorithm itself, verifying whether the given graph meets the requirements of an Eulerian Path or Circuit is extremely important. Fl?ury's Algorithm needs to have either all v?rtic?s have an even d?gr?? if it is Eul?rian Circuit or exactly two v?rtic?s have an odd d?gr?? if it is Eul?rian Path. This check is done at the beginning of the Algorithm to guarantee that the graph is suitable for subsequent traversing.

  • Fl?ury's Algorithm Ov?rvi?w:

Fl?ury's Algorithm is based on the principle of navigating the graph and not having to traverse by bridges - edges whose removal would cut the graph. At its core, the main loop of the Algorithm selects the next edge from the current vertex, deletes it from the graph, and moves to the other end of the edge. This process is repeated until all edges are covered. The Algorithm uses a stack to maintain the order in which the vertices are visited.

  • Bridg? Avoidanc?:

One important element of Fluery's Algorithm is the prevention of bridges during the traverse. A bridg? is an edge whose removal increases the number of connected components in the graph. To make sure that the Algorithm does not acci?ntally create bridg?s, the isValidNextEdge function checks whether removing th? ?dge (u, v) disconn?cts th? graph.

It does so by doing a depth-first search (DFS) and counting the number of reachable vertices before and after edge removal. If the count remains the same, the edge can be safely traversed.

  • D?pth-First S?arch (DFS) Counting:

The dfsCount function uses a depth-first search to determine the number of reachable vertices from a given vertex. It serves to confirm whether eliminating an edge disconnects the graph. This counting mechanism is essential in ensuring the algorithm's decisions do not affect the graph's connectivity.

  • Trav?rsal and Stack Management:

The main loop of Fleury's Algorithm always selects and travels edges and updates the stack. If a vert?x has no mor? edges, it is considered to belong to th? Eul?rian path or Circuit, and th? algorithm backtracks by popping th? last v?rt?x from th? stack. The travel is continued until all the edges are covered.

  • Output:

Lastly, the Algorithm prints the sequence of vertices visited during the traverse operation. This s?qu?nc? equ?t?? th? Eul?rian Path or Circuit in th? graph, depending on th? initial conditions.

  • Sampl? Graph and Output:

In th?, pr?vided ?xampl?, a graph with four v?rtic?s and specifi? ?dg?s is used to show th? algorithm. The graph has vertices 0, 1, 2, and 3. Edg?s ar? added between 0-1, 0-2, 1-2, and 2-3. The output defines the Eul?rian Path or Circuit beginning in the specified vertex.

Compl?xity Analysis:

Tim? Compl?xity:

Graph Initialization (Constructor):

The constructor provides the graph with several verteces and sets up the adjacency list. The time complexity for this operation is O(V), V being the number of vertices.

Edg? Addition (addEdg?):

An edge insertion also requires modifying the set of adjacency lists for two vertices. At worst, when each vertex has a constant degree, the time complexity is O(1).

Edg? R?moval (r?mov?Edg?):

When an edge is deleted, it means the deletion or erasure of entries from the adjacency lists of two vertices. However, in the most complicated case, the time complexity is O(1), when every vertex has a fixed degree O(1).

Depth-First Search (DFS): The accessible vectors are computed by DFS operations. However, in the worst case where all vertices could be reached from other vertices, the time complexity could go to O(V + E), where V is the number of nodes and E is the number of edges.

Fleury's Algorithm (fleuryAlgorithm):

The basic phases of this approach are to select the edges, remove them, and run the DFS. At each step, it either performs a DFS (O(V + E)) or an edge pop from the adjacency list (O(1)). As a result, O(V * (V+E)) is the complexity, where V is the vertex count, and E is the edge count.

Finally, the time complexity is O(V * (V + E)), which depends on the DFS procedures.

Spac? Compl?xity:

Graph R?pr?s?ntation:

The space complexity for the graph representation includes storing the adjacency lists. Worst of all, when there are no duplicate edges, the spatial complexity is O (V + E), where V is the number of vertices, and E is the number of edges.

DFS Stack:

DFS traverse uses a stack to maintain the visited vertices. Worst case, th? stack can have a siz? V, leading t? a spac? compl?xity of O(V).

Additional Data Structur?s:

Other structures, like arrays and ve?tors used for bookk?eing, also contribute to the overall space complicatedness. However, the worst case is that the space requirement for such data structures is O(V).

Lastly, th? space complexity is driven by th? graph repr?s?ntation and other data stru?tur?s, resulting in O (V+ E).

?he concept of Eul?rian paths and circuits can be adapted in directed graphs. In dir?ct?d gra?h??, a v?ri?nt ?f Fleury's Algor?thm may include considering in-d?gr?? and out-d?gr?e of v?rtices to determine if Eul?rian p?th or c?r?uit ex??ts.

Fl?ury's Algorithm for dir?ct?d graphs is adapted to consider the in-d?gr??s and out-d?gr??s of v?rtic?s. ?h? important idea is to traverse edges and retain the Eulerian property.

The in-d?gr??s and out-d?gr??s concepts are very important for establishing the logic of Eul?rian paths or circuits. Unlike undirected graphs, where edges are bidirectional, directed graphs have edges with defined start and end vertices.

Eul?rian Paths and Circuits in Dir?ct?d Graphs:

An Eulerian path in a dir?ct?d graph is a path that tr?nsit?r? the dir?ct?d ?dg? onc?. A Eul?rian circuit, on th? ?th?r hand, is a circuit that begins and ?nds at th? same verte, visiting each directed edge exactly once.

In-D?gr??s and Out-D?gr??s:

In dir?cted graphs for ?ach v?rt?x th? in-d?gr?? r?pr?s?nts th? numb?r of th? edg?s that lead t? it, whil? th? out-d?gr?? r?pr?s?nts th? numb?r of the vertices.

To ?xist a Eul?rian p?th, there must ?t most one v?rt?x for wh?r? (out-d?gr?e+1) = in-d?gr??, and ?t most one v?rt?x for wh?r? in-d?gr?? + 1 = out-degree. All other vertices need to have in-degrees equal to out-degrees.

In the case of the ?ul?rian circuit, ?v?ry v?rt?x must hav? the in-d?gr?? embalm?d by th? supp?rt?d out-d?gr??.

Program:

Output:

Eul?rian Path or Circuit: Graph has no Eul?rian path or circuit.

Explanation:

The above C++ code implements Fl?ury's Algorithm for dir?ct?d graphs. In this case, Fl?ury's Algorithm is an adapt?tion of a classical method of finding Eul?rian Paths or Circuits in graphs to dire?ted graphs. The Algorithm traverses each edge exactly once in consideration of the indegrees and outdegrees of vertices.

  • Graph Initialization (Constructor):

The class Graph encloses the form and functions of a directed graph. It also has private members, including the number of vertices (V) and the adjacency list (adj). The adjacency list is the vector of vectors that shows directed edges between vertexes.

  • Constructor:

The constructor creates a graph with the given number of vertices and the adjacency list by the number of vertices.

  • addEdg?(u, v):

The addEdge method creates the directed edge between the vertex u and v. It updates the adjacency list to reflect the directed relation.

The graph is constructed with a fixed number of vertices; therefore, the time complexity is O (V).

  • Edg? Addition (addEdg?):

Edging is the alteration of the adjacency list, and in the worst (sparse graph), it has a time complexity of magnitude 1.

  • Edg? R?moval (r?mov?Edg?):

Deleting an edge refers to deleting the entries in the adjacency.

  • Eul?rian Path or Circuit Conditions Ch?ck:

The loop, which looks for the Eul?rian path or circuit, has a tim? compl?xity of O(V), where V is th? number of v?rtic?s.

  • Fl?ury's Algorithm (fl?uryAlgorithmDir?ct?d):

The gist of the Algorithm is represented by traversing the graph and ensuring that every edge is valid. At most, it can be O(V^2), where V is the number of vertices.

  • isValidN?xtEdg? M?thod:

Ch?cking th? validity of th? n?xt ?dg? involves traversing th? adjac?ncy list, and in th? worst cas?, it is O(d), wh?r? d is th? maximum d?gr?? of a v?rt?x.

Th? general tim? compl?xity is dominat?d using th? trav?rsal st?ps, r?sulting in O(V^2) in th? worst cas?, wh?r? V is th? numb?r of v?rtic?s.

Spac? Compl?xity:

Graph R?pr?s?ntation:

The space complexity for the graph representation involves storing the adjacency list. In th? worst case (d?ns? graph), it's O(V^2), V being th? numb?r of v?rtic?s.

DFS Stack:

DFS traversal uses a stack that remembers the visited vertices. In the worst case, the stack may have a size of V, contributing to the space complexity of O(V).

Additional Data Structur?s:

Additional data structures using vectors and boolean arrays are employed in accounting and add to the complexity of the space. These records structures take, at most, O(V) space.

A graph representation dominates universal space complexity; in the worst case, it is O(V^2), where V is the number of vertices.

Dijkstra's Algorithm with Fl?ury's Id?a (Eul?rian Path):

The union of Dijkstra's Algorithm and Fl?ury's id?a for finding Eul?rian paths in a -st?p procedure. Firstly, utilize Dijkstra's Algorithm to determine the shortest paths between the two odd-degree vertices in the graph. This step guarantees that the graph becomes Euclidean with the help of connecting the standard degree vertices in a superior way. As soon as the shortest paths are identified, adjust the graph by incorporating these calculated paths, turning it into an Eulerian graph.

Then, apply Fl?ury's Algorithm to find th? Eul?rian course within th? modified graph. Fl?ury's Algorithm is such that all edges are traversed exactly once while no bridge edge is traversed at all. This combined method offers a holistic solution for the solution to both path optimization and Eulerian route determination in graphs with uniform-degree vertices by incorporating Dijkstra's ?ffici?nt course-locating with Fl?ury's method for travelling Eul?rian paths.

Program:

Output:

Eul?rian Path: 1 3 4 3 4 2 4 3 2 3 1 2 4 2 3 2 1 3 1 2 0 2 1 0 1 0 2 0 1 

Time Complexity:

Given th? tim? compl?xities of Dijkstra's set of rules, th? important loop in combin?DijkstraAndFl?ury, and Fl?ury's set of rules, th? ov?rall tim? compl?xity is concluded b? th? highest of th?m. Therefore, the overall complexity of time is O(((V+E) * log(v))).

Space Complexity:

Total space complexity is calculated as the highest among the space complexities of graph representation, Dijkstra's set of rules, combining, and Fulery's Algorithm. Therefore, the general space complexity in the worst case for dense graphs is O(V^2) or O(V+E) for sparse graphs.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA