+4  A: 

There are many algorithms that will do better than calculating all the possible paths. Breadth-first search is the basic starting point for the family of algorithms I have in mind, Best-first search is appropriate because you have vertex costs defined, and if you can get more information about your problem space, you may be able to use A* or Dijkstra's algorithm. (In each case, finding the paths from the set of allowed starting nodes.)

Re your edit: Your path constraint (the array of node types you need to satisfy) doesn't prevent you from working with these algorithms; quite the opposite, it helps them all work better. You just need to implement them in a way that allows the path constraint to be incorporated, limiting the vertices available at each step in the search to those that are valid given the constraint.

chaos
+1 for actually understanding the question. :-)
RBarryYoung
If you use Dijkstra's algorithm you want to have following tuple in priority queue: (cost_so_far, current_vertex, number_of_vertices_visited). Then when iterating through outcoming edges you just ignore edges that lead to not wanted types according to number_of_vertices_visited.
niteria
I can't use A* or Dijkstra.IF I USE BFS IT'S THE SAME OF CALCULATING ALL THE PATHS!
Alberto
You can if you follow the advice.
niteria
Best-first search is not the same as calculating all the paths, because it preferentially explores the ones with lower vertex costs.
chaos
To your re to my edit:I'm not sure to understand what you said... can I do this?
Alberto
At absolute minimum you can use best-first search, yes. I believe you're correct that you can't use A*. I'm not experienced enough with Dijkstra's to say for absolute certain, but it looks to me like you can, and niteria appears confident about it.
chaos
If you can revisit nodes, I'm not sure if Dijkstra still works. Dynamic programming is a way to go then.
niteria
Syncing with comments on question, since you can revisit, Dijkstra's algorithm is also out, so that leaves best-first search. Which, again, is much better than calculating all the paths.
chaos
it isn't better, it' exactly the same
Alberto
Um. Okay. I guess you're using "calculating all the paths" in a specialized manner that actually means "performing best-first search". Because normally, "calculating all the paths" means exhaustive search, which best-first search is an enormous improvement upon.
chaos
And really, if best-first search is "calculating all the paths", then what is breadth-first search? Calculating more than all the paths?
chaos
+1  A: 

As far as I understand your question you need a shortest path in a directed graph. http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm should give you an idea.

regards, Jan

no, thanks for reply but it's different! Please read my (editeed) question better.
Alberto
+1  A: 
  1. Calculate all the pairs of shortest paths within each equivalence block.
  2. Now build a graph which has NO inter-class edges, but whose edges between classes match the shortest path within that class, leading to the specific node of another class.

Hope this is clear.

This solution is not particularly efficient, but clearly polynomial.

EFraim
+1 for actually understanding the question. (:-))
RBarryYoung
this is the same to calculate all the possible paths...
Alberto
@Alberto: No, it is not.
EFraim
+5  A: 

You can use the Floyd–Warshall algorithm. Here's the pseudocode given by WikiPedia:

/* Assume a function edgeCost(i,j) which returns the cost of the edge from i to 
   (infinity if there is none).
   Also assume that n is the number of vertices and edgeCost(i,i)=0
*/

int path[][];

/* A 2-dimensional matrix. At each step in the algorithm, path[i][j] is the shortest path
   from i to j using intermediate vertices (1..k-1).  Each path[i][j] is initialized to
   edgeCost(i,j) or infinity if there is no edge between i and j.
*/

procedure FloydWarshall ()
    for k: = 1 to n
        for each (i,j) in {1,..,n}2
            path[i][j] = min ( path[i][j], path[i][k]+path[k][j] );

I had to write a program for an algorithms course about this same problem. This algorithm worked like a charm! Goodluck.

+1 for actually understanding the question. :-)
RBarryYoung
Dijkstra's algorithm has better complexity.
niteria
I can't see how you want to deal with type constraint.
niteria
How can I set the type rule?
Alberto
+2  A: 

On the revision of your question it seems you ask for one node per letter - in that case it is a simple dynamic programming solution: Calculate all the shortest paths of length 1, which satisfy the beginning of your sequence, between each pair of nodes. Then having for k all such paths for all node pairs, it is trivial to construct for k+1.

EFraim
this is the same to calculate all the possible paths...
Alberto
...but in a clever way. You reuse results from your earlier computation.
niteria
+3  A: 

As Jan mentioned, you just need a normal boring shortest path algorithm (like Dijkstra's or Floyd's algorithm); however, you need to transform your input graph so that the output path will respect your path constraint.

Given a path constraint of: A - B - A

Create a new graph G and insert all of the vertexes from A into G with new labels like a_01. Then insert all the vertexes from B into G and connect the A vertexes with the B vertexes (edges should be directed towards the newly inserted nodes) copying the costs from the original graph. You then repeat this step with A (and any other path components) connecting the newly inserted vertexes to those in B. Thus, you create a graph where only the paths that exist satisfy the path constraint. You can then use normal shortest path algorithms.

The key insight is that when you revisit a class you are actually visiting a distinct set of nodes and that you only want edges that connect adjacent classes of nodes.

Aaron Maenpaa
this is the same to calculate all the possible paths...
Alberto
@Alberto: This is *not* the same as calculating all possible paths. Just like Djikstra's algorithm isn't calculating all the possible paths in the regular shortest-path problem. It finds the best path without enumerating all the possible paths! Aaron's answer is the correct one. He and niteria (although I don't like the code) are right. The rest are wrong or unclear.
yairchu
+3  A: 
Kent Fredric
+1 for pretty pictures!
chaos
However, I don't think he has a start node like you depict; rather, if his path constraint starts with A, he starts from the set of A nodes.
chaos
very similar BUT if the rule is A-B-C I have to link an A-type point to a B-type to a C-type and STOP!!!!
Alberto
oh, I was stating with "..." It was ABC repeat :)
Kent Fredric
can you make the right picture so I can update the question?
Alberto
the image, it is now updated :)
Kent Fredric
now you are very very near.BUT AS I WROTE IN QUESTION: I have n points (p1, p2, p3, .. pn), each of them can connect to any others
Alberto
so there aren't unreachable nodes...
Alberto
Er, they are technically, because its not possible to get to those nodes while conforming to the join rule. IE: in my rule, you cant go from C to B, so those nodes *ARE* unreachable.
Kent Fredric
Ok but the A node in the bottom right can connect to B node in the middle left that can connect to the C node in the top left.So... there aren't unreachable nodes. I give you a vote-up!
Alberto
I updated the image to better represent the unlateral joins between all nodes, see what you think.
Kent Fredric
PERFECT! I'll edit!
Alberto
+2  A: 

Here is pseudocode with dynamic programming solution:

n - length of desired path
m - number of vertices
types[n] // desired type of ith node
vertice_types[m]
d[n][m] // our DP tab initially filled with infinities

d[0][0..m] = 0
for length from 1 to n 
  for b from 0 to m
    if types[length] == vertice_types[b]
      for a from 0 to m
        if types[length-1] == vertice_types[a]
          d[length][b] = min(d[length][b], d[length-1][a] + cost(a,b))

your minimum cost path is min(d[n][0..m])

you can reduce size of d table to 2 rows, but it would obfuscate the solution

niteria
imho it's much better to make your pseudo-code python-style, that is - no endif/endfor noise: indentation matters. congrats for a correct solution.
yairchu
ok, edited. It seemed incomplete without end{if,for}.
niteria