diff options
Diffstat (limited to 'src/Program/Analysis.cs')
-rw-r--r-- | src/Program/Analysis.cs | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/src/Program/Analysis.cs b/src/Program/Analysis.cs new file mode 100644 index 0000000..f817200 --- /dev/null +++ b/src/Program/Analysis.cs @@ -0,0 +1,89 @@ + +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Threading.Tasks; +using NetworkFlow; + +namespace Program +{ + public struct Result + { + public double MinCut; + public TimeSpan Runtime; + + public Result(double minCut, TimeSpan runtime) + { + MinCut = minCut; + Runtime = runtime; + } + + public override string ToString() => $"{MinCut} @ {Runtime.TotalMilliseconds}"; + } + + class Analysis + { + Graph _graph; + Result _exact = new(); + Result Exact { get => _exact.Equals(default(Result)) ? (_exact = CalculateExact()) : _exact; } + int Repetitions; + + public Analysis(Graph graph, int repetitions) + { + _graph = graph; + Repetitions = repetitions; + } + + public List<Result> Analize(int iterations) + { + List<Result> results = new(); + results.Add(Exact); + Console.WriteLine($"Exact {Exact}"); + for (int i = 0; i < Repetitions; i++) + { + results.Add(CalculateApprox(iterations)); + Console.WriteLine($"Contract_{i + 1} {results.Last()}"); + } + + return results; + } + + Result CalculateExact() + { + Stopwatch stopwatch = Stopwatch.StartNew(); + double result = double.MaxValue; + + // loop t for all nodes + foreach (var node in _graph.Nodes.Keys.ToList().Skip(1)) + { + var graphClone = (Graph)_graph.Clone(); + + // take min of current result and new result + double currentResult = graphClone.MaxFlow(0, node); + if (currentResult != 0) + result = Math.Min(result, currentResult); + } + + return new(result, stopwatch.Elapsed); + } + + Result CalculateApprox(int iterations) + { + Stopwatch stopwatch = Stopwatch.StartNew(); + double result = double.MaxValue; + + #region Parallel_Loop + Parallel.For(0, iterations, j => + { + var graphClone = (Graph)_graph.Clone(); + double currentResult = graphClone.Contraction(); + if (currentResult != 0) + result = Math.Min(result, currentResult); + }); + #endregion + + return new(result, stopwatch.Elapsed); + } + } +}
\ No newline at end of file |