In mathematics, computer science and especially graph theory, a distance matrix is a square matrix (two-dimensional array) containing the distances, taken pairwise, between the elements of a set. Depending upon the application involved, the distance being used to define this matrix may or may not be a metric. If there are elements, this matrix will have size . In graph-theoretic applications the elements are more often referred to as points, nodes or vertices. Non-metric distance matrix In general, a distance matrix is a weighted adjacency matrix of some graph. In a network, a directed graph with weights assigned to the arcs, the distance between two nodes of the network can be defined as the minimum of the sums of the weights on the shortest paths joining the two nodes. This distance function, while well defined, is not a metric. There need be no restrictions on the weights other than the need to be able to combine and compare them, so negative weights are used in some applications. Since paths are directed, symmetry can not be guaranteed, and if cycles exist the distance matrix may not be hollow. An algebraic formulation of the above can be obtained by using the min-plus algebra. Matrix multiplication in this system is defined as follows: Given two matrices and , their distance product is defined as an matrix such that Note that the off-diagonal elements that are not connected directly will need to be set to infinity or a suitable large value for the min-plus operations to work correctly. A zero in these locations will be incorrectly interpreted as an edge with no distance, cost, etc. If is an matrix containing the edge weights of a graph, then (using this distance product) gives the distances between vertices using paths of length at most edges, and is the distance matrix of the graph. An arbitrary graph on vertices can be modeled as a weighted complete graph on vertices by assigning a weight of one to each edge of the complete graph that corresponds to an edge of and zero to all other edges. for this complete graph is the adjacency matrix of . The distance matrix of can be computed from as above, however, calculated by the usual matrix multiplication only encodes the number of paths between any two vertices of length exactly . Metric distance matrix The value of a distance matrix formalism in many