Skip to content

Commit 14fb535

Browse files
committed
2 parents ef294e4 + a7e4def commit 14fb535

File tree

1 file changed

+65
-54
lines changed

1 file changed

+65
-54
lines changed

slides/graphtheory/trees.txt

Lines changed: 65 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,10 @@ something and i'll catch you next time.
354354

355355
ROOTING A TREE
356356

357-
Hello and welcome, welcome back for more tree videos, my name is William, and
357+
Hello and welcome for more tree videos, my name is William, and
358358
today we're looking at how to root a
359-
tree. This is a very basic transformation that's handy to have in your toolkit
359+
tree. This is one of those a very basic fundemental transformations
360+
that's handy to have in your toolkit
360361
in case you want to or need to work with a rooted tree.
361362

362363
The motivation for rooting a tree is that often it can help to add structure and
@@ -365,7 +366,8 @@ easily perform recursive algorithms, it also transforms a tree to have
365366
directed edges instead of undirected edges which are generally easier to
366367
work with.
367368

368-
To root a tree, first you need to select a root node. I'm going to pick node 0.
369+
To root a tree, first you need to select one of the tree's nodes to be the
370+
root node. I'm going to pick node 0.
369371

370372
Conceptually, rooting a tree is like "picking up" a tree by a specific node
371373
and having all the edges point downwards.
@@ -464,17 +466,17 @@ you'll see it from time to time as a subroutine in other algorithms, it can also
464466
be useful as a way of selecting a root node when you want to root a tree.
465467

466468
One thing to be aware of when finding the center of a tree is that there can be
467-
more than one center! Either you'll run into the case with a unique center which
469+
more than one center! Either you'll run into the case with a unique center, which
468470
is nice, or you'll bump into a tree like the one of the right where there are
469471
two centers. However, there can't be more than 2 centers, take a moment and
470472
think about why that's true..
471473

472474
You will notice that the center is always the middle vertex or the middle two
473475
vertices along the longest path of a tree.
474476

475-
For example, in pink is one of the possible longest paths on this tree
477+
For example, this pink path is one of the possible longest paths on this tree
476478

477-
and the center of the tree is the middle vertex along this path.
479+
and the center of the tree is the middle vertex along the path.
478480

479481
<press>
480482

@@ -499,7 +501,7 @@ After that, repeat the same process. Find identify the leaf nodes.
499501

500502
Prune them and update the degree values.
501503

502-
If you keep doing this you'll eventually reach the center of the tree.
504+
If you keep doing this, you will eventually reach the center of the tree.
503505

504506
It's a simple concept, let's do another example. Feel free to pause the video
505507
and find the center yourself using the algorithm I just described.
@@ -528,7 +530,7 @@ After this, I define two arrays. The first one is 'degree' of length 'n' which
528530
will capture the degree of each node, and 'leaves' is an array that contains the
529531
most recent layer of leaf nodes.
530532

531-
Then for the first bit of logic simply loop through all the nodes and compute
533+
Then for the first bit of logic, simply loop through all the nodes and compute
532534
the degree of each one. I do this by inspecting the adjacency list and counting
533535
the number of edges coming out of each node.
534536

@@ -552,14 +554,14 @@ process all the neighbors of those nodes
552554
and decrement the degree of the neighbor nodes. Since we're removing the current
553555
node this means that the degree of the neighboring node needs to go down. If the
554556
neighbor node after being decremented has a degree of 1 then we know it will be
555-
in the next layer of leaf nodes so add it to the new leaves array.
557+
in the next layer of leaf nodes so add it to the new_leaves array.
556558

557559
When we've finished processing the current layer increment the count variable
558560
and replaces the leaves array with the new leaves.
559561

560562
And finally return the centers
561563

562-
THanks for watching, if you learned something please give this video a thumbs
564+
Alright, thanks for watching, if you learned something please give this video a thumbs
563565
up, and subscribe for more mathematics and computer science videos.
564566

565567
--------------------------------------------------------------------
@@ -568,36 +570,32 @@ up, and subscribe for more mathematics and computer science videos.
568570

569571
Isomorphisms in trees
570572

571-
Hello and welcome, my name is William, and today we're discussing isomorphisms
573+
Hello and welcome, my name is William, and today we're diving into isomorphisms
572574
in trees -- a question of tree equality and what that means.
573575

574576
When we're talking about general graphs and ask whether two graphs are
575577
isomorphic we're asking whether they're structurally the same. In the middle,
576578
even though graphs G1 and G2 are labeled differently and may appear different
577579
they are structurally the same graph. You could for example conceptually unfold
578-
the graph on the right to match the one of the left and relabel the vertices'
580+
the graph on the right to match the one of the left and relabel its vertices'
579581
and you would have two identical graphs.
580582

