Is there any algorithm that will be able to find optimal solutions for least fees?
You will get your smallest size transaction per output by including all the outputs in a single transaction, and using the fewest number of inputs available to you such that the sum of those inputs is greater than or equal to the sum of the value of the new outputs plus the transaction fee you intend to pay.
Note, however, that in creating the optimal size transaction for a set of one or more transactions, you may be using inputs that might have been better used in a future set of transactions, thereby creating a situation where the total size of the two sets combined isn't as small as it could have been if you had created a slightly less optimal first set.
Note also, that if you try to ALWAYS create the most optimal size transactions, you will eventually be left with a growing number of small value outputs. These small value outputs will add very little value to a transaction if they are used as inputs, but they will add 149 bytes. Therefore the cost/value ratio for using them will be very low. When you finally need to use them, they will result in a very expensive transaction.
The fee-per-byte costs that are necessary for fast confirmation are constantly changing, due to continuous variation in:
- The number of transactions being broadcast
- The number of blocks being solved
- The fees that others are wiling to pay
Given my notes above, and the fact that the fee rate is not constant, it can be beneficial to create a slightly less than optimal size transaction during times when you notice the fee rate is exceptionally low. That way you can use up some of the smallest value unspent outputs when it isn't very expensive to do so, and save the larger value unspent outputs for a time when the fee rate is higher.
Any script we can use to auto create multiple address transaction with address and amt information, or we have to rely on wallet UI to manually do it?
I'm sure there are various implementations. Both Electrum and Bitcoin Core have each implemented pretty good algorithms, but there is probably room for improvement on both of them. If you think you can do better, then you can write your own program and create the transactions yourself. If you don't feel it is worth it to write your own program, then you can just rely on the wallet.