Finding the shortest path into a graph using BFS problem
i am working at a HomeWork from school. I have to find the shortest path into a graph, using the BFS algorithm. The start point is 2, 0 is wall, 1 is path and 3 is finish point. I read from a txt file a 2D Matrix, i need to transform that matrix into a graph using adjancency list, and then aplly the BFS algorithm for the graph resulted. So my problem is that when i am printing the path it display only "18". I think the my BPS algorithm don't populate the resultList, but i just can't find out why. Is there anybody who may help me please?
struct Graf {
int matrix[200][200];
int numberOfLines;
int numberOfColumns;
int intrare;
std::vector<int>iesiri;
std::map<int, std::vector<int>> grafNeor;
std::map<int, std::vector<int>>::iterator it;
void citire() {
std::ifstream f("Matrix.txt");
f >> numberOfLines >> numberOfColumns;
std::cout << "numberOfLines: " << numberOfLines << std::endl;
std::cout << "numberOfColumns: " << numberOfColumns << std::endl;
for (int lineNumber = 0; lineNumber < numberOfLines; lineNumber++)
for (int columnNumber = 0; columnNumber < numberOfColumns; columnNumber++)
f >> matrix[lineNumber][columnNumber];
f.close();
for (int i = 0; i < numberOfLines; ++i) {
for (int j = 0; j < numberOfColumns; ++j) {
if (matrix[i][j] == 3) {
intrare = numberOfColumns * i + j;
}
if (matrix[i][j] == 2) {
iesiri.push_back(numberOfColumns * i + j);
}
}
}
for (int lineNumber = 0; lineNumber < numberOfLines; lineNumber++) {
for (int columnNumber = 0; columnNumber < numberOfColumns; columnNumber++)
std::cout << matrix[lineNumber][columnNumber] << " ";
std::cout << std::endl;
}
}
void tranzitie() {
for (int i = 0; i < numberOfColumns * numberOfLines; i++)
grafNeor.insert(std::pair<int, std::vector<int> >(i, std::vector<int>()));
for (int i = 0; i < numberOfLines; i++) {
for (int j = 0; j < numberOfColumns; j++) {
int key = numberOfColumns * i + j;
if (matrix[i][j] != 0) {
// stanga
if (j > 0 && matrix[i][j - 1] != 0) {
int currentValue = numberOfColumns * i + j - 1;
it = grafNeor.find(key);
if (it != grafNeor.end())
grafNeor.insert(std::pair<int, std::vector<int> >(currentValue, std::vector<int>()));
}
// dreapta
if (j < numberOfColumns - 1 && matrix[i][j + 1] != 0) {
int currentValue = numberOfColumns * i + j + 1;
it = grafNeor.find(key);
if (it != grafNeor.end())
grafNeor.insert(std::pair<int, std::vector<int> >(currentValue, std::vector<int>()));
}
// sus
if (i > 0 && matrix[i - 1][j] != 0) {
int currentValue = numberOfColumns * (i - 1) + j;
it = grafNeor.find(key);
if (it != grafNeor.end())
grafNeor.insert(std::pair<int, std::vector<int> >(currentValue, std::vector<int>()));
}
// jos
if (i < numberOfLines - 1 && matrix[i + 1][j] != 0) {
int currentValue = numberOfColumns * (i + 1) + j;
it = grafNeor.find(key);
if (it != grafNeor.end())
grafNeor.insert(std::pair<int, std::vector<int> >(currentValue, std::vector<int>()));
}
}
}
}
}
void PBF() {
std::vector<int> noduri;
for (int i = 0; i < numberOfColumns * numberOfLines; i++)
noduri.push_back(i);
std::vector<int> noduriNevizitate;
noduriNevizitate.assign(noduri.begin(), noduri.end());
noduriNevizitate.erase(std::remove(noduriNevizitate.begin(), noduriNevizitate.end(), intrare));
std::vector<int> noduriCurente;
noduriCurente.push_back(intrare);
std::vector<int> noduriVizitate;
std::vector<int> drum;
for (int i = 0; i < numberOfColumns * numberOfLines; i++)
drum.push_back(0);
for (int i = 0; i < noduriNevizitate.size(); i++) {
drum.assign(noduriNevizitate.at(i), -1);
}
int current = 0;
while (noduriCurente.size() > 0)
{
current = noduriCurente.at(0);
for (int i = 0; i < iesiri.size(); i++) {
if (iesiri[i] == current)
break;
}
for (int vecin : grafNeor.at(current)) {
for (int i = 0; i <= noduriNevizitate.size(); i++) {
if (noduriNevizitate[i] == vecin) {
noduriNevizitate.erase(std::remove(noduriNevizitate.begin(), noduriNevizitate.end(), vecin));
noduriCurente.push_back(vecin);
drum.assign(vecin, current);
}
}
}
noduriCurente.erase(std::remove(noduriCurente.begin(), noduriCurente.end(), current));
noduriVizitate.push_back(current);
}
int before = current;
std::vector<int> resultList;
while (before != current) {
resultList.push_back(current);
before = drum.at(current);
current = before;
}
resultList.push_back(intrare);
std::cout << "drum: ";
for (int i = 0; i < resultList.size(); i++)
std::cout << resultList[i];
}
};
int main() {
Graf graf;
graf.citire();
graf.tranzitie();
graf.PBF();
system("pause");
return 0;
}
c++ graph-theory shortest-path breadth-first-search maze
add a comment |
i am working at a HomeWork from school. I have to find the shortest path into a graph, using the BFS algorithm. The start point is 2, 0 is wall, 1 is path and 3 is finish point. I read from a txt file a 2D Matrix, i need to transform that matrix into a graph using adjancency list, and then aplly the BFS algorithm for the graph resulted. So my problem is that when i am printing the path it display only "18". I think the my BPS algorithm don't populate the resultList, but i just can't find out why. Is there anybody who may help me please?
struct Graf {
int matrix[200][200];
int numberOfLines;
int numberOfColumns;
int intrare;
std::vector<int>iesiri;
std::map<int, std::vector<int>> grafNeor;
std::map<int, std::vector<int>>::iterator it;
void citire() {
std::ifstream f("Matrix.txt");
f >> numberOfLines >> numberOfColumns;
std::cout << "numberOfLines: " << numberOfLines << std::endl;
std::cout << "numberOfColumns: " << numberOfColumns << std::endl;
for (int lineNumber = 0; lineNumber < numberOfLines; lineNumber++)
for (int columnNumber = 0; columnNumber < numberOfColumns; columnNumber++)
f >> matrix[lineNumber][columnNumber];
f.close();
for (int i = 0; i < numberOfLines; ++i) {
for (int j = 0; j < numberOfColumns; ++j) {
if (matrix[i][j] == 3) {
intrare = numberOfColumns * i + j;
}
if (matrix[i][j] == 2) {
iesiri.push_back(numberOfColumns * i + j);
}
}
}
for (int lineNumber = 0; lineNumber < numberOfLines; lineNumber++) {
for (int columnNumber = 0; columnNumber < numberOfColumns; columnNumber++)
std::cout << matrix[lineNumber][columnNumber] << " ";
std::cout << std::endl;
}
}
void tranzitie() {
for (int i = 0; i < numberOfColumns * numberOfLines; i++)
grafNeor.insert(std::pair<int, std::vector<int> >(i, std::vector<int>()));
for (int i = 0; i < numberOfLines; i++) {
for (int j = 0; j < numberOfColumns; j++) {
int key = numberOfColumns * i + j;
if (matrix[i][j] != 0) {
// stanga
if (j > 0 && matrix[i][j - 1] != 0) {
int currentValue = numberOfColumns * i + j - 1;
it = grafNeor.find(key);
if (it != grafNeor.end())
grafNeor.insert(std::pair<int, std::vector<int> >(currentValue, std::vector<int>()));
}
// dreapta
if (j < numberOfColumns - 1 && matrix[i][j + 1] != 0) {
int currentValue = numberOfColumns * i + j + 1;
it = grafNeor.find(key);
if (it != grafNeor.end())
grafNeor.insert(std::pair<int, std::vector<int> >(currentValue, std::vector<int>()));
}
// sus
if (i > 0 && matrix[i - 1][j] != 0) {
int currentValue = numberOfColumns * (i - 1) + j;
it = grafNeor.find(key);
if (it != grafNeor.end())
grafNeor.insert(std::pair<int, std::vector<int> >(currentValue, std::vector<int>()));
}
// jos
if (i < numberOfLines - 1 && matrix[i + 1][j] != 0) {
int currentValue = numberOfColumns * (i + 1) + j;
it = grafNeor.find(key);
if (it != grafNeor.end())
grafNeor.insert(std::pair<int, std::vector<int> >(currentValue, std::vector<int>()));
}
}
}
}
}
void PBF() {
std::vector<int> noduri;
for (int i = 0; i < numberOfColumns * numberOfLines; i++)
noduri.push_back(i);
std::vector<int> noduriNevizitate;
noduriNevizitate.assign(noduri.begin(), noduri.end());
noduriNevizitate.erase(std::remove(noduriNevizitate.begin(), noduriNevizitate.end(), intrare));
std::vector<int> noduriCurente;
noduriCurente.push_back(intrare);
std::vector<int> noduriVizitate;
std::vector<int> drum;
for (int i = 0; i < numberOfColumns * numberOfLines; i++)
drum.push_back(0);
for (int i = 0; i < noduriNevizitate.size(); i++) {
drum.assign(noduriNevizitate.at(i), -1);
}
int current = 0;
while (noduriCurente.size() > 0)
{
current = noduriCurente.at(0);
for (int i = 0; i < iesiri.size(); i++) {
if (iesiri[i] == current)
break;
}
for (int vecin : grafNeor.at(current)) {
for (int i = 0; i <= noduriNevizitate.size(); i++) {
if (noduriNevizitate[i] == vecin) {
noduriNevizitate.erase(std::remove(noduriNevizitate.begin(), noduriNevizitate.end(), vecin));
noduriCurente.push_back(vecin);
drum.assign(vecin, current);
}
}
}
noduriCurente.erase(std::remove(noduriCurente.begin(), noduriCurente.end(), current));
noduriVizitate.push_back(current);
}
int before = current;
std::vector<int> resultList;
while (before != current) {
resultList.push_back(current);
before = drum.at(current);
current = before;
}
resultList.push_back(intrare);
std::cout << "drum: ";
for (int i = 0; i < resultList.size(); i++)
std::cout << resultList[i];
}
};
int main() {
Graf graf;
graf.citire();
graf.tranzitie();
graf.PBF();
system("pause");
return 0;
}
c++ graph-theory shortest-path breadth-first-search maze
You should create a Minimal, Complete, and Verifiable example: stackoverflow.com/help/mcve
– NiVeR
Nov 22 '18 at 11:00
The code inside your while loop (while (before != current) {}
) will never be executed, becausebefore
is initialized with the value ofcurrent
, and is not modified before the conditional. You probably don't wantint before = current;
.
– not an alien
Nov 22 '18 at 11:12
And how could i change it?
– Alin Bv
Nov 22 '18 at 13:57
add a comment |
i am working at a HomeWork from school. I have to find the shortest path into a graph, using the BFS algorithm. The start point is 2, 0 is wall, 1 is path and 3 is finish point. I read from a txt file a 2D Matrix, i need to transform that matrix into a graph using adjancency list, and then aplly the BFS algorithm for the graph resulted. So my problem is that when i am printing the path it display only "18". I think the my BPS algorithm don't populate the resultList, but i just can't find out why. Is there anybody who may help me please?
struct Graf {
int matrix[200][200];
int numberOfLines;
int numberOfColumns;
int intrare;
std::vector<int>iesiri;
std::map<int, std::vector<int>> grafNeor;
std::map<int, std::vector<int>>::iterator it;
void citire() {
std::ifstream f("Matrix.txt");
f >> numberOfLines >> numberOfColumns;
std::cout << "numberOfLines: " << numberOfLines << std::endl;
std::cout << "numberOfColumns: " << numberOfColumns << std::endl;
for (int lineNumber = 0; lineNumber < numberOfLines; lineNumber++)
for (int columnNumber = 0; columnNumber < numberOfColumns; columnNumber++)
f >> matrix[lineNumber][columnNumber];
f.close();
for (int i = 0; i < numberOfLines; ++i) {
for (int j = 0; j < numberOfColumns; ++j) {
if (matrix[i][j] == 3) {
intrare = numberOfColumns * i + j;
}
if (matrix[i][j] == 2) {
iesiri.push_back(numberOfColumns * i + j);
}
}
}
for (int lineNumber = 0; lineNumber < numberOfLines; lineNumber++) {
for (int columnNumber = 0; columnNumber < numberOfColumns; columnNumber++)
std::cout << matrix[lineNumber][columnNumber] << " ";
std::cout << std::endl;
}
}
void tranzitie() {
for (int i = 0; i < numberOfColumns * numberOfLines; i++)
grafNeor.insert(std::pair<int, std::vector<int> >(i, std::vector<int>()));
for (int i = 0; i < numberOfLines; i++) {
for (int j = 0; j < numberOfColumns; j++) {
int key = numberOfColumns * i + j;
if (matrix[i][j] != 0) {
// stanga
if (j > 0 && matrix[i][j - 1] != 0) {
int currentValue = numberOfColumns * i + j - 1;
it = grafNeor.find(key);
if (it != grafNeor.end())
grafNeor.insert(std::pair<int, std::vector<int> >(currentValue, std::vector<int>()));
}
// dreapta
if (j < numberOfColumns - 1 && matrix[i][j + 1] != 0) {
int currentValue = numberOfColumns * i + j + 1;
it = grafNeor.find(key);
if (it != grafNeor.end())
grafNeor.insert(std::pair<int, std::vector<int> >(currentValue, std::vector<int>()));
}
// sus
if (i > 0 && matrix[i - 1][j] != 0) {
int currentValue = numberOfColumns * (i - 1) + j;
it = grafNeor.find(key);
if (it != grafNeor.end())
grafNeor.insert(std::pair<int, std::vector<int> >(currentValue, std::vector<int>()));
}
// jos
if (i < numberOfLines - 1 && matrix[i + 1][j] != 0) {
int currentValue = numberOfColumns * (i + 1) + j;
it = grafNeor.find(key);
if (it != grafNeor.end())
grafNeor.insert(std::pair<int, std::vector<int> >(currentValue, std::vector<int>()));
}
}
}
}
}
void PBF() {
std::vector<int> noduri;
for (int i = 0; i < numberOfColumns * numberOfLines; i++)
noduri.push_back(i);
std::vector<int> noduriNevizitate;
noduriNevizitate.assign(noduri.begin(), noduri.end());
noduriNevizitate.erase(std::remove(noduriNevizitate.begin(), noduriNevizitate.end(), intrare));
std::vector<int> noduriCurente;
noduriCurente.push_back(intrare);
std::vector<int> noduriVizitate;
std::vector<int> drum;
for (int i = 0; i < numberOfColumns * numberOfLines; i++)
drum.push_back(0);
for (int i = 0; i < noduriNevizitate.size(); i++) {
drum.assign(noduriNevizitate.at(i), -1);
}
int current = 0;
while (noduriCurente.size() > 0)
{
current = noduriCurente.at(0);
for (int i = 0; i < iesiri.size(); i++) {
if (iesiri[i] == current)
break;
}
for (int vecin : grafNeor.at(current)) {
for (int i = 0; i <= noduriNevizitate.size(); i++) {
if (noduriNevizitate[i] == vecin) {
noduriNevizitate.erase(std::remove(noduriNevizitate.begin(), noduriNevizitate.end(), vecin));
noduriCurente.push_back(vecin);
drum.assign(vecin, current);
}
}
}
noduriCurente.erase(std::remove(noduriCurente.begin(), noduriCurente.end(), current));
noduriVizitate.push_back(current);
}
int before = current;
std::vector<int> resultList;
while (before != current) {
resultList.push_back(current);
before = drum.at(current);
current = before;
}
resultList.push_back(intrare);
std::cout << "drum: ";
for (int i = 0; i < resultList.size(); i++)
std::cout << resultList[i];
}
};
int main() {
Graf graf;
graf.citire();
graf.tranzitie();
graf.PBF();
system("pause");
return 0;
}
c++ graph-theory shortest-path breadth-first-search maze
i am working at a HomeWork from school. I have to find the shortest path into a graph, using the BFS algorithm. The start point is 2, 0 is wall, 1 is path and 3 is finish point. I read from a txt file a 2D Matrix, i need to transform that matrix into a graph using adjancency list, and then aplly the BFS algorithm for the graph resulted. So my problem is that when i am printing the path it display only "18". I think the my BPS algorithm don't populate the resultList, but i just can't find out why. Is there anybody who may help me please?
struct Graf {
int matrix[200][200];
int numberOfLines;
int numberOfColumns;
int intrare;
std::vector<int>iesiri;
std::map<int, std::vector<int>> grafNeor;
std::map<int, std::vector<int>>::iterator it;
void citire() {
std::ifstream f("Matrix.txt");
f >> numberOfLines >> numberOfColumns;
std::cout << "numberOfLines: " << numberOfLines << std::endl;
std::cout << "numberOfColumns: " << numberOfColumns << std::endl;
for (int lineNumber = 0; lineNumber < numberOfLines; lineNumber++)
for (int columnNumber = 0; columnNumber < numberOfColumns; columnNumber++)
f >> matrix[lineNumber][columnNumber];
f.close();
for (int i = 0; i < numberOfLines; ++i) {
for (int j = 0; j < numberOfColumns; ++j) {
if (matrix[i][j] == 3) {
intrare = numberOfColumns * i + j;
}
if (matrix[i][j] == 2) {
iesiri.push_back(numberOfColumns * i + j);
}
}
}
for (int lineNumber = 0; lineNumber < numberOfLines; lineNumber++) {
for (int columnNumber = 0; columnNumber < numberOfColumns; columnNumber++)
std::cout << matrix[lineNumber][columnNumber] << " ";
std::cout << std::endl;
}
}
void tranzitie() {
for (int i = 0; i < numberOfColumns * numberOfLines; i++)
grafNeor.insert(std::pair<int, std::vector<int> >(i, std::vector<int>()));
for (int i = 0; i < numberOfLines; i++) {
for (int j = 0; j < numberOfColumns; j++) {
int key = numberOfColumns * i + j;
if (matrix[i][j] != 0) {
// stanga
if (j > 0 && matrix[i][j - 1] != 0) {
int currentValue = numberOfColumns * i + j - 1;
it = grafNeor.find(key);
if (it != grafNeor.end())
grafNeor.insert(std::pair<int, std::vector<int> >(currentValue, std::vector<int>()));
}
// dreapta
if (j < numberOfColumns - 1 && matrix[i][j + 1] != 0) {
int currentValue = numberOfColumns * i + j + 1;
it = grafNeor.find(key);
if (it != grafNeor.end())
grafNeor.insert(std::pair<int, std::vector<int> >(currentValue, std::vector<int>()));
}
// sus
if (i > 0 && matrix[i - 1][j] != 0) {
int currentValue = numberOfColumns * (i - 1) + j;
it = grafNeor.find(key);
if (it != grafNeor.end())
grafNeor.insert(std::pair<int, std::vector<int> >(currentValue, std::vector<int>()));
}
// jos
if (i < numberOfLines - 1 && matrix[i + 1][j] != 0) {
int currentValue = numberOfColumns * (i + 1) + j;
it = grafNeor.find(key);
if (it != grafNeor.end())
grafNeor.insert(std::pair<int, std::vector<int> >(currentValue, std::vector<int>()));
}
}
}
}
}
void PBF() {
std::vector<int> noduri;
for (int i = 0; i < numberOfColumns * numberOfLines; i++)
noduri.push_back(i);
std::vector<int> noduriNevizitate;
noduriNevizitate.assign(noduri.begin(), noduri.end());
noduriNevizitate.erase(std::remove(noduriNevizitate.begin(), noduriNevizitate.end(), intrare));
std::vector<int> noduriCurente;
noduriCurente.push_back(intrare);
std::vector<int> noduriVizitate;
std::vector<int> drum;
for (int i = 0; i < numberOfColumns * numberOfLines; i++)
drum.push_back(0);
for (int i = 0; i < noduriNevizitate.size(); i++) {
drum.assign(noduriNevizitate.at(i), -1);
}
int current = 0;
while (noduriCurente.size() > 0)
{
current = noduriCurente.at(0);
for (int i = 0; i < iesiri.size(); i++) {
if (iesiri[i] == current)
break;
}
for (int vecin : grafNeor.at(current)) {
for (int i = 0; i <= noduriNevizitate.size(); i++) {
if (noduriNevizitate[i] == vecin) {
noduriNevizitate.erase(std::remove(noduriNevizitate.begin(), noduriNevizitate.end(), vecin));
noduriCurente.push_back(vecin);
drum.assign(vecin, current);
}
}
}
noduriCurente.erase(std::remove(noduriCurente.begin(), noduriCurente.end(), current));
noduriVizitate.push_back(current);
}
int before = current;
std::vector<int> resultList;
while (before != current) {
resultList.push_back(current);
before = drum.at(current);
current = before;
}
resultList.push_back(intrare);
std::cout << "drum: ";
for (int i = 0; i < resultList.size(); i++)
std::cout << resultList[i];
}
};
int main() {
Graf graf;
graf.citire();
graf.tranzitie();
graf.PBF();
system("pause");
return 0;
}
c++ graph-theory shortest-path breadth-first-search maze
c++ graph-theory shortest-path breadth-first-search maze
edited Nov 23 '18 at 10:43
Dominique Fortin
1,648816
1,648816
asked Nov 22 '18 at 10:53
Alin BvAlin Bv
42
42
You should create a Minimal, Complete, and Verifiable example: stackoverflow.com/help/mcve
– NiVeR
Nov 22 '18 at 11:00
The code inside your while loop (while (before != current) {}
) will never be executed, becausebefore
is initialized with the value ofcurrent
, and is not modified before the conditional. You probably don't wantint before = current;
.
– not an alien
Nov 22 '18 at 11:12
And how could i change it?
– Alin Bv
Nov 22 '18 at 13:57
add a comment |
You should create a Minimal, Complete, and Verifiable example: stackoverflow.com/help/mcve
– NiVeR
Nov 22 '18 at 11:00
The code inside your while loop (while (before != current) {}
) will never be executed, becausebefore
is initialized with the value ofcurrent
, and is not modified before the conditional. You probably don't wantint before = current;
.
– not an alien
Nov 22 '18 at 11:12
And how could i change it?
– Alin Bv
Nov 22 '18 at 13:57
You should create a Minimal, Complete, and Verifiable example: stackoverflow.com/help/mcve
– NiVeR
Nov 22 '18 at 11:00
You should create a Minimal, Complete, and Verifiable example: stackoverflow.com/help/mcve
– NiVeR
Nov 22 '18 at 11:00
The code inside your while loop (
while (before != current) {}
) will never be executed, because before
is initialized with the value of current
, and is not modified before the conditional. You probably don't want int before = current;
.– not an alien
Nov 22 '18 at 11:12
The code inside your while loop (
while (before != current) {}
) will never be executed, because before
is initialized with the value of current
, and is not modified before the conditional. You probably don't want int before = current;
.– not an alien
Nov 22 '18 at 11:12
And how could i change it?
– Alin Bv
Nov 22 '18 at 13:57
And how could i change it?
– Alin Bv
Nov 22 '18 at 13:57
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429353%2ffinding-the-shortest-path-into-a-graph-using-bfs-problem%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429353%2ffinding-the-shortest-path-into-a-graph-using-bfs-problem%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You should create a Minimal, Complete, and Verifiable example: stackoverflow.com/help/mcve
– NiVeR
Nov 22 '18 at 11:00
The code inside your while loop (
while (before != current) {}
) will never be executed, becausebefore
is initialized with the value ofcurrent
, and is not modified before the conditional. You probably don't wantint before = current;
.– not an alien
Nov 22 '18 at 11:12
And how could i change it?
– Alin Bv
Nov 22 '18 at 13:57