I use gambit to generate the mesh. You can find some other
software to do this.
The shape of the matrix only depends on the ordering of your elements.
You should reorder the indices of your elements so that you
can get a matrix with smallest band-width. More clearly, you had better
make those neighbor elements have close indices.
Maybe you don't need any algorithm for sparse matrix.