473,385 Members | 1,813 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.

Finding all paths between 2 nodes undirected multigraph

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:
0 - 1, Edge a
0 - 1, Edge c
1 - 2, Edge b
1 - 2, Edge d

Suppose I want to find all the paths between vertices 0 and 2. The
answer is: [a,b], [a,d], [c,b], [c,d].

In my implementation, I use a 'pathList' (to store each discovered
path) and a stack-based DFS. In my DFS implementation:

stack.push( target(starting_vertex)
while (!stack.empty())
{
edge e = stack.pop()
v = target(e)
if (color(v) == WHITE)
{
// tree edge
stack.push( adj_edges(v) )
pathList.append(e)
if (v == target_vertex)
break;
}
else if (color(v) == GRAY) || (color(v) == BLACK)
continue;
}
color(v) = BLACK

How can I extend this skeleton to allow for continuing the exploration
without restarting from the DFS walk from the start vertex? Instead, I
should be able to backtrack from the target_vertex back and pop out
the unwanted stack and pathList entries.

Regards
Nic

Sep 9 '08 #1
1 3300
On Sep 9, 1:57*am, nick <freesof...@gmail.comwrote:
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:
* 0 - 1, Edge a
* 0 - 1, Edge c
* 1 - 2, Edge b
* 1 - 2, Edge d

Suppose I want to find all the paths between vertices 0 and 2. The
answer is: [a,b], [a,d], [c,b], [c,d].

In my implementation, I use a 'pathList' (to store each discovered
path) and a stack-based DFS. In my DFS implementation:

stack.push( target(starting_vertex)
while (!stack.empty())
{
* * edge e = stack.pop()
* * v = target(e)
* * if (color(v) == WHITE)
* *{
* * * // tree edge
* * * stack.push( adj_edges(v) )
* * * pathList.append(e)
* * * if (v == target_vertex)
* * * * * break;
* *}
* *else if (color(v) == GRAY) || (color(v) == BLACK)
* * * *continue;}

color(v) = BLACK

How can I extend this skeleton to allow for continuing the exploration
without restarting from the DFS walk from the start vertex? Instead, I
should be able to backtrack from the target_vertex back and pop out
the unwanted stack and pathList entries.
Not sure if you are committed to this algorithm. If not, you could use
recursion.

The path from source vertex S to target vertex T is path from S to an
intermediate vertex I concatenated with the path from I to T.

So you would write a function

Path path (Vertex S, Vertex T)
{
if (S == T)
return Path(); // empty path assumes no self-cycles

// Accumulate all white edges sourced by S.
// If there are no white edges, then that means
// you are trapped, so...
return Path(); // again empty path
// For each such edge e,
// 1. mark it visited
// 2. determine target vertex I of e
// 3. pi = path(I, T)
// if (pi.is_empty())
// pf = Path(); // empty path
// else
// pf = e + pi
// mark all e as unvisited
// return pf.
}

I haven't checked this code, so it is most likely incorrect, but this
is basic idea.

-Le Chaud Lapin-
Sep 12 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

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: Greg | last post by:
Hi. I have a rather large xml document (object) that can have one or more nodes with a certain attribute throughout (at ANY depth, not at the same level necessarily). I need to find this...
6
by: Hans Kamp | last post by:
Is it possible to write a function like the following: string ReadURL(string URL) { .... } The purpose is that it reads the URL (determined by the parameter) and returns the string in which...
4
by: UJ | last post by:
I have a need to know what the domain name is when I'm running a site. Essentially the problem I have is that I let the user build a web page and then I display it using a IFRAME. But the IFRAME...
11
by: efialtis | last post by:
Hi to all, I am creating a program which plays a game. The game is played on a NxN square board. In every position of the board we may have a ball or not. Take for example the following 5x5...
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...
12
by: JD | last post by:
Hi, I need help for a task looks very simple: I got a python list like: , , , , , , ] Each item in the list need to be merged.
1
by: Glenton | last post by:
Hi All Here is a very simple little class for finding a shortest route on a network, following Dijkstra's Algorithm: #!/usr/bin/env python #This is meant to solve a maze with Dijkstra's...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.