Kompyuter injeneringi


Floyd-Warshall algoritmi dasturi



Yüklə 52,37 Kb.
səhifə3/3
tarix07.01.2024
ölçüsü52,37 Kb.
#202644
1   2   3
5 amaliy topshiriq

Floyd-Warshall algoritmi dasturi:
import java.util.Arrays;

public class FloydWarshallAlgorithm {
private static final int INF = Integer.MAX_VALUE;

public static void floydWarshall(int[][] graph) {
int vertexCount = graph.length;
int[][] distances = new int[vertexCount][vertexCount];

for (int i = 0; i < vertexCount; i++) {
for (int j = 0; j < vertexCount; j++) {
distances[i][j] = graph[i][j];
}
}

for (int k = 0; k < vertexCount; k++) {
for (int i = 0; i < vertexCount; i++) {
for (int j = 0; j < vertexCount; j++) {
if (distances[i][k] != INF && distances[k][j] != INF && distances[i][k] + distances[k][j] < distances[i][j]) {
distances[i][j] = distances[i][k] + distances[k][j];
}
}
}
}

System.out.println("Shortest distances between all pairs of vertices:");
for (int i = 0; i < vertexCount; i++) {
for (int j = 0; j < vertexCount; j++) {
if (distances[i][j] == INF) {
System.out.print("INF ");
} else {
System.out.print(distances[i][j] + " ");
}
}
System.out.println();
}
}

public static void main(String[] args) {
int[][] graph = {
{0, 2, 4, INF, INF, INF},
{INF, 0, INF, 7, 3, INF},
{INF, INF, 0, INF, 1, INF},
{INF, INF, INF, 0, 5, 2},
{INF, INF, INF, INF, 0, INF},
{INF, INF, INF, INF, INF, 0}
};

floydWarshall(graph);
}
}
Yüklə 52,37 Kb.

Dostları ilə paylaş:
1   2   3




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin