<p>I'm looking for a graph algorithm with some unusual properties.</p>

<p>Each edge in the graph is either an "up" edge or a "down" edge.</p>

<p>A valid path can go an indefinite number of "up"'s followed by an indefinite number of "down"'s, or vice versa. However it cannot change direction more than once.</p>

<p>E.g., a valid path might be A "up" B "up" C "down" E "down" F
an invalid path might be A "up" B "down" C "up" D</p>

<p>What is a good algorithm for finding the shortest valid path between two nodes? What about finding all of the equal length shortest paths?</p>

[{'answer_id': 52920, 'author': 'Christian Oudard', 'author_id': 3757, 'author_profile': 'https://Stackoverflow.com/users/3757', 'pm_score': 5, 'selected': True, 'text': '<p>Assuming you don\'t have any heuristics, a variation of <a href="http://en.wikipedia.org/wiki/Dijkstra&#39;s_algorithm" rel="nofollow noreferrer">dijkstra\'s algorithm</a> should suffice pretty well. Every time you consider a new edge, store information about its "ancestors". Then, check for the invariant (only one direction change), and backtrack if it is violated.</p>\n\n<p>The ancestors here are all the edges that were traversed to get to the current node, along the shortest path. One good way to store the ancestor information would be as a pair of numbers. If U is up, and D is down, a particular edge\'s ancestors could be <code>UUUDDDD</code>, which would be the pair <code>3, 4</code>. You will not need a third number, because of the invariant.</p>\n\n<p>Since we have used dijkstra\'s algorithm, finding multiple shortest paths is already taken care of.</p>\n'}, {'answer_id': 52921, 'author': 'Mendelt', 'author_id': 3320, 'author_profile': 'https://Stackoverflow.com/users/3320', 'pm_score': 2, 'selected': False, 'text': '<p>Maybe you can transform your graph into a normal directed graph and then use existing algorithms.</p>\n\n<p>One way would be to split the graph into two graphs, one with all the up edges and one with all the down edges and with directed edges between all the nodes on graph one and the corresponding node on graph two.</p>\n\n<p>First solve for starting in graph one and ending in graph two and then the other way around, then check the shortest solution.</p>\n'}, {'answer_id': 52923, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>One would think your standard <a href="http://en.wikipedia.org/wiki/Breadth-first_search" rel="nofollow noreferrer">BFS</a> should work here. Whenever you add a node to the open list, you can wrap it into a struct that holds which direction it is using (up or down) and a boolean flag indicating whether it has switched directions yet. These can be used to determine which outgoing edges from that node are valid.</p>\n\n<p>To find all shortest paths of equal length, include the number of edges traversed so far in your struct. When you find your first shortest path, make a note of the path length and stop adding nodes to the open list. Keep going through the remaining nodes on the list until you have checked all paths of the current length, then stop.</p>\n'}, {'answer_id': 52939, 'author': 'Tobi', 'author_id': 5422, 'author_profile': 'https://Stackoverflow.com/users/5422', 'pm_score': 2, 'selected': False, 'text': '<p><a href="http://en.wikipedia.org/wiki/A*_search_algorithm" rel="nofollow noreferrer">A*</a> with a specially crafted cost (G score) and heuristic (H score) function can handle it.</p>\n\n<p>For the cost you could keep track of the number of direction changes in the path and add infinite cost on the second change (ie. cut off the search for those branches).</p>\n\n<p>The heuristic takes some more thought, especially when you want to keep the heuristic admissible (never overestimates minimum distance to goal) and monotonic. (Only way to guarantee A* finds an optimal solution.)</p>\n\n<p>Maybe there is more information about the domain available to create the heuristic? (ie. x,y coordinates of the nodes in the graph?)</p>\n\n<p>Of course, depending on the size of the graph you want to solve, you could first try simpler algorithms like breadth first search or Dijkstra\'s algorithm: basically every search algorithm will do, and for every one you will need a cost function (or similar) anyway.</p>\n'}, {'answer_id': 985840, 'author': 'ilya n.', 'author_id': 115200, 'author_profile': 'https://Stackoverflow.com/users/115200', 'pm_score': 1, 'selected': False, 'text': "<p>If you have a standard graph search function, say <code>Graph.shortest(from, to)</code> in a library, you can loop and minimize, in C#/pseudocode: </p>\n\n<pre><code>[ (fst.shortest(A, C) + nxt.shortest(C, B)) \n    for C in nodes , (fst, nxt) in [(up, down), (down, up)] ].reduce(min)\n</code></pre>\n\n<p>If you need to remember the minimum path/paths and it so happens that your standard function returns you the data, you could also pronounce </p>\n\n<pre><code>[ [fst, nxt, C, fst.shortest(A, C), nxt.shortest(C,B)]\n    for C in nodes , (fst, nxt) in [(up, down), (down, up)] ].reduce(myMin)\n</code></pre>\n\n<p>where <code>myMin</code> should compare two <code>[fst, nxt, C, AC, BD]</code> tuples and leave the one that has lower distance, or both and assuming <code>reduce</code> is a smart function.</p>\n\n<p>This has some memory overhead if our graphs are large and don't use memory at all (which is possible if they are generated dynamically), but not really any speed overhead, imho.</p>\n"}]
['https://Stackoverflow.com/questions/52883', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3146/']