AutoSkill Floyd-Warshall Algorithm with Iterative Matrix Output
Implements the Floyd-Warshall algorithm to find all-pairs shortest paths, printing the Distance (D) and Predecessor (P) matrices at every iteration. The P matrix specifically tracks the highest index of the intermediate vertex on the shortest path.
install
source · Clone the upstream repo
git clone https://github.com/ECNU-ICALK/AutoSkill
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ECNU-ICALK/AutoSkill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/SkillBank/ConvSkill/english_gpt4_8/floyd-warshall-algorithm-with-iterative-matrix-output" ~/.claude/skills/ecnu-icalk-autoskill-floyd-warshall-algorithm-with-iterative-matrix-output && rm -rf "$T"
manifest:
SkillBank/ConvSkill/english_gpt4_8/floyd-warshall-algorithm-with-iterative-matrix-output/SKILL.mdsource content
Floyd-Warshall Algorithm with Iterative Matrix Output
Implements the Floyd-Warshall algorithm to find all-pairs shortest paths, printing the Distance (D) and Predecessor (P) matrices at every iteration. The P matrix specifically tracks the highest index of the intermediate vertex on the shortest path.
Prompt
Role & Objective
Act as a Python programmer and algorithm expert. Implement the Floyd-Warshall algorithm to find all-pairs shortest paths in a weighted graph.
Operational Rules & Constraints
- Input: Accept the number of vertices and a list of edges (start_node, end_node, weight).
- Initialization:
- Initialize Distance matrix
withD
(infinity),inf
on the diagonal, and edge weights for direct connections.0 - Initialize Predecessor matrix
to track the highest index of the intermediate vertex on the shortest path. InitializeP
withP
or0
as appropriate for the context (usually 0 if no intermediate).None
- Initialize Distance matrix
- Algorithm Execution:
- Iterate through each vertex
as an intermediate node.k - For every pair of vertices
andi
, check if the path fromj
toi
throughj
is shorter than the current path.k - If
:D[i][k] + D[k][j] < D[i][j]- Update
.D[i][j] = D[i][k] + D[k][j] - Update
(to store the highest index intermediate vertex).P[i][j] = k
- Update
- Iterate through each vertex
- Output Requirements:
- Print the Distance matrix
and Predecessor matrixD
at every iteration, including the initial state (D0, P0) and the final state (Dn, Pn).P - Format the output clearly, labeling each iteration (e.g., "After iteration 1, D1:").
- Print the Distance matrix
Anti-Patterns
- Do not only print the final matrices.
- Do not use the standard "immediate predecessor" logic for P unless specified; use the "highest index intermediate" logic requested.
Triggers
- Use Floyd's algorithm to find all pair shortest paths
- Construct the matrix D and matrix P which contains the highest indices of the intermediate vertices
- Write a program to get the desired output
- print the matrices at each iteration