A list of puns related to "Disjoint union of graphs"
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 β‘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.
EDIT: Typo in the title. It's maximum disjoint trees in S, not in T. The book used a squiggly font T instead of S.
EDIT: Resolved. The problem is actually that the book doesn't have a definition of subtree, which I assumed was any subgraph that is also a tree. Much thanks to Minus_A_Twelfth for figuring this out and for typing up a sketch of the proof:
>Is there by any chance a rigorous definition of subtree in the book? Perhaps by looking at the definition the statement becomes clearer, since it seemed to me as if subtree here would be defined as a tree that is a subset of the original tree but must include any subsequent children in T. Not sure how to make this clear but for example you shouldnβt be allowed to pick just a single node as a subtree unless that single node is in fact a dead end of the tree.
This is problem 2.24 from the 5th edition of Diestel's graph theory. (This is not homework, I'm reading the book on my own.)
I think something must be wrong here, because there's no way this is correct:
Let D be the disjoint subtrees of T. All of the trivial subtrees are disjoint, and if T contains more than two edges, you can delete any non-leaf vertex and get a pair of disjoint subtrees, one of which will not be trivial (e.g. delete one of the internal vertices of a 4-vertex path). So the least cardinality is no smaller than the number of vertices in the graph but can clearly be bigger, i.e. |D| >= |V|.
But deleting any number of vertices from T leaves either a single subtree (including a trivial subtree) or a forest of subtrees of T (including trivial subtrees). So the only way to leave no subtree at all when deleting vertices is to delete them all, so |X| = |V|. Since |X| can't possibly be larger than |V|, |D| >= |X|, and this is an equality only if the number of vertices is small enough that there aren't any non-trivial disjoint subtrees.
Is the question just written wrong? Is there some common result that sounds close enough to this to guess what the author meant here? This problem doesn't really seem to have much at all to do with this chapter of the textbook either (the chapter is Matching, Covering, and Packing).
While it's possible that it's meant to be a statement to disprove, the author usually gives some hint (or even explicit statement) that a statement in the exerci
... keep reading on reddit β‘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/
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?
I'm having trouble proving that if
Let Tau be a set of subtrees of a tree T, and k β N. Show that either Tau contains k disjoint trees or there is a set of at most kβ1 vertices of T meeting every tree in Tau .
If it helps the other part to the problem that I proved was that if their pairwise intersections are non-empty then their overall intersections are also non-empty.
Thanks 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.
Thank you. Can you also give me an example?
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?"
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 : )
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).
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!
So this is a little long and I need to build it up. Some definitions: E(G) is the edge set of the graph G. Unfortunately, also denote some fixed edge set as E.
the event A (which is a subset of all possible graphs on vertices V say), is cylindrical on E if for every graph G in A, if graph G' satisfies E(G') β© E = E(G) β© E, then G' is in A.
If I understand this correctly, you need more information(at least one element) to actually have a concrete example of a cylindrical event. Example: Consider the graph 1-2-3 4 (ie, V={1,2,3,4} and E={ {1,2}, {2,3} }. Suppose 1-2-4 is in A. Then 1-2 4 = 1-2 is in A, but 1 2 is not in A and neither are 1-2-3 or 1-2-3-4. There are other elements of A which are not nicely representable in a straight line, but the list is independent on choice of 'initial graph', e.g. we could have supposed that 1-2 4 was in A and we would have constructed the same A.
Skippable paragraph I made up to motivate the name 'cylindrical': a (hollow, infinitely long) cylinder C radius r with z-axis as its axis in R^3 can be seen as an infinite union of many equal sized circles; it doesn't matter what z value you have, a point x=(x,y,z) is in C if β(x^2 + y^(2)) is equal to some predetermined number, r. Similarly, a cylindrical event A on E doesn't care what edges you have outside of E, a graph G is in A if if the edges of G that are in E, E(G)nE is equal to some predetermined set, E(G')nE.
Finally the question! Say i have two disjoint edge sets, E and F, with A,B cylindrical A living(sic) on E and B living on F. Prove that A and B are independent. Hint: prove that if A lives on E, then there exists a set M β π(E) such that G β A β E(G) β© E = M.
So I have proven the hint below, and if you want to try it out, skip the following paragraph.
We explicitly construct M: Take any G' β A. Then we define M:= { F β π(E) : F β© E = E(G') β© E } works: Fix some G on V and suppose G β A. Then E(G) β© E = E(G') β© E β M by definition of cylindrical. Conversely, suppose E(G) β© E β M. Then E(G) β© E = ( E(G) β© E ) β© E = E(G') β© E β M so G β A. β
I have not progressed much further though. Here is what I've tried:
There exist M_A β π(E), M_B β π(F) as in the Hint. But E,F are disjoint; thus, so are M_A and M_B.
β(A β© B) = β({G β Ξ© | E(G) β© E β M_A } β© {G β Ξ© | E(G) β© F β M_B }) = β({G | E(G) β© (E β¨ F) β M_A β¨ M_B }) (I think!)
Any help you can offer?
Edit: This is also an experiment in using unicode symbols; if nothing else, i
... keep reading on reddit β‘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 β‘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.