-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUCS.cpp
More file actions
96 lines (80 loc) · 3.03 KB
/
UCS.cpp
File metadata and controls
96 lines (80 loc) · 3.03 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include<bits/stdc++.h>
using namespace std;
// Function to perform Uniform Cost Search (UCS)
void uniformCostSearch(int start, int goal, const map<int, vector<pair<int, int>>>& graph) {
// Priority queue to select the node with the minimum cost
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
map<int, int> distance; // Store minimum distances from start node
map<int, int> parent; // Track the parent node for path reconstruction
// Initialize distances with a high value (infinity)
for (const auto& node : graph) {
distance[node.first] = INT_MAX;
}
// Start from the initial node
pq.push({0, start}); // {cost, node}
distance[start] = 0;
parent[start] = -1; // Start node has no parent
while (!pq.empty()) {
int currentCost = pq.top().first;
int currentNode = pq.top().second;
pq.pop();
// If we reached the goal node, stop the search
if (currentNode == goal) {
cout << "Goal reached with cost: " << currentCost << endl;
break;
}
// Explore neighbors of the current node
for (const auto& neighbor : graph.at(currentNode)) {
int neighborNode = neighbor.first;
int edgeCost = neighbor.second;
int newCost = currentCost + edgeCost;
// If a cheaper path to the neighbor is found, update and push to the queue
if (newCost < distance[neighborNode]) {
distance[neighborNode] = newCost;
pq.push({newCost, neighborNode});
parent[neighborNode] = currentNode; // Update parent to reconstruct path
}
}
}
// Print the path from start to goal
if (distance[goal] == INT_MAX) {
cout << "No path found from " << start << " to " << goal << endl;
} else {
cout << "Path from " << start << " to " << goal << ": ";
vector<int> path;
for (int node = goal; node != -1; node = parent[node]) {
path.push_back(node);
}
reverse(path.begin(), path.end());
for (int node : path) {
cout << node << " ";
}
cout << endl;
}
}
int main() {
// Graph represented as an adjacency list
// Each node has a vector of pairs {neighbor, weight}
map<int, vector<pair<int, int>>> graph;
int edges;
cout << "Enter number of edges: ";
cin >> edges;
cout << "Enter edges (format: node1 node2 weight):" << endl;
for (int i = 0; i < edges; ++i) {
int u, v, weight;
cin >> u >> v >> weight;
graph[u].push_back({v, weight});
graph[v].push_back({u, weight}); // For undirected graph
}
int start, goal;
cout << "Enter start and goal nodes: ";
cin >> start >> goal;
// Perform UCS
uniformCostSearch(start, goal, graph);
return 0;
}