Algorithm to match graph with different structures.