Algorithm 如何在一定条件下生成最优树

Algorithm 如何在一定条件下生成最优树,algorithm,graph,tree,minimum,Algorithm,Graph,Tree,Minimum,我们有一个加权图,我们希望在以下条件下生成一棵最优树: 1树应包含图中的所有顶点 2所有的树边都应该在图中 3从顶点U开始,到达具有最小路径的任何其他顶点 根据初始图和条件,我们希望生成一棵具有最小权重的树 例: 输入 6.8 1230 1230 2350 4200 2540 3 5 10 3650 5660 四, 输出: 230 说明: 我们有6个顶点和8条边,然后我们有8条带树号的线。例如,2 3 50表示顶点2与权重为50的顶点3相连 在最后,我们有一个数字显示起始顶点 因此,如果我们从顶

我们有一个加权图,我们希望在以下条件下生成一棵最优树:

1树应包含图中的所有顶点

2所有的树边都应该在图中

3从顶点U开始,到达具有最小路径的任何其他顶点

根据初始图和条件,我们希望生成一棵具有最小权重的树

例:

输入

6.8

1230

1230

2350

4200

2540

3 5 10

3650

5660

四,

输出: 230

说明: 我们有6个顶点和8条边,然后我们有8条带树号的线。例如,2 3 50表示顶点2与权重为50的顶点3相连

在最后,我们有一个数字显示起始顶点

因此,如果我们从顶点4开始,并以最小路径到达所有其他顶点,我们可以得到一棵总权重为230的树,您可以使用U作为起始节点。如果你只考虑在所有顶点计算最短距离时使用的边,那么你将得到所需的树。 现在要获得所有的边,需要对算法进行一些修改。您需要维护一个父数组,该数组在计算最短距离时保留有关当前顶点所依赖的顶点的信息

例如,我们有两个顶点U和V,从某些源到所有顶点的距离S存储在距离[]数组中。 现在假设在U和V之间有一条边E,权重为W,条件距离[U]>满足距离[V]+W,那么U的父元素将是V,因为距离[U]现在取决于距离[V]

因此,我们将在算法中增加一个步骤,更新距离。最初,父[源]将是源本身,因为它不依赖于任何其他顶点

最后,要获得所有边,需要遍历父数组并打印索引parent[index]

Java中的示例代码:

输出:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class ModifiedDijkstra {
    public static final long INF = (long) 1e15;

    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int totalVertices = sc.nextInt();
        int totalEdges = sc.nextInt();

        ArrayList<Edge> adjacencyList[] = new ArrayList[totalVertices + 1];

        for (int i = 1; i <= totalVertices; i++)
            adjacencyList[i] = new ArrayList<>();

        for (int i = 0; i < totalEdges; i++) {
            int u = sc.nextInt();
            int v = sc.nextInt();
            long weight = sc.nextInt();

            adjacencyList[u].add(new Edge(v, weight));
            adjacencyList[v].add(new Edge(u, weight));
        }

        int source = sc.nextInt();   //Source Index
        long distance[] = new long[totalVertices + 1];
        long edgesWeights[] = new long[totalVertices + 1];
        Arrays.fill(distance, INF);

        int parent[] = new int[totalVertices + 1];
        distance[source] = 0;
        parent[source] = source;

        Queue<Integer> queue = new LinkedList<>();
        queue.add(source);

        while (!queue.isEmpty()) {
            int currVertex = queue.poll();

            for (Edge edge : adjacencyList[currVertex]) {
                if (distance[edge.endVertex] > distance[currVertex] + edge.weight) {
                    distance[edge.endVertex] = distance[currVertex] + edge.weight;
                    parent[edge.endVertex] = currVertex;
                    edgesWeights[edge.endVertex] = edge.weight;
                    queue.add(edge.endVertex);
                }
            }
        }

        System.out.println("TREE : ");

        long edgesSum = 0;

        for (int i = 1; i <= totalVertices; i++) {
            if (parent[i] == i) //source
                continue;

            //Vertex1 <-> Vertex2 : Weight
            System.out.println(i + " <-> " + parent[i] + " : " + edgesWeights[i]);
            edgesSum += edgesWeights[i];
        }

        System.out.println("Sum of the weights of all edges is : " + edgesSum);


    }
}

class Edge {
    int endVertex;
    long weight;

    public Edge(int endVertex, long weight) {
        this.endVertex = endVertex;
        this.weight = weight;
    }
}
6 8

1 2 30

1 3 20

2 3 50

4 2 100

2 5 40

3 5 10

3 6 50

5 6 60

4
TREE : 
1 <-> 2 : 30
2 <-> 4 : 100
3 <-> 2 : 50
5 <-> 2 : 40
6 <-> 3 : 50
Sum of the weights of all edges is : 270