Graph algorithm