Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Diagnostics;
- namespace lab6
- {
- class FloydWarshallAlgo
- {
- public const int cst = 9999;
- private static void Print(int[,] distance, int verticesCount)
- {
- Console.WriteLine("Shortest distances between every pair of vertices:");
- for (int i = 0; i < verticesCount; ++i)
- {
- for (int j = 0; j < verticesCount; ++j)
- {
- if (distance[i, j] == cst)
- Console.Write("cst".PadLeft(7));
- else
- Console.Write(distance[i, j].ToString().PadLeft(7));
- }
- Console.WriteLine();
- }
- }
- public static void FloydWarshall(int[,] graph, int verticesCount)
- {
- int[,] distance = new int[verticesCount, verticesCount];
- for (int i = 0; i < verticesCount; ++i)
- for (int j = 0; j < verticesCount; ++j)
- distance[i, j] = graph[i, j];
- for (int k = 0; k < verticesCount; ++k)
- {
- for (int i = 0; i < verticesCount; ++i)
- {
- for (int j = 0; j < verticesCount; ++j)
- {
- if (distance[i, k] + distance[k, j] < distance[i, j])
- distance[i, j] = distance[i, k] + distance[k, j];
- }
- }
- }
- Print(distance, verticesCount);
- }
- static void Main(string[] args)
- {
- int[,] graph = {
- { cst, 2, 3, 6 },
- { 5, cst, cst, 2 },
- { 4, cst, cst, 4 },
- { cst, 5, 4, cst }
- };
- FloydWarshall(graph, 4);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement