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 6929689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:20:59+00:00 2026-05-27T11:20:59+00:00

I apologize if this question has already been answered in: Topological Sort with Grouping

  • 0

I apologize if this question has already been answered in:
Topological Sort with Grouping

However, I do not completely understand the answer as I am new to graph theory.

I have the following items:

c01,a11,b12,a21, b22,c23, c31,b32, a33.

Each of these is a three tuple.

Tup[0]: ‘letter to group by’

Tup[1]: ‘group number where dependencies are valid’

Tup[2]: ‘sort order of dependencies’

I would like to group by tup[0] as closely as possible while maintaining the sort order described by the groups in item[1] and item[2]. Items 1,2 allow us to create the dependencies, from here we just need to create the groups.

so we can create the following depencies:

a11<-b12

a21<-b22, b22<-c23

c31<-b32, b32<-a33

c01

From here I would like to group by letter while maintaining the dependencies. One such solution would be

a11, a21, b12, b22, c01, c23, c31, b32, a33

We can see that a11<-b12, a21<-b22<-c23, c31<-b32<-a33, c01

Any thoughts would be greatly appreciated,
Thanks,
Rob

one solution:

def groupPreserveSorted(listOfPairs):
    """

    we want to group by tup[0], but maintain the order passed in according to tup[1]

    >>> lop = [['A',0], ['B',1], ['C',0], ['D',2], ['E',2]]
    >>> print groupPreserveSorted(lop)
    [('A', 0), ('B', 1), ('C', 0), ('D', 2), ('E', 2)]

    >>> lop = [['c',0], ['a',1], ['b',1], ['a',2], ['b',2], ['a', 3], ['b', 3], ['c', 3], ['a', 4], ['b', 4]]
    >>> print groupPreserveSorted(lop)
    [('c', 0), ('a', 1), ('a', 2), ('a', 3), ('a', 4), ('b', 1), ('b', 2), ('b', 3), ('b', 4), ('c', 3)]

    >>> lop = [['c',0], ['a',1], ['b',1], ['a',2], ['b',2], ['a', 3], ['b', 3], ['c', 3], ['c', 4], ['a', 4], ['b', 4]]
    >>> print groupPreserveSorted(lop)
    [('c', 0), ('a', 1), ('a', 2), ('a', 3), ('b', 1), ('b', 2), ('b', 3), ('c', 3), ('c', 4), ('a', 4), ('b', 4)]


    """
    groupCount = 0
    groupMap = {} #map contains the "level" to the highest group
    maxGroupDic = {} #this contains a map from tup[1] to the highest level attained by tup[1]
    groupTypeToMapItem = {} #this contains all the levels that items in tup[0] are placed on

    for groupType, dependencyGroup in listOfPairs:
        if groupCount == 0:
            groupMap[0] = [(groupType, dependencyGroup)]
            maxGroupDic[dependencyGroup] = 0
            groupTypeToMapItem[groupType] = [0]
            groupCount+=1
        else:
            if groupType not in groupTypeToMapItem:#need to make new group
                groupMap[groupCount] = [(groupType, dependencyGroup)]
                maxGroupDic[dependencyGroup] = groupCount
                groupTypeToMapItem[groupType] = [groupCount]
                groupCount+=1
            else:
                maxGroupTypeItem  = groupTypeToMapItem[groupType][-1]
                if dependencyGroup in maxGroupDic: #then we just need to check where to add to a new level or to an old level
                    maxItem = maxGroupDic[dependencyGroup]
                    if maxItem>maxGroupTypeItem: #then we need to make a enw group
                        groupMap[groupCount] = [(groupType, dependencyGroup)]
                        maxGroupDic[dependencyGroup] = groupCount
                        groupTypeToMapItem[groupType] = [groupCount]
                        groupCount+=1
                    else:
                        countToUse = [item for item in groupTypeToMapItem[groupType] if item>=maxItem][0]
                        groupMap[countToUse].append((groupType, dependencyGroup))
                        maxGroupDic[dependencyGroup]=countToUse
                else: #we haven't added this groupType yet just add to lowest level
                    countToUse = groupTypeToMapItem[groupType][0]
                    groupMap[countToUse].append((groupType, dependencyGroup))
                    maxGroupDic[dependencyGroup]=countToUse

    return flatten([groupMap[count] for count in xrange(groupCount)], depth = 1)

this is a nice solution as it is o(n), but it is definitely not the cleanest answer:)

  • 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-27T11:21:00+00:00Added an answer on May 27, 2026 at 11:21 am

    This is my solution

    >>> data
    ['c01', 'a11', 'b12', 'a21', 'b22', 'c23', 'c31', 'b32', 'a33']
    >>> data="c01,a11,b12,a21, b22,c23, c31,b32, a33"
    >>> data=[x.strip() for x in data.split(",")]
    >>> data=sorted(data,key=operator.itemgetter(0))
    >>> data=sorted(data,key=operator.itemgetter(1))
    >>> data=sorted(data,key=operator.itemgetter(2))
    >>> data
    ['c01', 'a11', 'a21', 'c31', 'b12', 'b22', 'b32', 'c23', 'a33']
    >>> 
    

    or as a single line solution

    >>> data
    ['c01', 'a11', 'b12', 'a21', 'b22', 'c23', 'c31', 'b32', 'a33']
    >>> [data.sort(key=operator.itemgetter(x)) for x in [0,1,2]]
    >>> data
    ['c01', 'a11', 'a21', 'c31', 'b12', 'b22', 'b32', 'c23', 'a33']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I apologize if this question has been answered already, but I cannot seem to
Apologies if this question has already been asked but I do not think I
Apologies if this question has already been answered, I've already done an extensive search
I apologize if this question has already been covered on this site, but I
I'm sure this question has probably been answered before, so I apologize, but I
I'm a python newbie so I apologize in advance if this question has been
I feel like this question has probably been asked a thousand times already, so
I apologize if this has already been asked a different way but I couldn't
I apologize if this question was asked already before but I could not find
I apologise if this question has already been asked on here before but I

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.