add root node first to prevent self-loops