Representing graphs implicitly using almost optimal space