Identifying Isomorphic Trees | Graph Theory

WilliamFiset · Beginner ·⚡ Algorithms & Data Structures ·6y ago

Key Takeaways

The video demonstrates the process of identifying isomorphic trees using graph theory, specifically through the use of serialization and the ahu algorithm, and provides a step-by-step guide on how to implement this in code.

Full Transcript

[Music] hello and welcome my name is William and today we're diving into isomorphisms with trees a question of tree equality and what that means when we're talking about general graphs and asked whether two graphs are isomorphic we're essentially asking whether they're structurally the same in the middle of this slide you'll see two graphs G 1 and G 2 they are labeled differently and kind of appear different but they are structurally the same graph so they're isomorphic you could for example conceptually unfold the graph on the right to match the one on the left and then real able its vertices and you would have two identical graphs we can also define the notion of a graph isomorphism more rigorously because saying that two graphs have the same structure is not very well defined so if we define a graph G 1 as the set of edges e1 and the vertices V 1 and another graph G 2 in the same manner we say that the two graphs G 1 and G 2 are isomorphic if there exists a bi junction between the sets of vertices v1 and v2 such that for all the pairs of vertices which form an edge in G 1 applying the function Phi to the nodes of all those edges always results in an edge which is present in G 2 so in layman's terms this means that for an isomorphism to exist there needs to be a function which can map all the nodes and edges in G 1 2 G 2 and vice versa as it turns out determining if two graphs are isomorphic is not only not obvious to the human eye but is actually also a very difficult problem for it is still an open question as to whether the graph isomorphism problem is np-complete however luckily for us there are many polynomial time algorithms which exists for specialized graph types including trees for determining whether they're isomorphic or not let's have a look at a few examples I'm going to show you two trees and you have to tell me whether they're isomorphic or not so our tree 1 and tree 2 isomorphic no they're not because they're structurally different what about 3 3 and 3/4 yes they're isomorphic in terms of writing an algorithm for identifying isomorphic trees there are several very quick probabilistic usually hash of heuristic based algorithms for identifying isomorphic trees these algorithms tend to be very fast but also more error-prone due to hash collisions in a limited integer space however they do have their place when it comes to competitive programming and testing the Equality of absolutely enormous trees however the method we'll be looking at today involves serializing a tree into a unique encoding this encoding is simply a string that represents the tree and if another tree has the same encoding then we say that both trees are isomorphic when we're going about encoding a tree you can either directly serialize the unrooted tree but in practice I find it much easier to write the code to serialize a rooted tree this is just my personal preference however one small caveat to watch out for if you're going for the rooted tree approach is to make sure you route both of your trees t1 and t2 with the same root node before you begin the serialization process otherwise you get two different encodings one trick we can use to help ourselves select a common node between both trees is to reuse what we learned in the last video about finding the center of a tree now let's have a look at the entire flow of how encoding a tree is going to work first we start out with two trees t1 and t2 which may or may not be isomorphic that's what we're trying to figure out from here find the center of both trees for now don't worry about the case where one of the trees has more than one Center we'll see how to handle this later next route the trees at their center nodes then generate the encoding for each tree and compare the serialized tree values for equality the dream coding is simply a sequence of left and right brackets however you can also think of them as zeros and ones which is just a really large number if you prefer from this encoding it should also be possible to reconstruct the original tree solely based on this encoding I will leave this as an exercise to the reader the ahu algorithm short for the initials of the three authors who created the algorithm is a clever serialization technique for representing a tree as a unique string unlike many tree isomorphism invariance in heuristics ahu is able to capture a complete history of a trees degree spectrum and structure in turn this ensures a deterministic method of checking for tree isomorphisms let's take a closer look at how this works suppose we have this tree and we wish to encode it the first thing we do is assign all leaf nodes Knuth tuples which are a pair of left and right brackets after that for all the nodes with greyed out children combine their children's labels and wrap them in a new pair of brackets it's clearer if I highlight how the brackets were combined if you look at the leftmost branch you can see that we combined the labels from node six and node seven and then wrapped them in a new bracket set to create the label for node two if we continue and process all nodes which have greyed out children which is currently only node one then you see that we've combined the labels from node four and node five to create node one's new label one thing you'll notice is that in the combining phase the child labels need to get sorted this is very important because it is what ensures uniqueness now we get to process the last node here we combine and sort the labels from nodes two one and three to create the final serialised encoding which is always at the root note in hindsight this algorithm isn't too complicated but I find it extremely clever props to the original authors who created it in summary of what we just looked at one leaf nodes are assigned Knuth tuples consisting of a left and right bracket to begin with two every time you move up a layer the labels of the previous sub trees and need to get sorted lexicographically and then wrapped in brackets three you cannot process a node until you have processed all its children okay good now let's have a look at some pseudocode for testing tree isomorphisms the trees are isomorphic method takes in two undirected trees as input tree one and tree two both of which are stored as adjacency lists the first thing we do is find the center node or nodes of these two trees this is a method I'm borrowing from one slide deck ago after that a root tree one using the first center node which there will always be at least one of the root tree method is the same one we covered two videos ago check the description if you need a refresher on that now as a reminder I'm storing rooted trees in this code as tree node objects defined on this slide I do this to facilitate writing recursive algorithms but also to keep our tree data structure organized once we have rooted our tree we can serialize it with encode method let's take a closer look at what's going on there the encode method is the one which generates the unique string for our tree the first thing I do is handle the base case where we have a null node for this we can return an empty string which will cause all leaf nodes to have a left and right bracket as a starting value upon the callback for each node we maintain a list of labels for all the sub trees to generate the labels iterate through all the children of this node and recursively call the encode method adding the results to the labels list after the for loop the recursive calls have returned and the labels list is populated and ready to be sorted afterwards concatenate the labels and wrap the result in brackets coming back to the main method now the next thing we want to do is encode the second tree and compare the encoded results but if the tree has two centers we don't know which center node in the second tree is the correct one so we need to try both for this iterate over both centers and root the tree comparing the encoded result with the one from the first tree if there's any match then we know for sure that the trees are isomorphic awesome thank you for watching and if you learned something please give this video a thumbs up and subscribe for more than that like some keep your science videos thank you

