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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:43:19+00:00 2026-06-18T09:43:19+00:00

Given a tuple of lists, I need to find all unique path from that:

  • 0

Given a tuple of lists, I need to find all unique path from that:

Example I/P: [(1,2),(2,3),(3,4),(9,11),(4,5),(5,6),(6,7),(3,9)]
O/P: [[(1,2),(2,3),(3,4),(4,5),(5,6),(6,7)],[(1,2),(2,3),(3,9),(9,11)]]

Two tuples can connect if the second element of the tuple matches with the first element of the other tuple i.e: One tuple is (_,a) and other tuple is like (a,_).

What is the most efficient implementation for this ? I need to find the best data structure suited for it. Any suggestions ? The number of tuples in which I will execute the algorithm will be like more than 400,000.

  • 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-18T09:43:21+00:00Added an answer on June 18, 2026 at 9:43 am
    {-# LANGUAGE NoMonomorphismRestriction #-}
    import Data.List (permutations, nub)
    
    path :: Eq a => [(a, a)] -> [(a, a)]
    path [] = []
    path [x] = [x]
    path (u@(_, a):v@(b, _):xs) = if a == b then u:path (v:xs) else [u]
    
    allPaths = nub . map path . permutations
    

    (you can optimize chain generation but I think this problem has exponential time complexity)

    EDITED

    In general, you must to define more preciselly what paths you want to return.

    Ignoring cycle invariant ([(1,2),(2,3),(3,1)] == [(2,3),(3,1),(1,3)]) you can generate all paths (without using permutations)

    {-# LANGUAGE NoMonomorphismRestriction #-}
    import Data.List (permutations, nub, sortBy, isInfixOf)
    
    data Tree a = Node a [Tree a] deriving Show
    
    treeFromList :: Eq a => a -> [(a, a)] -> Tree a
    treeFromList a [] = Node a []
    treeFromList a xs = Node a $ map subTree $ filter ((a==).fst) xs
      where subTree v@(_, b) = treeFromList b $ filter (v/=) xs
    
    treesFromList :: Eq a => [(a, a)] -> [Tree a]
    treesFromList xs = map (flip treeFromList xs) $ nub $ map fst xs ++ map snd xs
    
    treeToList :: Tree a -> [[a]]
    treeToList (Node a []) = [[a]]
    treeToList (Node a xs) = [a:ws | ws <- concatMap treeToList xs]
    
    treesToList :: [Tree a] -> [[a]]
    treesToList = concatMap treeToList
    
    uniqTrees :: Eq a => [[a]] -> [[a]]
    uniqTrees = f . reverse . sortBy ((.length).compare.length)
      where f [] = []
            f (x:xs) = x: filter (not.flip isInfixOf x) (f xs)
    
    allPaths = uniqTrees . treesToList . treesFromList
    

    then

    *Main> allPaths [(1, 2), (1, 3), (2, 3), (2, 4), (3, 4), (4, 1)]
    [[2,4,1,2,3,4],[2,3,4,1,2,4],[1,3,4,1,2,4],[1,3,4,1,2,3],[1,2,4,1,3,4],[1,2,3,4,1,3]]
    

    uniqTrees has poor efficiency and, in general, you can do many optimizations.

    If you want to avoid cycle invariant, you can normalize a cycle selecting minimum base10 representation, in previous example ([(1,2),(2,3),(3,1)] == [(2,3),(3,1),(1,3)]) 1231 < 2313 then

    normalize [(2,3),(3,1),(1,3)] == [(1,2),(2,3),(3,1)]
    

    you can normalize a path rotating it n-times and taking “head . sortBy toBase10 . rotations”.

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

Sidebar

Related Questions

Given two tuples of the same arity, how can I lexicographically compare them? It
I need to take the list of tuples from a file and multiple 2nd
Given a list of tuples, where each tuple represents a row in a table,
So I have a list of tuples of two floats each. Each tuple represents
I have two lists of fixed-length tuples. This function calculates a fraction (ratio) for
How can I control the loop of Tuple repetition? Someone has given me a
So i know that comparisons on tuples work lexicographically: Tuples and lists are compared
How do I get a tuple/list element given a condition in python? This occurs
Given a tuple (specifically, a functions varargs), I want to prepend a list containing
I've come across a tuples problem where given a list of pair tuples it

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.