Stable Sort
Stable Sort
  • 17
  • 666 331
Disjoint Set Data Structure - Union Find Tutorial
In this tutorial we will learn all about a data structure called Disjoint-Set. Sometimes it’s also referred to as Union-Find or Merge-Find Set.
This is a remarkably simple data structure yet it has the power to solve certain problems that are otherwise very difficult to deal with. To better understand its purpose, let’s consider the following problem:
Suppose you are implementing a new social network, similar to Facebook. There are users, each of which could be friends with some number of other users. In this illustration, lines connecting 2 users indicated that those two users are friends and thus form a group. When a user from one group makes a friend with a user from another group, then those two groups become connected and from that point on they are considered to have been merged into a single large group. Meaning, all of the users in that one large group are now friends with one another.
In more concrete terms, imagine that each user has an integer ID and you are given a list of integer pairs. Each integer pair indicates that those two particular users are friends.
When you have millions and millions of users, the challenge is to answer the following questions:
Given two user IDs, can you check if they are friends?
What is the size of the largest group of friends?
Alternate formulations of the same problem are used in coding interviews. Here are a couple of samples:
leetcode 547 "Number of Provinces":
leetcode.com/problems/number-of-provinces/
hackerrank "Components in a graph":
www.hackerrank.com/challenges/components-in-graph/problem
Java implementation of Disjoint Set with Union Find operations:
bitbucket.org/StableSort/play/src/master/src/com/stablesort/djset/DisjointSet.java
More information on Ackermann function:
en.wikipedia.org/wiki/Ackermann_function
Music: bensound.com
Переглядів: 14 757

Відео

