-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathGreedyBestFirstSearch.cpp
More file actions
46 lines (46 loc) · 1 KB
/
GreedyBestFirstSearch.cpp
File metadata and controls
46 lines (46 loc) · 1 KB
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
#include<stdio.h>
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pi;
vector<vector<pi> > 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));
}
void best_first_search(int source, int target, int n)
{
vector<bool> visited(n, false);
priority_queue<pi, vector<pi>, greater<pi> > pq;
pq.push(make_pair(0, source));
int s = source;
visited[s] = true; while (!pq.empty())
{
int x = pq.top().second;
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));
}
}
}
}
int main()
{
int v = 4; graph.resize(v);
addedge(0, 1, 3);
addedge(0, 2, 6);
addedge(1, 2, 12);
addedge(2, 0, 9);
addedge(2, 3, 8);
addedge(3, 3, 5);
int source = 0;
int target = 3;
best_first_search(source, target, v);
return 0;
}