mark: simpler version for SO
mark: simplifying this further
mark: introducing adjacency matrix, should make vectorisation easier
mark: vectorised version of part of the algorihtm