A graph theoretical algorithm for computing the (co)homology of polyhedra