给定n 个电气元件和m根电线来连接它们,其中每根电线都有长度。找出两个组件之间的优化导线长度。
示例:
Input : Source = A
Destination = C
Output: 4
Explanation: There are five different paths from node A to node C i.e., A->B->C, A->B->D->C, A->D->C, A->E->D->C, A->D->B->C. But the path with smallest or optimized length is A->E->D->C with length as 4.
方法:
给定 n 个组件和 m 条线构成一个无向加权图。任务是计算两个组件之间的优化长度,即两个组件之间的最小长度。问题是 Dijkstra 算法的应用。由于源可用,因此可以使用 Dijkstra 算法计算从源到所有节点的最短长度。这将给出给定节点源和所有其他节点之间的最短可能长度作为数组。现在,可以使用此数组给出从源到目标的最短长度。让我们借助一个例子来理解这一点。
示例:
Input: Source = A
Destination = D
Output: 9
Explanation: Use Dijkstra’s algorithm to calculate shortest length from source A to all other nodes.
Vertex Distance from source
A 0
B 4
C 5
D 9
E 5
F 6
G 8
The shortest length can be found for any component from source A. Final answer will be the shortest length from A to D i.e., 9.
下面是上述方法的实现:
Java
// Java program for implementation
// of above approach
import java.util.*;
class GFG {
// n is no. of nodes and m is no. of edges
public static int n, m;
// adjacency list representation of graph
public static List > graph
= new ArrayList >();
// source and destination points for shortest path
public static int src, dest;
static class Node {
// node's label
public int label;
// length of edge to this node
public int length;
public Node(int v, int w)
{
label = v;
length = w;
}
}
// Driver program
public static void main(String[] args) throws Exception
{
n = 5;
m = 7;
// Initialize adjacency list structure
// to empty lists:
for (int i = 0; i <= n; i++) {
List item = new ArrayList();
graph.add(item);
}
graph.get(1).add(new Node(2, 2));
graph.get(2).add(new Node(1, 2));
graph.get(1).add(new Node(4, 4));
graph.get(4).add(new Node(1, 4));
graph.get(1).add(new Node(5, 2));
graph.get(5).add(new Node(1, 2));
graph.get(4).add(new Node(5, 1));
graph.get(5).add(new Node(4, 1));
graph.get(2).add(new Node(4, 3));
graph.get(4).add(new Node(2, 3));
graph.get(2).add(new Node(3, 3));
graph.get(3).add(new Node(2, 3));
graph.get(4).add(new Node(3, 1));
graph.get(3).add(new Node(4, 1));
// Source node
src = 1;
// Destination node
dest = 3;
dijkstra();
}
// Function to implement Dijkstra's algorithm
public static void dijkstra()
{
// array to keep track of unvisited nodes
boolean[] done = new boolean[n + 1];
// node array to keep track of path
// from source to all other nodes
Node[] table = new Node[n + 1];
// intialise all nodes
for (int i = 1; i <= n; i++)
table[i] = new Node(-1, Integer.MAX_VALUE);
// source to source length is 0
table[src].length = 0;
// Dijkstra's algorithm implementation
for (int count = 1; count <= n; count++) {
int min = Integer.MAX_VALUE;
int minNode = -1;
// find the minimum length node
// from unvisited nodes
for (int i = 1; i <= n; i++) {
if (!done[i] && table[i].length < min) {
min = table[i].length;
minNode = i;
}
}
// visit the minNode
done[minNode] = true;
// iterator to traverse all connected
// nodes to minNode
ListIterator iter
= graph.get(minNode).listIterator();
while (iter.hasNext()) {
Node nd = (Node)iter.next();
int v = nd.label;
int w = nd.length;
// update the distance from minNode
// of unvisited nodes
if (!done[v]
&& table[minNode].length + w
< table[v].length) {
table[v].length
= table[minNode].length + w;
table[v].label = minNode;
}
}
}
// length is now available rom source to all nodes
System.out.println("Wire froms " + dest + " to "
+ src + " with length "
+ table[dest].length);
int next = table[dest].label;
System.out.print("Path is : " + dest + " ");
// path from destination to source via all
// intermediate nodes with minimum length
while (next >= 0) {
System.out.print(next + " ");
next = table[next].label;
}
System.out.println();
}
}
Wire froms 3 to 1 with length 4
Path is : 3 4 5 1
时间复杂度:上述 Dijkstra 算法实现的时间复杂度为 O(n^2)。
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live