algorythm implemented, working for small graph