views:

148

answers:

3

I need to find the shortest path in a graph with the least number of added nodes. The start and end nodes are not important. If there is no path in a graph just between specified n-nodes, I can add some nodes to complete the shortest tree but I want to add as few new nodes as possible.

What algorithm can I use to solve this problem?

+1  A: 

Start with the start node.

if it is the target node, you are done.

Check every connected node, if it is the target node. If true you are done

Check if any of the connected nodes is connected to the target node. If true you are done.

Else add a node that is connected to start and end node. done.

Jens Schauder
You missed the edge case where the start node is the target node (path length=0)
MSalters
A: 

I recommend you to use genetic algorithm. More information here and here. Quickly explaining it, GA is an algorithm to find exact or approximate solutions to optimization and search problems.

You create initial population of possible solutions. You evaluate them with fitness function in order to find out, which of them are most suitable. After that, you use evolutionary algorithms that use techniques inspired by evolutionary biology such as inheritance, mutation, selection, and crossover.

After several generations, you'll find the most suitable (read shortest) solution to the problem.

nhaa123
Could you please describe it in more detailed way? Now this sounds just like "See Donald Knuth's book."
Olexiy
Olexiy: The short explanation I gave was not necessary but wrote it anyway to satisfy your will. The two links I initially gave, gives in-depth and thorough explanation of GA with sample-code.
nhaa123
A: 

You want to minimize the number of nodes in the path (instead of the sum-of-weight as in general algorithms).

If that is the case, assign equal weight to all the edges and find the shortest path (using the generic algorithms). You will have what you needed.

And if there is no path, just add that edge to the graph.

Sands.

PS: If you give a value of 1 for each edge, the number of nodes in the path would be the weight-1 (excluding the source and destination nodes)

ThisIsMeMoony