581583
We can also define the notion of a graph isomorphism more rigorously because
582584
simply saying that two graphs have the same structure is not well defined.
583-
584-
If we
585-
define a graph G1 as a set of edges E1 and vertices V1 and another graph G2 in
586-
the same manner,
587-
588-
So we can say that two graph g1 and g2 are isomorphic is there exists a
585+
If we define a graph G1 as a set of edges E1 and vertices V1 and another graph G2 in
586+
the same manner, we can say that two graph g1 and g2 are isomorphic if there exists a
589587
bijection between the sets v1 and v2 such that:
590-
For all pairs of vertices which form an edge in g1, applying the function phi to
591-
the nodes of all those edges results in an edge which is present in the
588+
For all pairs of vertices which form an edge in g1, applying a function phi to
589+
the nodes of all those edges always results in an edge which is present in the
592590
graph g2.
593591
In simple terms, for an isomorphism to exist there needs to be a function which
594592
can map all the nodes/edges in G1 to G2 and vice-versa.
595593

596594
As it turns out, determining if two graphs are isomorphic is not only not
597595
obvious to the human eye, but also a difficult problem for computers.
598596
It is still an open question as to whether the graph isomorphism problem is NP
599-
complete. However, many polynomial time isomorphism algorithms exist for
600-
specialized graphs in including trees.
597+
complete. However, many polynomial time algorithms exist for
598+
specialized graph types in including trees.
601599

602600
Let's have a look at a few examples, I'm going to show you two trees are you
603601
have to tell me if they're isomorphic or not.
@@ -614,17 +612,17 @@ In terms of writing an algorithm for identifying isomorphic trees, there are
614612
several very quick probabilistic, usually hash or heuristic based algorithms
615613
for identifying isomorphic trees. These tend to be very fast, but also more
616614
error prone due to hash collisions in a limited integer space. However,
617-
they do have a place when it comes to absolutely enormous trees and in
618-
a competitive programming setting.
615+
they do have a place when it comes to competitive programming and testing
616+
the equality of absolutely enormous trees.
619617
The method we'll be looking at today involves serializing a tree into a unique
620-
encoding. The encoding is simply a string that represents a tree, and if
618+
encoding. This encoding is simply a string that represents the tree, and if
621619
another tree has the same encoding then both trees are isomorphic.
622620

623621
When going about encoding a tree, you can directly serialize an unrooted tree,
624622
but in practice I find it easier to write code to serialize a rooted tree, just
625623
my personal preference.
626624
However, one small caveat to watch out for if your going for the rooted tree
627-
approach is to make sure you root your two trees T1 and T2 with the same root
625+
approach is to make sure you root both your two trees T1 and T2 with the same root
628626
node before you begin the serialization process, otherwise you'll get two
629627
different encodings.
630628

@@ -638,7 +636,7 @@ isomorphic, that's what we're trying to figure out.
638636
From here, find the center of both trees, don't worry about the case where one of
639637
the trees has more than one center, we'll see how to handle that later.
640638

641-
Next, root the trees where their center nodes are.
639+
Next, root the trees at their center nodes.
642640

643641
Then generate the encoding for each tree and compare the serialized tree values
644642
for equality.
@@ -647,21 +645,23 @@ The tree encoding is simply a sequence of left '(' and right ')' brackets.
647645
However, you can also think of them as 1’s and 0’s which is just a really large
648646
number if you prefer.
649647

650-
From this encoding is should also be possible to reconstruct the original tree
648+
From this encoding it should also be possible to reconstruct the original tree
651649
solely based on the encoding. I will leave this as an exercise to the reader.
652650

653-
The AHU, short for the three authors of the algorithm, is a clever serialization
654-
technique for representing a tree as a unique string.
651+
The AHU algorithm, short for the initials of the three authors who created the
652+
algorithm, is a clever serialization technique for representing a tree as a unique string.
655653
Unlike many tree isomorphism invariants and heuristics, AHU is able to capture
656-
a complete history of a tree’s degree spectrum and structure. In turn this
657-
ensures a deterministic method of checking for tree isomorphisms. Let's take a
654+
a complete history of a tree’s degree spectrum and structure. In turn, this
655+
ensures a deterministic method of checking tree isomorphisms. Let's take a
658656
closer look at how it works.
659657

660-
Suppose we have this tree we wish to serialize.
658+
Suppose we have this tree that we wish to serialize.
661659

662660
The first thing we do is assign all leaf nodes Knuth tuples which are a pair of
663661
left and right brackets.
664662

