-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortestPath.cpp
147 lines (119 loc) · 3.92 KB
/
shortestPath.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include <vector>
#include <queue>
#include <limits>
#include <cmath>
using namespace std;
#define INF numeric_limits<int>::max()
static double sizeofshortest=0;
// Define a structure for representing edges
struct Edge {
int to;
int weight;
};
// Function to perform Dijkstra's algorithm
void dijkstra(vector<vector<Edge>>& graph, int source, vector<int>& dist, vector<int>& prev) {
int V = graph.size();
dist.assign(V, INF);
prev.assign(V, -1);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
dist[source] = 0;
pq.push({0, source});
while (!pq.empty()) {
int u = pq.top().second;
int d = pq.top().first;
pq.pop();
if (d > dist[u])
continue;
for (const Edge& e : graph[u]) {
int v = e.to;
int w = e.weight;
if (dist[u] + w < dist[v]) {
dist[v] = dist[u] + w;
prev[v] = u;
pq.push({dist[v], v});
}
}
}
}
// Function to print the shortest path from source to target
void printShortestPath(vector<int>& prev, int target) {
vector<int> path;
for (int at = target; at != -1; at = prev[at])
path.push_back(at);
cout << "Shortest Path: ";
for (int i = path.size() - 1; i >= 0; --i) {
cout << path[i];
if (i != 0)
cout << " -> ";
}
sizeofshortest=path.size();
cout << endl;
}
// Function to compare paths
bool comparePaths(const vector<int>& shortestPath, const vector<int>& samplePath) {
if (shortestPath.size() != samplePath.size())
return false;
for (size_t i = 0; i < shortestPath.size(); ++i) {
if (shortestPath[i] != samplePath[i])
return false;
}
return true;
}
// Function to calculate the score based on path similarity
double calculateScore(const vector<int>& shortestPath, const vector<int>& userPath) {
// Calculate score based on the formula provided
// double shortestPathLength = shortestPath.size();
double totalPathLength = userPath.size();
double score = (sizeofshortest / totalPathLength) * 100;
return score;
}
int main() {
// Example graph representation (Adjacency List)
int V = 10; // Number of tiles
vector<vector<Edge>> graph(V);
graph[0].push_back({1, 1});
graph[0].push_back({2, 3});
graph[1].push_back({3, 4});
graph[1].push_back({4, 2});
graph[2].push_back({5, 2});
graph[3].push_back({6, 5});
graph[3].push_back({7, 2});
graph[4].push_back({8, 3});
graph[5].push_back({8, 1});
graph[5].push_back({9, 4});
graph[6].push_back({9, 2});
graph[7].push_back({9, 3});
graph[8].push_back({9, 1});
// Source and target nodes
int start = 0;
int end = 9;
// Vectors to store distances and previous nodes
vector<int> dist, prev;
// Apply Dijkstra's algorithm
dijkstra(graph, start, dist, prev);
// Print shortest path to the target node
printShortestPath(prev, end);
// Sample paths taken by different users
vector<vector<int>> samplePaths = {
{0, 1, 3, 7, 8, 9}, // Shortest path
{0, 1, 3, 7, 8, 9, 9, 9, 9, 9}, // Extended by repeating the last node
{0, 1, 3, 7, 8, 9, 4, 5, 6, 7, 8, 9}, // Extended by adding more nodes
{0, 1, 3, 7, 8, 9, 2, 5, 8, 9} // Extended by adding alternative nodes
// You can create variations of extended paths similarly
};
// Calculate score for each user path and store in a max heap
priority_queue<double> leaderboard;
for (int i = 0; i < samplePaths.size(); ++i) {
double score = calculateScore(prev, samplePaths[i]);
// Store score in the max heap
leaderboard.push(score);
}
// Display leaderboard
cout << "Leaderboard:" << endl;
while (!leaderboard.empty()) {
cout << leaderboard.top() << endl;
leaderboard.pop();
}
return 0;
}