Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate matrices which satisfy the triangle inequality?

Let's consider square matrix

enter image description here

(n is a dimension of the matrix E and fixed (for example n = 4 or n=5)). Matrix entries a_{ij} satisfy following conditions:

enter image description here

enter image description here

The task is to generate all matrices E. My question is how to do that? Is there any common approach or algorithm? Is that even possible? What to start with?

like image 991
angry_gopher Avatar asked Jun 10 '13 06:06

angry_gopher


1 Answers

Naive solution

A naive solution to consider is to generate every possible n-by-n matrix E where each component is a nonnegative integer no greater than n, then take from those only the matrices that satisfy the additional constraints. What would be the complexity of that?

Each component can take on n + 1 values, and there are n^2 components, so there are O((n+1)^(n^2)) candidate matrices. That has an insanely high growth rate.

Link: WolframAlpha analysis of (n+1)^(n^2)

I think it's safe to safe that this not a feasible approach.

Better solution

A better solution follows. It involves a lot of math.

Let S be the set of all matrices E that satisfy your requirements. Let N = {1, 2, ..., n}.

Definitions:

  • Let a metric on N to have the usual definition, except with the requirement of symmetry omitted.

  • Let I and J partition the set N. Let D(I,J) be the n x n matrix that has D_ij = 1 when i is in I and j is in J, and D_ij = 0 otherwise.

  • Let A and B be in S. Then A is adjacent to B if and only if there exist I and J partitioning N such that A + D(I,J) = B.

    We say A and B are adjacent if and only if A is adjacent to B or B is adjacent to A.

  • Two matrices A and B in S are path-connected if and only if there exists a sequence of adjacent elements of S between them.

  • Let the function M(E) denote the sum of the elements of matrix E.

Lemma 1:
E = D(I,J) is a metric on N.

Proof:
This is a trivial statement except for the case of an edge going from I to J. Let i be in I and j be in J. Then E_ij = 1 by definition of D(I,J). Let k be in N. If k is in I, then E_ik = 0 and E_kj = 1, so E_ik + E_kj >= E_ij. If k is in J, then E_ik = 1 and E_kj = 0, so E_ij + E_kj >= E_ij.

Lemma 2:
Let E be in S such that E != zeros(n,n). Then there exist I and J partitioning N such that E' = E - D(I,J) is in S with M(E') < M(E).

Proof:
Let (i,j) be such that E_ij > 0. Let I be the subset of N that can be reached from i by a directed path of cost 0. I cannot be empty, because i is in I. I cannot be N, because j is not in I. This is because E satisfies the triangle inequality and E_ij > 0.

Let J = N - I. Then I and J are both nonempty and partition N. By the definition of I, there does not exist any (x,y) such that E_xy = 0 and x is in I and y is in J. Therefore E_xy >= 1 for all x in I and y in J.

Thus E' = E - D(I,J) >= 0. That M(E') < M(E) is obvious, because all we have done is subtract from elements of E to get E'. Now, since E is a metric on N and D(I,J) is a metric on N (by Lemma 1) and E >= D(I,J), we have E' is a metric on N. Therefore E' is in S.

Theorem:
Let E be in S. Then E and zeros(n,n) are path-connected.

Proof (by induction):
If E = zeros(n,n), then the statement is trivial.

Suppose E != zeros(n,n). Let M(E) be the sum of the values in E. Then, by induction, we can assume that the statement is true for any matrix E' having M(E') < M(E).

Since E != zeros(n,n), by Lemma 2 we have some E' in S such that M(E') < M(E). Then by the inductive hypothesis E' is path-connected to zeros(n,n). Therefore E is path-connected to zeros(n,n).

Corollary:
The set S is path-connected.

Proof:
Let A and B be in S. By the Theorem, A and B are both path-connected to zeros(n,n). Therefore A is path-connected to B.

Algorithm

The Corollary tells us that everything in S is path-connected. So an effective way to discover all of the elements of S is to perform a breadth-first search over the graph defined by the following.

  • The elements of S are the nodes of the graph
  • Nodes of the graph are connected by an edge if and only if they are adjacent

Given a node E, you can find all of the (potentially) unvisited neighbors of E by simply enumerating all of the possible matrices D(I,J) (of which there are 2^n) and generating E' = E + D(I,J) for each. Enumerating the D(I,J) should be relatively straightforward (there is one for every possible subset I of D, except for the empty set and D).

Note that, in the preceding paragraph, E and D(I,J) are both metrics on N. So when you generate E' = E + D(I,J), you don't have to check that it satisfies the triangle inequality - E' is the sum of two metrics, so it is a metric. To check that E' is in S, all you have to do is verify that the maximum element in E' does not exceed n.

You can start the breadth-first search from any element of S and be guaranteed that you won't miss any of S. So you can start the search with zeros(n,n).


Be aware that the cardinality of the set S grows extremely fast as n increases, so computing the entire set S will only be tractable for small n.

like image 121
12 revs Avatar answered Nov 15 '22 06:11

12 revs