Refactored code for is_subgraph_free