import { json, alg, type Path } from "@dagrejs/graphlib";
type D = { [node: string]: Path };
document.querySelectorAll(".vizdom.ants").forEach((container) => {
const data = container.getAttribute("data-beoe")
? JSON.parse(container.getAttribute("data-beoe")!)
const graph = json.read(data);
container.querySelectorAll(".node").forEach((node) => {
node.classList.remove("active");
node.classList.remove("selected");
.querySelectorAll(".edge")
.forEach((node) => node.classList.remove("active"));
function highlight(id: string) {
alg.postorder(graph, [id]).forEach((node) => {
container.querySelector(`#node-${node}`)?.classList.add("active");
graph.outEdges(node)?.forEach(({ name }) => {
container.querySelector(`#edge-${name}`)?.classList.add("active");
function walkPath(d: D, node: string) {
container.querySelector(`#node-${node}`)?.classList.add("active");
if (d[node].distance === 0 || d[node].distance === Infinity) return;
graph.outEdges(d[node].predecessor, node)?.forEach(({ name }) => {
container.querySelector(`#edge-${name}`)?.classList.add("active");
walkPath(d, d[node].predecessor);
function drawShortestpath(a: string, b: string) {
const first = alg.dijkstra(graph, a);
if (first[b].distance != Infinity) {
const second = alg.dijkstra(graph, b);
if (second[a].distance != Infinity) {
let selected = new Set<string>();
// if two nodes selected will show shortest path
container.addEventListener("click", (e) => {
const node = e.target?.closest(".node");
const id = node.getAttribute("id").replace("node-", "");
selected.delete([...selected][0]);
if (selected.size === 0) return;
if (selected.size === 1) {
const id = [...selected][0];
container.querySelector(`#node-${id}`)?.classList.add("selected");
const [a, b] = [...selected];
container.querySelector(`#node-${a}`)?.classList.add("selected");
container.querySelector(`#node-${b}`)?.classList.add("selected");
let currentHover: string | null = null;
container.addEventListener("mouseover", (e) => {
if (selected.size > 1) return;
const node = e.target?.closest(".node");
if (selected.size === 0) {
const id = node.getAttribute("id").replace("node-", "");
if (currentHover == id) return;
if (currentHover == null) return;
const selectedId = [...selected][0];
const id = node.getAttribute("id").replace("node-", "");
if (currentHover == id) return;
.querySelector(`#node-${selectedId}`)
?.classList.add("selected");
drawShortestpath(selectedId, id);
if (currentHover == null) return;
.querySelector(`#node-${selectedId}`)
?.classList.add("selected");