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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:42:12+00:00 2026-06-11T06:42:12+00:00

I am identifying loops in directional graphs. My function returns a list of lists

  • 0

I am identifying loops in directional graphs. My function returns a list of lists which store the nodes in any loops found.

For instance in a graph where the nodes are connected like this:

(1,2)(2,3)(3,4)(3,5)(5,2)

a loop is found at 2 – 3 – 5 so the function would return:

[[2,3,5]]

There are occasions where there are multiple loops which would return something like:

[[2,3,4][6,7,8,9]]

This is great, but if there are multiple start points in a graph which join the same loop at different points, such as in the graph:

(1,2)(2,3)(3,4)(3,5)(5,2)(6,3)

both nodes 1 and 6 join the same loop at different points which would return:

[[2,3,5][3,5,2]]

So here there are two identical loops, which are not identical lists. I want to identify such duplication and remove all but one (it doesn’t matter which).

Note, there may be cases where there are multiple loops, one which is duplicated, such as:

[[2,3,5][3,5,2][7,8,9,6]]

I’ve tried looking into itertools:

loops.sort()
list(loops for loops,_ in itertools.groupby(loops))

but that’s not helped, and I’m not 100% sure that this is appropriate anyway. Any ideas? I’m on python 2.4. Thanks for any help.

  • 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-11T06:42:13+00:00Added an answer on June 11, 2026 at 6:42 am

    If you only care about the elements of each loop, and not the order, I would canonicalize each loop by sorting it, and then take the set:

    >>> loops = [[2,3,5],[3,5,2],[7,8,9,6]]
    >>> set(tuple(sorted(loop)) for loop in loops)
    set([(2, 3, 5), (6, 7, 8, 9)])
    

    In order to use set here you need to convert to a tuple. You could convert the tuples back to lists, or turn the final set back into a list (maybe even using sorted to get a canonical order), but whether you’d actually need to would depend upon what you’d be doing with it.

    If you need to preserve path order, I’d canonicalize in a different way:

    def rotated(l, n):
        return l[n:] + l[:n]
    
    def canonicalize(l):
        m = min(l)
        where = l.index(m)
        return rotated(l, where)
    

    and then

    >>> loops = [[2,5,3], [5,3,2], [7,8,6,9]]
    >>> set(tuple(canonicalize(loop)) for loop in loops)
    set([(2, 5, 3), (6, 9, 7, 8)])
    

    [Edit: note that this simple canonicalization only works if each vertex can only be visited once in a path.]

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

Sidebar

Related Questions

I'm having a hard time correctly identifying a loop invariant for the following function:
I'm having trouble identifying entries in my DB. I need it to show me
What options exist for identifying the client name of a particular client from within
I need help identifying what these ANSI escape sequences represent. I'm currently filtering/evaluating the
I'm busy with identifying my website's users via single sign on OAuth providers. The
I am in the process of identifying a framework for building a local OAuth
I need to be able to pass identifying information through to Authorize.net's server so
What is the best-practise way of Identifying dynamically generated element on page? Let me
I have read many fine algorithms for identifying the most significant bit for 32-
I would like to extract and construct some strings after identifying a substring that

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.