Graph Algorithm: Similiar to TSP -


i want solve problem similar tsp( travelling salesman problem).

i have n ( n > 0, n < 20 ) nodes , must visit nodes.

the cost between nodes equal.

i can visit node unlimited times.

i want find more 1 path , cost has no restriction.

tell me effective algorithms problem?

why not a simple dfs run? o(v + e)

multiple paths can obtained starting algorithm different roots.

bool[] visited; list<int> path; void visit(int u) {     if (visited[u]) return; // visit each node once     visited[u] = true;     path.add(u); // add path     int pathsize = path.count;     foreach(int v in nbs[u]) {         visit(v);         if(path.count > pathsize)         {             // if node visited, add node again             // can move next unvisited subtree             path.add(u);             pathsize = path.count;         }     } } 
  • starting 1: 1 > 2 > 1 > 3 > 4 > 3 > 1
  • starting 2: 2 > 1 > 3 > 4 > 3 > 1 > 2

labeled graph

you can update algorithm ends moment last node visited.


Comments