Rooting a tree | Graph Theory

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

Key Takeaways

Explains how to root a tree at a particular node in graph theory, including the motivation and algorithm for doing so

Full Transcript

[Music] hello and welcome my name is William and today we're having a look at how to root a tree this is one of those very basic fundamental transformations that's handy to have in your tool kit in case you want to or need to work with a rooted tree the motivation for rooting a tree is that often it can help to add structure and simplify the problem you're trying to solve routing a tree enables you to easily perform recursive algorithms it also transforms the tree to have directed edges instead of undirected edges which are generally a lot easier to work with conceptually routing a tree is like picking up a tree by a specific node and having all the edges point downwards you can retreat using any of its nodes however be cautious because not every node you select will result in a well balanced tree and if that's your objective you may need to be a little bit more selective in some situations it's also useful to have a reference to the parent node in order to be able to walk up the tree I Allah strated parent node pointers as dotted lines on this slide let's have a look at an example of how to route a tree one of the best ways to do this is with a depth-first search through the original tree and to create the rooted tree during the traversal the algorithm starts on the designated root node the new rooted tree is being displayed on the right from the root node begin the depth-first search and add notes to the rooted tree as the algorithm proceeds I will let the animation play and it should be clear what's going on you and that's ruining a tree in a nutshell let's have a look at some pseudocode for this on this slide I define a tree node object which we will use to store one of the nodes of our tree each node has a unique integer ID to identify the node as well as a reference to its parent pointer this member is generally optional but I thought to include it for completeness taking note that every node will have a parent pointer except for the root node whose parent pointer will be null additionally each node also has a list of all its child and nodes here's the algorithm to root a tree it's relatively short and sweet the input to the root tree function takes a graph G as input which is the tree we want to route the other input is the ID of the designated root node by default this is node 0 but it can be any other node the first line of the root tree method creates the root tree node object with the ID route ID a parent reference of null and no child nodes then I call the build tree method to start the depth first search reversal to root the tree as input parameters I pass in the graph G the root node as the current node and the roots parent which is null the build tree method takes exactly the same parameters we just talked about the graph the current node and the current nodes parent node reference next we enter a for loop that loops over all the neighbors of the current node which in turn will become the children of the current node because edges are undirected in the original tree we absolutely need to avoid the situation where we add a directed edge pointing back to the current nodes parent first check that the parent is not null so we don't get a null pointer exception when we're trying to access the parents ID then check if the child is equal to the parent ID and skip this note if this is true otherwise we're dealing with a proper child note so create a new node and add the child tree node to the list of the current nodes children afterwards dig deeper depth the first into the tree and do the same thing but for the newly created child node once we've finished iterating over all the neighbors of this node returned the current node and that's how you root a tree all right thank you for watching please give this video a thumbs up if you learn something and subscribe for more mathematics and computer science videos

Original Description

How to root a tree at a particular node Support me by purchasing the full graph theory course on Udemy which includes additional problems, exercises and quizzes not available on YouTube: https://www.udemy.com/course/graph-theory-algorithms Algorithms repository: https://github.com/williamfiset/algorithms Rooting tree source code: https://github.com/williamfiset/Algorithms/tree/master/com/williamfiset/algorithms/graphtheory/treealgorithms Video slides: https://github.com/williamfiset/Algorithms/tree/master/slides 0:00 Intro 0:26 How do root a tree 2:18 Rooting a tree 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

Related Reads

Chapters (3)

Intro
0:26 How do root a tree
2:18 Rooting a tree pseudocode
Up next
Stump Grinder Carbide Wheel Grinds Hardwood To Chips
Innoforge Studio
Watch →