📜  无向加权图中最短路径的数量

📅  最后修改于: 2021-10-25 03:07:45             🧑  作者: Mango

给定一个加权无向图G和一个整数S ,任务是打印最短路径的距离以及每个节点从给定顶点S 开始的最短路径数

例子:

方法:给定的问题可以使用 Dijkstra 算法解决。请按照以下步骤解决问题:

  • 使用 ArrayList> 形成给定图的邻接列表并将其存储在一个变量中,例如adj。
  • 初始化两个整数,数组表示Dist[]Paths[]所有元素都为0以存储每个节点的最短距离以及与源节点S距离最短的路径计数。
  • 定义一个函数,比如Dijkstra()来找到每个节点的最短距离并计算距离最短的路径:
    • 初始化一个 min PriorityQueue 说PQ和一个字符串的 HashSet 说已解决以存储边缘是否被访问。
    • 0分配给Dist[S] ,将1分配给Paths[S]。
    • 现在迭代直到PQ不为空()并执行以下操作:
      • 找到PQ的顶部 Node 并将 Node 值存储在变量u 中
      • 弹出 PQ 的顶部元素。
      • 遍历 ArrayList adj[u]并执行以下操作
        • 将相邻节点存储在变量 say to 中,将边成本存储在变量 say cost 中
        • 如果访问了边{u, to} ,则继续。
        • 如果dist[to]大于dist[u]+cost,则将dist[u]+cost分配给dist[to] ,然后将Paths[u]分配给Paths[to]。
        • 否则,如果Paths[to]等于dist[u]+cost,则将Paths[to]增加1。
        • 现在,马克,当前边 {u, to}结算中访问过。
  • 调用函数Dijkstra()
  • 最后,打印数组dist[]Paths[]

下面是上述方法的实现:

Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG {
  
    // Node class
    static class Node implements Comparator {
  
        // Stores the node
        public int node;
  
        // Stores the weight
        // of the edge
        public int cost;
  
        public Node() {}
  
        // Constructor
        public Node(int node, int cost)
        {
            this.node = node;
            this.cost = cost;
        }
  
        // Costume comparator
        @Override
        public int compare(Node node1, Node node2)
        {
            if (node1.cost < node2.cost)
                return -1;
            if (node1.cost > node2.cost)
                return 1;
            return 0;
        }
    }
  
    // Function to insert a node
    // in adjacency list
    static void addEdge(ArrayList > adj,
                        int x, int y, int w)
    {
        adj.get(x).add(new Node(y, w));
        adj.get(y).add(new Node(x, w));
    }
  
    // Auxiliary function to find shortest paths using
    // Dijekstra
    static void dijkstra(ArrayList > adj,
                         int src, int n, int dist[],
                         int paths[])
    {
        // Stores the distances of every node in shortest
        // order
        PriorityQueue pq
            = new PriorityQueue(n + 1, new Node());
  
        // Stores if a vertex has been visited or not
        Set settled = new HashSet();
  
        // Adds the source node with 0 distance to pq
        pq.add(new Node(src, 0));
  
        dist[src] = 0;
        paths[src] = 1;
  
        // While pq is not empty()
        while (!pq.isEmpty()) {
  
            // Stores the top node of pq
            int u = pq.peek().node;
  
            // Stores the distance
            // of node u from s
            int d = pq.peek().cost;
  
            // Pop the top element
            pq.poll();
  
            for (int i = 0; i < adj.get(u).size(); i++) {
                int to = adj.get(u).get(i).node;
                int cost = adj.get(u).get(i).cost;
  
                // If edge is marked
                if (settled.contains(to + " " + u))
                    continue;
  
                // If dist[to] is greater
                // than dist[u] + cost
                if (dist[to] > dist[u] + cost) {
  
                    // Add the node to to the pq
                    pq.add(new Node(to, d + cost));
  
                    // Update dist[to]
                    dist[to] = dist[u] + cost;
  
                    // Update paths[to]
                    paths[to] = paths[u];
                }
  
                // Otherwise
                else if (dist[to] == dist[u] + cost) {
                    paths[to] = (paths[to] + paths[u]);
                }
  
                // Mark the edge visited
                settled.add(to + " " + u);
            }
        }
    }
  
    // Function to find the count of shortest path and
    // distances from source node to every other node
    static void
    findShortestPaths(ArrayList > adj,
                      int s, int n)
    {
        // Stores the distances of a
        // node from source node
        int[] dist = new int[n + 5];
  
        // Stores the count of shortest
        // paths of a node from
        // source node
        int[] paths = new int[n + 5];
  
        for (int i = 0; i <= n; i++)
            dist[i] = Integer.MAX_VALUE;
  
        for (int i = 0; i <= n; i++)
            paths[i] = 0;
  
        // Function call to find
        // the shortest paths
        dijkstra(adj, s, n, dist, paths);
  
        System.out.print("Shortest Paths distances are : ");
        for (int i = 1; i <= n; i++) {
            System.out.print(dist[i] + " ");
        }
  
        System.out.println();
  
        System.out.print(
            "Numbers of the shortest Paths are: ");
        for (int i = 1; i <= n; i++)
            System.out.print(paths[i] + " ");
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        // Input
        int N = 9;
        int M = 14;
  
        ArrayList > adj = new ArrayList<>();
  
        for (int i = 0; i <= N; i++) {
            adj.add(new ArrayList());
        }
  
        addEdge(adj, 1, 2, 1);
        addEdge(adj, 2, 3, 1);
        addEdge(adj, 3, 4, 2);
        addEdge(adj, 4, 5, 1);
        addEdge(adj, 5, 6, 2);
        addEdge(adj, 6, 7, 2);
        addEdge(adj, 7, 8, 1);
        addEdge(adj, 8, 1, 1);
        addEdge(adj, 2, 8, 2);
        addEdge(adj, 3, 9, 1);
        addEdge(adj, 8, 9, 2);
        addEdge(adj, 7, 9, 2);
        addEdge(adj, 3, 6, 1);
        addEdge(adj, 4, 6, 1);
  
        // Function call
        findShortestPaths(adj, 1, N);
    }
}


输出:
Shortest Paths distances are : 0 1 2 4 5 3 2 1 3 
Numbers of the shortest Paths are: 1 1 1 2 3 1 1 1 2

时间复杂度: O(M + N * log(N))
辅助空间: O(M)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程