Abhishek
Abhishek

Reputation: 462

Floyd-Warshall Shortest Path Algorithm Error

Problem Statement: https://www.hackerrank.com/challenges/floyd-city-of-blinding-lights

Code:

import scala.io.StdIn._
import scala.collection.mutable
object Solution {
    def FloydWarshall(n: Int, adj: Array[Array[Int]]): Array[Array[Int]] = {
        val distance: Array[Array[Int]] = adj
        for(k <- 0 until n){
            for(u <- 0 until n){
                for(v <- 0 until n){
                    distance(u)(v) = minimum(distance(u)(v), distance(u)(k) + distance(k)(v))
                }
            }
        }
        distance
    }

    def minimum(a: Int, b: Int):Int = {
        if(a < b){
            a
        } else {
            b
        }
    }

    def main(args: Array[String]) {
        var input: Array[Int] = readLine().split(" ").map(_.toInt)
        val n: Int = input(0)
        val m: Int = input(1)    
        val adj: Array[Array[Int]] = Array.fill(n, n)(351)

        for(_ <- 1 to m){
            input = readLine().split(" ").map(_.toInt)
            val u: Int = input(0)
            val v: Int = input(1)
            val w: Int = input(2)
            adj(u-1)(v-1) = w
        }

        for(i <- 0 until n){
            adj(i)(i) = 0
        }

        val q: Int = readInt()
        val distance: Array[Array[Int]] = FloydWarshall(n, adj)
        val results: mutable.ListBuffer[Int] = mutable.ListBuffer[Int]()
        for(_ <- 1 to q) {
            input = readLine().split(" ").map(_.toInt)
            val u: Int = input(0)
            val v: Int = input(1)
            val result: Int = if(distance(u-1)(v-1) == 351) -1 else distance(u-1)(v-1)    
            results += result
        }
        println(results.mkString("\n"))
    }
}

Failing Test Case:

Input : https://paste.ubuntu.com/24406100/

Output: https://paste.ubuntu.com/24406106/

This is the only failing test case and I am not able to figure out the problem with my code.

EDIT: Fixed code, as @qwertyman pointed it out, shortest paths with two or mode edges would exceed the weight 351. The correct infinite value for this problem would be, MaxEdgeWeight * MaxNodes-1 + 1 = 350 * (400-1) + 1.

Fixed Code:

import scala.io.StdIn._
import scala.collection.mutable
import util.control.Breaks._
object Solution {
    def FloydWarshall(n: Int, adj: Array[Array[Int]]): Array[Array[Int]] = {
        val distance: Array[Array[Int]] = adj
        for(k <- 0 until n){
            for(u <- 0 until n){
                for(v <- 0 until n){
                        distance(u)(v) = minimum(distance(u)(v), distance(u)(k) + distance(k)(v))    
                }
            }
        }
        distance
    }

    def minimum(a: Int, b: Int):Int = {
        if(a < b){
            a
        } else {
            b
        }
    }

    def main(args: Array[String]) {
        var input: Array[Int] = readLine().split(" ").map(_.toInt)
        val n: Int = input(0)
        val m: Int = input(1)
        val infinity: Int = 350 * 399 + 1// maximum shortest path N-1 edges
        val adj: Array[Array[Int]] = Array.fill(n, n)(infinity)

        for(_ <- 1 to m){
            input = readLine().split(" ").map(_.toInt)
            val u: Int = input(0) - 1
            val v: Int = input(1) - 1
            val w: Int = input(2)
            adj(u)(v) = w
        }

        for(i <- 0 until n){
            adj(i)(i) = 0
        }

        val q: Int = readInt()
        val distance: Array[Array[Int]] = FloydWarshall(n, adj)
        val results: mutable.ListBuffer[Int] = mutable.ListBuffer[Int]()
        for(_ <- 1 to q) {
            input = readLine().split(" ").map(_.toInt)
            val u: Int = input(0) - 1 
            val v: Int = input(1) - 1
            val result: Int = if(distance(u)(v) == infinity) -1 else distance(u)(v)
            results += result
        }
        println(results.mkString("\n"))
    }
}

Upvotes: 0

Views: 196

Answers (1)

danbanica
danbanica

Reputation: 3038

The program uses the value 351 as a marker for invalid distances, which seems to be the problem.

Although the maximum weight of each edge is known to be 350, nevertheless, a distance with value 351 can still be reached, through a path consisting of two or more edges.

Upvotes: 2

Related Questions