Eulerian Path Algorithm | Graph Theory | Source Code

WilliamFiset · Intermediate ·⚡ Algorithms & Data Structures ·7y ago

Key Takeaways

The video implements the Eulerian Path algorithm in source code, explaining the class constructor, instance variables, and methods to find an Eulerian path in a directed graph.

Full Transcript

[Music] hello and welcome my name is William and today we're going to look at some source code for the oil aryan path algorithm this video follows off my last videos explaining the oil aryan pathaayiram itself so I highly recommend you watch those videos before watching this one there should be a link to that in the description below also all the source code I'm going to present today is available on github github.com slash William fizzy slash algorithms also link to that in the description so without further ado let's dive right in awesome here we are in the source code for the oil aryan path algorithm this code works by first instantiating this boil aryan path solver class and then calling a method to fetch the oil aryan path itself should it exists let's begin by taking a look at the class constructor in the constructor what you do is you pass in a directed graph to the algorithm as input and then the constructor verifies that you actually passed it in a graph that's not null and it also initializes a few variables including n the number of nodes in the graph and the path link to list before we go too far let's have a look at some of the instance variables for this class we already talked about n the number of nodes in the graph next we have edge count which we will compute dynamically from the input graph followed by in and out which are integer arrays to track the in and out degree of each node then we have path which is the Euler in path solution as well as a reference to the input graph so once you create an instance of this class there's only one public and method and that's get euler Ian path which does exactly what it says it will return to you an integer path consisting of the nodes you need to traverse to get a valid or they are in path or null if no path exists so there's a few things that get over there and path does which we'll cover step by step the first thing and they get where they are in path method is the setup method so let's have a look at that first all the set method does is loop through all the edges and increment the in-and-out array degrees as well as compute the number of edges in the graph which is being tracked by the edge count variable back to the ghetto Euler in path method the next thing is to check if the edge count is zero and return null if we don't have any edges to work with following this I call the graph has Euler in path method which verifies that our graph actually has no other in path because most graphs don't if the graph has or there and path method is also fairly simple what we want to do is make sure that no node has too many outgoing edges or too many incoming edges as well as ensure that there's the correct amount of start and end nodes for an Euler in path to exist the variables start nodes and end nodes keep track of how many nodes have either exactly one extra outgoing edge or one extra incoming edge for an Euler and path to exist there has to be at most one start and end node so when we're inside the for loop we have three conditions the first is to identify if the current node has too many incoming or outgoing edges which mathematically means that the difference between the in and out degree or vice versa is greater than 1 in this case return false because the path is impossible there will be no Euler in path in such an event the other conditions we care about our whether the current node might be a start node or an end node and if it is then we increment the start node and end node counters respectively the last step is to actually check that we have the correct number of start nodes and end nodes and return the boolean value returning back to the ghetto layer in path method the next thing and algorithm is to actually find the earlier in path now that we know one exists to do this we find a valid starting node and feed that as the first node to the depth-first search method so let's have a look at both of those we don't want to start our or therein path anywhere as we saw in the first video because this doesn't ensure that we find an Euler and path even though we know one exists the finds start no the method does exactly what it sounds like it looks for a node which is a valid starting node meaning a node with exactly one extra outgoing edge or in the case of an or there in circuit just any node with an outgoing edge it's important that we start at a node with an outgoing edge because our graph might contain singleton nodes that have no outgoing edges but another component in the graph might have outgoing edges which is where we really want to start if we are to find an order in path next up is the depth-first search method where things get interesting it turns out the depth-first search method is really short and could even be shorter but at the expense of readability remember that when calling this method the first note is the starting node which is the at variable in this method which if you haven't guessed it yet is the current node index we're currently at in essence what's happening in this method is that while the current node still has unvisited edges we're going to select the next node to explore and call the depth-first search method recursively each time we take an edge we decrease the number of outgoing edges for that node which means that eventually there will be no more outgoing edges for the current node and the loop will terminate once this happens we can add the current node to the front of the solution the key realization in this method I think is that you have to notice that the out array is being used as both a way of determining if there are any unvisited edges left at the current node as well as an index for reaching into the adjacency list to grab the next note to visit let's go back up to the get oil Arian path method once we finished executing the depth-first search the next thing to do is ensure that we found in order in path it could be the case that the graph is disconnected into multiple components in which case the correct thing to do is to return null because no Euler in path exists checking that the graph is disconnected is not something the graph has oil errand path method verifies and this is intentional because it's easier to do after running the depth-first search by ensuring that the solution actually has a size equal to edge count plus 1 the next thing I do before returning the solution which is optional is simply to empty the contents of the linked list into a primitive integer array just for convenience I do this because it's easier for the color to index an array than it is a linked list the rest of the file are just helper methods for creating a directed graph and adding directed edges to the graph I also provide two examples one from the previous slides and another that I made up I encourage you to look them over to understand how this program works thank you for watching please like this video if you learned something and subscribe for more mathematics and computer science videos I'll catch you [Music]

