I have a directed graph (more specifically, a control flow graph), and each of the vertices has a set of properties.
I'd like to find (or write) an algorithm that, given a vertex V with property P, finds the longest path to some vertex E such that all vertices along all possible paths from V to E contain the property P.
Example 1
Say I had the following graph. (Please excuse the bad ascii drawing.)
+----+
+--------+P +--------+
| +----+ |
| V1 |
| |
| |
+--v--+ |
+----+P ++ |
| +-----++ +--v--+
| | +----+P |
| | | +-----+
+--v--+ +--v--+ |
|P +-+ +-+P | |
+-----+ | | +-----+ |
| | |
| | |
+v-v--+ |
V6 |P +---------+ |
+-----+ | |
| |
| |
| |
| |
+-v--v-+
V7 |P |
+---+--+
|
|
+---v--+
V8 |!P |
+------+
Starting at V1, the longest path where P always holds on all possible paths is V1 -> V7. Note that the other paths, say V1 -> V6, are "valid" in that P always holds, but V1 -> V7 is the longest.
Example 2
This example is the same as above, except now the P doesn't hold in V3:
+----+
+--------+P +--------+
| +----+ |
| V1 |
| |
| |
+--v--+ |
+----+P ++ |
| +-----++ +--v--+
| | +----+!P | V3
| | | +-----+
+--v--+ +--v--+ |
|P +-+ +-+P | |
+-----+ | | +-----+ |
| | |
| | |
+v-v--+ |
V6 |P +---------+ |
+-----+ | |
| |
| |
| |
| |
+-v--v-+
V7 |P |
+---+--+
|
|
+---v--+
V8 |!P |
+------+
In this case, starting at V1, the longest path where P always holds in all possible paths is V1 -> V6. The path V1 -> V7 is not valid, because there is a path between V1 and V7 in which P does not hold.
Further notes about my situation
- The graph could be cyclic.
- The graph will be of a "small to medium" size, with maybe 1000 vertices or less.
- The graph does not necessarily always have one root and one leaf, like my examples above.
Question
Is there a standard algorithm for computing such paths?
Sto some vertexEsuch thatPholds on all vertices on all paths betweenSandE? That seems a very strict constraint... - Vincent van der Weelep, and find longest path). Since Hamiltonian path is NP-Complete, so is this problem, and there is no known polynomial solution to it. - amit