In this article, a discrete-time distributed optimization algorithm is proposed for solving the economic dispatch (ED) problem with some groups of generator units to communicate over a connected graph, which is independent of the power system. The ED problem is converted to a distributed optimization problem with an objective of the sum of individual convex functions and constraints of local generators. Based on the optimal conditions, a class of distributed algorithms is designed to find the solution to the ED problem. The distributed algorithm can be realized as a multiagent system with a connected graph, whose convergence can be proved using the dynamic analysis method. Moreover, experiments with simulations are presented to demonstrate the performance of the proposed algorithm.