1st to Kth shortest path lengths from node 1 to N in given Graph (original) (raw)

`using System; using System.Collections.Generic; using System.Linq;

public class ShortestPath { public static void FindKShortest(int[, ] edges, int n, int m, int k) { // Initialize graph List<List<KeyValuePair<int, int> > > g = new List<List<KeyValuePair<int, int> > >(); for (int i = 0; i <= n; i++) { g.Add(new List<KeyValuePair<int, int> >()); } for (int i = 0; i < m; i++) { // Storing edges g[edges[i, 0]].Add(new KeyValuePair<int, int>( edges[i, 1], edges[i, 2])); }

    // Vector to store distances
    int[][] dis = new int[n + 1][];
    for (int i = 0; i <= n; i++) {
        dis[i] = Enumerable.Repeat(int.MaxValue, k)
                     .ToArray();
    }

    // Initialization of priority queue
    var pq
        = new PriorityQueue<KeyValuePair<int, int> >();
    pq.Enqueue(new KeyValuePair<int, int>(0, 1));
    dis[1][0] = 0;

    // while pq has elements
    while (pq.Count > 0) {
        // Storing the node value
        int u = pq.Peek().Value;

        // Storing the distance value
        int d = pq.Peek().Key;
        pq.Dequeue();
        if (dis[u][k - 1] < d)
            continue;
        List<KeyValuePair<int, int> > v = g[u];

        // Traversing the adjacency list
        for (int i = 0; i < v.Count; i++) {
            int dest = v[i].Key;
            int cost = v[i].Value;

            // Checking for the cost
            if (d + cost < dis[dest][k - 1]) {
                dis[dest][k - 1] = d + cost;

                // Sorting the distances
                Array.Sort(dis[dest]);

                // Pushing elements to priority queue
                pq.Enqueue(new KeyValuePair<int, int>(
                    d + cost, dest));
            }
        }
    }

    // Printing K shortest paths
    for (int i = 0; i < k; i++) {
        Console.Write(dis[n][i] + " ");
    }
}

static void Main(string[] args)
{
    // Given Input
    int N = 4, M = 6, K = 3;
    int[, ] edges
        = { { 1, 2, 1 }, { 1, 3, 3 }, { 2, 3, 2 },
            { 2, 4, 6 }, { 3, 2, 8 }, { 3, 4, 1 } };

    // Function Call
    FindKShortest(edges, N, M, K);

    Console.ReadKey();
}

}

// Priority queue implementation public class PriorityQueue where T : IComparable { private List data;

public PriorityQueue() { this.data = new List<T>(); }

public void Enqueue(T item)
{
    data.Add(item);
    int ci
        = data.Count - 1; // child index; start at end
    while (ci > 0) {
        int pi = (ci - 1) / 2; // parent index
        if (data[ci].CompareTo(data[pi]) >= 0)
            break; // child item is larger than (or
                   // equal Checking for the cost
        if (d + cost < dis[dest][k - 1]) {
            dis[dest][k - 1] = d + cost;

            // Sorting the distances
            dis[dest].Sort();

            // Pushing elements to priority queue
            pq.Enqueue(new KeyValuePair<int, int>(
                d + cost, dest));
        }
    }
}

}

// Printing K shortest paths for (int i = 0; i < k; i++) { Console.Write(dis[n][i] + " "); } }

// Driver Code static void Main(string[] args) { // Given Input int N = 4, M = 6, K = 3; int[, ] edges = { { 1, 2, 1 }, { 1, 3, 3 }, { 2, 3, 2 }, { 2, 4, 6 }, { 3, 2, 8 }, { 3, 4, 1 } };

// Function Call
findKShortest(edges, N, M, K);

} } }

// This code is contributed by Akash Jha

`