Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
183
Task/Floyd-Warshall-algorithm/Ada/floyd-warshall-algorithm.adb
Normal file
183
Task/Floyd-Warshall-algorithm/Ada/floyd-warshall-algorithm.adb
Normal file
|
|
@ -0,0 +1,183 @@
|
|||
--
|
||||
-- Floyd-Warshall algorithm.
|
||||
--
|
||||
-- See https://en.wikipedia.org/w/index.php?title=Floyd%E2%80%93Warshall_algorithm&oldid=1082310013
|
||||
--
|
||||
|
||||
with Ada.Containers.Vectors;
|
||||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
with Interfaces; use Interfaces;
|
||||
|
||||
with Ada.Numerics.Generic_Elementary_Functions;
|
||||
|
||||
procedure floyd_warshall_task
|
||||
is
|
||||
Floyd_Warshall_Exception : exception;
|
||||
|
||||
-- The floating point type we shall use is one that has infinities.
|
||||
subtype FloatPt is IEEE_Float_32;
|
||||
package FloatPt_Elementary_Functions is new Ada.Numerics
|
||||
.Generic_Elementary_Functions
|
||||
(FloatPt);
|
||||
use FloatPt_Elementary_Functions;
|
||||
|
||||
-- The following should overflow and give us an IEEE infinity. But I
|
||||
-- have kept the code so you could use some non-IEEE floating point
|
||||
-- format and set ENORMOUS_FloatPt to some value that is finite but
|
||||
-- much larger than actual graph traversal distances.
|
||||
ENORMOUS_FloatPt : constant FloatPt :=
|
||||
(FloatPt (1.0) / FloatPt (1.0e-37))**1.0e37;
|
||||
|
||||
--
|
||||
-- Input is a Vector of records representing the edges of a graph.
|
||||
--
|
||||
-- Vertices are identified by integers from 1 .. n.
|
||||
--
|
||||
|
||||
type edge is record
|
||||
u : Positive;
|
||||
weight : FloatPt;
|
||||
v : Positive;
|
||||
end record;
|
||||
|
||||
package Edge_Vectors is new Ada.Containers.Vectors
|
||||
(Index_Type => Positive, Element_Type => edge);
|
||||
use Edge_Vectors;
|
||||
subtype edge_vector is Edge_Vectors.Vector;
|
||||
|
||||
--
|
||||
-- Floyd-Warshall.
|
||||
--
|
||||
|
||||
type distance_array is
|
||||
array (Positive range <>, Positive range <>) of FloatPt;
|
||||
|
||||
type next_vertex_array is
|
||||
array (Positive range <>, Positive range <>) of Natural;
|
||||
Nil_Vertex : constant Natural := 0;
|
||||
|
||||
function find_max_vertex -- Find the maximum vertex number.
|
||||
(edges : in edge_vector)
|
||||
return Positive
|
||||
is
|
||||
max_vertex : Positive;
|
||||
begin
|
||||
if Is_Empty (edges) then
|
||||
raise Floyd_Warshall_Exception with "no edges";
|
||||
end if;
|
||||
max_vertex := 1;
|
||||
for i in edges.First_Index .. edges.Last_Index loop
|
||||
max_vertex := Positive'Max (max_vertex, edges.Element (i).u);
|
||||
max_vertex := Positive'Max (max_vertex, edges.Element (i).v);
|
||||
end loop;
|
||||
return max_vertex;
|
||||
end find_max_vertex;
|
||||
|
||||
procedure floyd_warshall -- Perform Floyd-Warshall.
|
||||
(edges : in edge_vector;
|
||||
max_vertex : in Positive;
|
||||
distance : out distance_array;
|
||||
next_vertex : out next_vertex_array)
|
||||
is
|
||||
u, v : Positive;
|
||||
dist_ikj : FloatPt;
|
||||
begin
|
||||
|
||||
-- Initialize.
|
||||
|
||||
for i in 1 .. max_vertex loop
|
||||
for j in 1 .. max_vertex loop
|
||||
distance (i, j) := ENORMOUS_FloatPt;
|
||||
next_vertex (i, j) := Nil_Vertex;
|
||||
end loop;
|
||||
end loop;
|
||||
for i in edges.First_Index .. edges.Last_Index loop
|
||||
u := edges.Element (i).u;
|
||||
v := edges.Element (i).v;
|
||||
distance (u, v) := edges.Element (i).weight;
|
||||
next_vertex (u, v) := v;
|
||||
end loop;
|
||||
for i in 1 .. max_vertex loop
|
||||
distance (i, i) :=
|
||||
FloatPt (0.0); -- Distance from a vertex to itself.
|
||||
next_vertex (i, i) := i;
|
||||
end loop;
|
||||
|
||||
-- Perform the algorithm.
|
||||
|
||||
for k in 1 .. max_vertex loop
|
||||
for i in 1 .. max_vertex loop
|
||||
for j in 1 .. max_vertex loop
|
||||
dist_ikj := distance (i, k) + distance (k, j);
|
||||
if dist_ikj < distance (i, j) then
|
||||
distance (i, j) := dist_ikj;
|
||||
next_vertex (i, j) := next_vertex (i, k);
|
||||
end if;
|
||||
end loop;
|
||||
end loop;
|
||||
end loop;
|
||||
|
||||
end floyd_warshall;
|
||||
|
||||
--
|
||||
-- Path reconstruction.
|
||||
--
|
||||
|
||||
procedure put_path
|
||||
(next_vertex : in next_vertex_array;
|
||||
u, v : in Positive)
|
||||
is
|
||||
i : Positive;
|
||||
begin
|
||||
if next_vertex (u, v) /= Nil_Vertex then
|
||||
i := u;
|
||||
Put (Positive'Image (i));
|
||||
while i /= v loop
|
||||
Put (" ->");
|
||||
i := next_vertex (i, v);
|
||||
Put (Positive'Image (i));
|
||||
end loop;
|
||||
end if;
|
||||
end put_path;
|
||||
|
||||
example_graph : edge_vector;
|
||||
max_vertex : Positive;
|
||||
|
||||
begin
|
||||
Append (example_graph, (u => 1, weight => FloatPt (-2.0), v => 3));
|
||||
Append (example_graph, (u => 3, weight => FloatPt (+2.0), v => 4));
|
||||
Append (example_graph, (u => 4, weight => FloatPt (-1.0), v => 2));
|
||||
Append (example_graph, (u => 2, weight => FloatPt (+4.0), v => 1));
|
||||
Append (example_graph, (u => 2, weight => FloatPt (+3.0), v => 3));
|
||||
|
||||
max_vertex := find_max_vertex (example_graph);
|
||||
|
||||
declare
|
||||
|
||||
distance : distance_array (1 .. max_vertex, 1 .. max_vertex);
|
||||
next_vertex : next_vertex_array
|
||||
(1 .. max_vertex, 1 .. max_vertex);
|
||||
|
||||
begin
|
||||
|
||||
floyd_warshall (example_graph, max_vertex, distance, next_vertex);
|
||||
|
||||
Put_Line (" pair distance path");
|
||||
Put_Line ("---------------------------------------------");
|
||||
for u in 1 .. max_vertex loop
|
||||
for v in 1 .. max_vertex loop
|
||||
if u /= v then
|
||||
Put (Positive'Image (u));
|
||||
Put (" ->");
|
||||
Put (Positive'Image (v));
|
||||
Put (" ");
|
||||
Put (FloatPt'Image (distance (u, v)));
|
||||
Put (" ");
|
||||
put_path (next_vertex, u, v);
|
||||
Put_Line ("");
|
||||
end if;
|
||||
end loop;
|
||||
end loop;
|
||||
|
||||
end;
|
||||
end floyd_warshall_task;
|
||||
183
Task/Floyd-Warshall-algorithm/COBOL/floyd-warshall-algorithm.cob
Normal file
183
Task/Floyd-Warshall-algorithm/COBOL/floyd-warshall-algorithm.cob
Normal file
|
|
@ -0,0 +1,183 @@
|
|||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. FLOYD-WARSHALL.
|
||||
|
||||
DATA DIVISION.
|
||||
WORKING-STORAGE SECTION.
|
||||
|
||||
01 WS-NUM-VERTICES PIC 9 VALUE 4.
|
||||
01 WS-NUM-WEIGHTS PIC 9 VALUE 5.
|
||||
01 WS-INFINITY PIC 9(7)V9(2) VALUE 9999999.99.
|
||||
|
||||
01 WS-WEIGHTS.
|
||||
05 WS-W OCCURS 5 TIMES.
|
||||
10 WS-FROM PIC 9.
|
||||
10 WS-TO PIC 9.
|
||||
10 WS-COST PIC S9(3) SIGN LEADING SEPARATE.
|
||||
|
||||
01 WS-DIST.
|
||||
05 WS-D OCCURS 4 TIMES.
|
||||
10 WS-D-COL OCCURS 4 TIMES.
|
||||
15 WS-DIST-VAL PIC S9(7)V9(2).
|
||||
|
||||
01 WS-NEXT.
|
||||
05 WS-N OCCURS 4 TIMES.
|
||||
10 WS-N-COL OCCURS 4 TIMES.
|
||||
15 WS-NEXT-VAL PIC 9.
|
||||
|
||||
01 WS-I PIC 9.
|
||||
01 WS-J PIC 9.
|
||||
01 WS-K PIC 9.
|
||||
01 WS-U PIC 9.
|
||||
01 WS-V PIC 9.
|
||||
01 WS-IDX PIC 9.
|
||||
01 WS-TEMP-SUM PIC S9(7)V9(2).
|
||||
01 WS-DIST-IJ PIC S9(7)V9(2).
|
||||
01 WS-DIST-IK PIC S9(7)V9(2).
|
||||
01 WS-DIST-KJ PIC S9(7)V9(2).
|
||||
|
||||
*> Path and display working fields
|
||||
01 WS-PATH PIC X(200).
|
||||
01 WS-PATH-TEMP PIC X(200).
|
||||
01 WS-DIST-INT PIC S9(5).
|
||||
01 WS-DIST-EDIT PIC -(4)9.
|
||||
01 WS-DIST-TRIM PIC X(6).
|
||||
01 WS-OUTPUT-LINE PIC X(80).
|
||||
01 WS-NODE-CHAR PIC 9.
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
MAIN-PARA.
|
||||
MOVE 1 TO WS-FROM(1)
|
||||
MOVE 3 TO WS-TO(1)
|
||||
MOVE -2 TO WS-COST(1)
|
||||
|
||||
MOVE 2 TO WS-FROM(2)
|
||||
MOVE 1 TO WS-TO(2)
|
||||
MOVE 4 TO WS-COST(2)
|
||||
|
||||
MOVE 2 TO WS-FROM(3)
|
||||
MOVE 3 TO WS-TO(3)
|
||||
MOVE 3 TO WS-COST(3)
|
||||
|
||||
MOVE 3 TO WS-FROM(4)
|
||||
MOVE 4 TO WS-TO(4)
|
||||
MOVE 2 TO WS-COST(4)
|
||||
|
||||
MOVE 4 TO WS-FROM(5)
|
||||
MOVE 2 TO WS-TO(5)
|
||||
MOVE -1 TO WS-COST(5)
|
||||
|
||||
PERFORM INIT-DIST
|
||||
PERFORM INIT-NEXT
|
||||
PERFORM FLOYD-WARSHALL
|
||||
PERFORM PRINT-RESULT
|
||||
STOP RUN.
|
||||
|
||||
INIT-DIST.
|
||||
PERFORM VARYING WS-I FROM 1 BY 1
|
||||
UNTIL WS-I > WS-NUM-VERTICES
|
||||
PERFORM VARYING WS-J FROM 1 BY 1
|
||||
UNTIL WS-J > WS-NUM-VERTICES
|
||||
MOVE WS-INFINITY TO WS-DIST-VAL(WS-I, WS-J)
|
||||
END-PERFORM
|
||||
END-PERFORM
|
||||
PERFORM VARYING WS-IDX FROM 1 BY 1
|
||||
UNTIL WS-IDX > WS-NUM-WEIGHTS
|
||||
MOVE WS-FROM(WS-IDX) TO WS-I
|
||||
MOVE WS-TO(WS-IDX) TO WS-J
|
||||
MOVE WS-COST(WS-IDX) TO WS-DIST-VAL(WS-I, WS-J)
|
||||
END-PERFORM.
|
||||
|
||||
INIT-NEXT.
|
||||
PERFORM VARYING WS-I FROM 1 BY 1
|
||||
UNTIL WS-I > WS-NUM-VERTICES
|
||||
PERFORM VARYING WS-J FROM 1 BY 1
|
||||
UNTIL WS-J > WS-NUM-VERTICES
|
||||
IF WS-I NOT EQUAL WS-J
|
||||
MOVE WS-J TO WS-NEXT-VAL(WS-I, WS-J)
|
||||
ELSE
|
||||
MOVE 0 TO WS-NEXT-VAL(WS-I, WS-J)
|
||||
END-IF
|
||||
END-PERFORM
|
||||
END-PERFORM.
|
||||
|
||||
FLOYD-WARSHALL.
|
||||
PERFORM VARYING WS-K FROM 1 BY 1
|
||||
UNTIL WS-K > WS-NUM-VERTICES
|
||||
PERFORM VARYING WS-I FROM 1 BY 1
|
||||
UNTIL WS-I > WS-NUM-VERTICES
|
||||
PERFORM VARYING WS-J FROM 1 BY 1
|
||||
UNTIL WS-J > WS-NUM-VERTICES
|
||||
MOVE WS-DIST-VAL(WS-I, WS-K) TO WS-DIST-IK
|
||||
MOVE WS-DIST-VAL(WS-K, WS-J) TO WS-DIST-KJ
|
||||
MOVE WS-DIST-VAL(WS-I, WS-J) TO WS-DIST-IJ
|
||||
IF WS-DIST-IK < WS-INFINITY AND
|
||||
WS-DIST-KJ < WS-INFINITY
|
||||
COMPUTE WS-TEMP-SUM =
|
||||
WS-DIST-IK + WS-DIST-KJ
|
||||
IF WS-TEMP-SUM < WS-DIST-IJ
|
||||
MOVE WS-TEMP-SUM
|
||||
TO WS-DIST-VAL(WS-I, WS-J)
|
||||
MOVE WS-NEXT-VAL(WS-I, WS-K)
|
||||
TO WS-NEXT-VAL(WS-I, WS-J)
|
||||
END-IF
|
||||
END-IF
|
||||
END-PERFORM
|
||||
END-PERFORM
|
||||
END-PERFORM.
|
||||
|
||||
PRINT-RESULT.
|
||||
DISPLAY "pair dist path"
|
||||
PERFORM VARYING WS-I FROM 1 BY 1
|
||||
UNTIL WS-I > WS-NUM-VERTICES
|
||||
PERFORM VARYING WS-J FROM 1 BY 1
|
||||
UNTIL WS-J > WS-NUM-VERTICES
|
||||
IF WS-I NOT EQUAL WS-J
|
||||
|
||||
*> Set up source and destination
|
||||
MOVE WS-I TO WS-U
|
||||
MOVE WS-J TO WS-V
|
||||
|
||||
*> Seed path with just the source node
|
||||
MOVE SPACES TO WS-PATH
|
||||
MOVE WS-I TO WS-NODE-CHAR
|
||||
MOVE WS-NODE-CHAR TO WS-PATH(1:1)
|
||||
|
||||
*> Walk next-hop until we reach destination
|
||||
PERFORM UNTIL WS-U = WS-V
|
||||
MOVE WS-NEXT-VAL(WS-U, WS-V) TO WS-U
|
||||
MOVE WS-U TO WS-NODE-CHAR
|
||||
MOVE SPACES TO WS-PATH-TEMP
|
||||
STRING
|
||||
FUNCTION TRIM(WS-PATH TRAILING)
|
||||
DELIMITED SIZE
|
||||
" -> " DELIMITED SIZE
|
||||
WS-NODE-CHAR DELIMITED SIZE
|
||||
INTO WS-PATH-TEMP
|
||||
MOVE WS-PATH-TEMP TO WS-PATH
|
||||
END-PERFORM
|
||||
|
||||
*> Format distance: move to integer then edit picture
|
||||
*> to get clean signed number without leading zeros
|
||||
COMPUTE WS-DIST-INT =
|
||||
WS-DIST-VAL(WS-I, WS-J)
|
||||
MOVE WS-DIST-INT TO WS-DIST-EDIT
|
||||
MOVE FUNCTION TRIM(WS-DIST-EDIT LEADING)
|
||||
TO WS-DIST-TRIM
|
||||
|
||||
*> Assemble and print output line
|
||||
MOVE SPACES TO WS-OUTPUT-LINE
|
||||
STRING
|
||||
WS-I DELIMITED SIZE
|
||||
" -> " DELIMITED SIZE
|
||||
WS-J DELIMITED SIZE
|
||||
" " DELIMITED SIZE
|
||||
FUNCTION TRIM(WS-DIST-TRIM LEADING)
|
||||
DELIMITED SIZE
|
||||
" " DELIMITED SIZE
|
||||
FUNCTION TRIM(WS-PATH TRAILING)
|
||||
DELIMITED SIZE
|
||||
INTO WS-OUTPUT-LINE
|
||||
DISPLAY FUNCTION TRIM(WS-OUTPUT-LINE TRAILING)
|
||||
END-IF
|
||||
END-PERFORM
|
||||
END-PERFORM.
|
||||
Loading…
Add table
Add a link
Reference in a new issue