graph reduction in C++