Eulerian Path Algorithm | Graph Theory | Source Code
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
JES Image Manipulation - 2 - Installation
WilliamFiset
JES Image Manipulation - 3 - User Interface
WilliamFiset
JES Image Manipulation - 5 - Negative
WilliamFiset
JES Image Manipulation - 6 - Black & White
WilliamFiset
JES Image Manipulation - 4 - Grayscale
WilliamFiset
JES Image Manipulation - 8 - Blur
WilliamFiset
JES Image Manipulation - 7 - Edge Detection
WilliamFiset
JES Image Manipulation - 9 - Blend
WilliamFiset
JES Image Manipulation - 10 - Matte
WilliamFiset
JES Image Manipulation - 13 - Rotate90
WilliamFiset
JES Image Manipulation - 12 - Mirroring Picture
WilliamFiset
JES Image Manipulation - 11 - Crop Image
WilliamFiset
JES Image Manipulation - 14 - Stretch picture
WilliamFiset
Java Fractal Explorer [6/8]
WilliamFiset
Java Fractal Explorer [4/8]
WilliamFiset
Java Fractal Explorer [8/8]
WilliamFiset
Java Fractal Explorer [5/8]
WilliamFiset
Java Fractal Explorer [2/8]
WilliamFiset
Java Fractal Explorer [7/8]
WilliamFiset
Java Fractal Explorer [1/8]
WilliamFiset
Java Fractal Explorer [3/8]
WilliamFiset
Introduction [Programming Competition Problems]
WilliamFiset
String Manipulation 1 [Programming Competition Problems]
WilliamFiset
String Manipulation 2 [Programming Competition Problems]
WilliamFiset
Graph Theory 1 [Programming Competition Problems]
WilliamFiset
Logic 1 [Programming Competition Problems]
WilliamFiset
Grid Problems 1 [Programming Competition Problems]
WilliamFiset
Dynamic Programming 1 [Programming Competition Problems]
WilliamFiset
Introduction to Big-O
WilliamFiset
Dynamic and Static Arrays
WilliamFiset
Dynamic Array Code
WilliamFiset
Linked Lists Introduction
WilliamFiset
Doubly Linked List Code
WilliamFiset
Stack Introduction
WilliamFiset
Stack Implementation
WilliamFiset
Stack Code
WilliamFiset
Queue Introduction
WilliamFiset
Queue Implementation
WilliamFiset
Queue Code
WilliamFiset
Priority Queue Introduction
WilliamFiset
Priority Queue Min Heaps and Max Heaps
WilliamFiset
Priority Queue Inserting Elements
WilliamFiset
Priority Queue Removing Elements
WilliamFiset
Priority Queue Code
WilliamFiset
Union Find Introduction
WilliamFiset
Union Find Kruskal's Algorithm
WilliamFiset
Union Find - Union and Find Operations
WilliamFiset
Union Find Path Compression
WilliamFiset
Union Find Code
WilliamFiset
Binary Search Tree Introduction
WilliamFiset
Binary Search Tree Insertion
WilliamFiset
Binary Search Tree Removal
WilliamFiset
Binary Search Tree Traversals
WilliamFiset
Binary Search Tree Code
WilliamFiset
Fenwick Tree range queries
WilliamFiset
Fenwick Tree point updates
WilliamFiset
Fenwick Tree construction
WilliamFiset
Fenwick tree source code
WilliamFiset
Hash table hash function
WilliamFiset
Hash table separate chaining
WilliamFiset
More on: Algorithm Basics
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
Bloom Filters, Explained Properly
Dev.to · Daksh Gargas
Prefix Sums: The Preprocessing Trick That Makes Range Queries Instant
Medium · Programming
I Thought I Was Ready for the Interview — Then One Simple Math Question Destroyed Me
Medium · Programming
Week 2(Day 10): LeetCode Two Pointers(slow & fast): Remove Duplicates from Sorted Array (Brute…
Medium · Python
🎓
Tutor Explanation
DeepCamp AI