I want to detect specific number of communities in networks, which require algorithms, where number of communities are specified in advance. Also if there are any, I would like to know how to implement (R, Python).
Fixing the number of communities contradicts the quality.
Assuming you have 5 disconnected components, but insist on 4 communities, the algorithm would have to randomly merge 2 disconnected parts.
Most users will not be accepting such a loss in quality, so you will need to implement such a variant yourself. Please begin writing code, and update your question with code when you are having issues.