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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:29:27+00:00 2026-06-12T10:29:27+00:00

I have an array of set of node connections defined by: var arr =

  • 0

I have an array of set of node connections defined by:

var arr = ['1-2','1-6','2-6','2-3','1-4','1-5','6-7','4-7','7-8'];

1-2 means node 1 connects to node 2 and so on. There are multiple connections as you can see.

If correctly manipulated, you can notice various unique paths such as 1-2-6-7-8, 1-5 etc. I would like to calculate paths as:

path = ['1-2-6-7-8','1-6-7-8','1-2-3','1-4-7-8','1-5']

I have done by checking each set by another set but I think my code is too long and does not perform well. What is the best way to get path array. (path ends at leaf node)

Thanks.

enter image description here

  • 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-12T10:29:28+00:00Added an answer on June 12, 2026 at 10:29 am

    How about this solution, and I think it only works with DAG: directed acyclic graphs

    function toGraph(arr){
        d = {}
        for (var e=0; e<arr.length; e++){
            var edge = arr[e].split('-')
            if(!d[edge[0]]){d[edge[0]]=[]}
            if(!d[edge[1]]){d[edge[1]]=[]}
            d[edge[0]].push(edge[1])
        }
        return d
    }    
    
    function DFS(G,node,path,paths,visited){
        visited[node] = true
        path.push(node)
        if (G[node].length==0 && path.length>1) paths.push('['+path.join('-')+']')
    
        for (var s=0; s<G[node].length; s++){
            successor = G[node][s]
            if(!visited[successor]){
                DFS(G,successor,path,paths,visited)
            }
        }
        visited[node] = false
        path.pop()
    }
    
    function go(){
        var arr = ['1-2','1-6','2-6','2-3','1-4','1-5','6-7','4-7','7-8']   
        var paths = []
        DFS(toGraph(arr),'1',[],paths,{})
        return paths.toString()
    }
    

    and when you call go(), you get the following output

    [1-2-6-7-8], [1-2-3], [1-6-7-8], [1-4-7-8], [1-5]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background I have an array of objects (Users) defined and set as follows: //
I have a multidimensional array of bools, with each element set to true: var
I have set a node app up on my server and i can ssh
I have a set of objects in a hierachy. There's a top root node
I have an array set inside a velocity template that contains some paths. The
I have set an array in my config file that I use global in
I have an array which contains a set of UIImage in byte forms. --My
I have an array of checkboxes that I edit at once to set up
I have an array of integers in my core data model. It is set
I have a numerical indexed array of strings. Another array contains a set of

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.