Skip to content

Instantly share code, notes, and snippets.

@AnthonyMikh
Last active June 28, 2020 09:14
Show Gist options
  • Save AnthonyMikh/1b16242c6aaf9586749b62a22404d10e to your computer and use it in GitHub Desktop.
Save AnthonyMikh/1b16242c6aaf9586749b62a22404d10e to your computer and use it in GitHub Desktop.
Generation of vizualisation of problematic test case for "Cheapest Flights Within K Stops" Leetcode problem
fn main() {
let width = 9_usize;
let total = width * width + 2;
let len = width * width * (width - 1) + width * 2;
let mut out = Vec::with_capacity(len);
for i in 0..width {
out.push([0, i + 1, 100]);
}
for layer in 0..width - 1 {
let base = layer * width + 1;
for src in base..base + width {
for dst in base + width..base + width * 2 {
out.push([src, dst, 100])
}
}
}
let last_base = (width - 1) * width + 1;
for src in last_base..last_base + width {
out.push([src, total - 1, 100])
}
// Printing
println!(r"digraph {{
rankdir=TB
splines=line
ranksep=1.0");
for &[src, dest, _cost] in &out {
println!(" {}->{};", src, dest);
}
// Add invisible edges in order to put nodes within same layer in order.
// This is a hack, but AFAIK there is no better way to do it with graphviz
for layer in 0..width {
let base = layer * width + 1;
for node in base..base + width - 1 {
println!(r#" {}->{} [style="invis"];"#, node, node + 1);
}
}
// Tell graphviz that nodes within each layer have same rank
for layer in 0..width {
let base = layer * width + 1;
print!(" {{\n rank=same;\n ");
for node in base..base + width {
print!("{};", node);
}
println!("\n }}");
}
println!("}}")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment