  We propose a model for demonstrating spontaneous emergence of collective
intelligent behavior from selfish individual agents. Agents' behavior is
modeled using our proposed selfish algorithm ($SA$) with three learning
mechanisms: reinforced learning ($SAL$), trust ($SAT$) and connection ($SAC$).
Each of these mechanisms provides a distinctly different way an agent can
increase the individual benefit accrued through playing the prisoner's dilemma
game ($PDG$) with other agents. The $SA$ provides a generalization of the
self-organized temporal criticality ($SOTC$) model and shows that
self-interested individuals can simultaneously produce maximum social benefit
from their decisions. The mechanisms in the $SA$ are self-tuned by the internal
dynamics and without having a pre-established network structure. Our results
demonstrate emergence of mutual cooperation, emergence of dynamic networks, and
adaptation and resilience of social systems after perturbations. The
implications and applications of the $SA$ are discussed.
