LinuxCommandLibrary

dijkstra

Find shortest paths in graphs

TLDR

Compute distances from a given source node in a graph file

$ dijkstra [source_node_file]
copy

Treat the graph as [d]irected when computing distances
$ dijkstra -d [source_node_file]
copy

Record the [p]revious closest node for each node on the shortest path
$ dijkstra -p [source_node_file]
copy

[a]ssign large distance values to unreachable nodes
$ dijkstra -a [source_node_file]
copy

SYNOPSIS

dijkstra [-h] [-s source] [-t target] [-f file] [graph-data]

PARAMETERS

-h
    Display help and usage information

-s source
    Specify starting node for path computation

-t target
    Specify ending node (optional; computes to all if omitted)

-f file
    Read graph from file instead of stdin

--weighted
    Enable weighted edge support (default in most impls)

-o file
    Output path/distances to file

DESCRIPTION

The dijkstra command is not a standard Linux utility included in core distributions like GNU coreutils or busybox. It typically refers to user-contributed or package-specific implementations of Dijkstra's algorithm for finding the shortest path in a weighted graph.

Such tools are often found in graph theory packages, programming contest environments, or custom scripts. For example, implementations may exist in repositories like AOSP, Debian contrib, or third-party GitHub projects (e.g., a C-based CLI tool parsing graph input). They read graph data from stdin/files (nodes, edges, weights) and output shortest paths from source to target.

Without a specific package installed (e.g., via apt install dijkstra if available), running dijkstra results in 'command not found'. Standard alternatives use libraries like NetworkX (Python) or igraph. Usage focuses on academic/network analysis, not sysadmin tasks.

CAVEATS

Not in standard PATH; requires manual install. Input formats vary (e.g., adjacency list/matrix). Inefficient for large graphs (>10k nodes). No POSIX guarantee; may segfault on invalid input.

INPUT FORMAT

Common: First line N M (nodes edges), then M lines 'u v w' (from to weight).
Example:
4 5
0 1 1
0 2 4
...See tool docs for variants.

ALTERNATIVES

Use python -c 'import networkx as nx; nx.dijkstra_path(G,src,tgt)' or igraph CLI wrappers for production.

HISTORY

Named after Edsger W. Dijkstra (1959 algorithm). CLI tools emerged in 2000s for education/contests (e.g., ACM ICPC). Sparse Linux packaging; popularized via Python's networkx.dijkstra_path since 2008.

SEE ALSO

dot(1), neato(1), traceroute(8), mtr(8)

Copied to clipboard