663+
<press>
664+
665665
After that, for all nodes with all grayed out children combine their children's
666666
labels and wrap them in a new pair of brackets.
667667

@@ -671,11 +671,12 @@ and then wrapped them in a new bracket set to create the label for node 2.
671671

672672
<press>
673673

674-
Let's continue and process all nodes with grayed out children which is just
675-
node 1, then you see that we've combined the labels from node 4 and node 5.
674+
If we continue and process all nodes which have all grayed out children which is
675+
currently only node 1, then you see that we've combined the labels from
676+
node 4 and node 5 to create node 1's new label.
676677

677678
One thing you'll notice is that in the combining phase the child labels need to
678-
get sorted, this is very important.
679+
get sorted, this is very important because it is what ensures uniqueness.
679680

680681
<press>
681682

@@ -684,60 +685,70 @@ and now we get to process the last node
684685
here we combine and sort the labels from nodes 2, 1 and 3 to create the final
685686
serialized encoding which will always be at the root node. In hindsight this
686687
algorithm isn't too complicated, but I find it extremely clever. Props to the
687-
original authors.
688+
original authors who created it.
688689

689690
<press>
690691

691692
In summary of what we just looked at:
692-
1) First, leaf nodes are assigned Knuth tuples consisting of a left and right
693+
1) One, leaf nodes are assigned Knuth tuples consisting of a left and right
693694
bracket to begin with.
694-
2) Every time you move up a layer the labels of the previous subtrees get sorted
695+
2) Two, every time you move up a layer, the labels of the previous subtrees get sorted
695696
lexicographically and wrapped in brackets.
696-
3) You cannot process a node until you have processed all its children.
697+
3) Three, you cannot process a node until you have processed all its children.
697698

698699
Alright, let's have a look at some pseudocode.
699700

700701
The treesAreIsomorphioc method takes in as input two undirected trees, tree1 and
701702
tree2 stored as adjacency lists.
702703

703704
The first thing we do is find the center node or nodes of these two trees.
704-
This is a method i'm borrowing from two slide decks ago.
705+
This is a method i'm borrowing from 1 slide deck ago.
705706

706-
After that I root tree1 using the first center node, which there will always be
707-
at least 1 of. The rootTree method is the same one we covered in the last
708-
video, check the description if you missed it.
707+
After that, I root tree1 using the first center node, which there will always be
708+
at least 1 of. The rootTree method is the same one we covered 2 videos ago,
709+
check the description if you missed it.
709710

710-
As a reminder, rooted trees are stored recursively in tree node objects. We do
711-
this to facilitate writing recursive algorithms, but also to keep our tree data
711+
As a reminder, I'm storing the rooted trees in this code as tree node objects
712+
as defined on this slide.
713+
We do this to facilitate writing recursive algorithms, but also to keep our tree data
712714
structure organized.
713715

714-
Once we have rooted our tree we can serialize it with the encode method. Let's
716+
Once we have rooted our tree, we can serialize it with the encode method. Let's
715717
take a closer look at what's going on in there.
716718

717719
The encode method is the one which generates the unique string for our tree.
718720

719721
The first thing I do is handle the base case where we have a null node. For this
720-
we can return an empty string which will cause all leaf nodes to have a Knuth
721-
tuple as a starting value on the callback.
722+
we can return an empty string which will cause all leaf nodes to have a
723+
left and right bracket as a starting value upon the callback.
722724

723-
For each node we maintain a list of labels for all this nodes subtrees.
725+
For each node, we maintain a list of labels for all the subtrees. To generate
726+
the labels,
724727
Iterate through all the children of this node and recursively call the encode
725-
method and add the result to the labels list.
728+
method adding the results to the labels list.
726729

727-
After the recursive calls return and the labels list of populated sort
728-
the labels.
730+
After the for loop, the recursive calls have returned and the labels list
731+
is populated and ready to be sorted.
729732

730733
Afterwards, concatenate the labels and wrap the result in brackets.
731734

732735
Coming back to the main method. Now the next thing we want to do is encode the
733736
second tree and compare the encoded results, but if the tree has 2 centers we
734737
don't know which center node in the second tree is the correct one, so we need
735-
to try both. For this iterate over both centers and root the tree comparing the
738+
to try both. For this, iterate over both centers and root the tree comparing the
736739
encoded result with the one from the first tree. If there's any match then we
737740
know the trees are isomorphic.
738741

739742
<press>
740743

744+
Thank you for watching, if you learned something please give this video a thumbs up
745+
and subscribe for more mathematics and computer science videos.
746+
747+
748+
749+
750+
751+
741752

742753
--------------------------------------------------------------------
743754
--------------------------------------------------------------------

0 commit comments

Comments
 (0)