Disjoint Set Graph Learning
Disjoint set is a data structures used in Kruskal minimal spanning tree.
This data structures allows us to create the union of two or more nodes.
It lets us determine if two nodes belong to the same component of the graph of not.
Time complexity is O(4alpha) (if we are using path compression which we are, else it will be logarithmic) which is constant time complexity that has been proven.
For more information refer to
class Main{ int parent[] = new int[100000]; int rank[] = new int[100000]; void makeSet(){ for(int i=1;i<=n;i++){ parent[i] = i; // initially parent of node i is i itself rank[i] =0;// intially rank of all the nodes are 0 } } int findParent(int node){ if(node == parent[node]) return node; // ie if the parent of 'node' is 'node' itself then just return the node. //return findParent(parent[node]);// else recursively call findParent to get the parent of this union. // in order to path conpress (making sure that every node is connected to its parent in the given union) return parent[node] = findParent(parent[node]); //example : 7->6->4->3 , here 3 is the parent of this union, so in order to get the parent of 7 which is 3 we can path compress it. like 7->3,6->3,4->3 etc. } void union(int u, int v){ u = findParent(u); v = findParent(v); if(rank[u] < rank[v]) parent[u] =v; else if (rank[u] > rank[v]) parent[v] = u; else parent[u] =v; // or parent[v] = u; // here u and v had the same rank //and now v became parent of u hence its rank will increase rank[v]++; } public static void main(String args[]) throws Exception{ Main m = new Main(); //if we where given no of nodes as n BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); while(n>0){ int u = n--; int v = n--; m.union(u,v);// this will create union of n nodes } // if 2 and 3 belong to the same component or not find out if(findParent(2)! = findParent(3)) System.out.println("Different component"); else System.out.println("Same component"); } }
The above is the detailed content of Disjoint Set Graph Learning. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...
