Data update
This commit is contained in:
parent
4d5544505c
commit
4924dd0264
3073 changed files with 55820 additions and 4408 deletions
|
|
@ -0,0 +1,54 @@
|
|||
void main() {
|
||||
List<List<int>> weights = [
|
||||
[1, 3, -2],
|
||||
[2, 1, 4],
|
||||
[2, 3, 3],
|
||||
[3, 4, 2],
|
||||
[4, 2, -1]
|
||||
];
|
||||
int numVertices = 4;
|
||||
floydWarshall(weights, numVertices);
|
||||
}
|
||||
|
||||
void floydWarshall(List<List<int>> weights, int numVertices) {
|
||||
List<List<double>> dist = List.generate(
|
||||
numVertices, (_) => List.filled(numVertices, double.infinity));
|
||||
|
||||
for (var w in weights) {
|
||||
dist[w[0] - 1][w[1] - 1] = w[2].toDouble();
|
||||
}
|
||||
|
||||
List<List<int>> next = List.generate(
|
||||
numVertices, (i) => List.generate(numVertices, (j) => j != i ? j + 1 : 0));
|
||||
|
||||
for (int k = 0; k < numVertices; k++) {
|
||||
for (int i = 0; i < numVertices; i++) {
|
||||
for (int j = 0; j < numVertices; j++) {
|
||||
if (dist[i][k] + dist[k][j] < dist[i][j]) {
|
||||
dist[i][j] = dist[i][k] + dist[k][j];
|
||||
next[i][j] = next[i][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printResult(dist, next);
|
||||
}
|
||||
|
||||
void printResult(List<List<double>> dist, List<List<int>> next) {
|
||||
print("pair dist path");
|
||||
for (int i = 0; i < next.length; i++) {
|
||||
for (int j = 0; j < next.length; j++) {
|
||||
if (i != j) {
|
||||
int u = i + 1;
|
||||
int v = j + 1;
|
||||
String path = "$u -> $v ${dist[i][j].toInt()} $u";
|
||||
do {
|
||||
u = next[u - 1][v - 1];
|
||||
path += " -> $u";
|
||||
} while (u != v);
|
||||
print(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
-module(floyd_warshall).
|
||||
-export([main/2]).
|
||||
|
||||
|
||||
main(N, Edge) ->
|
||||
{Dist, Next} = setup(N, Edge),
|
||||
{Dist2, Next2} = shortest_path(N, Dist, Next),
|
||||
print(N, Dist2, Next2).
|
||||
|
||||
setup(N, Edge) ->
|
||||
Big = 1.0e300,
|
||||
Dist = maps:from_list([{{I,J}, case I == J of true -> 0; false -> Big end}
|
||||
|| I <- lists:seq(1, N), J <- lists:seq(1, N)]),
|
||||
Next = maps:from_list([{{I,J}, nil}
|
||||
|| I <- lists:seq(1, N), J <- lists:seq(1, N)]),
|
||||
lists:foldl(fun({U, V, W}, {Dst, Nxt}) ->
|
||||
{maps:put({U, V}, W, Dst), maps:put({U, V}, V, Nxt)}
|
||||
end, {Dist, Next}, Edge).
|
||||
|
||||
shortest_path(N, Dist, Next) ->
|
||||
Combinations = [{K, I, J} || K <- lists:seq(1, N),
|
||||
I <- lists:seq(1, N),
|
||||
J <- lists:seq(1, N)],
|
||||
lists:foldl(fun({K, I, J}, {Dst, Nxt}) ->
|
||||
IJ_Dist = maps:get({I, J}, Dst),
|
||||
IK_Dist = maps:get({I, K}, Dst),
|
||||
KJ_Dist = maps:get({K, J}, Dst),
|
||||
case IJ_Dist > IK_Dist + KJ_Dist of
|
||||
true ->
|
||||
NewDist = maps:put({I, J}, IK_Dist + KJ_Dist, Dst),
|
||||
NewNext = maps:put({I, J}, maps:get({I, K}, Nxt), Nxt),
|
||||
{NewDist, NewNext};
|
||||
false ->
|
||||
{Dst, Nxt}
|
||||
end
|
||||
end, {Dist, Next}, Combinations).
|
||||
|
||||
print(N, Dist, Next) ->
|
||||
io:format("pair dist path~n"),
|
||||
[io:format("~w -> ~w ~4w ~s~n", [I, J, maps:get({I, J}, Dist), path(Next, I, J)])
|
||||
|| I <- lists:seq(1, N), J <- lists:seq(1, N), I =/= J].
|
||||
|
||||
path(Next, I, J) ->
|
||||
PathList = path(Next, I, J, [I]),
|
||||
string:join([integer_to_list(X) || X <- PathList], " -> ").
|
||||
|
||||
path(_Next, I, I, List) ->
|
||||
lists:reverse(List);
|
||||
path(Next, I, J, List) ->
|
||||
U = maps:get({I, J}, Next),
|
||||
path(Next, U, J, [U | List]).
|
||||
|
||||
% Usage example:
|
||||
main(_) -> main(4, [{1, 3, -2}, {2, 1, 4}, {2, 3, 3}, {3, 4, 2}, {4, 2, -1}]).
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
require "table2"
|
||||
require "math2"
|
||||
|
||||
class floydWarshall
|
||||
static function do_calcs(weights, nvertices)
|
||||
local dist = {}
|
||||
local next = {}
|
||||
for i = 1, nvertices do
|
||||
dist[i] = table.rep(nvertices, 1/0)
|
||||
next[i] = table.rep(nvertices, 1)
|
||||
for j = 1, nvertices do
|
||||
if i != j then next[i][j] = j end
|
||||
end
|
||||
end
|
||||
for weights as w do dist[w[1]][w[2]] = w[3] end
|
||||
for k = 1, nvertices do
|
||||
for i = 1, nvertices do
|
||||
for j = 1, nvertices do
|
||||
if (dist[i][k] + dist[k][j] < dist[i][j]) then
|
||||
dist[i][j] = dist[i][k] + dist[k][j]
|
||||
next[i][j] = next[i][k]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
floydWarshall.print_result(dist, next)
|
||||
end
|
||||
|
||||
static function print_result(dist, next)
|
||||
print("pair dist path")
|
||||
for i = 1, #next do
|
||||
for j = 1, #next do
|
||||
if i != j then
|
||||
local u = i
|
||||
local v = j
|
||||
local path = string.format("%d -> %d %2d %s", u, v, math.trunc(dist[i][j]), u)
|
||||
while true do
|
||||
u = next[u][v]
|
||||
path ..= " -> " .. u
|
||||
if u == v then break end
|
||||
end
|
||||
print(path)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local weights = { {1, 3, -2}, {2, 1, 4}, {2, 3, 3}, {3, 4, 2}, {4, 2, -1} }
|
||||
local nvertices = 4
|
||||
floydWarshall.do_calcs(weights, nvertices)
|
||||
67
Task/Floyd-Warshall-algorithm/R/floyd-warshall-algorithm.r
Normal file
67
Task/Floyd-Warshall-algorithm/R/floyd-warshall-algorithm.r
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
floydWarshall <- function(weights, numVertices) {
|
||||
# Initialize distance matrix with infinity
|
||||
dist <- matrix(Inf, nrow = numVertices, ncol = numVertices)
|
||||
|
||||
# Set diagonal elements to zero
|
||||
diag(dist) <- 0
|
||||
|
||||
# Fill the distance matrix with the given weights
|
||||
for (i in 1:nrow(weights)) {
|
||||
dist[weights[i, 1], weights[i, 2]] <- weights[i, 3]
|
||||
}
|
||||
|
||||
# Initialize nextNode matrix
|
||||
nextNode <- matrix(0, nrow = numVertices, ncol = numVertices)
|
||||
for (i in 1:numVertices) {
|
||||
for (j in 1:numVertices) {
|
||||
if (i != j) {
|
||||
nextNode[i, j] <- j
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Floyd-Warshall algorithm
|
||||
for (k in 1:numVertices) {
|
||||
for (i in 1:numVertices) {
|
||||
for (j in 1:numVertices) {
|
||||
if (dist[i, k] + dist[k, j] < dist[i, j]) {
|
||||
dist[i, j] <- dist[i, k] + dist[k, j]
|
||||
nextNode[i, j] <- nextNode[i, k]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printResult(dist, nextNode)
|
||||
}
|
||||
|
||||
printResult <- function(dist, nextNode) {
|
||||
cat("pair dist path\n")
|
||||
for (i in 1:nrow(nextNode)) {
|
||||
for (j in 1:ncol(nextNode)) {
|
||||
if (i != j) {
|
||||
u <- i
|
||||
v <- j
|
||||
path <- sprintf("%d -> %d %2d %s", i, j, dist[i, j], i)
|
||||
repeat {
|
||||
u <- nextNode[u, v]
|
||||
path <- paste(path, "->", u)
|
||||
if (u == v) break
|
||||
}
|
||||
cat(path, "\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Example usage
|
||||
weights <- matrix(c(
|
||||
1, 3, -2,
|
||||
2, 1, 4,
|
||||
2, 3, 3,
|
||||
3, 4, 2,
|
||||
4, 2, -1
|
||||
), ncol = 3, byrow = TRUE)
|
||||
|
||||
numVertices <- 4
|
||||
floydWarshall(weights, numVertices)
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
func floydWarshall(_ weights: [[Int]], _ numVertices: Int) {
|
||||
var dist = Array(repeating: Array(repeating: Double.infinity, count: numVertices), count: numVertices)
|
||||
var next = Array(repeating: Array(repeating: 0, count: numVertices), count: numVertices)
|
||||
|
||||
for weight in weights {
|
||||
dist[weight[0] - 1][weight[1] - 1] = Double(weight[2])
|
||||
}
|
||||
|
||||
for i in 0..<numVertices {
|
||||
for j in 0..<numVertices {
|
||||
if i != j {
|
||||
next[i][j] = j + 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for k in 0..<numVertices {
|
||||
for i in 0..<numVertices {
|
||||
for j in 0..<numVertices {
|
||||
if dist[i][k] + dist[k][j] < dist[i][j] {
|
||||
dist[i][j] = dist[i][k] + dist[k][j]
|
||||
next[i][j] = next[i][k]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printResult(dist, next)
|
||||
}
|
||||
|
||||
func printResult(_ dist: [[Double]], _ next: [[Int]]) {
|
||||
print("pair dist path")
|
||||
for i in 0..<next.count {
|
||||
for j in 0..<next.count {
|
||||
if i != j {
|
||||
var u = i + 1
|
||||
let v = j + 1
|
||||
var path = "\(u) -> \(v) \(Int(dist[i][j])) \(u)"
|
||||
repeat {
|
||||
u = next[u - 1][v - 1]
|
||||
path += " -> \(u)"
|
||||
} while u != v
|
||||
print(path)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let weights = [[1, 3, -2], [2, 1, 4], [2, 3, 3], [3, 4, 2], [4, 2, -1]]
|
||||
let numVertices = 4
|
||||
floydWarshall(weights, numVertices)
|
||||
Loading…
Add table
Add a link
Reference in a new issue