Goal-directed dynamics of network topology