Finding node-dispoint paths from A to B - algorithm

This is the problem I got stumped on the exam:
Given a directed graph G = (V,E), and let A and B be partition of V(union of A and B is V and they are disjoint). I want to find a set of paths such that:
1) Every path contains exactly one node in A
2) Each node in A is the first node of corresponding path
3) For each path, the last node is in B
4) All paths are node-disjoint
Given A,B, and G=(B,E), I have to design a poly-time algorithm to decide whether such a set of paths exist.
The observation I made is that if I have a cut (A,B) and then look at the number of arcs going from A to B, then the number of arcs must be at least |A| for such a set of paths to exist. Also, every node in A must be adjacent to at least one of nodes in B. Also, once a path crosses the cut from A to B, you can't cross from B to A because of the constraint (1), but what's difficult is how to find node-disjoint paths among the vertices in B... How should I approach this?

Related

What algorithm should I use to get all possible paths in a directed weighted graph, with positive weights?

I have a directed weighted graph, with positive weights, which looks something like this :-
What I am trying to do is:-
Find all possible paths between two nodes.
Arrange the paths in ascending order, based on their path length (as given by the edge weights), say top 5 atleast.
Use an optimal way to do so, so that even in cases of larger number of nodes, the program won't take much time computing.
E.g.:- Say my initial node is d, and final node is c.
So the output should be something like
d to c = 11
d to e to c = 17
d to b to c = 25
d to b to a to c = 31
d to b to a to f to c = 38
How can I achieve this?
The best approach would be to take the Dijkstra’s shortest path algorithm, we can get a shortest path in O(E + VLogV) time.
Take this basic approach to help you find the shortest path possible:
Look at all nodes directly adjacent to the starting node. The values carried by the edges connecting the start and these adjacent nodes are the shortest distances to each respective node.
Record these distances on the node - overwriting infinity - and also cross off the nodes, meaning that their shortest path has been found.
Select one of the nodes which has had its shortest path calculated, we’ll call this our pivot. Look at the nodes adjacent to it (we’ll call these our destination nodes) and the distances separating them.
For every ending (destination node):
If the value in the pivot plus the edge value connecting it totals less than the destination node’s value, then update its value, as a new shorter path has been found.
If all routes to this destination node have been explored, it can be crossed off.
Repeat step 2 until all nodes have been crossed off. We now have a graph where the values held in any node will be the shortest distance to it from the start node.
Find all possible paths between two nodes
You could use bruteforce here, but it is possible, that you get a lot of paths, and it will really take years for bigger graphs (>100 nodes, depending on a lot of facotrs).
Arrange the paths in ascending order, based on their path length (as given by the edge weights), say top 5 atleast.
Simply sort them, and take the 5 first. (You could use a combination of a list of edges and an integer/double for the length of the path).
Use an optimal way to do so, so that even in cases of larger number of nodes, the program won't take much time computing.
Even finding all possible paths between two nodes is NP-Hard (Source, it's for undirected graphs, but is still valid). You will have to use heuristics.
What do you mean with a larger number of nodes? Do you mean 100 or 100 million? It depends on your context.

Finding number of paths between two nodes in a directed acyclic graph

My idea of solving this problem is to adjust DFS so that it stops when we hit the destination node, then set up a counter that adds up all of the neighbors of the starting node, then the neighbors of the starting node and its neighrbours recursively.
I'm just wondering if this will only count the paths from the source to the destination, and not any stray paths that don't lead to the destination node.
Thank you for the help.
You can use dynamic programming. You have a directed acyclic graph so you have a node (say s) with no arcs pointing into s. You also have a node (say t) that has no arcs pointing out of t. Because it is acyclic, you can use a topological sorting algorithm to find an ordering of the nodes such that every arc points away from s and towards t.
So start at s. The number of paths from s to s is 1, the empty path. Because the graph is acyclic, s must have a neighbour u such that the only arc pointing into u is su. Now you just repeat. In general, for a node w that has arcs from v1,...vk pointing into it. Then the number of paths from s to w is just the sum of the number of sv1 paths, ..., svk paths.
This is in the case of a single arc between each node. If there are multiple arcs you multiply so it would be (number of v1w arcs)(number of sv1paths) + ... + (number of vkw arcs)(number of svk paths)
And at each step you can use the fact that it is acyclic to find the node w such that you have already calculated all the sv1 to svk paths.
I would use BFS.
Let's call the source node s and the target node t.
When you BFS starting from s, all the paths with length 1 will be found and put into a queue. You can, then, take the first element of the queue (call it u) and find all the paths of size 2 (s -> u -> ...). Repeat the same thing for each distance, until you find all paths of all lengths from s to t.
A trick to speed it up would be: after you exhausted all the paths of a node w, store how many paths from w to t there are, and when another node (above w) reach w, you won't need to recompute all the paths.

Find the all chain of one to one node in graph

I go the un-directed graph G an my goal is to find all the possible chains longest than N of nodes in a one to one relation.
For example:
In the the next Graph the "chains" of length more than 2 of nodes in one to one relation are:
- d -> e -> f -> g
- c -> k -> l -> m
So what is the best approach or algorithm to solve this problem ?
If you want to find all paths so that each vertex in it has a degree <=2, then the simple approach may be as follows.
Remove all vertices with degree >2 from your graph. You are left with a graph with each vertex having a degree <=2. It is easy to prove that every connected component of such a graph is either a simple way, either a simple loop, and it is easy to distinguish them (for example, running a DFS from one node and seeing whether you ever return to it).
So, every component that is a path is a path you look for. Every component that is a loop is also a path you look for, or can be easily converted to such a path by removing an edge or a vertex, depending on whether you allow a loop as the needed path.

Minimizing the number of connected-checks in finding a shortest path in an implicit graph

I'm quite surprised I couldn't find anything on this anywhere, it seems to be a problem that should be quite well known:
Consider the Euclidean shortest path problem, in two dimensions. Given a set of obstacle polygons P and two points a and b, we want to find the shortest path from a to b not intersecting the (interior of) any p in P.
To solve this, one can create the visibility graph for this problem, the graph whose nodes are the vertices of the elements of P, and where two nodes are connected if the straight line between them does not intersect any element of P. The edge weight for any such edge is simply the Euclidean distance between such two points. To solve this, one can then determine the shortest path from a to b in the graph, let's say with A*.
However, this is not a good approach. Creating the visibility graph in advance requires checking if any two vertices from any two polygons are connected, a check that has higher complexity than determining the distance between two nodes. So working with a modified version of A* that "does everything what it can before checking if two nodes are actually connected" actually speeds up the problem.
Still, A* and all other shortest path problems always start with an explicitly given graph for which adjacent vertices can be traversed cheaply. So my question is, is there a good (optimal?) algorithm for finding a shortest path between two nodes a and b in an "implicit graph" that minimizes checking if two nodes are connected?
Edit:
To clarify what I mean, this is an example of what I'm looking for:
Let V be a set, a, b elements of V. Suppose w: V x V -> D is a weighing function (to some linearly ordered set D) and c: V x V -> {true, false} returns true iff two elements of V are considered to be connected. Then the following algorithm finds the shortest path from a to b in V, i.e., returns a list [x_i | i < n] such that x_0 = a, x_{n-1} = b, and c(x_i, x_{i+1}) = true for all i < n - 1.
Let (V, E) be the complete graph with vertex set V.
do
Compute shortest path from a to b in (V, E) and put it in P = [p_0, ..., p_{n-1}]
if P = empty (there is no shortest path), return NoShortestPath
Let all_good = true
for i = 0 ... n - 2 do
if c(p_i, p_{i+1}) == false, remove edge (p_i, p_{i+1}) from E, set all_good = false and exit for loop
while all_good = false
For computing the shortest paths in the loop, one could use A* if an appropriate heuristic exists. Obviously this algorithm produces a shortest path from a to b.
Also, I suppose this algorithm is somehow optimal in calling c as rarely as possible. For its found shortest path, it must have ruled out all shorter paths that the function w would have allowed for.
But surely there is a better way?
Edit 2:
So I found a solution that works relatively well for what I'm trying to do: Using A*, when relaxing a node, instead of going through the neighbors and adding them to / updating them in the priority queue, I put all vertices into the priority queue, marked as hypothetical, together with hypothetical f and g values and the hypothetical parent. Then, when picking the next element from the priority queue, I check if the node's connection to its parent is actually given. If so, the node is progressed as normal, if not, it is discarded.
This greatly reduces the number of connectivity checks and improves performance for me a lot. But I'm sure there's still a more elegant way, in particular one where the "hypothetical new path" doesn't just extend by length one (parents are always actual, not hypothetical).
A* or Dijkstra's algorithm do not need an explicit graph to work, they actually only need:
source vertex (s)
A function next:V->2^V such that next(v)={u | there is an edge from v to u }
A function isGoal:V->{0,1} such that isGoal(v) = 1 iff v is a target node.
A weight function w:E->R such that w(u,v)= cost to move from u to v
And, of course, in addition A* is going to need a heuristic function h:V->R such that h(v) is the cost approximation.
With these functions, you can generate only the portion of the graph that is needed to find shortest path, on the fly.
In fact, A* algorithm is often used on infinite graphs (or huge graphs that do not fit in any existing storage) in artificial inteliigence problems using this approach.
The idea is, you only look on edges in A* from a given node (all (u,v) in E for some given u). You don't need the entire edges set E in order to do it, you can just use your next(u) function instead.

find all alternate paths shorter than a given distance

graph algorithm question for you.
I have a graph, used to represent a road network. So there are cycles in it (a roundabout would be a trivial one). Also some edges are bi-directional, some are uni-directional (one way streets). Edges are weighted by their length.
Let's say I have two nodes and have already have computed the shortest path between them. What I'd like to do is find all the other paths that connect the two nodes that are shorter than some distance X. Call these paths the "alternates".
An example in ascii art is below, where I have labelled the edges with letters and the nodes with numbers.
F
5----6
E / \ G
3--------4
/ D \
B / \ C
1--------------2
A
Let's say I have the path covering edge A that goes from 1->2 and I want to find alternates. One alternate to that path would be BDC, provided that its length is less than X. BEFGC is another one.
Another example path would be BD that connects nodes 1->4. An alternate to that one would be AC.
More requirements:
alternates should not include any part of the main path. So if the main path is A, any alternate that contains A is not a valid alternate. In the BD example above that connects 1->4, BEFG is not a valid alternate because it includes B which is in the main path.
alternates should not have internal cycles. For example this alternate path would not be allowed for connecting 1->2: BDGFEDC because it traverses edge D twice.
Thanks!
If you run Dijkstra's algorithm to find the shortest path, you have a table which gives you, for each node, the shortest distance to that node from the source. I would delete from the graph the points on the shortest path, run Dijkstra's algorithm, and then do a depth first search from the target, terminating the search every time the path you are currently investigating becomes a cycle, or the sum of the distance on the path so far and the shortest distance from the current node to the source is more than X.
Every time you actually reach the source node you can print out the path so far.

Resources