Haskell sum types from set theory's disjoint union

So as I understand, something like this

data List a = Nil | Cons a (List a)

is considered a sum type because it is a sort of set theory "sum" or "disjoint union" of the two constructors. A disjoint union, as I understand, is just when you have a union of sets that have nothing shared; you "summed" the sets. So we imagine our List above a sort of disjoint union of Nil and Cons a (List a). Correct? What gets confusing is from a Wikipedia article on Algebraic data types :

>In set theory the equivalent of a sum type is a disjoint union, a set whose elements are pairs consisting of a tag (equivalent to a constructor) and an object of a type corresponding to the tag (equivalent to the constructor arguments).

The disjoint union article was fairly understandable, but I'm not making the connection between my List above and this quote above about tags and constructors and constructor arguments. Nice if someone could shine some light on this for me.

πŸ‘︎ 8
πŸ’¬︎
πŸ‘€︎ u/teilchen010
πŸ“…︎ Nov 19 2021
🚨︎ report
Disjoint set/Union-Find maze generation - how to?

I have a general understanding of the data structure itself, and am allowed to use the textbook code for union find itself. I have this pretty cool looking assignment but am not quite sure I get how it works code wise. I will try to say my point of view below, to show my logic and hopefully someone could nitpick.

Assignment:

----------------------------------------------------------

Write a program that generates mazes of arbitrary sizeΒ using the union-find algorithm.Β  A simple algorithm to generate the maze is to start by creating an N x M grid of cells separated by walls on all sides, except for entrance and exit.Β  Then continually choose a wall randomly, and knock it down if the cells are not already connected to each other. If we repeat the process until the starting and ending cells are connected, we have a maze.Β  It is better to continue knocking down the walls until every cell is reachable from every cell as this would generate more false leads in the maze.

Test your algorithm by creating a 10 x 10 grid, and print all the walls that have been knocked down.Β  Draw the resulting maze (hand-drawing is acceptable) andΒ include the output of your programΒ (see below).

  • (If you don't include the output of your program, I won't be able to check that your drawing is correct, since your program will use random numbers.)

Solutions that do not use the union-find algorithm will receive 0 points. You can use the textbook code or implement union-find yourself, but I recommend using the textbook code. If you implement it yourself, any bugs in your implementation will affect your correctness grade.

Suggested strategy:

This is one way to approach this problem; you don't have to follow it, as long as you still make use of the union-find algorithm.

  1. One way to represent a maze is with a 2-dimensional array ("NxM grid" is a strong hint that you want to use a 2D array):
  • int[][] maze = new int[n][m]; // Assume n and m are already defined
  • Once you have this variable declared, maze[i][j] represents one square in the grid, assuming 0 < i < n and 0 < j < m.
  1. You can now think of the maze as a set of (i, j) pairs. A pair (i, j) represents the cell in row i and column j of your 2D array.
  2. Your sites for union-find consist of this set of (i, j) pairs. (You may want to define your own Pair class that has two int fields; or you can use an array of length 2.)
  3. When you call union() on tw
... keep reading on reddit ➑

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/Willy988
πŸ“…︎ Sep 30 2021
🚨︎ report
More confused than ever... how do I use Union Find / disjoint set to generate a maze??

Context: I am using https://algs4.cs.princeton.edu/15uf/UF.java.html as my union finder. These are assignment instructions: https://imgur.com/a/aFgqR4f

Basically, I have to have a "m x n" maze, and then use UF to generate a path in which all cells or "pairs" are connected. The thing is, the random chosen cell to have a wall broken down must be adjacent to the other cell, and if the cell is already connected, don't break the wall.

The issue is Union Find only takes a single int argument, presumably to create an array.

In my code I created "Pair" class like coordinates for a grid. What do I do for Union Find to find a random cell and then do it's work? Here is my code so far for reference of my thought process.

import edu.princeton.cs.algs4.UF;
import java.util.Random;

public class MazeGenerator {
    Pair[] points;
    int x = 0;