KD-Tree Nearest Neighbor Data Structure
Переглядів 119 тис.4 роки тому
KD-Tree is a data structure useful when organizing data by several criteria all at once. Consider an example where you have a set of points on a 2 dimensional plane. Now suppose you are asked to find the nearest neighbor of some target point. What data structure would you use to store these points to be able to solve that problem? en.wikipedia.org/wiki/K-d_tree A naive approach would be to perh...
Toggling US Electoral Districts in O(log N) - Coding Interview Problem
Переглядів 1,6 тис.4 роки тому
Given a Boolean array of districts, write two methods: isDem(i) - to determine if the district at position i is Democratic or Republican toggle(i, j) - to toggle all of the districts in the range form i to j But here is the kicker - both methods should run in less than O(n) time complexity. This is a fun brain teaser problem that you may face during a coding/programming interview for Software E...
Godel's 1st Incompleteness Theorem - Proof by Diagonalization
Переглядів 68 тис.4 роки тому
Godel’s Incompleteness Theorem states that for any consistent formal system, within which a certain amount of arithmetic can be carried out, there are statements which can neither be proved nor disproved. Thereby setting bounds to what mathematics could prove. English translation of the original paper: www.jamesrmeyer.com/ffgit/godel-original-english.html In this video we’ll examine the context...
Matrix Chain Multiplication - Dynamic Programming (DP) Print Parentheses - Java source code
Переглядів 11 тис.4 роки тому
In this tutorial, we show how to print parenthesis around matrices such that the cost of multiplication is minimized. Matrix Chain Multiplication is a classic problem in computer science that involves finding the most optimal way of multiplying a chain of 2 dimensional matrices. Since matrix multiplication is associative, matrixes could be multiplied simply sequentially: A1 * A2 * A3 ... Or, as...
Convex Hull Algorithm - Graham Scan and Jarvis March tutorial
Переглядів 148 тис.4 роки тому
Given a set of points on a 2 dimensional plane, a Convex Hull is a geometric object, a polygon, that encloses all of those points. The vertices of this polygon maximize the area while minimizing the circumference. Note that if some additional points were to be included, then the covering area is reduced while the circumference is increased. Likewise, if some of the vertices of the Convex Hull a...
Knuth Morris Pratt (KMP) String Search Algorithm - tutorial with failure function in Java
Переглядів 51 тис.4 роки тому
This tutorial explains how the Knuth-Morris-Pratt (KMP) pattern matching algorithm works. Animated examples are used to quickly visualize the basic concept. Then the source code of an implementation written in Java is discussed, along with its running time complexity. There are two parts to the algorithm. The first one creates a "failure function", which is nothing more than an array that holds...
Partition to K Equal Sum Subsets - source code & running time recurrence relation
Переглядів 10 тис.4 роки тому
Partition a set of positive integers into K subsets, such that the sums of the numbers of each subset are equal. 698. Partition to K Equal Sum Subsets leetcode.com/problems/partition-to-k-equal-sum-subsets/ So the function doesn’t just return true or false based on whether it is possible to create K partitions or not. But actually creates those partitions. We assume, these are multi-sets which ...
Partition Problem - 2 subsets of equal sum, as closely as possible - tutorial and source code
Переглядів 23 тис.4 роки тому
Partition a set of positive integers into two subsets such that the sum of the numbers in each subset adds up to the same amount, as closely as possible. This is an NP-complete problem, dubbed as “the easiest hard problem”. First, we’ll develop a recursive solution with a source code walk through. And then we’ll discuss a completely different solution that uses dynamic programming. For this pro...
Treap (Tree + Heap) Data Structure - Tutorial with Statistical Analysis
Переглядів 15 тис.4 роки тому
A computer science data structure called "Treap" is a combination of a Binary Search Tree and a Heap. This introductory tutorial explains how Treap data structure uses randomly generated numbers, called "priority" to construct and maintain a reasonably balanced tree. The tree is constructed as if it’s a heap, with maximum priority value being at the root of the tree. Note that the tree construc...
K Lists of Sorted Integers, Find Min Range - Google Interview Problem
Переглядів 2,5 тис.4 роки тому
leetcode 632 Smallest Range You have K lists of sorted integers. Write a function that finds the smallest range that encompasses at least one number from each of the K lists. This is a popular software engineering interview problem, given by companies such as Google, Amazon, and Facebook. In this episode, we’ll develop a strategy to solve it and then we’ll step through the source code of an act...
N Coins in a Row Game (Pots of Gold interview problem) optimal solution in O(n)
Переглядів 12 тис.4 роки тому
We’ll present 3 different solutions. The first one relies on nothing more than just logical reasoning and requires no computer science background whatsoever. The second solution is based on a recursive function that makes use of memorization technique and has order n-squared running time. Finally, we’ll go over an algorithm that finds an optimal solution and runs in linear time. The game is pla...
Segment Tree Data Structure - Min Max Queries - Java source code
Переглядів 27 тис.4 роки тому
In this tutorial we’ll talk about a data structure called Segment Tree. We’ll go over what it’s for, when to use it, and then we’ll step through the source code of a particularly efficient implementation, both in terms of running time and the amount of space that it requires, written in Java. It uses an array that's exactly twice the size of the original input array. Thus, no space is being was...
Fenwick Tree (Binary Index Tree) - Quick Tutorial and Source Code Explanation
Переглядів 59 тис.4 роки тому
In this tutorial we’ll discuss a computer science data structure called "Fenwick Tree", also known as "Binary Index Tree". This data structure is used to efficiently perform range queries, such as addition, multiplication and XOR. We’ll develop a simple visual explanation of how it works. Then, we’ll dive into the details of how to actually implement it by walking over source code of sum() and ...
Rolling Hash Function Tutorial, used by Rabin-Karp String Searching Algorithm
Переглядів 35 тис.4 роки тому
In this tutorial we are going to discuss how to implement a "rolling hash function". We will derive a concrete implementation and show how it could be used to efficiently find a pattern string in a text. The search algorithm that makes use of it was invented by Richard M. Karp and Michael O. Rabin (1987). The main benefit of a rolling hash function is that regardless of the length of the search...
0/1 Knapsack Problem Using Dynamic Programming - Tutorial & Source Code
Переглядів 13 тис.4 роки тому
0/1 Knapsack Problem Using Dynamic Programming - Tutorial & Source Code
Longest Increasing Subsequence O(n log n) dynamic programming Java source code
Переглядів 57 тис.4 роки тому
Longest Increasing Subsequence O(n log n) dynamic programming Java source code

