Good Math, Bad Math : Graph Searches and Disjoint Sets: the Union-Find Problem scienceblogs.com/goodmath…
πŸ‘︎ 28
πŸ’¬︎
πŸ‘€︎ u/gst
πŸ“…︎ Sep 06 2007
🚨︎ report
The Lightning Network is Centralizing. If certain core nodes are removed, the network graph could become disjoint and compartmentalize. arxiv.org/abs/2111.13494
πŸ‘︎ 25
πŸ’¬︎
πŸ‘€︎ u/donkey-hotei
πŸ“…︎ Dec 21 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 < 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 ➑

πŸ‘︎ 6
πŸ’¬︎
πŸ‘€︎ u/Willy988
πŸ“…︎ Oct 03 2021
🚨︎ report
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.

πŸ‘︎ 9
πŸ’¬︎
πŸ‘€︎ u/teilchen010
πŸ“…︎ Nov 19 2021
🚨︎ report
Marching ants & counting vertex disjoint cycle covers in planar graphs v.redd.it/4qq0ugr5pr861
πŸ‘︎ 913
πŸ’¬︎
πŸ‘€︎ u/BandagedGroup
πŸ“…︎ Jan 01 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
[Graph Theory] Let T be a tree and S a set of subtrees of T. Show that the maximum number of disjoint trees in T equals the least cardinality of a set X s.t. T - X contains no tree from S.

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.

Even weirder, none of the four graph theory texts I have in the house define subtree, but this definition does solve the issue I was having.

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 ➑

πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/midwayfair
πŸ“…︎ May 30 2020
🚨︎ report
Gujarat is one State of India out of its 28 States and 6 Union Territories. The graph below indicates how the import of Silver by the State of Gujarat in India has soared in 2021. It is an indicator of the things to come in 2022. So wake up and follow what Indians are doing - just stacking Silver.
πŸ‘︎ 113
πŸ’¬︎
πŸ‘€︎ u/ashokwenyan
πŸ“…︎ Jan 02 2022
🚨︎ report
The power of Unions illustrated by a single graph
πŸ‘︎ 298
πŸ’¬︎
πŸ‘€︎ u/hovdeisfunny
πŸ“…︎ Dec 22 2021
🚨︎ 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/

πŸ‘︎ 10
πŸ’¬︎
πŸ‘€︎ u/poream3387
πŸ“…︎ Apr 17 2020
🚨︎ 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?

πŸ‘︎ 11
πŸ’¬︎
πŸ‘€︎ u/theoreition
πŸ“…︎ Jul 09 2020
🚨︎ report
[Graph Theory] A set of subtrees contains k disjoint trees or a set of at most k-1 vertices in common.

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!

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/rpgrocks
πŸ“…︎ Sep 07 2017
🚨︎ 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.

πŸ‘︎ 34
πŸ’¬︎
πŸ‘€︎ u/DDOtten
πŸ“…︎ Oct 29 2018
🚨︎ report
Can someone please explain what does internally disjoint paths and edge-disjoint paths mean in graph theory?

Thank you. Can you also give me an example?

πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/klavda
πŸ“…︎ Nov 28 2019
🚨︎ 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!

πŸ‘︎ 5
πŸ’¬︎
πŸ‘€︎ 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
[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
Graph of the Parliamentary Assembly of the Union of Socialist Republics
πŸ‘︎ 1k
πŸ’¬︎
πŸ‘€︎ u/Cubeseer
πŸ“…︎ May 12 2021
🚨︎ 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
[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
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
The European Union πŸ‡ͺπŸ‡Ί is jumping on board by launching data project Ontochain, Decentralised Knowledge Graph (DKG) with TRAC. ontochain.ngi.eu/content/…
πŸ‘︎ 13
πŸ’¬︎
πŸ‘€︎ u/El_ai
πŸ“…︎ Dec 21 2021
🚨︎ report
[UK Undergrad: Basic Probabalistic Graph Theory] Prove that cylindrical events on disjoint edge sets are independent

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 ➑

πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/Godivine
πŸ“…︎ Apr 27 2013
🚨︎ report
Made a graph highlighting the Union and Fire's non-penalty xG + xA vs Shot Creating Actions before tonight's match. Personally, I think Monteiro doesn't get the love league wide that he deserves. Also, note placement of Jack Elliot.
πŸ‘︎ 15
πŸ’¬︎
πŸ‘€︎ u/MikeVerm
πŸ“…︎ Aug 01 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 ➑

πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/Willy988
πŸ“…︎ Oct 03 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

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 ➑

πŸ‘︎ 5
πŸ’¬︎
πŸ‘€︎ 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.