Yes, the answer can be different. For instance, if the graph has 4 vertices and the following undirected edges:
1-2 1
2-3 1
3-4 1
1-4 100
1-3 2
2-4 2
The optimal path is 1-2-3-4
with a weight 1 + 1 + 1 = 3, but the weight of the same cycle is 1 + 1 + 1 + 100 = 103. However, the weight of the cycle 1-3-4-2
is 2 + 1 + 2 + 1 = 6 and the weight of this path is 2 + 1 + 2 = 5, so the optimal cycle and the optimal path are different.
If you're looking for a path, not a cycle, you can use the same algorithm, but you don't need to add the weight of the last edge to the start vertex, that is
for($k = 1; $k < $nb_nodes; $k++) $res[] = [$c["($bits,$k)"][0] + $matrix[$k][0], $k];
should be for($k = 1; $k < $nb_nodes; $k++) $res[] = [$c["($bits,$k)"][0], $k];