КОМЕНТАРІ

  • @ThereIsNoRoot
    @ThereIsNoRoot 5 днів тому

    What! You can't tease an O(n log(h)) algorithm!

  • @vetiarvind
    @vetiarvind 6 днів тому

    not sure why in Query, if you skip the right node in a To, parent will always catch it.. what it loop terminates before that? I'm so stupid

  • @ValentynPonomarenko-h8k
    @ValentynPonomarenko-h8k 8 днів тому

    Thank you for clear explanation.

  • @mrmrigank7154
    @mrmrigank7154 13 днів тому

    awesome explanation to give the intuition, now I just have to think about finding the right pile in logn for each card, and nice shirt btw!

  • @JP-ic5we
    @JP-ic5we 20 днів тому

    God bless you zen programmer this information is super useful

  • @RomuloMagalhaesAutoTOPO
    @RomuloMagalhaesAutoTOPO 22 дні тому

    Clear like crystal.

  • @parthivreddy7989
    @parthivreddy7989 23 дні тому

    orz

  • @DevInDetour
    @DevInDetour Місяць тому

    Awesome explanation! Thanks

  • @SanjilDhakal-1729
    @SanjilDhakal-1729 Місяць тому

    Why we are defining such function which can not give all possiable Gödel numbers ..I'm talking about negation function you defined 10:20 ..F j(n) => ~P(F n(n))..this function checks provability of statement of diagonal Gödel number only ...Or I'm still not getting please explain .?????🤯🤯

  • @EvanMilliken
    @EvanMilliken Місяць тому

    The best possible explanation of both algorithms; The clear and exact visual representation of the processes, made it easier to understand. The explanation was quite precise and to the point.

  • @rikardez
    @rikardez Місяць тому

    Que gran explicación muy fácil de entender

  • @r6518
    @r6518 Місяць тому

    amazing video!

  • @Wuaners
    @Wuaners Місяць тому

    What about if result is negative after subtract value of i from target?

  • @SHUBHAMJHA-o3g
    @SHUBHAMJHA-o3g Місяць тому

    Best explanation of fenvik tree I have seen

  • @williamrhopkins
    @williamrhopkins Місяць тому

    Excellent. This is by far and away the best exposition of Godel's work I have come across. I appreciate the programming language approach at the end. I believe this was how Alan Turing came at the subject.

  • @StopBuggingMeGoogleIHateYou
    @StopBuggingMeGoogleIHateYou Місяць тому

    Your content was great; it's too bad you stopped, but thanks for the stuff that you did share with the rest of us.

  • @LeiZhu-v4t
    @LeiZhu-v4t Місяць тому

    Other than the mistake with Pr(s) being a predicate instead of a set of Godel numbers, the first proof is incorrect in defining F_i(n) as functions that generate natural numbers. It needs to generate statements since the input A of P(A) is a statement not a natural number, otherwise P(F_n(n)) wouldn't make sense.

  • @humanvegetable
    @humanvegetable Місяць тому

    🫡

  • @larsprins3200
    @larsprins3200 2 місяці тому

    At 13:07 : we just care about "valid" functions, requiring that their character string is of finite length. Defining Q(n) as F_n(n), its character string would need to include the character string for the formula of each F_i(n) of which there are infinitely many. Doesn't this mean that Q(n) is of *_infinite length_* and hence not a "valid" function? Q(n) would thus not be a contradiction to the original assumption that it is possible to list all "valid" functions, i.e., list all possible formulas with one free variable?

  • @Sleeping_Aizawa
    @Sleeping_Aizawa 2 місяці тому

    This is what I have been looking for

  • @homomorphichomosexual
    @homomorphichomosexual 2 місяці тому

    lovely video, thank you so much!

  • @Az-_-3.1415
    @Az-_-3.1415 2 місяці тому

    nice vid bro, but switch the shirt

  • @JIHYELEE-h2m
    @JIHYELEE-h2m 2 місяці тому

    4:14 so cute

  • @dashdoom8452
    @dashdoom8452 2 місяці тому

    This content deserves millions of subs, I hope you make more videos soon!

  • @estring123
    @estring123 2 місяці тому

    wow i learned something from this video

  • @unexpectedTrajectory
    @unexpectedTrajectory 2 місяці тому

    Your 2nd proof is quite interesting (my background is also CS though I love math). I think, though, that it's a bit off, or that your conclusion is. The problem I'd assumption/criteria #4 for the functions/programs. You can't know for all programs which do or don't halt (I'm pretty sure you know that.) So you absolutely CAN make a sequential list of programs that compile (setting aside my spidy sense that certain programs probably are valid but don't compile because they would require the compiler to solve the halting problem...). What you can't do is write a generic program that diagonalizes across those programs or arbitrarily simulates any of them and then ads 1 AND always gives an output, because you still haven't solved the halting problem. I think you basically recreated a proof of the halting problem, rather then proving that the programs aren't innumerable. The set of all valid programs is clearly a proper subset of the set of all strings (interpreted as programs), and the set of all strings is clearly of the same cardinality as the rational, which Cantor showed to be numerable (countable) by the other kind of diagonalization. Let me know if you think I misunderstood you. Thanks.

    • @unexpectedTrajectory
      @unexpectedTrajectory 2 місяці тому

      Ok, re-relistened to that section. Yes, I'm convinced the issue is #4. You can innumerate/list all valid programs, but you can't list all programs that halt. Q(n) IS on the list of all programs, it just doesn't halt. Actually imagine it running on itself. It's going to infinitely recurse (simulating f requires simulating f requires simulating f requires...)

  • @unexpectedTrajectory
    @unexpectedTrajectory 2 місяці тому

    3:25 I thought we also knew that no system could prove its own consistency?

  • @Neamhain97
    @Neamhain97 3 місяці тому

    2:05-2:25 I don't like how vague these words are and how there seems to be no consensus on what these words actually mean.

  • @JuranHuang
    @JuranHuang 3 місяці тому

    Every second of this 9 mins video is worth watching. You sir really are a talented teacher, showing the source code makes the concept so much easier to comprehend.

  • @alexanderying1558
    @alexanderying1558 3 місяці тому

    10:01 Isn't the set of sequences here uncountable, i.e. there exists no such enumeration? If so, that would be a big issue in this proof idea...

  • @dashdoom8452
    @dashdoom8452 3 місяці тому

    You are the goat

  • @dashdoom8452
    @dashdoom8452 3 місяці тому

    Amazing explanation, you are a god

  • @TranCao-w8r
    @TranCao-w8r 3 місяці тому

    how come the point (13,3) is closer to (9,4) than (8,7)? and because of that we never traverse to the point (10,2), why does this happen?

  • @FlorinGN
    @FlorinGN 3 місяці тому

    Really nice explanation, thank you!

  • @SilvioZanin
    @SilvioZanin 3 місяці тому

    My question is: When you call A = for example, F1(n) = n+1, g(A) is the Gödel number of this, and there's a proof of F1(n) that's for example, Fn(n) = B, and g(B) = p, that's the proof of statement A. When you call P(A), this is the proof of statement A, that's p (g(B) or is ANOTHER proposition that's "A is provable"? Because both can be associated with a Gödel number and, if there's a number for both of them, there's a formula Fj(n)...

  • @idontknow-wl6su
    @idontknow-wl6su 3 місяці тому

    FINALLY,!!!! i understand this :D, this video helpme a lot, thanks : )

  • @mahanmoshiry7304
    @mahanmoshiry7304 4 місяці тому

    I really do appreciate that you described the theorem rigorously and didn’t “dumb it down” , while maintaining attraction.

  • @Khashayarissi-ob4yj
    @Khashayarissi-ob4yj 4 місяці тому

    With luck and more power to you.۔ hoping for more videos.

  • @SyntekkTeam
    @SyntekkTeam 4 місяці тому

    My solution involved bitmasks. I think that can bring it down to constant time, but I may be overlooking something

  • @himachal8252
    @himachal8252 4 місяці тому

    Not that kind of Hash

  • @zohanahmed-l4g
    @zohanahmed-l4g 4 місяці тому

    the actual Goat!!!!

  • @asmithgames5926
    @asmithgames5926 4 місяці тому

    I believe all proofs of Godel's FIrst Incompleteness Theorem are invalid, but the theorem itself is true. Self-contradictory statements shouldn't be allowed (and aren't even a normal feature of theorems we write in math). Think about all the hand-waving these arguments have. And in Godel numbering, attempting to encode "This statement is not provable" wouldn't be so easy. There is no "this statement" symbol, so to encode this concept in the Nth statement, we'd have Statement(N): "Statement(N) is not provable". Encoding this would create a large number much greater than N, which would result in this NOT being the Nth valid statement. So the Godel numbering itself would prevent against self-referentiality, unless the arithmetic specifically had a symbol for encoding the concept "this statement". So for any system, you could add a "this statement" symbol to its Godel numbering, but by doing so you'd be making your system 'weird', in that most mathematical systems don't bother with self-referentiality. Basically, I'm conjecturing that Godel's First Incompleteness Theorem is "incomplete" (unproven) for non-self-referential systems. (Meaning, it is possibly true, but hasn't been proven.) Put another way, Godel's First Incompleteness Theorem is the Tyler Durden of mathematics. This is still the best video I've seen on the topic so far.

  • @asmithgames5926
    @asmithgames5926 4 місяці тому

    Eagerly awaiting your Second Incompleteness Theorem video!!

  • @stretch8390
    @stretch8390 4 місяці тому

    You are a fantastic educator. If you've ever watched competitive programming streams where they explain the answers you'll know being good at something and being able to explain something are two very different things. You have done the community quite a service in bringing such clear explanations!

  • @AL-jr7zu
    @AL-jr7zu 4 місяці тому

    Thank you, I loved this video. It was short and simple.

  • @plucas2003
    @plucas2003 5 місяців тому

    isso é sensacional, fico muito triste que não pude ver isso na minha turma de lógica matemática!

  • @ggorkk
    @ggorkk 5 місяців тому

    Hello sir, please can you explain how does query work for array with size being non power of 2?

  • @ivangaborige8623
    @ivangaborige8623 5 місяців тому

    Gödel's first incompleteness theorem is fundamentally logically flawed. Even the math could be complete. - A fact of scientific history. He is afraid, but rather very close to the whole, a profession in the field of mathematics, he laughed and laughed when Gödel already formalized the mathematical formalization. He just wanted to use numbers. It didn't even go through, it already failed. In mathematics. This, in turn, helped Neumann in computing. Because, paradoxically, Gödel's only usable theory was Gödel numbering / coding. Which also had practical benefits. Of course, only at first in computer technology. When every character and bit had to be saved. Well, what was useful was exactly what failed in mathematics. What is useless and wrong has been raised on the altar.

  • @danieleppelsheimer9273
    @danieleppelsheimer9273 5 місяців тому

    Great explanation!!! Thank you

  • @pallharaldsson9015
    @pallharaldsson9015 5 місяців тому

    7:35 The Gödel's encodings clever but it does NOT result in finite numbers, even with a finitely long statement (in general). Is that a problem? I don't know. I.e. the empty statement is always 0, but even a single-letter statement seems problematic. While e.g. the single-letter "f" is 2^3 = 8 *given* the table of letters there ("f" = 3 is ok, it doesn't have to correspond to ASCII). Unlike ASCII and Unicode which are finite encodings, for any possible statement the set of letters needs to be infinite, and then "f" could be an arbitrarily high number depending on where you put it into the encoding table, and having it in the first few elements of the table is an arbitrary ordering of letters. I'm not saying it needs to be "last", infinitely high number, but it could be. There ARE infinitely many numbers, integers, AND rationals, so it's unclear to me if it's a problem, i.e. you can encode each pair of numbers for a statement and a proof into a rational (only not divide by 0, doesn't seem like a real limitation, though I'm not sure).