Consider a special social network where people are called connected if one person is connected to other with any number of intermediate connections. For example if a person x is connected with y and y is connected with z, then x is also considered to be connected with z. We are given a set of friend requests as input. We are also given a set of queries where each query has input pair i and j. For each query, we need to tell whether i and j are connected or not.
Input : Connections :
connect(0, 1), connect(1, 2), connect(0, 3), connect(5, 6), connect (0, 7)
Explanation : Note that 0 is connected to 2 and 0 is also connected to 7. Therefore 2 and 7 are considered as connected.
Input : Connections :
connect(0, 2), connect(4, 2), connect(1, 3)
The idea is to use disjoint set data structure. With this data structure, we can solve all queries in O(1) amortized time.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
- Creating a Network in Docker and Connecting a Container to That Network
- Introduction to the Probabilistic Data Structure
- Types of Sources of Data in Data Mining
- Processing of Raw Data to Tidy Data in R
- Data Normalization in Data Mining
- Data Integration in Data Mining
- Introduction to Artificial Neural Network | Set 2
- A single neuron neural network in Python
- Introduction to ANN | Set 4 (Network Architectures)
- Applying Convolutional Neural Network on mnist dataset
- Generative Adversarial Network (GAN)
- Pattern Recognition | Basics and Design Principles
- Difference Between Architectural Style, Architectural Patterns and Design Patterns
- Getting Started with System Design
- Disjoint Set Data Structures
- Data Mining
- Data Warehousing
- Classifying data using Support Vector Machines(SVMs) in Python
- Data Preprocessing for Machine learning in Python
- Analysis of test data using K-Means Clustering in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.