'Create 1D numpy arrays from 2D array of connectivities

I have a list of connectivities for outer/boundary edges of tri mesh shown below:

2d_connectivity

arranged like this:

edges = \
[[ 1  0]
 [10 11]
 [ 2  1]
 [11 12]
 [ 3  2]
 [12 13]
 [ 4  3]
 [13 14]
 [ 5  4]
 [14 15]
 [ 6  5]
 [15 16]
 [ 7  6]
 [16 17]
 [ 8  7]
 [17 18]
 [ 9  8]
 [18 19]
 [ 0  9]
 [19 10]]

I want to create 2 arrays containing connectivity of loops (inner and outer circles) like this

a = [0 1 2 3 4 5 6 7 8 9]
b = [10 11 12 13 14 15 16 17 18 19]

The loops just happen to have continuous numbering in this case but in general this is not the case and I can have more than 2 loops. In general the 2D edge connectivity can have completely random numbering. My O(n^2) algorithm that picks one point on the loop and finds the next one is too slow. What is an optimal way to extract multiple loop information from 2D connectivity?

Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source