A list of puns related to "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.
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).
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.
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.
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 < n; j++) {
for (int i = 0; i < 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 && (p.x == q.x+1 || p.x == q.x-1))
return true;
if (p.x == q.x && (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 β‘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 < m; j++) {
for (int i = 0; i < 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() > 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 β‘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?
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?
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/
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!
This crate is published on crates.io and fully documented.
The main struct of this crate is PartitionVec<T>
which has the functionality of a Vec<T>
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.
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 & { kind: 'foo' }
But this doesn't seem to be working, the compiler just applies & { kind: 'foo' }
to all members, even though this is contradictory.
What is the right way to achieve what I want?
Thanks!
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?"
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?
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 : )
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.
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).
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 < maze.length; i++) {
for (int j = 0; j < maze[i].length; j++) {
maze[i][j] = 0;
}
}
for (int j = 0; j < n; j++) {
for (int i = 0; i < 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 && (p.x == q.x+1 || p.x == q.x-1))
return true;
if (p.x == q.x && (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 β‘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 < maze.length; i++) {
for (int j = 0; j < maze[i].length; j++) {
maze[i][j] = 0;
}
}
for (int j = 0; j < n; j++) {
for (int i = 0; i < 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 && (p.x == q.x+1 || p.x == q.x-1))
return true;
if (p.x == q.x && (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 β‘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.