Detecting when matrix multiplication is possible

后端 未结 2 721
隐瞒了意图╮
隐瞒了意图╮ 2020-12-16 01:01

Here is an interesting problem that I encountered in a programming competition:

Problem statement: Given the dimensions of n matrices,

相关标签:
2条回答
  • 2020-12-16 01:39

    Create a compatibility matrix (let's call it CM) such as CM[x,y] = 1 if the matrix x can be multuplied by y, 0 if not. if Determinant(CM) <> 0 there is an order.

    It's just an intuition, I apologize if I'm wrong (unfortunately I couldn't find a solid proof).

    0 讨论(0)
  • 2020-12-16 01:40
    1. Create a node for each dimension length. That is, if there is a matrix of dimension (m,n), then m and n will be vertices in the graph.

    2. For every matrix of size (m,n) connect nodes m and n with a directed edge (there can be multiple edges between two nodes).

    3. Now finding a eularian trail would give the multiplication order.

    See http://en.wikipedia.org/wiki/Euler_path for finding Eularian trails. Complexity is pretty close to linear ( O(nlog^3n loglogn) where n is the number of edges = number of matrices) .

    0 讨论(0)
提交回复
热议问题