Implemented Erdos-Gallai theorem