combine build matrix function and transtive closure computing