1 Replies - 90 Views - Last Post: 12 July 2019 - 12:51 PM

#1 Sisina_9898   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-May 19

Find ways between two node in graph

Posted 12 July 2019 - 12:30 PM

Hello, I am writing here because I had a little problem .I have to find all possible ways from node a to node d ,using recursion.
I found the connections with startTownKey ,but how I will get the ways on each node and find all ways to reach "d" ? I set property "visited" ,to set it true after that link is visited once. And I have to store all ways with they names. If someone can help me, thanks :)/> ! P.s Just simple code,no need of struct like queue ,stack or ect..

     var nodes =[{"name":"a","key":10},{"name":"b","key":15},{"name":"d" ,"key":3}..{}{}{}];
      var links = [{"from":10,"to":3},{"from":15,"to":10}..{}{}{}];
      var startTown = "a";
      var startTownKey=10,endTownKey=3;
      var isStartFound = false,isEndFound = false;
      var endTown = "d";
      
  var arrLinksNode=[]; 
      function recursiveFindWays(startTownKey){
      for(var i=0; i<links.length; i++){
        var actualLink = links[i];
        var obj={};
        if(actualLink.from === startTownKey || actualLink.to === startTownKey){
          obj["key"] = startTownKey;
          obj["from"] = actualLink.from;
          obj["to"] = actualLink.to;
          obj["visited"] = false;  
          arrLinksNode.push(obj);
        }
      }
    }


    recursiveFindWays(startTownKey);
:code:

This post has been edited by modi123_1: 12 July 2019 - 12:43 PM
Reason for edit:: In the future, please use the [code] tag button in the editor.


Is This A Good Question/Topic? 0
  • +

Replies To: Find ways between two node in graph

#2 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2334
  • View blog
  • Posts: 7,111
  • Joined: 15-January 14

Re: Find ways between two node in graph

Posted 12 July 2019 - 12:51 PM

It seems like you could use one of the shortest path algorithms, maybe a modified version to look at all paths instead of trying to find the shortest. If there are any 2 nodes that link to each other though, or any set of nodes that form a circle, then there will be an infinite number of paths so you'll need to account for that. If you've already traveled down one link you shouldn't travel down it again.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1