How do I use the XPub to generate the Public addresses Given the XPub and an address, can I write an algorithm to say whether or not that public key was derived from that XPub and consequently belongs to the same user?
Given the XPub and an address, can I write an algorithm to say whether or not that public key was derived from that XPub and consequently belongs to the same user?


[Edit: my terminology was off, answer edited substantially to be correct]

Given the xpub and an address, you would indeed be able to discern whether the two are related. 

BIP32 is the specification that describes hierarchical deterministic (HD) wallets. To extend a parent public key, it is combined with the chain code and the index, then hashed through HMAC-SHA512. The hash output is split in two, and the 'left half of the hash' is added to the parent public key, to produce the child public key. 

So by knowing the xpub (parent public key + chaincode), you can write an algorithm to determine if the two addresses have a parent/child relationship, and you would be able to find additional child addresses.

Without the xpub, remember that BIP32 derived keys are otherwise indistinguishable from any other keys. So you can not just look at an arbitrary address and know with certainty how it was derived.