Original Description

A source code implementation of how to find an Eulerian Path Euler path/circuit existance: https://youtu.be/xR4sGgwtR2I Euler path/circuit algorithm: https://youtu.be/8MpoO2zA2l4 Algorithms repository: https://github.com/williamfiset/algorithms#graph-theory Video slides: https://github.com/williamfiset/Algorithms/tree/master/slides =================================== Practicing for interviews? I have used, and recommend `Cracking the Coding Interview` which got me a job at Google. Link on Amazon: https://amzn.to/3cvMof5 A lot of the content on this channel is inspired by the book `Competitive Programming` by Steven Halim which I frequently use as a resource and reference. Link on Amazon: https://amzn.to/3wC2nix
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from WilliamFiset · WilliamFiset · 0 of 60

← Previous Next →
1 JES Image Manipulation - 2 - Installation
JES Image Manipulation - 2 - Installation
WilliamFiset
2 JES Image Manipulation - 3 - User Interface
JES Image Manipulation - 3 - User Interface
WilliamFiset
3 JES Image Manipulation - 5 - Negative
JES Image Manipulation - 5 - Negative
WilliamFiset
4 JES Image Manipulation - 6 - Black & White
JES Image Manipulation - 6 - Black & White
WilliamFiset
5 JES Image Manipulation - 4 - Grayscale
JES Image Manipulation - 4 - Grayscale
WilliamFiset
6 JES Image Manipulation - 8 - Blur
JES Image Manipulation - 8 - Blur
WilliamFiset
7 JES Image Manipulation - 7 - Edge Detection
JES Image Manipulation - 7 - Edge Detection
WilliamFiset
8 JES Image Manipulation - 9 - Blend
JES Image Manipulation - 9 - Blend
WilliamFiset
9 JES Image Manipulation - 10 - Matte
JES Image Manipulation - 10 - Matte
WilliamFiset
10 JES Image Manipulation - 13 - Rotate90
JES Image Manipulation - 13 - Rotate90
WilliamFiset
11 JES Image Manipulation - 12 - Mirroring Picture
JES Image Manipulation - 12 - Mirroring Picture
WilliamFiset
12 JES Image Manipulation - 11  - Crop Image
JES Image Manipulation - 11 - Crop Image
WilliamFiset
13 JES Image Manipulation - 14 - Stretch picture
JES Image Manipulation - 14 - Stretch picture
WilliamFiset
14 Java Fractal Explorer [6/8]
Java Fractal Explorer [6/8]
WilliamFiset
15 Java Fractal Explorer [4/8]
Java Fractal Explorer [4/8]
WilliamFiset
16 Java Fractal Explorer [8/8]
Java Fractal Explorer [8/8]
WilliamFiset
17 Java Fractal Explorer [5/8]
Java Fractal Explorer [5/8]
WilliamFiset
18 Java Fractal Explorer [2/8]
Java Fractal Explorer [2/8]
WilliamFiset
19 Java Fractal Explorer [7/8]
Java Fractal Explorer [7/8]
WilliamFiset
20 Java Fractal Explorer [1/8]
Java Fractal Explorer [1/8]
WilliamFiset
21 Java Fractal Explorer [3/8]
Java Fractal Explorer [3/8]
WilliamFiset
22 Introduction [Programming Competition Problems]
Introduction [Programming Competition Problems]
WilliamFiset
23 String Manipulation 1 [Programming Competition Problems]
String Manipulation 1 [Programming Competition Problems]
WilliamFiset
24 String Manipulation 2 [Programming Competition Problems]
String Manipulation 2 [Programming Competition Problems]
WilliamFiset
25 Graph Theory 1 [Programming Competition Problems]
Graph Theory 1 [Programming Competition Problems]
WilliamFiset
26 Logic 1 [Programming Competition Problems]
Logic 1 [Programming Competition Problems]
WilliamFiset
27 Grid Problems 1 [Programming Competition Problems]
Grid Problems 1 [Programming Competition Problems]
WilliamFiset
28 Dynamic Programming 1 [Programming Competition Problems]
Dynamic Programming 1 [Programming Competition Problems]
WilliamFiset
29 Introduction to Big-O
Introduction to Big-O
WilliamFiset
30 Dynamic and Static Arrays
Dynamic and Static Arrays
WilliamFiset
31 Dynamic Array Code
Dynamic Array Code
WilliamFiset
32 Linked Lists Introduction
Linked Lists Introduction
WilliamFiset
33 Doubly Linked List Code
Doubly Linked List Code
WilliamFiset
34 Stack Introduction
Stack Introduction
WilliamFiset
35 Stack Implementation
Stack Implementation
WilliamFiset
36 Stack Code
Stack Code
WilliamFiset
37 Queue Introduction
Queue Introduction
WilliamFiset
38 Queue Implementation
Queue Implementation
WilliamFiset
39 Queue Code
Queue Code
WilliamFiset
40 Priority Queue Introduction
Priority Queue Introduction
WilliamFiset
41 Priority Queue Min Heaps and Max Heaps
Priority Queue Min Heaps and Max Heaps
WilliamFiset
42 Priority Queue Inserting Elements
Priority Queue Inserting Elements
WilliamFiset
43 Priority Queue Removing Elements
Priority Queue Removing Elements
WilliamFiset
44 Priority Queue Code
Priority Queue Code
WilliamFiset
45 Union Find Introduction
Union Find Introduction
WilliamFiset
46 Union Find Kruskal's Algorithm
Union Find Kruskal's Algorithm
WilliamFiset
47 Union Find - Union and Find Operations
Union Find - Union and Find Operations
WilliamFiset
48 Union Find Path Compression
Union Find Path Compression
WilliamFiset
49 Union Find Code
Union Find Code
WilliamFiset
50 Binary Search Tree Introduction
Binary Search Tree Introduction
WilliamFiset
51 Binary Search Tree Insertion
Binary Search Tree Insertion
WilliamFiset
52 Binary Search Tree Removal
Binary Search Tree Removal
WilliamFiset
53 Binary Search Tree Traversals
Binary Search Tree Traversals
WilliamFiset
54 Binary Search Tree Code
Binary Search Tree Code
WilliamFiset
55 Fenwick Tree range queries
Fenwick Tree range queries
WilliamFiset
56 Fenwick Tree point updates
Fenwick Tree point updates
WilliamFiset
57 Fenwick Tree construction
Fenwick Tree construction
WilliamFiset
58 Fenwick tree source code
Fenwick tree source code
WilliamFiset
59 Hash table hash function
Hash table hash function
WilliamFiset
60 Hash table separate chaining
Hash table separate chaining
WilliamFiset

