forked from ZigRazor/CXXGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Best First Search.cpp
65 lines (54 loc) · 1.15 KB
/
Best First Search.cpp
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
55
56
57
58
59
60
61
62
63
64
65
Best First Search
//code
#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 actual_Src, int target, int n)
{
vector<bool> visited(n, false);
priority_queue<pi, vector<pi>, greater<pi> > pq;
pq.push(make_pair(0, actual_Src));
int s = actual_Src;
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 = 14;
graph.resize(v);
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;
best_first_search(source, target, v);
return 0;
}