Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vincenty.rs and Graph Search Algorithms #282

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
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
96 changes: 96 additions & 0 deletions A* Search Algorithm
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
interface Node {
id: number;
g: number; // Cost from start to this node
h: number; // Heuristic cost from this node to target
f: number; // Total cost
parent?: Node; // Parent node for path reconstruction
}

class GraphAStar {
private adjList: Map<number, number[]>;

constructor() {
this.adjList = new Map();
}

addVertex(vertex: number) {
this.adjList.set(vertex, []);
}

addEdge(v1: number, v2: number) {
this.adjList.get(v1)?.push(v2);
this.adjList.get(v2)?.push(v1); // for undirected graph
}

heuristic(node: number, target: number): number {
// This is a placeholder heuristic; replace with an actual heuristic function
return Math.abs(target - node);
}

astar(start: number, target: number): number[] | null {
const openSet: Node[] = [];
const closedSet: Set<number> = new Set();

const startNode: Node = { id: start, g: 0, h: this.heuristic(start, target), f: 0 };
openSet.push(startNode);

while (openSet.length) {
// Get the node with the lowest f score
openSet.sort((a, b) => a.f - b.f);
const currentNode = openSet.shift()!;

if (currentNode.id === target) {
const path: number[] = [];
let temp: Node | undefined = currentNode;

while (temp) {
path.push(temp.id);
temp = temp.parent;
}

return path.reverse(); // Return reversed path
}

closedSet.add(currentNode.id);

const neighbors = this.adjList.get(currentNode.id) || [];
for (const neighborId of neighbors) {
if (closedSet.has(neighborId)) continue;

const gScore = currentNode.g + 1; // Assuming cost is 1 for each edge
const hScore = this.heuristic(neighborId, target);
const fScore = gScore + hScore;

const existingNodeIndex = openSet.findIndex(node => node.id === neighborId);
if (existingNodeIndex === -1 || fScore < openSet[existingNodeIndex].f) {
const neighborNode: Node = {
id: neighborId,
g: gScore,
h: hScore,
f: fScore,
parent: currentNode
};

if (existingNodeIndex === -1) {
openSet.push(neighborNode);
} else {
openSet[existingNodeIndex] = neighborNode;
}
}
}
}

return null; // No path found
}
}

// Example usage
const graphAStar = new GraphAStar();
graphAStar.addVertex(1);
graphAStar.addVertex(2);
graphAStar.addVertex(3);
graphAStar.addVertex(4);
graphAStar.addEdge(1, 2);
graphAStar.addEdge(2, 3);
graphAStar.addEdge(3, 4);
console.log(graphAStar.astar(1, 4)); // Output: [1, 2, 3, 4]
48 changes: 48 additions & 0 deletions bfs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class GraphBFS {
private adjList: Map<number, number[]>;

constructor() {
this.adjList = new Map();
}

addVertex(vertex: number) {
this.adjList.set(vertex, []);
}

addEdge(v1: number, v2: number) {
this.adjList.get(v1)?.push(v2);
this.adjList.get(v2)?.push(v1); // for undirected graph
}

bfs(start: number): number[] {
const visited = new Set<number>();
const queue: number[] = [start];
const result: number[] = [];

visited.add(start);

while (queue.length) {
const vertex = queue.shift()!;
result.push(vertex);

const neighbors = this.adjList.get(vertex) || [];
for (const neighbor of neighbors) {
if (!visited.has(neighbor)) {
visited.add(neighbor);
queue.push(neighbor);
}
}
}

return result;
}
}

// Example usage
const graphBFS = new GraphBFS();
graphBFS.addVertex(1);
graphBFS.addVertex(2);
graphBFS.addVertex(3);
graphBFS.addEdge(1, 2);
graphBFS.addEdge(1, 3);
console.log(graphBFS.bfs(1)); // Output: [1, 2, 3] (or some other order depending on graph structure)
45 changes: 45 additions & 0 deletions dfs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class Graph {
private adjList: Map<number, number[]>;

constructor() {
this.adjList = new Map();
}

addVertex(vertex: number) {
this.adjList.set(vertex, []);
}

addEdge(v1: number, v2: number) {
this.adjList.get(v1)?.push(v2);
this.adjList.get(v2)?.push(v1); // for undirected graph
}

dfs(start: number): number[] {
const visited = new Set<number>();
const result: number[] = [];

const dfsHelper = (vertex: number) => {
visited.add(vertex);
result.push(vertex);
const neighbors = this.adjList.get(vertex) || [];

for (const neighbor of neighbors) {
if (!visited.has(neighbor)) {
dfsHelper(neighbor);
}
}
};

dfsHelper(start);
return result;
}
}