Original Description

Identifying and encoding isomorphic trees Algorithms repository: https://github.com/williamfiset/algorithms#tree-algorithms Video slides: https://github.com/williamfiset/Algorithms/tree/master/slides Video source code: https://github.com/williamfiset/Algorithms/tree/master/com/williamfiset/algorithms/graphtheory/treealgorithms Rooting a tree video: https://youtu.be/2FFq2_je7Lg Tree center video: https://youtu.be/Fa3VYhQPTOI Source code video: https://youtu.be/40Jx-at2P5k 0:00 Introduction 0:24 Graph Isomorphism 3:04 Identifying Isomorphic Trees 5:39 Generating the tree encoding 7:57 Tree Encoding Summary 8:23 Unrooted tree encoding pseudocode =================================== 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

This video teaches how to identify isomorphic trees using graph theory and serialization, and provides a step-by-step guide on implementing this in code. It covers key concepts such as graph isomorphism, tree equality, and graph serialization, and demonstrates how to use algorithms and code repositories to solve this problem.

Key Takeaways
  1. Find the center of both trees
  2. Route the trees at their center nodes
  3. Generate the encoding for each tree
  4. Compare the serialized tree values for equality
  5. Assign Knuth tuples to leaf nodes
  6. Combine child labels and wrap in new brackets for nodes with greyed out children
  7. Sort child labels lexicographically
  8. Reconstruct original tree from serialized encoding
  9. Iterate through all children of a node
  10. Recursively call the encode method for each child
💡 The ahu algorithm provides a deterministic method for checking tree isomorphisms, and serialization can be used to uniquely encode trees for comparison.

Related Reads

Chapters (6)

Introduction
0:24 Graph Isomorphism
3:04 Identifying Isomorphic Trees
5:39 Generating the tree encoding
7:57 Tree Encoding Summary
8:23 Unrooted tree encoding pseudocode
Up next
Stump Grinder Carbide Wheel Grinds Hardwood To Chips
Innoforge Studio
Watch →