473,385 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

all paths between two nodes

Hi all,

I am trying to find out what is the best (time efficient) algorithm to accomplish the task described below.

I have a set of records. For this set of records I have connection data which indicates how pairs of records from this set connect to one another (basically a graph, the records being the vertices and the connection data the edges).

All of the records in the set have connection information (i.e. no orphan records are present; each record in the set connects to one or more other records in the set).

I want to choose any two (arbitrary) records from the set and be able to show all ways possible the chosen records connect (either directly or through other connections).

For example:

If I have the following records:
A, B, C, D, E

and the following represents the connections(links):
(A,B),(A,C),(B,A),(B,D),(B,E),(B,F),(C,A),(C,E),
(C,F),(D,B),(E,C),(E,F),(F,B),(F,C),(F,E)

[where (A,B) means record A connects to record B]
If I chose B as my starting record and E as my ending record, I would want to find all paths through the record connections that would connect record B to record E.

All paths connecting B to E:
B->E
B->F->E
B->F->C->E
B->A->C->E
B->A->C->F->E
This is an example, in practice I may have sets containing hundreds of thousands of records

I can go for c/c++.
Guide me in accomplishing the task.

Thanks,
Anil Kumar Y.
Apr 19 '10 #1
5 6097
jkmyoung
2,057 Expert 2GB
I'm assuming that you can't use the same node twice?
I suggest marking which nodes you've used by having it in some string, say path. Pseudocode:
Expand|Select|Wrap|Line Numbers
  1. start -> end
  2. Mark start node.
  3. current = start.
  4. String path += start // stringwise
  5.  
  6. FindPath()
  7.  
  8. FindPath(){
  9.   for each connection -> node j from current node
  10.     if j is not in path{
  11.       path += j  // stringwise
  12.       current = j
  13.       if (j = end) Print path
  14.       else FindPath() //recurse
  15.  
  16.       path -= j // stringwise, so we can reset to the original state.
  17.     }
  18. }
If this pseudocode is too complicated, you can always find your own original method for tackling this problem.
Apr 19 '10 #2
donbock
2,426 Expert 2GB
Where did this problem come from?

Hundreds of thousands of nodes ... I don't remember why, but I have a strong feeling that this problem is not NP unless there are severe constraints on the connectedness of your graph.

I recommend you look into graph theory before embarking on an implementation.
Apr 19 '10 #3
weaknessforcats
9,208 Expert Mod 8TB
B->E
B->F->E
B->F->C->E
B->A->C->E
B->A->C->F->E

You can probably use a multiset container for this.

You would add to the container:

B->E
B->F
B->A
F->E
F->C
A->C

Now to query on B create a temporary multiset containing all connections to B:

F->E
F->C
A->C

where B->E is part of your result.

Now query on F and A separately. For F a temporary multiset woudl contain:

F->C

Since the original query is on B, F->E represents B->F->E.

Repeat and query on C:

C->E
C->F

Since the original query is on B, then F, C->E represents
B->F->C->E.

C->F is ignored since this is a circular reference to F (the current query).

This concludes the F leg so you now have all B->F-> etc... C

Now do the A leg. You have A->C so create a temporary multiset for all C connections:

C->E
C->F

C->E is part of your result an represents B->A->C->E. The temporary multiset contains:

F->E

This represents B->A->C->F->E.

There are no entries for the temporary multiset, so you are done.

Hence the total query result is:

B->E
B->F->E
B->F->C->E
B->A->C->E
B->A->C->F->E
Apr 19 '10 #4
donbock
2,426 Expert 2GB
@donbock
Apparently I was needlessly pessimistic. I think the OP is referring to the classic all simple paths problem. There are suggested solutions all over the internet.
Apr 19 '10 #5
jkmyoung
2,057 Expert 2GB
In the worst case, there are all connections, resulting in a n! time. It definitely is not np.

I guess my initial response was the depth first search.

There may be optimizations, by storing subpaths, but then you have to compare subpath to subpath to avoid intersection. One way you could check if a node is in a path is as follows:
Number the nodes, 0 -> n-1.
Create an n bit number which represents the nodes used in the path, initially set to 0. When you encounter a node, say i, set the ith bit, or 1 << i. When coming back from the recursive call, unset 1 << i.

Comparing 2 paths, you simply take the bitwise & of the 2 numbers, and if there are any intersections, the result will not be 0.
====
The multiset implementation is interesting too! Reminds me of prolog. My worry is that it would be inefficient, but I can't quite figure out how to calculate on the runtime.
Apr 19 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: jnc | last post by:
Hi, I was wondering if anybody had any pointers on the following: I need to write some code which takes a list of file paths and transforms them into a treeview which shows the directory...
2
by: Bryan Galvin | last post by:
Hi all, Is it possible to 'cherry-pick' child nodes but retain their parentage. Given an example... <user> <first_name>George</first_name> <sur_name>George</sur_name> <dob>George</dob>...
8
by: Daniele | last post by:
Hi there is a way to put in a struct of vectors all the possibles routes in a graph from s to d? Hi thought Dijkstra but I don't know how can i do. Thanks Dax
19
by: Christian Fowler | last post by:
I have a VERY LARGE pile of geographic data that I am importing into a database (db of choice is postgres, though may hop to oracle if necessary). The data is strictly hierarchical - each node has...
3
by: JeffDotNet | last post by:
I wrote a small data processing application that writes a summary of several hundred files. I use drag and drop on a panel (Panel1) to grab the absolute path to each of these files. Then I begin...
5
by: costantinos | last post by:
Hello. I have implemented the Dijkstra shortest path algorithm, it works fine but I have one question on how I can improve something. I want to find all the possible shortest paths from a node...
3
by: dkacher | last post by:
Hi - I'm looking for a way to generate a list of the fully-qualified paths to all of the leaf nodes in an XML Schema. The reason: I have a large schema for which I'm building a transform...
1
by: nick | last post by:
Hi, I have a undirected multigraph i.e there can be more than 1 edge between 2 vertices in the undirected graph. 0 --a-- 1 --b-- 2 |__c__|__d__| Vertices: 0, 1, 2 Edges:
7
by: latalui | last post by:
i am a beginner, guys plizzzzzzzz help me. i am given a assignment to find short paths either using dijkstra algorithm or using binary search algorithm in linux OS. check out what is wrong the code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.