### Assistance with Greedy Best First Search Algorithm

I am trying to implement a best first search which takes in input of points(x,y) from a test.txt file. After its traversal it should output the same points/vertices in order(for my test file example), but its giving me a different output for instance if the "test.txt" file contains: (0,1),(0,2),(1,2),(1,3), but what I am getting is: (0,1),(0,2),(0,2),(1,3). This what I have so far:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283`` ``````#include #include #include #include #include using namespace std; class Graph { int numVertices; list *adjacencyList; public: Graph(int numVertices); void addEdge(int v, int w); void BestFirstSearch(int start); }; Graph::Graph(int numVertices) { this->numVertices = numVertices; adjacencyList = new list[numVertices]; } void Graph::addEdge(int v, int w) { adjacencyList[v].push_back(w); } void Graph::BestFirstSearch(int start) { bool *visited = new bool[numVertices]; for(int i = 0; i < numVertices; i++) { visited[i] = false; } list queue; visited[start] = true; queue.push_back(start); list::iterator i; while(!queue.empty()) { start = queue.front(); cout << start << " " << '\n'; queue.pop_front(); for(i = adjacencyList[start].begin(); i != adjacencyList[start].end(); i++) { if(!visited[*i]) { visited[*i] = true; queue.push_back(*i); } } } } int main() { int x, y, lines; float distance; Graph g(4); //Create 4 vertices. ifstream read("test.txt"); while(read>>x>>y) { g.addEdge(x, y); //makes edges with point provided. g.BestFirstSearch(0); //start at the first node/vertex. } //lines = c1.size(); //distance = sqrt(pow(e, 2) + pow(f, 2)); //cout << distance << "" << endl; return 0; } ``````

I am also suppose to compute the distance between the points, but I am not sure how to add it to the greedy best first search. Any help would be appreciated.
Last edited on