Graph theory algorithm matlab code