    MazeGenerator(int m, int n) {
        points = new Pair[m * n];
        UF find = new UF (m*n);
        find.union(2,3);
        System.out.println(find.count());

        for (int j = 0; j &lt; n; j++) {
            for (int i = 0; i &lt; m; i++) {
                points[x++] = new Pair(i, j);
            }
        }
    }

    public class Pair {
        Pair(int x, int y) {
            this.x = x;
            this.y = y;
        }
        @Override
        public String toString() {
            return ("(" + x + ", " + y + ")");
        }
        int x;
        int y;
    }

    public boolean adjacent (Pair p, Pair q) {
        if (p.y == q.y &amp;&amp; (p.x == q.x+1 || p.x == q.x-1))
            return true;
        if (p.x == q.x &amp;&amp; (p.y == q.y+1 || p.y == q.y-1))
            return true;

        return false;
    }

    public static void main(String[] args) {
        MazeGenerator test = new MazeGenerator(4, 4);
        System.out.println(test.maze[3][3]);
        System.out.println(test.points[5]);
        System.out.println(test.points[9]);
        Pair p = test.points[5];
        Pair q = test.points[5];
        System.out.println(test.adjacent(p, q));
    }
}

I made a post about this before but was still left confused how in the world does Union Find work in conjunction to Pair, and how will I still use

... keep reading on reddit ➑

πŸ‘︎ 4
πŸ’¬︎
πŸ‘€︎ u/Willy988
πŸ“…︎ Oct 03 2021
🚨︎ report
Making a maze generator using Union-Find/Disjoint Set, some of my logic is just wrong... need advice.

Thanks for the help yesterday, I got some ideas and started hacking away at the problem. Basically the instructions are that we need a theoretical maze using points, and then union points that are adjacent to each other (aka breaking walls/edges). Randomly choose cells to connect until all cells are in one component.

The issue: I am not sure how of two things I am doing-

My method to find the adjacent cell is definitely not working right... here is my attempt to find an adjacent cell that is either: South, west, east, north. When I did a test run with my 4 x 4 grid, it seems like findAdjacent()
is not doing the checks I would expect, maybe my logic is wrong.

What I expect is for said method to generate a number 0-3 (north, south, east, or west), and then switch should ideally update the point accordingly. The issue is that it is outputting (4,3)
when the biggest coordinate in a 4 x 4 grid is (3,3).

I see the issue, being that if one case does not pass the if test, it will test the next case but not the previous case. I just don't know what to do....

Attempt so far:

import edu.princeton.cs.algs4.UF;
import java.util.Random;

public class MazeGenerator {
    Pair[] points;
    int x = 0;
    int rows;
    int columns;
    UF map;
    Random rand = new Random();
    
    MazeGenerator(int m, int n) {
        points = new Pair[m * n];
        rows = m;
        columns = n;

        //Note: each index in UF is to be mapped to its Pair index counterpart.
        //Example: points[1] == find[1]
        map = new UF (m*n);

        //Populate points array with Pair objects
        for (int j = 0; j &lt; m; j++) {
            for (int i = 0; i &lt; n; i++) {
                points[x++] = new Pair(i, j);
            }
        }

        //test. delete later
        map.union(encode(points[3]),encode(points[4]));
        System.out.println(map.count());

    }