The video teaches how to implement the Eulerian Path algorithm in source code, covering the class constructor, instance variables, and methods to find an Eulerian path in a directed graph. It provides a comprehensive understanding of the algorithm and its application in graph theory.

Key Takeaways
  1. Create a directed graph
  2. Initialize the Eulerian Path solver class
  3. Call the getEulerianPath method
  4. Check if the graph has an Eulerian path
  5. Find a valid starting node
  6. Perform depth-first search
  7. Verify the solution
💡 The Eulerian Path algorithm can be implemented using a depth-first search approach, and it's essential to verify the graph's connectivity to ensure the existence of an Eulerian path.

Related AI Lessons

Bloom Filters, Explained Properly
Learn how Bloom filters work and their benefits, including tiny memory and blazing speed, in exchange for potential false positives.
Dev.to · Daksh Gargas
Prefix Sums: The Preprocessing Trick That Makes Range Queries Instant
Learn how prefix sums enable instant range queries in arrays, boosting performance in various applications
Medium · Programming
I Thought I Was Ready for the Interview — Then One Simple Math Question Destroyed Me
A simple math question can destroy a developer's interview, highlighting the importance of being prepared for unexpected questions
Medium · Programming
Week 2(Day 10): LeetCode Two Pointers(slow & fast): Remove Duplicates from Sorted Array (Brute…
Learn to remove duplicates from a sorted array using the two pointers technique, improving from brute force to optimized solutions
Medium · Python
Up next
Stump Grinder Carbide Wheel Grinds Hardwood To Chips
Innoforge Studio
Watch →