Group Abstract Group Abstract

Message Boards Message Boards

How do I get all possible paths in terms of edges, and not vertices?

Posted 10 years ago

Hi everyone,

Suppose I have an undirected graph

G = Graph[{1 <-> 2, 2 <-> 3, 2 <-> 4, 3 <-> 4}, EdgeLabels -> {1 <-> 2 -> A, 2 <-> 3 -> B, 2 <-> 4 -> C, 
        3 <-> 4 -> D}]

![enter image description here][1]

I used FindPath[G, 1, 4, Infinity, All] and got the result as

{{1, 2, 4}, {1, 2, 3, 4}}

Although this result is true, I want this result to display possible paths in terms of edges, and not vertices. For above example, I want the result to be

{{A, C}, {A, B, D}}

Is there any way to do this? Actually, I have a large graph with 107 possible paths from source node to destination node, and I want to identify those paths with respect to edges. Please help. Thank you.

Attachments:
5 Replies
Posted 10 years ago
POSTED BY: Dana DeLouis
Posted 10 years ago

If you study the result you should notice that it is correctly translating verticies ...,3,5,... but not translating verticies ...,5,3,... Within the graph functions provided by Mathematica it understands your edges are undirected. But the simple pattern matching that I showed did not consider this. Try this on your paths

edgerule = {1 <-> 2 -> A, 2 <-> 3 -> B, 3 <-> 4 -> C, 4 <-> 5 -> D, 2 <-> 6 -> E, 4 <-> 6 -> F, 5 <-> 7 -> H, 6 <-> 7 -> I,
  2 <-> 8 -> J, 6 <-> 9 -> K, 7 <-> 10 -> L, 8 <-> 9 -> M, 8 <-> 11 -> N, 9 <-> 10 -> O, 9 <-> 12 -> P, 10 <-> 13 -> Q,
  11 <-> 12 -> R, 11 <-> 14 -> S, 11 <-> 15 -> T, 12 <-> 13 -> U, 13 <-> 14 -> V, 14 <-> 15 -> W} /. UndirectedEdge -> List;
edgerule = Flatten[edgerule /. Rule[List[h_, t_], l_] -> {Rule[List[h, t], l], Rule[List[t, h], l]}];

That expands your edgerule to include paths going in either direction between two verticies. Then

Map[Partition[#, 2, 1] &, ...yourvertexsolution...]/. edgerule

should give you the solution labeled as you desire.

Study the details of this method carefully until you can fully understand the ideas used. That will help you in the future.

POSTED BY: Bill Simpson
Posted 10 years ago

This uses your EdgeLabels and the list of paths from your example. Can use adapt this to get what you want?

In[1]:= edgerule = {1 <-> 2 -> A, 2 <-> 3 -> B, 2 <-> 4 -> C, 3 <-> 4 -> D} /. UndirectedEdge -> List; 
Map[Partition[#, 2, 1] &, {{1, 2, 4}, {1, 2, 3, 4}}] /. edgerule

Out[1]= {{A, C}, {A, B, D}}
POSTED BY: Bill Simpson

This is perfect what you have done. Got it. Thanks a ton. But is there not a way to list all paths displaying edges?

I have applied your technique to my notebook uploaded. But I am getting combinations of edges and vertices.

Attachments:
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard