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

  • Home
  • SEARCH
  • 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 8414653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:13:15+00:00 2026-06-10T01:13:15+00:00

Possible Duplicate: Python: simple list merging based on intersections I am trying to classify

  • 0

Possible Duplicate:
Python: simple list merging based on intersections

I am trying to classify objects. Each object is identified by a unique identifier property called id. So my classification logic goes like this. First i prepare a list of objects and then the classification function takes 2 objects at a time and returns a frozenset containing their id. So if object1 and object5 are in the same category a frozenset(id1,id5) is returned. Now i keep adding these frozensets to a set so in the end i have a set like this

matched_set=(
             frozenset(id1,id2),
             frozenset(id9,id3),
             frozenset(id9,id2),
             frozenset(id24,id22),
             frozenset(id1,id23),
             frozenset(id25,id24),
             frozenset(id30,id24)
            )

Now because objects with id1 and id2 are in the same category, objects with id9 and id3 are in the same category, objects with id9 and id2 are in the same category, objects with id1,id2,id3,id9 should be in same category. So i should have a set like this set(id1,id2,id3,id9)
Can someone provide an algorithm to do so?
Thanks

  • 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-06-10T01:13:16+00:00Added an answer on June 10, 2026 at 1:13 am

    It sounds like you’re looking for a disjoint-set datastructure.

    Given your set of id’s, your categories separate them into disjoint subsets. A disjoint-set datastructure represents each category by choosing a representative id, which will be returned by a query of any of its members. (isolated id’s form one category apiece, and return themselves)

    Updates to a disjoint-set datastructure combine the categories of any two id’s, so that future queries return the same representative for members of both subsets. (if the two id’s are already members of the same category, the update is functionally a no-op)

    The usual method is to represent each category as a reverse-tree: each id has a parent link, but no child links. The “representative element” is the root of the tree, which is easy to query by following the parent links. An update requires finding the root of the trees of both id’s, and (if they are different) merging the trees by making one root the parent of the other.

    By adding a couple of simple optimizations (queries “collapse” the query path to point directly to the root, and updates always choose the root of the deepest tree as the merge parent), this algorithm becomes extremely efficient, running in “almost-O(1)” amortized time.

    If you want online access to the complete list of id’s in each category, you should maintain a cumulative list attached to each category root, and concatenate them in each merge. In general, it can be convenient to maintain any number of statistics about your categories this way.

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

Sidebar

Related Questions

Possible Duplicate: Python: simple list merging based on intersections I have a multiple list:
Possible Duplicate: Python: How to remove /n from a list element? I'm trying to
Possible Duplicate: Python 2D list has weird behavor when trying to modify a single
Possible Duplicate: Why doesn't this division work in python? A simple problem I'm having
Possible Duplicate: Python : how to append new elements in a list of list?
Possible Duplicate: Python: What is the best way to check if a list is
Possible Duplicate: Python: Get object by id Typically when you see the string representation
Possible Duplicate: Python, compute list difference I have two lists For example: A =
Possible Duplicate: Python analog of natsort function (sort a list using a “natural order”
Possible Duplicate: How do you split a list into evenly sized chunks in Python?

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.