From graph to matrix