// Example usage
const graph = new Graph();
graph.addVertex(1);
graph.addVertex(2);
graph.addVertex(3);
graph.addEdge(1, 2);
graph.addEdge(1, 3);
console.log(graph.dfs(1)); // Output: [1, 2, 3] (or some other order depending on graph structure)
73 changes: 73 additions & 0 deletions vincenty.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// vincenty.rs
const WGS84_A: f64 = 6378137.0; // Semi-major axis in meters
const WGS84_B: f64 = 6356752.314245; // Semi-minor axis in meters
const WGS84_F: f64 = 1.0 / 298.257223563; // Flattening

pub fn vincenty(lat1: f64, lon1: f64, lat2: f64, lon2: f64) -> f64 {
let u1 = (1.0 - WGS84_F) * lat1.to_radians().tan();
let u2 = (1.0 - WGS84_F) * lat2.to_radians().tan();
let l = (lon2 - lon1).to_radians();

let sin_u1 = u1.sin();
let cos_u1 = u1.cos();
let sin_u2 = u2.sin();
let cos_u2 = u2.cos();

let lambda = l;
let (sin_lambda, cos_lambda) = (lambda.sin(), lambda.cos());

let mut sin_sigma;
let mut cos_sigma;
let mut cos_sq_alpha;
let mut cos2_sigma_m;
let mut sigma = 0.0;

let mut iteration_limit = 100;
let mut lambda_prev = 0.0;

while (lambda - lambda_prev).abs() > 1e-12 && iteration_limit > 0 {
let sin_lambda = lambda.sin();
let cos_lambda = lambda.cos();

sin_sigma = ((cos_u2 * sin_lambda).powi(2) +
(cos_u1 * sin_u2 - sin_u1 * cos_u2 * cos_lambda).powi(2)).sqrt();

if sin_sigma == 0.0 {
return 0.0; // coincident points
}

cos_sigma = sin_u1 * sin_u2 + cos_u1 * cos_u2 * cos_lambda;
sigma = sin_sigma.atan2(cos_sigma);

let sin_alpha = cos_u1 * cos_u2 * sin_lambda;
cos_sq_alpha = 1.0 - sin_alpha.powi(2);

cos2_sigma_m = if cos_sq_alpha == 0.0 {
0.0
} else {
cos_u2 * cos_u1 * cos_lambda / cos_sq_alpha
};

let c = WGS84_F / 16.0 * cos_sq_alpha * (4.0 + WGS84_F * (4.0 - 3.0 * cos_sq_alpha));

lambda_prev = lambda;
lambda = l + (1.0 - c) * WGS84_F * sin_alpha * (sigma + c * sin_sigma * (cos2_sigma_m + c * cos_sigma * (-1.0 + 2.0 * cos2_sigma_m.powi(2))));

iteration_limit -= 1;
}

if iteration_limit == 0 {
return f64::NAN; // formula failed to converge
}

let u_sq = cos_sq_alpha * (WGS84_A.powi(2) - WGS84_B.powi(2)) / WGS84_B.powi(2);
let a = 1.0 + u_sq / 16384.0 * (4096.0 + u_sq * (-768.0 + u_sq * (320.0 - 175.0 * u_sq)));
let b = u_sq / 1024.0 * (256.0 + u_sq * (-128.0 + u_sq * (74.0 - 47.0 * u_sq)));

let delta_sigma = b * sin_sigma * (cos2_sigma_m + b / 4.0 * (cos_sigma * (-1.0 + 2.0 * cos2_sigma_m.powi(2)) -
b / 6.0 * cos2_sigma_m * (-3.0 + 4.0 * sin_sigma.powi(2)) * (-3.0 + 4.0 * cos2_sigma_m.powi(2))));

let s = WGS84_B * a * (sigma - delta_sigma);

s // distance in meters
}