    public void findPath() {
        int current;
        int neighbor;

        while(map.count() &gt; 1) {
            current = rand.nextInt(rows * columns);
            neighbor = findAdjacent(points[current]);

            if (map.find(encode(points[current])) == map.find(neighbor)) {
                System.out.println("Connected in same component already!");
            }
            else
... keep reading on reddit ➑

πŸ‘︎ 5
πŸ’¬︎
πŸ‘€︎ u/Willy988
πŸ“…︎ Oct 03 2021
🚨︎ report
Is the union of a countably infinite amount of countably infinite disjoint sets countably infinite?

Put another way, can we map the natural numbers to (countably infinite set) union (countably infinite set) union (countably infinite set) union... where each countably infinite set is unique?

πŸ‘︎ 3
πŸ’¬︎
πŸ“…︎ Jul 02 2020
🚨︎ report
What do we get when we take the union of uncountably many mutually disjoint sets with non-zero (sigma-additive) measure?

Edit2: OK, I think my question is a little confused. I will first have to go back and try to understand the proof better before I can try to make it clearer.

Edit: context is pure set theory, say, ZFC

Just trying to wrap my head around measurable cardinal numbers, sorry for (quite possible) confusion. In proving that if there is a (non-trivial proabilistic sigma-additive) measure on some set S, then some cardinal k less or equal |S| is weakly inaccesible, we use a lemma that shows that there is no uncountable, mutually disjoint subset of P(S) such that each element of it has non-zero measure.

This raised the question: what do we get if we have an uncountable, mutually disjoint collection of sets each with measure > 0?

Let X be a set such that some subset of P(X) is an uncountable and mutually disjoint collection of sets with non-zero measure. Can we say anything interesting about X, apart from that fact that it can't itself be measurable? Do these kinds of set have a name?

πŸ‘︎ 12
πŸ’¬︎
πŸ‘€︎ u/theoreition
πŸ“…︎ Jul 09 2020
🚨︎ report
Can I use 'path halving' instead of 'path compression' in 'Disjoint Set Union'?

Regarding Disjoint Set Union, the most used method of optimizing the algorithm is to use the rank and path compression. However, I got to hear about path halving in the Wikipedia link below and in the article that chooses to use path halving for implementing. Does path halving still a great choice for the Disjoint Set Union Algorithm? Are there any ups and downs to these methods?

Thank you!

> Path compression flattens the structure of the tree by making every node point to the root whenever Find is used on it

> Path halving makes every other node on the path point to its grandparent.

Wikipedia: https://en.wikipedia.org/wiki/Disjoint-set_data_structure

Article of implementing 'Disjoint Set Union': https://www.hackerearth.com/practice/notes/disjoint-set-union-union-find/

πŸ‘︎ 8
πŸ’¬︎
πŸ‘€︎ u/poream3387
πŸ“…︎ Apr 17 2020
🚨︎ report
Help with Union-Find Disjoint Sets problem

This is the problem link: https://www.codechef.com/problems/ABROADS
I want to ask is it possible to keep track of how many sets are there (without doing linear search) and is it possible (and if it is, how) to know if I need to "rebuild" my sets when I delete a road (I may delete it but still other roads are connecting the graph).
Please, if you are willing to do so, try not to write code, but try to explain the concept of solving this problem (as much as you want). Thank you in advance!

πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/ZenWoR
πŸ“…︎ Apr 21 2020
🚨︎ report
Related to the disjoint union
πŸ‘︎ 19
πŸ’¬︎
πŸ‘€︎ u/Jonahthan314
πŸ“…︎ Dec 08 2019
🚨︎ report
Using Disjoint Set (Union-Find) to create Maze coderscat.com/using-disjo…
πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/alfrednoon
πŸ“…︎ Jan 19 2020
🚨︎ report
Partitions - A disjoint-sets/union-find implementation that allows for efficient iteration over the elements of a set.

This crate is published on crates.io and fully documented.

The main struct of this crate is PartitionVec&lt;T&gt; which has the functionality of a Vec&lt;T&gt; and in addition divides the elements of this vector in sets. The elements each start in their own set and sets can be joined with the union method. You can check if elements share a set with the same_set method and iterate on the elements in a set with the set method. The union and same_set methods are extremely fast and have an amortized complexity of O(Ξ±(n)) where 'Ξ±' is the inverse Ackermann function and n is the length. This complexity is proven to be optimal and Ξ±(n) has value below 5 for any n that can be written in the observable universe. The next element of the iterator returned by set is found in O(1) time.

We store a link to the next element of the set which allows for efficient iteration in constant time. For this we store an additional usize value called the link in addition to the rank and parent that are normaly stored in a Disjoint-Sets implementation. To compensate for this a compact implementation feature is provided that, when turned on, only stores two usize values. Because rank is always very small we will only ever need 6 bits to store it. An explanation of this is in the comments in the metadata module. We use 3 bits of both the link and the parent to store this value. This does mean that the maximum amounts of elements stored on 32 and 64 bit systems are 536 870 912 and 2 305 843 009 213 693 952 respectively. This limit should never be reached under any normal circumstances but if you do the struct will panic gracefully.

πŸ‘︎ 35
πŸ’¬︎
πŸ‘€︎ u/DDOtten
πŸ“…︎ Oct 29 2018
🚨︎ report
How to get one member of disjoint union?

Suppose I have a union type:

type MyType = 
    { kind: 'foo', a: string } |
    { kind: 'bar', b: number } |
    { kind: 'baz', c: boolean }

If I only have MyType available (so, its sub-types are not defined as separate interfaces), how can I extract a particular member out of it?

I've tried the following:

type Test = MyType &amp; { kind: 'foo' }

But this doesn't seem to be working, the compiler just applies &amp; { kind: 'foo' } to all members, even though this is contradictory.

What is the right way to achieve what I want?

Thanks!

πŸ‘︎ 7
πŸ’¬︎
πŸ‘€︎ u/smthamazing
πŸ“…︎ May 20 2018
🚨︎ report
Can any set in n dimensions be approximated arbitrarily well by the union of disjoint convex sets?

I know I saw this stated in a paper somewhere, but I have no idea where to look. Does anyone know where I would find a reference to such a theorem? (this is for original research, not a class)

Edit: In title, read "region in n dimensions" as opposed to "set in n dimensions"

Edit 2: It looks like I phrased my question badly. What I mean is:

"Can any region in n dimensions be approximated arbitrarily well by the union of disjoint convex sets that are subsets of the region?"

πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/Pieater314159
πŸ“…︎ Dec 29 2016
🚨︎ report
Disjoint-set union data structure in C++ and Swift medium.com/flawless-app-s…
πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/HassanElDesouky
πŸ“…︎ Apr 12 2019
🚨︎ report
Disjoint sets Union or Union All

I was wondering if I had to combine disjoint sets from different tables, whether it would be more efficient to use Union or Union All. I realize that if I use Union All everything will be done however, since I only have to combine sets would it be smarter to use Union, while suffering no performance loss?

πŸ‘︎ 6
πŸ’¬︎
πŸ‘€︎ u/jokasaber
πŸ“…︎ Oct 28 2016
🚨︎ report
[first year CS] union of disjoint intervals

Hey guys I'm going through past exam papers getting ready for my exams next week and this question or something very similar is on all of them.

I can't find anything in my lecture notes and I can't find anything online or if I have I haven't recognized it as relating to the question. I was hoping someone could help me figure out what I need to know in order to solve this problem and also if anyone knows of online material that can help with this. I appreciate any help in this stressful time : )

πŸ‘︎ 3
πŸ’¬︎
πŸ“…︎ Dec 10 2016
🚨︎ report
If we have a half-open rectangle called P alongside a set of mutually disjoint half-open rectangles such that their union is a subset of P, how do we show the maximum of the sums of their areas is the area of P?
πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/kidrobot96
πŸ“…︎ Jun 26 2016
🚨︎ report
Question about maximum clique size for an edge-disjoint union

Suppose G=(V,E) is a simple but not necessarily finite graph, and let E',E'' be a disjoint partitioning of E into two (not necessarily finite) subsets, so that G is the edge disjoint union of the graphs G'=(V,E') and G''=(V,E'').

My (admittedly vague) question is: when can the maximum clique size of G be related to the maximum clique sizes of G' and G''? In the case that G is infinite, is it even clear that maximum clique size of G and G' both finite implies that the maximum clique size of G is finite?

I did some poking around in the literature and couldn't find any general results about this. I'm guessing that in complete generality probably little can be said, because a clique of G need not respect the clique structures of G' and G''. But in my setting, I have a particular pair of graphs G', G'' that I know a little bit about- both are (locally!) infinite, both have finite maximum cliques (and I know these sizes explicitly), both have finite chromatic number, they're bi-lipschitz equivalent to each other, etc. So I'd also be happy with results that assume extra structure (but not finiteness) on G,G',G''.

Thanks for reading; any ideas would be greatly appreciated.

EDIT: I just realized that more is true about my graphs G', G'': The automorphisms of G' and G'' are the same, and each of these automorphisms induces an automorphism of the whole graph G. Said differently, there is an infinite subgroup H of the automorphism group of G which is literally equal to (as opposed to "abstractly isomorphic to") the automorphism group of both G' and G''. So intuitively, G' and G'' sit "nicely" inside of G.

πŸ‘︎ 7
πŸ’¬︎
πŸ“…︎ Jul 23 2013
🚨︎ report
Kruskal's algorithm + Disjoint set union in Real-life applications blog.hackerearth.com/mini…
πŸ‘︎ 5
πŸ’¬︎
πŸ‘€︎ u/NarendhiranS
πŸ“…︎ Jan 27 2017
🚨︎ report
Kruskal's algorithm + Disjoint set union in Real-life applications blog.hackerearth.com/mini…
πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/NarendhiranS
πŸ“…︎ Jan 27 2017
🚨︎ report
Applicative Programming, Disjoint Unions, Semigroups and Non-breaking Error Handling applicative-errors-scala.…
πŸ‘︎ 30
πŸ’¬︎
πŸ‘€︎ u/Andrey_Karpov_N
πŸ“…︎ Oct 21 2011
🚨︎ report
[Analysis] Disjoint sets such that the measure of the union of the sets is strictly smaller than the sum of the sets

Edit: title should be ".... strictly smaller than the sum of the measures of the sets"

What the title says, I'd like an example of sets A and B in R such that

i) A ∩ B = βˆ…, but

ii) Β΅(A βˆͺ B) < Β΅(A)+Β΅(B).

πŸ‘︎ 2
πŸ’¬︎
πŸ“…︎ Mar 07 2017
🚨︎ report
Good Math, Bad Math : Graph Searches and Disjoint Sets: the Union-Find Problem scienceblogs.com/goodmath…
πŸ‘︎ 28
πŸ’¬︎
πŸ‘€︎ u/gst
πŸ“…︎ Sep 06 2007
🚨︎ report
Kruskal's algorithm + Disjoint set union in Real-life applications blog.hackerearth.com/mini…
πŸ‘︎ 13
πŸ’¬︎
πŸ‘€︎ u/NarendhiranS
πŸ“…︎ Jan 27 2017
🚨︎ report
Union-Find and Disjoint-Set Forests in Haskell jng.imagine27.com/index.p…
πŸ‘︎ 8
πŸ’¬︎
πŸ‘€︎ u/lucyfor
πŸ“…︎ Aug 22 2012
🚨︎ report
Union-Find and Disjoint-Set Forests (implemented in Shen) jng.imagine27.com/index.p…
πŸ‘︎ 4
πŸ’¬︎
πŸ‘€︎ u/lucyfor
πŸ“…︎ Aug 20 2012
🚨︎ report
Trying to complete a maze generator assignment using Union-Find / Disjoint-set, need help.

Issue: I am not sure how to use the union-find data structure provided by Sedgewick, which I imported. I need to use it in conjunction to my code I already have. Princeton UF code

You see, it only takes an argument of a single int. How will I union points if the argument is a single int??

Perhaps this is a super simple question and my brain is being smooth, but I just want to be able to namely use union() and find() in my methods...

What I did so far: I coded some things to test if the cells are adjacent, and made a general structure to test if my arrays are working.

import edu.princeton.cs.algs4.UF;
import java.util.Random;

public class MazeGenerator {
    int[][] maze;
    Pair[] points;
    int x = 0;


    MazeGenerator(int m, int n) {
        maze = new int[m][n];
        points = new Pair[m * n];
        UF find = new UF (m*n);
        find.union(2,3);

        for (int i = 0; i &lt; maze.length; i++) {
            for (int j = 0; j &lt; maze[i].length; j++) {
                maze[i][j] = 0;
            }
        }

        for (int j = 0; j &lt; n; j++) {
            for (int i = 0; i &lt; m; i++) {
                points[x++] = new Pair(i, j);
            }
        }
    }

    public class Pair {
        Pair(int x, int y) {
            this.x = x;
            this.y = y;
        }
        @Override
        public String toString() {
            return ("(" + x + ", " + y + ")");
        }
        int x;
        int y;
    }

    public boolean adjacent (Pair p, Pair q) {
        if (p.y == q.y &amp;&amp; (p.x == q.x+1 || p.x == q.x-1))
            return true;
        if (p.x == q.x &amp;&amp; (p.y == q.y+1 || p.y == q.y-1))
            return true;

        return false;
    }

    public static void main(String[] args) {
        MazeGenerator test = new MazeGenerator(4, 4);
        System.out.println(test.maze[3][3]);
        System.out.println(test.points[5]);
        System.out.println(test.points[9]);
        Pair p = test.points[5];
        Pair q = test.points[5];
        System.out.println(test.adjacent(p, q));
    }
}

So, this code works right now but now it's time to implement UF....

Goal: I want to be able to make some method

... keep reading on reddit ➑

πŸ‘︎ 6
πŸ’¬︎
πŸ‘€︎ u/Willy988
πŸ“…︎ Oct 02 2021
🚨︎ report
Trying to complete a maze generator assignment using Union-Find / Disjoint-set, need help.

Issue: I am not sure how to use the union-find data structure provided by Sedgewick, which I imported. I need to use it in conjunction to my code I already have. Princeton UF code

Perhaps this is a super simple question and my brain is being smooth, but I just want to be able to namely use union() and find() in my methods...

What I did so far: I coded some things to test if the cells are adjacent, and made a general structure to test if my arrays are working.

import edu.princeton.cs.algs4.UF;
import java.util.Random;

public class MazeGenerator {
    int[][] maze;
    Pair[] points;
    int x = 0;


    MazeGenerator(int m, int n) {
        maze = new int[m][n];
        points = new Pair[m * n];
        UF find = new UF (m*n);
        find.union(2,3);

        for (int i = 0; i &lt; maze.length; i++) {
            for (int j = 0; j &lt; maze[i].length; j++) {
                maze[i][j] = 0;
            }
        }

        for (int j = 0; j &lt; n; j++) {
            for (int i = 0; i &lt; m; i++) {
                points[x++] = new Pair(i, j);
            }
        }
    }

    public class Pair {
        Pair(int x, int y) {
            this.x = x;
            this.y = y;
        }
        @Override
        public String toString() {
            return ("(" + x + ", " + y + ")");
        }
        int x;
        int y;
    }

    public boolean adjacent (Pair p, Pair q) {
        if (p.y == q.y &amp;&amp; (p.x == q.x+1 || p.x == q.x-1))
            return true;
        if (p.x == q.x &amp;&amp; (p.y == q.y+1 || p.y == q.y-1))
            return true;

        return false;
    }

    public static void main(String[] args) {
        MazeGenerator test = new MazeGenerator(4, 4);
        System.out.println(test.maze[3][3]);
        System.out.println(test.points[5]);
        System.out.println(test.points[9]);
        Pair p = test.points[5];
        Pair q = test.points[5];
        System.out.println(test.adjacent(p, q));
    }
}

So, this code works right now but now it's time to implement UF....

Goal: I want to be able to make some method that will randomly choose a cell, and union it with an adjacent cell until all cells are in one compo

... keep reading on reddit ➑

πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/Willy988
πŸ“…︎ Oct 02 2021
🚨︎ report

Please note that this site uses cookies to personalise content and adverts, to provide social media features, and to analyse web traffic. Click here for more information.