Using string as graph for connected graph