Mar 22, 2021 at 4:20pm UTC
Hello, I'm having a very hard time solving this problem
My work is to read from the text file, apply shortest path searching (BFS or UCS), and print out the shortest path. But what I have done is only read from the file, here is my program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <iostream>
#include <vector>
#include <fstream>
class graph {
private :
int total, source, goal;
std::vector<std::vector<int >> g;
public :
void read_file(const char * name);
};
void graph::read_file(const char * name) {
std::ifstream fin(name);
fin >> total >> source >> goal;
for (int i = 0; i < g.size(); i++) {
for (int j = 0; j < g[i].size(); j++) {
fin >> g[i][j];
}
}
fin.close();
}
Until now, I don't know what to do next. Can anyone help me solve this problem? Thank you!
Last edited on Mar 25, 2021 at 4:40am UTC
Mar 22, 2021 at 4:31pm UTC
How about writing int main()? Then you could compile it! And check it.
Then you can start coding one "search" routine at a time.
Why have you called your class "matrix"? - it isn't. g is the matrix. The class is (the rudiments of) a "graph".
Mar 22, 2021 at 4:39pm UTC
@lastchance
Hello sir, thanks for your reply!
I haven't written the int main() yet because it's for the output, and I don't know how to implement the search algorithm to output the shortest path.
I will change the class into "graph".
Last edited on Mar 22, 2021 at 4:47pm UTC
Mar 23, 2021 at 4:27am UTC
@lastchance
Hello sir, what should I test in the int main() if I'm stuck at writing the search algorithm, the only thing I can do it's reading from the file and assign those numbers to variables.
Mar 23, 2021 at 6:13am UTC
Write an int main();
Instantiate a member of your class.
Get it to read the data.
Write out the matrix to check that it has been read correctly.
Then ... and only then ... start writing your routines to search.
Mar 23, 2021 at 11:30am UTC
@lastchance
Hello sir, I have tested my read file function and it works, the output to the console is the same as the text file.
Last edited on Mar 25, 2021 at 4:02am UTC
Mar 23, 2021 at 11:53am UTC
Good!
And now you can introduce the first function - say
int graph::bfs( int source, int destination )
that will return the shortest path from source to target.
An algorithm that will do it with a queue is explained at
https://cp-algorithms.com/graph/breadth-first-search.html
but there are plenty of other sources. Note that, as your graph is
weighted , you may have to run it till all vertices have been considered - not just take the first time that the destination is visited. Are you allowed to use Dijkstra's variant here?
I presume that you will code the same way as you have been taught the algorithm.
Last edited on Mar 23, 2021 at 12:34pm UTC
Mar 24, 2021 at 3:44pm UTC
I have modified the program, written the function for BFS, and print out the path between 2 vertices
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
std::vector<bool > visited;
std::vector<int > p;
void BFS(std::vector<std::vector<int >>& g, int source) {
int total = g.size();
visited = std::vector<bool >(total, false );
std::queue<int > q;
while (!q.empty()) {
int temp = q.front();
q.pop();
for (int i = 0; i < total; i++) {
if (!visited[i]) {
visited[i] = true ;
q.push(i);
}
}
}
}
void print_path(int source, int goal) {
if (p[goal] == source)
std::cout << source << " " << goal;
else {
print_path(source, p[goal]);
std::cout << " " << goal;
}
}
int main() {
std::vector<std::vector<int >> g;
int total, source, goal;
std::ifstream fin("matrix.txt" );
fin >> total >> source >> goal;
g.resize(total);
for (int i = 0; i < g.size(); i++) {
for (int j = 0; j < g[i].size(); j++) {
fin >> g[i][j];
}
}
fin.close();
BFS(g, source);
if (visited[goal])
print_path(source, goal);
}
The output for the program is wrong, any help with this? Thanks!
Last edited on Mar 25, 2021 at 5:07am UTC
Mar 24, 2021 at 5:04pm UTC
Why have you changed your graph class?
Your code isn't making any use of your adjacency matrix g at all - either to decide what is accessible or to use its weights.
What have you been taught in the way of this algorithm? Does Dijkstra's algorithm come under the category of BFS?
Mar 25, 2021 at 4:06am UTC
Never mind, I figure it out.
Last edited on Mar 25, 2021 at 4:09am UTC