Skip to content

Commit

Permalink
Create FirstSearch.cpp
Browse files Browse the repository at this point in the history
Best first search implemented in c++.
  • Loading branch information
sultanarif-p authored Oct 16, 2021
1 parent 7962a23 commit 0356be2
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions C++/FirstSearch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// C++ program to implement Best First Search using priority
// queue
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pi;

vector<vector<pi> > graph;

// Function for adding edges to graph
void addedge(int x, int y, int cost)
{
graph[x].push_back(make_pair(cost, y));
graph[y].push_back(make_pair(cost, x));
}

// Function For Implementing Best First Search
// Gives output path having lowest cost
void best_first_search(int source, int target, int n)
{
vector<bool> visited(n, false);
// MIN HEAP priority queue
priority_queue<pi, vector<pi>, greater<pi> > pq;
// sorting in pq gets done by first value of pair
pq.push(make_pair(0, source));
int s = source;
visited[s] = true;
while (!pq.empty()) {
int x = pq.top().second;
// Displaying the path having lowest cost
cout << x << " ";
pq.pop();
if (x == target)
break;

for (int i = 0; i < graph[x].size(); i++) {
if (!visited[graph[x][i].second]) {
visited[graph[x][i].second] = true;
pq.push(make_pair(graph[x][i].first,graph[x][i].second));
}
}
}
}

// Driver code to test above methods
int main()
{
// No. of Nodes
int v = 14;
graph.resize(v);

// The nodes shown in above example(by alphabets) are
// implemented using integers addedge(x,y,cost);
addedge(0, 1, 3);
addedge(0, 2, 6);
addedge(0, 3, 5);
addedge(1, 4, 9);
addedge(1, 5, 8);
addedge(2, 6, 12);
addedge(2, 7, 14);
addedge(3, 8, 7);
addedge(8, 9, 5);
addedge(8, 10, 6);
addedge(9, 11, 1);
addedge(9, 12, 10);
addedge(9, 13, 2);

int source = 0;
int target = 9;

// Function call
best_first_search(source, target, v);

return 0;
}

0 comments on commit 0356be2

Please sign in to comment.