Algorithms on Trees and Graphs