Matrix sort

Oscar Benjamin oscar.j.benjamin at gmail.com
Wed Aug 21 06:40:02 EDT 2013


On 21 August 2013 10:24,  <vijayendramunikoti at gmail.com> wrote:
> Hi
> I have a matrix of numbers representing the nodal points as follows:
>
> Element No.    Nodes
>
> 1                 1 2 3 4
> 2                 5 6 7 8
> 3                 2 3 9 10
> ...........................
> ...........................
> x                 9 10 11 12
> ...........................
>
> so this is a matrix of numbers 4 x n
> Elements 1 and 3 are neighbours (as they share nodes 2 3). Similarly elements 3 and x are neighbours (they share nodes 9 and 10). I want to sort the matrix in such a way all the elements are sequentially arranged. How could I script it? can any one help me?

I think you want a topological sort algorithm. See here:
http://en.wikipedia.org/wiki/Topological_sorting

Before that though you'll want to preprocess your matrix into a data
structure that allows you to easily find the elements adjacent to any
given element. A list of lists is one approach:

graph = [
    [3], # nodes adjacent to element 1
    [],  #  element 2
    [1, x],  # element 3
    ...
    [3]  # element x
]


Oscar



More information about the Python-list mailing list