handled the exceptions for empty graphs and invalid root nodes