Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3214874
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:06:43+00:00 2026-05-17T15:06:43+00:00

I have some data like this: 1 2 3 4 5 9 2 6

  • 0

I have some data like this:

1 2
3 4
5 9
2 6
3 7

and am looking for an output like this (group-id and the members of that group):

1: 1 2 6
2: 3 4 7
3: 5 9

First row because 1 is “connected” to 2 and 2 is connected to 6.
Second row because 3 is connected to 4 and 3 is connected to 7

This looked to me like a graph traversal but the final order does not matter so I was wondering if someone can suggest a simpler solution that I can use on a large dataset (billions of entries).


From the comments:

  • The problem is to find the set of disjoint sub-graphs given a set of edges.
  • The edges are not directed; the line ‘1 2’ means that 1 is connected to 2 and 2 is connected to 1.
  • The ‘1:’ in the sample output could be ‘A:’ without changing the meaning of the answer.

EDIT 1:

Problem looks solved now. Thanks to everyone for their help. I need some more help picking the best solution that can be used on billions of such entries.

EDIT 2:

Test Input file:

1 27
1 134
1 137
1 161
1 171
1 275
1 309
1 413
1 464
1 627
1 744
2 135
2 398
2 437
2 548
2 594
2 717
2 738
2 783
2 798
2 912
5 74
5 223
7 53
7 65
7 122
7 237
7 314
7 701
7 730
7 755
7 821
7 875
7 884
7 898
7 900
7 930
8 115
9 207
9 305
9 342
9 364
9 493
9 600
9 676
9 830
9 941
10 164
10 283
10 380
10 423
10 468
10 577
11 72
11 132
11 276
11 306
11 401
11 515
11 599
12 95
12 126
12 294
13 64
13 172
13 528
14 396
15 35
15 66
15 210
15 226
15 360
15 588
17 263
17 415
17 474
17 648
17 986
21 543
21 771
22 47
23 70
23 203
23 427
23 590
24 286
24 565
25 175
26 678
27 137
27 161
27 171
27 275
27 309
27 413
27 464
27 627
27 684
27 744
29 787

Benchmarks:

I tried out everything and the version posted by TokenMacGuy is the fastest on the sample dataset that I tried. The dataset has about 1 million entries for which it took me about 6 seconds on a Dual Quad-Core 2.4GHz machine. I haven’t gotten a chance to run it on the entire dataset yet but I will post the benchmark as soon as it is available.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:06:43+00:00Added an answer on May 17, 2026 at 3:06 pm

    I’ve managed O(n log n).

    Here is a (somewhat intense) C++ implementation:

    #include <boost/pending/disjoint_sets.hpp>
    #include <boost/property_map/property_map.hpp>
    
    #include <map>
    #include <set>
    #include <iostream>
    
    
    typedef std::map<int, int> rank_t;
    typedef std::map<int, int> parent_t;
    
    typedef boost::associative_property_map< rank_t > rank_pmap_t;
    typedef boost::associative_property_map< parent_t > parent_pmap_t;
    
    typedef boost::disjoint_sets< rank_pmap_t, parent_pmap_t > group_sets_t;
    
    typedef std::set<int> key_set;
    typedef std::map<int, std::set<int> > output;
    

    With some typedefs out of the way, here’s the real meat. I’m using boost::disjoint_sets, which is just happens to be an exceptionally good representation for the problem. This first function checks to see if either of the keys given have been seen before, and adds them to the collections if needed. the important part is really the union_set(a, b) which links the two sets together. If one or the other of the sets are already in the groups collection, they get linked too.

    void add_data(int a, int b, group_sets_t & groups, key_set & keys)
    {
      if (keys.count(a) < 1) groups.make_set(a);
      if (keys.count(b) < 1) groups.make_set(b);
      groups.union_set(a, b);
      keys.insert(a);
      keys.insert(b);
    }
    

    This isn’t too exciting, it just iterates through all of the keys we’ve seen and gets the representative key for that key, then adds the pair (representative, key) to a map. Once that’s done, print out the map.

    void build_output(group_sets_t & groups, key_set & keys)
    {
      output out;
      for (key_set::iterator i(keys.begin()); i != keys.end(); i++)
        out[groups.find_set(*i)].insert(*i);
    
      for (output::iterator i(out.begin()); i != out.end(); i++)
      {
        std::cout << i->first << ": ";
        for (output::mapped_type::iterator j(i->second.begin()); j != i->second.end(); j++)
          std::cout << *j << " ";
        std::cout << std::endl;
      }
    }
    
    int main()
    {
    
      rank_t rank;
      parent_t parent;
      rank_pmap_t rank_index(rank);
      parent_pmap_t parent_index(parent);
    
    
      group_sets_t groups( rank_index, parent_index );
      key_set keys;
    
      int a, b;
      while (std::cin >> a)
      {
        std::cin >> b;
        add_data(a, b, groups, keys);
      }  
    
    
      build_output(groups, keys);
      //std::cout << "number of sets: " << 
      //  groups.count_sets(keys.begin()), keys.end()) << std::endl;
    
    }
    

    I stayed up many hours learning how to use boost::disjoint_sets on this problem. There doesn’t seem to be much of any documentation on it.

    About the performance. The disjoint_sets structure is O(α(n) ) for its key operations (make_set, find_set and union_set) which is pretty close to constant, and so if it were just a matter of building the structure, the whole algorithm would be O(n α(n) ) (which is effectively O(n) ) but we have to print it out. That means we have to build up some associative containers, which cannot perform better than O(n log n). It might be possible to get a constant factor speedup by choosing a different associative containers (say, hash_set etc), since once you populate the initial list, you can reserve an optimal amount of space.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some configuration data that I'd like to model in code as so:
Say I have some data like this: number_stream = [0,0,0,7,8,0,0,2,5,6,10,11,10,13,5,0,1,0,...] I want to process
I'm considering the following: I have some data stream which I'd like to protect
I have been researching options for printing report-like data via a web application. Some
I have some data. I want to go through that data and change cells
I have some data that I want to store somewhere in my Rails app
I have some data with messed-up accented characters. For example in the data we
I have some data grouped in a table by a certain criteria, and for
I have some data of the form Key ID Link 1 MASTER 123 2
I have some data stored as ArrayList . And when I want to backup

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.