Data update
This commit is contained in:
parent
72eb4943cb
commit
4d5544505c
2347 changed files with 62432 additions and 16731 deletions
43
Task/Dijkstras-algorithm/Crystal/dijkstras-algorithm.cr
Normal file
43
Task/Dijkstras-algorithm/Crystal/dijkstras-algorithm.cr
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
def shortest_paths (edges edgelist, start)
|
||||
# put the edges in a hash for easier access
|
||||
edges = Hash(Symbol, Array({Symbol, Int32})).new {|h, k| h[k] = [] of {Symbol, Int32} }
|
||||
edgelist.each do |from, to, cost|
|
||||
edges[from] << { to, cost }
|
||||
end
|
||||
vertices = { start => { cost: 0, prev: :"" } }
|
||||
queue = Deque.new [start]
|
||||
while queue.present?
|
||||
node = queue.shift
|
||||
current_cost = vertices[node][:cost]
|
||||
if dests = edges[node]?
|
||||
dests.each do |dest, cost|
|
||||
if (v = vertices[dest]?).nil? || v[:cost] > current_cost + cost
|
||||
vertices[dest] = { cost: current_cost + cost, prev: node }
|
||||
queue.push dest
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
vertices.map {|vertex, t|
|
||||
# reconstruct path
|
||||
path = [vertex]
|
||||
prev = t[:prev]
|
||||
while prev != :""
|
||||
path << prev
|
||||
prev = vertices[prev][:prev]
|
||||
end
|
||||
{ path.reverse, t[:cost] }
|
||||
}
|
||||
end
|
||||
|
||||
edges = [{:a, :b, 7}, {:a, :c, 9}, {:a, :f, 14}, {:b, :c, 10}, {:b, :d, 15},
|
||||
{:c, :d, 11}, {:c, :f, 2}, {:d, :e, 6}, {:e, :f, 9}]
|
||||
|
||||
# show the shortest path to each reachable vertex
|
||||
puts "Shortest paths from :a to each reachable vertex:"
|
||||
pp shortest_paths(edges, :a)
|
||||
|
||||
# interpret the output from above and use it to output
|
||||
# the shortest path from a to e and f
|
||||
puts "\nShortest paths from :a to :e and :f:"
|
||||
pp shortest_paths(edges, :a).select {|path, _| path[0] == :a && path[-1].in? [:e, :f] }
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
global con[][] n .
|
||||
proc read . .
|
||||
proc read .
|
||||
repeat
|
||||
s$ = input
|
||||
until s$ = ""
|
||||
|
|
@ -20,10 +20,8 @@ read
|
|||
len cost[] n
|
||||
len prev[] n
|
||||
#
|
||||
proc dijkstra . .
|
||||
for i = 2 to len cost[]
|
||||
cost[i] = 1 / 0
|
||||
.
|
||||
proc dijkstra .
|
||||
for i = 2 to len cost[] : cost[i] = 1 / 0
|
||||
len todo[] n
|
||||
todo[1] = 1
|
||||
repeat
|
||||
|
|
|
|||
|
|
@ -1,16 +1,34 @@
|
|||
proc makeUndirectedGraph arcs {
|
||||
proc makeDirectedGraph arcs {
|
||||
# Assume that all nodes are connected to something
|
||||
foreach arc $arcs {
|
||||
lassign $arc v1 v2 cost
|
||||
dict set graph $v1 $v2 $cost
|
||||
dict set graph $v2 $v1 $cost
|
||||
# dict set graph $v2 $v1 $cost ; # make undirected by adding reverse weight
|
||||
}
|
||||
return $graph
|
||||
}
|
||||
|
||||
# a----7---b
|
||||
# / \ / \
|
||||
# / 9 10 15
|
||||
# / \ / \
|
||||
# 14 c---11---d---6---e
|
||||
# / / /
|
||||
# / 2 9
|
||||
# f-------/----------------/
|
||||
#
|
||||
#
|
||||
|
||||
# directed edges
|
||||
set arcs {
|
||||
{a b 7} {a c 9} {b c 10} {b d 15} {c d 11}
|
||||
{d e 6} {a f 14} {c f 2} {e f 9}
|
||||
{a b 7} {a c 9} {a f 14}
|
||||
{b c 10} {b d 15}
|
||||
{c d 11} {c f 2}
|
||||
{d e 6}
|
||||
{e f 9}
|
||||
}
|
||||
lassign [dijkstra [makeUndirectedGraph $arcs] "a"] costs path
|
||||
|
||||
# processing starts here
|
||||
lassign [dijkstra [makeDirectedGraph $arcs] "a"] costs path
|
||||
puts "path from a to e costs [dict get $costs e]"
|
||||
puts "route from a to e is: [join [dict get $path e] { -> }]"
|
||||
|
|
|
|||
158
Task/Dijkstras-algorithm/Zig/dijkstras-algorithm.zig
Normal file
158
Task/Dijkstras-algorithm/Zig/dijkstras-algorithm.zig
Normal file
|
|
@ -0,0 +1,158 @@
|
|||
const std = @import("std");
|
||||
|
||||
// ───────── data types ─────────
|
||||
const Edge = struct {
|
||||
vertex: usize,
|
||||
weight: i32,
|
||||
};
|
||||
|
||||
const Vertex = struct {
|
||||
edges : std.ArrayList(Edge),
|
||||
dist : i32 = std.math.maxInt(i32),
|
||||
prev : usize = 0,
|
||||
visited : bool = false,
|
||||
|
||||
fn init(alloc: std.mem.Allocator) Vertex {
|
||||
return .{ .edges = std.ArrayList(Edge).init(alloc) };
|
||||
}
|
||||
};
|
||||
|
||||
const Graph = struct {
|
||||
verts : std.ArrayList(?*Vertex),
|
||||
|
||||
fn init(alloc: std.mem.Allocator) Graph {
|
||||
return .{ .verts = std.ArrayList(?*Vertex).init(alloc) };
|
||||
}
|
||||
|
||||
fn ensureVertex(self: *Graph, alloc: std.mem.Allocator, idx: usize) !void {
|
||||
if (idx >= self.verts.items.len) {
|
||||
const old_len = self.verts.items.len;
|
||||
try self.verts.resize(idx + 1); // grow list
|
||||
// set all freshly‑appended slots to null
|
||||
for (self.verts.items[old_len..]) |*p| p.* = null;
|
||||
}
|
||||
|
||||
if (self.verts.items[idx] == null) {
|
||||
const vp = try alloc.create(Vertex);
|
||||
vp.* = Vertex.init(alloc);
|
||||
self.verts.items[idx] = vp;
|
||||
}
|
||||
}
|
||||
|
||||
fn addEdge(self: *Graph, alloc: std.mem.Allocator,
|
||||
a: u8, b: u8, w: i32) !void {
|
||||
const ai = a - 'a';
|
||||
const bi = b - 'a';
|
||||
|
||||
try self.ensureVertex(alloc, ai);
|
||||
try self.ensureVertex(alloc, bi);
|
||||
|
||||
try self.verts.items[ai].?.edges.append(.{ .vertex = bi, .weight = w });
|
||||
}
|
||||
|
||||
fn deinit(self: *Graph, alloc: std.mem.Allocator) void {
|
||||
for (self.verts.items) |maybe_v| {
|
||||
if (maybe_v) |v| {
|
||||
v.edges.deinit();
|
||||
alloc.destroy(v);
|
||||
}
|
||||
}
|
||||
self.verts.deinit();
|
||||
}
|
||||
};
|
||||
|
||||
// ───────── Dijkstra ─────────
|
||||
fn compare(g: *Graph, a: usize, b: usize) std.math.Order {
|
||||
const da = g.verts.items[a].?.dist;
|
||||
const db = g.verts.items[b].?.dist;
|
||||
return if (da < db) .lt else if (da > db) .gt else .eq;
|
||||
}
|
||||
|
||||
fn dijkstra(g: *Graph, alloc: std.mem.Allocator, src: u8, dst: u8) !void {
|
||||
const src_i = src - 'a';
|
||||
const dst_i = dst - 'a';
|
||||
|
||||
// reset
|
||||
for (g.verts.items) |maybe_v| {
|
||||
if (maybe_v) |v| {
|
||||
v.dist = std.math.maxInt(i32);
|
||||
v.prev = 0;
|
||||
v.visited = false;
|
||||
}
|
||||
}
|
||||
|
||||
var pq = std.PriorityQueue(usize, *Graph, compare).init(alloc, g);
|
||||
defer pq.deinit();
|
||||
|
||||
g.verts.items[src_i].?.dist = 0;
|
||||
try pq.add(src_i);
|
||||
|
||||
while (pq.removeOrNull()) |u_i| {
|
||||
if (u_i == dst_i) break;
|
||||
|
||||
var u = g.verts.items[u_i].?;
|
||||
if (u.visited) continue;
|
||||
u.visited = true;
|
||||
|
||||
for (u.edges.items) |e| {
|
||||
var v = g.verts.items[e.vertex].?;
|
||||
if (v.visited) continue;
|
||||
|
||||
const alt = u.dist + e.weight;
|
||||
if (alt < v.dist) {
|
||||
v.dist = alt;
|
||||
v.prev = u_i;
|
||||
try pq.add(e.vertex); // duplicates ok
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ───────── output ─────────
|
||||
fn printPath(g: *Graph, dst: u8) void {
|
||||
const dst_i = dst - 'a';
|
||||
const v = g.verts.items[dst_i].?;
|
||||
|
||||
if (v.dist == std.math.maxInt(i32)) {
|
||||
std.debug.print("no path\n", .{});
|
||||
return;
|
||||
}
|
||||
|
||||
var buf: [26]u8 = undefined;
|
||||
var n: usize = 0;
|
||||
var cur: usize = dst_i;
|
||||
while (true) {
|
||||
buf[n] = @as(u8, @intCast(cur)) + 'a';
|
||||
n += 1;
|
||||
if (g.verts.items[cur].?.dist == 0) break;
|
||||
cur = g.verts.items[cur].?.prev;
|
||||
}
|
||||
|
||||
std.debug.print("{} ", .{v.dist});
|
||||
var i: isize = @as(isize, @intCast(n)) - 1;
|
||||
while (i >= 0) : (i -= 1) {
|
||||
std.debug.print("{c}", .{buf[@as(usize, @intCast(i))]});
|
||||
}
|
||||
std.debug.print("\n", .{});
|
||||
}
|
||||
|
||||
// ───────── main ─────────
|
||||
pub fn main() !void {
|
||||
const alloc = std.heap.page_allocator;
|
||||
|
||||
var g = Graph.init(alloc);
|
||||
defer g.deinit(alloc);
|
||||
|
||||
try g.addEdge(alloc, 'a','b', 7);
|
||||
try g.addEdge(alloc, 'a','c', 9);
|
||||
try g.addEdge(alloc, 'a','f',14);
|
||||
try g.addEdge(alloc, 'b','c',10);
|
||||
try g.addEdge(alloc, 'b','d',15);
|
||||
try g.addEdge(alloc, 'c','d',11);
|
||||
try g.addEdge(alloc, 'c','f', 2);
|
||||
try g.addEdge(alloc, 'd','e', 6);
|
||||
try g.addEdge(alloc, 'e','f', 9);
|
||||
|
||||
try dijkstra(&g, alloc, 'a', 'e');
|
||||
printPath(&g, 'e');
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue