  We present distributed algorithms that can be used by multiple agents to
align their estimates with a particular value over a network with time-varying
connectivity. Our framework is general in that this value can represent a
consensus value among multiple agents or an optimal solution of an optimization
problem, where the global objective function is a combination of local agent
objective functions. Our main focus is on constrained problems where the
estimate of each agent is restricted to lie in a different constraint set.
  To highlight the effects of constraints, we first consider a constrained
consensus problem and present a distributed ``projected consensus algorithm''
in which agents combine their local averaging operation with projection on
their individual constraint sets. This algorithm can be viewed as a version of
an alternating projection method with weights that are varying over time and
across agents. We establish convergence and convergence rate results for the
projected consensus algorithm. We next study a constrained optimization problem
for optimizing the sum of local objective functions of the agents subject to
the intersection of their local constraint sets. We present a distributed
``projected subgradient algorithm'' which involves each agent performing a
local averaging operation, taking a subgradient step to minimize its own
objective function, and projecting on its constraint set. We show that, with an
appropriately selected stepsize rule, the agent estimates generated by this
algorithm converge to the same optimal solution for the cases when the weights
are constant and equal, and when the weights are time-varying but all agents
have the same constraint set.
