1

Basically I have a graph with 12 nodes (representing cities) and 13 edges (representing routes).

enter image description here

Now let's say that (randomly) I have a plan for visiting n nodes, departing from a specific one (A). So (having N <= (12-1)) and then come to the starting point.

For what I've been looking, it seems almost like the Traveling Salesman Problem but with the difference that in my salesman doesn't necessarily needs to visit all nodes.

What algorithm am I looking for?

EDIT

Apparently this is not going to be a TSP, because TSP says that the graph must be closed and we go through every city (node) only once. In my case, it can cross a city more than once, if it makes the route shorter.

A few more examples for what am I looking for:

Example one:

Depart from: A
Need to visit (B,D,E,L,G,J,K)
Come back to: A

Example two:

Depart from: A
Need to visit (B,C,D,G,H,I,J,K)
Come back to: A

Rules:

- Get shortest path
- No specific order
- Can visit one node (city) more than once

Remember, this is for a project in C, so this is just pre-coding research.

eduardev
  • 473
  • 8
  • 26
  • Homework? Show us what you have done? – JoeC Nov 19 '13 at 20:47
  • This is called the traveling salesman problem. try googling "traveling salesman problem" – DwB Nov 19 '13 at 20:48
  • 1
    Just ignore the nodes you don't have to visit and you're still facing the travelling salesman problem. – Nir Alfasi Nov 19 '13 at 20:54
  • OP - I'm guessing any N of the nodes are acceptable? Also, 12 nodes and 13 edges is pretty sparse, there may often be no solution, or just one, in which case the search for the optimal solution (the goal is the same as traveling salesperson, to choose paths that minimize the cost, right?) is kind of meaningless. – jon_darkstar Nov 19 '13 at 21:00
  • sorry guys for the time for reply. It's a C project i'm on. Still on research phase, no coding yet. Just wondering how am I gonna deal with this one problem. And yes, N can be any number (random generated) being at least one node to visit. – eduardev Nov 19 '13 at 21:50

2 Answers2

1

There are a lot of algorithms out there doing this. The catchword is path-finding.

The best algorithm to learn from at the beginning is the good old Dijkstra http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm

Then for larger graphs (that are no maze) you might want an algorithm with some direction heuristics making evaluation faster like the A* algorithm. http://en.wikipedia.org/wiki/A*

There are others, but these are tthe two most common.

Update from the discussion:

From our discussion I think going trough all permutations of the "must have nodes" B|L|I|D|G|K|J, starting from A and then going to A again would be an approach to solve it:

// Prepare a two dimensional array for the permutations
Node permutation[permutationCount][7];

// Fill it with all permutations
...

int cost[permutationCount];

for (int i = 0; i < permutationCount; ++i) {
    cost[i] =   dijkstraCost(nodeA,             permutation[i][0]) 
              + dijkstraCost(permutation[i][0], permutation[i][1])
              + dijkstraCost(permutation[i][1], permutation[i][2])
              + dijkstraCost(permutation[i][2], permutation[i][3])
              + dijkstraCost(permutation[i][3], permutation[i][4])
              + dijkstraCost(permutation[i][4], permutation[i][5])
              + dijkstraCost(permutation[i][5], permutation[i][6])
              + dijkstraCost(permutation[i][6], nodeA);
}

// Now Evaluate lowest cost and you have your shortest path(s)
....

I think that should work.

Marcel Blanck
  • 867
  • 7
  • 12
  • Both Dijkstra and A* are for a path with least cost, so not what I'm looking at, or am I missing something here. It's more like the Salesman Problem – eduardev Nov 19 '13 at 21:53
  • Ok but what are the rules for nodes you do not need to visit. Or do you only want to go from K to D without having to visit all the nodes. Maybe I do not get the question here, becaus,e as the others stated, TSM would require to remove the nodes not to visit. - I would say: "Visit all nodes" - TSM, "Visit all notes exept some specified" - TSM with removing the nodes from the net, "Go from here to there, without the necessity to visit all nodes" - pathfinding – Marcel Blanck Nov 24 '13 at 06:42
  • Hi. I need to depart from node A, visit a given set of cities (ex.g. B,L,I,D,G,K,J) and then come back to A. Shortest route. Rules: none really. I can pass through a node (city) more than once, if it will make my route shorter. Apparently, it's not the TSP because TSP says pass through a city ONLY once. – eduardev Nov 25 '13 at 18:29
  • Ok do you need to visit the nodes B,L,I,D,G,K,J in that order? Because then again I would use Dijkstra. If not it will be much harder to come up with something. - Maybe (still Dijkstra)s tarting with a, calculationg trought all Permutations of B,L,I,D,G,K,J + going back. Evaluationg each route in cost and then decide for one. – Marcel Blanck Nov 25 '13 at 18:46
  • No order, only shortest path... I tough about testing all permutations (aka brute force) but then again, any algorithm for that? I also tought about a minimum spanning tree and then Dijkstra for the way back (on every ending node choosing the shortest) but... – eduardev Nov 25 '13 at 18:58
  • Yeah algorithm would be like int permutationcost = dijkstraCost(A,B) + dijkstraCost(B,L) + ... + dijkstraCost(K,J) + dijkstraCost(J,A) => doing that for all permutations. Would be my fist naive approach. I can not come up with something other than dijkstra because it looks perfectly suited to me for this. – Marcel Blanck Nov 25 '13 at 19:05
  • Ok check my update! You only have to create the given network of nodes and implement pathfinding with Dijkstra for it. Then go for brute force ^^ – Marcel Blanck Nov 25 '13 at 19:29
  • Marcel nice approach, let me think and work on it, and I I'll get back to you, and will probably accept your answer. Thanks :) – eduardev Nov 25 '13 at 19:35
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/41896/discussion-between-eduardo-and-marcel-blanck) – eduardev Nov 25 '13 at 19:40
0

You are right it is a TSP, but what you need to do is too reduce the graph so it only contains nodes that are to be visited.

How to reduce the graph is left as an exercise for the reader ;-)

Bertil Baron
  • 4,923
  • 1
  • 15
  • 24
  • I suppose, remove the actual nodes form it, and add up their edges weight. Hmmm and what about intersection nodes!? ... interesting! Still, this is a C application, I've been told math was going to be important at some point ;) – eduardev Nov 20 '13 at 16:03
  • 1
    Example: if you don't need to visit node I in the graph above you would remove this node and adding the edges: H <--> L weight 7, L <--> J weight 10, J <--> H weight 9 – Bertil Baron Nov 21 '13 at 11:19
  • Apparently this is not TSP, because TSP says that we pass through a city only once. And about removing the node, is there an algorithm I can program? – eduardev Nov 25 '13 at 18:31