Is there are written code for mapping public address to an user?
You can write both of those queries in a way that takes nearly linear time and space.

Make a hash table that maps addresses to &quot;neighbor addresses.&quot; A neighbor address is an address that was used as an input alongside another address. To find the addresses a user owns, query the hash table for the address. Add all of the addresses that are neighbors of that address. Recurse for all of the neighbors.
This can be created in O(n*k) time and space. (n = number of transactions, k = average number of inputs from distinct addresses per transaction)

Make a hash table that maps addresses to the block they were first seen in. For each address of each transaction, you check the hash table to see if it has been seen yet. If not, you create an entry for it, pointing to the current block and transaction. Then, while querying, you can quickly tell if a transaction contains addresses that have been seen for the first time.
This can be created in O(n) time and space. (n = number of inputs)


(It doesn't need to be a hash table specifically. Other approaches would work too.)