The ability of web services to build and integrate loosely-coupled systems has attracted a great deal of attention from researchers in the field of the automatic web service composition. The combination of different web services to build complex systems can be carried out using different control structures to coordinate the execution flow and, therefore, finding the optimal combination of web services represents a non-trivial search effort. Furthermore, the time restrictions together with the growing number of available services complicate further the composition problem. In this paper the authors present an optimal and complete algorithm which finds all valid compositions from the point of view of the semantic input-output message structure matching. Given a request, a service dependency graph which represents a suboptimal solution is dynamically generated. Then, the solution is improved using a backward heuristic search based on the A* algorithm which finds all the possible solutions with different number of services and runpath. Moreover, in order to improve the scalability of our approach, a set of dynamic optimization techniques have been included. The proposal has been validated using eight different repositories from the Web Service Challenge 2008, obtaining all optimal solutions with minimal overhead. . He has authored or coauthored more than 50 papers in international journals, book chapters, and conferences. His current research interests are evolutionary algorithms, genetic fuzzy systems, motion planning, and control in robotics, visual SLAM (Simultaneous Localization and Mapping), web services and process mining. Juan C. Vidal was born in Lausanne, Switzerland, in 1975. He received the BEng degree in computer science from the University of La Coruña, Spain, in 2000, worked as senior consultant for an IT firm several years, and received his PhD degree from the University of Santiago de Compostela, Spain, 2010. His research interests include knowledge discovery, semantic annotation, semantic modeling of workflows and services, and the use of artificial intelligence for business intelligence. Manuel Lama is associate professor in the Department of Electronics and Computer Science at the University of Santiago de Compostela. He received his PhD in computer science from the University of Santiago de Compostela in 2000. His research interests focuses on discovery and composition of web services, semantic annotation, and process mining. 2 Note that these values are only indicative. Smaller values have been found by our algorithm and by other participants. 3The speedup is calculated as the division of the non-optimized result by the optimized result. Thus, a speedup of 2.0x indicates that the optimized result is two times faster than the non-optimized one.