Journal of Algorithms 42, 41–53 (2002) doi:10.1006/jagm.2001.1201, available online at http://www.idealibrary.com on The Exact Path Length Problem 1 Matti Nyk¨ anen 2 and Esko Ukkonen 3 Department of Computer Science, University of Helsinki, P.O. Box 26 (Teollisuuskatu 23), 00014 Helsinki, Finland E-mail: matti.nykanen, esko.ukkonen@cs.helsinki.fi Received March 2, 2000 We study a problem related to finding shortest paths in weighted graphs. We ask whether or not there is a path between two nodes that has a given total cost k. The edge weights of the graph can be both positive and negative integers or even integer vectors. We show that many variants of this problem are NP-complete. We develop a pseudo-polynomial algorithm for (both positive and negative) integer weights. The running time of this algorithm is OW 2 n 3 +k minkW n 2 , where n is the number of nodes in the graph, W is the largest absolute value of any edge weight, and k is the target cost. The algorithm is based on preprocessing the graph with a relaxation algorithm to eliminate the effects of weight sign alternations along a path. This preprocessing stage is applicable to other problems as well. For example, we show how to find the minimum absolute cost of any path between two given nodes in a graph with integer weights in OW 2 n 3 time. 2002 Elsevier Science Key Words: graph algorithms; path problems; pseudo-polynomial time algorithms; NP-completeness. 1. INTRODUCTION Finding shortest paths in weighted graphs is one of the most central problems in graph algorithms, with plenty of applications. The problem is now well understood, with several polynomial time solution algorithms [4, Chapters 25 and 26]. On the other hand, finding a longest path is a well- known NP-complete problem [7, Problem ND29]. 1 A preliminary version of this work appeared as Finding paths with the right cost in “Proceedings of the 16th Annual Symposium on Theoretical Aspects of Computer Science (STACS 99)” (C. Meinel and S. Tison, Eds.), Lecture Notes in Computer Science, Vol. 1563, 345–355, Springer-Verlag, Berlin/New York, 1999. 2 Supported by the Academy of Finland Grant 42977. 3 Supported by the Academy of Finland Grant 44449. 41 0196-6774/02 $35.00 2002 Elsevier Science All rights reserved.