Skip to content

Instantly share code, notes, and snippets.

@whoo24
Created November 6, 2016 14:05
Show Gist options
  • Save whoo24/7b5f74bd4696fa8171b820e6f48955f1 to your computer and use it in GitHub Desktop.
Save whoo24/7b5f74bd4696fa8171b820e6f48955f1 to your computer and use it in GitHub Desktop.
floyd algorithm for finding shortestpath
using System;
using System.Collections.Generic;
namespace FloydAlgorithm {
public class Floyd {
public void floyd (int n, Graph W, ref Graph D) {
int i, j, k;
D.CopyFrom(W);
for (k = 0; k < n; k++) {
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
D[i,j] = (int)Math.Min((long)D[i,j], (long)D[i,k] + D[k,j]);
}
}
}
}
public void floyd2 (int n, Graph W, ref Graph D, ref int[,] P) {
int i, j, k;
for (i = 0; i < n; i++) {
for (j = 0; j < n; ++j) {
P[i, j] = -1;
}
}
D.CopyFrom(W);
for (k = 0; k < n; k++) {
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if ((long)D[i, k] + (long)D[k,j] < (long)D[i, j]) {
P[i, j] = k;
D[i, j] = D[i, k] + D[k, j];
}
}
}
}
}
public void path (int[,] P, int q, int r, ref List<int> result) {
if (P[q,r] == -1) {
return;
}
path(P, q, P[q, r], ref result);
result.Add(P[q, r]);
path(P, P[q, r], r, ref result);
}
}
}
using System.Linq;
using System.Text;
using ValueType = System.Int32;
using Container = System.Collections.Generic.List<int>;
namespace FloydAlgorithm {
public class Graph {
private Container m_ = new Container();
private int n_;
public Graph (ValueType n) {
m_.Capacity = n;
m_.AddRange(Enumerable.Repeat(0, n * n));
n_ = n;
}
public ValueType At (int r, int c) {
return this[r, c];
}
public ValueType this [int r, int c] {
get {
return m_[r * n_ + c];
}
set {
m_[r * n_ + c] = value;
}
}
public ValueType[] this[int r] {
set {
for(int i = 0; i < value.Length; ++i) {
m_[n_ * r + i] = value[i];
}
}
}
public override string ToString () {
StringBuilder str = new StringBuilder();
for(int r = 0; r < n_; ++r) {
for (int c = 0; c < n_; ++c) {
str.AppendFormat("{0,2:#0} ", At(r, c) == int.MaxValue ? -1 : At(r, c) );
}
str.AppendLine();
}
return str.ToString();
}
public static string ToString (int[,] m, int n) {
StringBuilder str = new StringBuilder();
for (int r = 0; r < n; ++r) {
for (int c = 0; c < n; ++c) {
str.AppendFormat("{0,2:#0} ", m[r, c] == int.MaxValue ? -1 : m[r, c]);
}
str.AppendLine();
}
return str.ToString();
}
public void CopyFrom (Graph rhs) {
m_.Clear();
m_.AddRange(rhs.m_);
n_ = rhs.n_;
}
}
}
using System;
using System.Collections.Generic;
namespace FloydAlgorithm {
class Program {
static void Main (string[] args) {
Graph W = new Graph(5);
W[0] = new Int32[] { 0, 1, int.MaxValue, 1, 5 };
W[1] = new Int32[] { 9, 0, 3, 2, int.MaxValue };
W[2] = new Int32[] { int.MaxValue, int.MaxValue, 0, 4, int.MaxValue };
W[3] = new Int32[] { int.MaxValue, int.MaxValue, 2, 0, 3 };
W[4] = new Int32[] { 3, int.MaxValue, int.MaxValue, int.MaxValue, 0 };
System.Console.Write(W.ToString());
Floyd floyd = new Floyd();
Graph D = new Graph(5);
floyd.floyd(5, W, ref D);
System.Console.WriteLine();
System.Console.Write(D.ToString());
int[,] P = new int[5, 5];
floyd.floyd2(5, W, ref D, ref P);
System.Console.WriteLine();
System.Console.Write(D.ToString());
System.Console.WriteLine();
System.Console.Write(Graph.ToString(P, 5));
System.Console.WriteLine();
List<int> result = new List<int>();
floyd.path(P, 4, 2, ref result);
foreach(int v in result) {
System.Console.WriteLine(v);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment