Cooperative communication has been proposed to increase the capacity of wireless networks. By exploiting a relay node, it achieves spatial diversity to cope with fading channel without requiring wireless nodes to be equipped with multiple antennas. However, the selection of relay nodes has a significant impact on the final capacity. In this paper, we study the problem of relay assignment in cooperative networks, where multiple source-destination transmission pairs share the same set of relay nodes. Specifically, we propose a system model where a relay node can be shared by multiple source-destination pairs and present a corresponding formulation for the capacity calculation. Our objective is to find a relay assignment to maximize the total capacity of the network. As the main contribution, we develop an optimal relay assignment algorithm to solve this problem in polynomial time. We also show that our algorithm has several attractive properties.