Closed anayyee closed 5 months ago
very nice ! we'll merge this, but there are issues i am noticing on this code:
main.cpp
, so it won't run if we compile this code (you should be writing your code snippet under the todo i wrote in the main.cpp
file)next_permutation
method will cause a compile errortotalWeight
while you're printing out the paths, method getWeight()
doesn't exist in the code. don't worry about this one, i wrote it ingetWeight()
method does exist and is implemented, the incorrect output is given:
( on debugging, it seems the permutation function is asking for the weight from Riverside -> Riverside, which does not exist in the graph)i think you would have an easier time using Heap's algorithm to generate all permutations, and then just tack riverside to the beginning and end of each permutation that's generated
how would i do number 2? Do you think Chayrae might be able to help with that? Also what is Heap's method 😅Sent from my iPhoneOn May 25, 2024, at 10:16 AM, gray @.***> wrote: very nice ! we'll merge this, but there are issues i am noticing on this code:
you did not put your code snippet in main.cpp, so it won't run if we compile this code (you should be writing your code snippet under the todo i wrote in the main.cpp file) you forgot to import the algorithms header in your code, so the next_permutation method will cause a compile error when you add the edge weights to the totalWeight while you're printing out the paths, method getWeight() doesn't exist in the code. don't worry about this one, i wrote it in when the getWeight() method does exist and is implemented, the incorrect output is given: image.png (view on web) ( on debugging, it seems the permutation function is asking for the weight from Riverside -> Riverside, which does not exist in the graph)
i think you would have an easier time using Heap's algorithm to generate all permutations, and then just tack riverside to the beginning and end of each permutation that's generated
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>
in regards to my email thank you for including a link to heaps algorithm i missed that the first time 😅Sent from my iPhoneOn May 25, 2024, at 10:16 AM, gray @.***> wrote: very nice ! we'll merge this, but there are issues i am noticing on this code:
you did not put your code snippet in main.cpp, so it won't run if we compile this code (you should be writing your code snippet under the todo i wrote in the main.cpp file) you forgot to import the algorithms header in your code, so the next_permutation method will cause a compile error when you add the edge weights to the totalWeight while you're printing out the paths, method getWeight() doesn't exist in the code. don't worry about this one, i wrote it in when the getWeight() method does exist and is implemented, the incorrect output is given: image.png (view on web) ( on debugging, it seems the permutation function is asking for the weight from Riverside -> Riverside, which does not exist in the graph)
i think you would have an easier time using Heap's algorithm to generate all permutations, and then just tack riverside to the beginning and end of each permutation that's generated
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>
yw! and for #2 just tack #include <algorithms>
at the top of main.cpp
and compile from there :-)
vector<vector> permutations;
do {
vector perm = {"Riverside"};
perm.insert(perm.end(), cities.begin(), cities.end());
perm.push_back("Riverside");
permutations.push_back(perm);
} while (next_permutation(cities.begin(), cities.end()));
// Calculate the weight for each permutation and print them out for (int i = 0; i < permutations.size(); ++i) { float totalWeight = 0; for (int j = 0; j < permutations[i].size() - 1; ++j) { totalWeight += G.getWeight(permutations[i][j], permutations[i][j+1]); } cout << "Trip " << i+1 << ": "; for (int k = 0; k < permutations[i].size(); ++k) { cout << permutations[i][k]; if (k < permutations[i].size() - 1) cout << " -> "; } cout << " (Total Weight: " << totalWeight << ")" << endl; }
return 0; }