Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"configurations": [
{
"name": "linux-gcc-x64",
"includePath": [
"${workspaceFolder}/**"
],
"compilerPath": "/usr/bin/gcc",
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "linux-gcc-x64",
"compilerArgs": [
""
]
}
],
"version": 4
}
24 changes: 24 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++ Runner: Debug Session",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"externalConsole": false,
"cwd": "/home/wu/DIT_code_folder/Navigation_folder/Navigation_Tutorial",
"program": "/home/wu/DIT_code_folder/Navigation_folder/Navigation_Tutorial/build/Debug/outDebug",
"MIMode": "gdb",
"miDebuggerPath": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
59 changes: 59 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"C_Cpp_Runner.cCompilerPath": "gcc",
"C_Cpp_Runner.cppCompilerPath": "g++",
"C_Cpp_Runner.debuggerPath": "gdb",
"C_Cpp_Runner.cStandard": "",
"C_Cpp_Runner.cppStandard": "",
"C_Cpp_Runner.msvcBatchPath": "",
"C_Cpp_Runner.useMsvc": false,
"C_Cpp_Runner.warnings": [
"-Wall",
"-Wextra",
"-Wpedantic",
"-Wshadow",
"-Wformat=2",
"-Wcast-align",
"-Wconversion",
"-Wsign-conversion",
"-Wnull-dereference"
],
"C_Cpp_Runner.msvcWarnings": [
"/W4",
"/permissive-",
"/w14242",
"/w14287",
"/w14296",
"/w14311",
"/w14826",
"/w44062",
"/w44242",
"/w14905",
"/w14906",
"/w14263",
"/w44265",
"/w14928"
],
"C_Cpp_Runner.enableWarnings": true,
"C_Cpp_Runner.warningsAsError": false,
"C_Cpp_Runner.compilerArgs": [],
"C_Cpp_Runner.linkerArgs": [],
"C_Cpp_Runner.includePaths": [],
"C_Cpp_Runner.includeSearch": [
"*",
"**/*"
],
"C_Cpp_Runner.excludeSearch": [
"**/build",
"**/build/**",
"**/.*",
"**/.*/**",
"**/.vscode",
"**/.vscode/**"
],
"C_Cpp_Runner.useAddressSanitizer": false,
"C_Cpp_Runner.useUndefinedSanitizer": false,
"C_Cpp_Runner.useLeakSanitizer": false,
"C_Cpp_Runner.showCompilationTime": false,
"C_Cpp_Runner.useLinkTimeOptimization": false,
"C_Cpp_Runner.msvcSecureNoWarnings": false
}
56 changes: 55 additions & 1 deletion AStar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <limits>
#include <utility> // for pair
#include <stack> // for reconstructing the path
#include <climits>

using namespace std;

Expand Down Expand Up @@ -58,13 +59,66 @@ class Graph {
// TODO: Implement A* algorithm
void aStarSearch(int src, int dest) {
// Your implementation goes here

// 1. Create a priority queue for vertices being processed
// 2. Create arrays for g_score (actual distance from start)
// and f_score (g_score + heuristic)
// 3. Initialize all g_scores as INFINITE and src g_score as 0
// 4. Process vertices in order of their f_score (not just distance)
// 5. Reconstruct and print the shortest path from src to dest

using pii = pair<int, int>; // typedef pair<int, int> pii; 把最小的城市放在前面 <fScore, city>
priority_queue<pii, vector<pii>, greater<pii>> Set;

vector<int> gScore(V, INT_MAX);
vector<int> fScore(V, INT_MAX);
vector<int> From(V, -1); // notes every vertices where are you from

gScore[src] = 0;
fScore[src] = heuristic[src];

Set.push({fScore[src], src});

while(!Set.empty()){
int current = Set.top().second;
Set.pop();

if (current == dest){ // find destination, backtrack the path
stack<int> path;
int temp = dest;
while (temp != -1){
path.push(temp);
temp = From[temp];
}

cout << "Shortest travel time " << gScore[dest] << " hours." << endl;
cout << "Path: ";
while (!path.empty()){
cout << path.top();
path.pop();
if (!path.empty()){
cout << " -> ";
}
}
cout << endl;
return;
}

for (auto neighborPair : adj[current]){
int neighbor = neighborPair.first;
int weight = neighborPair.second;

int temp_gScore = gScore[current] + weight;

if (temp_gScore < gScore[neighbor]){
From[neighbor] = current;
gScore[neighbor] = temp_gScore;
fScore[neighbor] = temp_gScore + heuristic[neighbor];
Set.push({fScore[neighbor], neighbor});
}
}
}

cout << "No path exists from " << src << " to " << dest << "." << endl;
}
};

Expand Down
67 changes: 58 additions & 9 deletions Dijkstra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
#include <iostream>
#include <vector>
#include <queue>
#include <limits>
#include <climits>
#include <utility> // for pair
#include <stack> // for reconstructing the path
#include <algorithm>

using namespace std;

Expand All @@ -48,20 +49,68 @@ class Graph {

// TODO: Implement Dijkstra's algorithm
void shortestPath(int src, int dest) {
// Your implementation goes here

// 1. Create a priority queue for vertices being processed
// 2. Create arrays for distances and for tracking the path
// 3. Initialize all distances as INFINITE and src distance as 0
// 4. Process vertices in order of their distance from src
// 5. Reconstruct and print the shortest path from src to dest

vector<int> dist(V, INT_MAX);
vector<int> prev(V, -1);
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<>> pq;

dist[src] = 0;
pq.push({0, src}); // {distance, vertices}

while (!pq.empty()){
int u = pq.top().second;
pq.pop();

for (auto [v,w] : adj[u]){
if (dist[u] + w < dist[v]){
dist[v] = dist[u] + w;
prev[v] = u; //紀錄點
pq.push({dist[v], v});
}
}
}

if (dist[dest] == INT_MAX) {
cout << "No path.\n";
return;
}

// 印出最短時間
cout << "Shortest travel time: " << dist[dest] << " hours\n";

// 回溯路徑
vector<int> path;
for (int at = dest; at != -1; at = prev[at])
path.push_back(at);
reverse(path.begin(), path.end());

cout << "Path: ";
for (size_t i = 0; i < path.size(); ++i) {
cout << path[i];
if (i != path.size() - 1) cout << " -> ";
}
cout << endl;
}
};
/*
void define_the_map(Graph& g){

cout<<"Enter the edges: (u, v, w)";

}
*/
int main() {
// Create graph with 6 cities (labeled 0 to 5)
Graph g(6);
/*
int v;
cout<<"Please Enter the number of city.";
cin>>v;
Graph g(v);

define_the_map(g); */

Graph g(6);

// Add roads with travel times (directed edges with weights)
g.addEdge(0, 1, 2); // From city 0 to city 1, travel time: 2 hours
g.addEdge(0, 2, 4); // From city 0 to city 2, travel time: 4 hours
Expand Down