A labeling algorithm