public static int EdmondsKarp(IGraph graph, int source, int target) { int maxFlow = 0; //create flow graph MatrixGraph flow = new MatrixGraph(graph.VerticesCount, true, graph.GetEdges()); foreach (Edge edge in flow.GetEdges()) { flow.UpdateEdgeWeight(edge.From, edge.To, -edge.Weight); } //create residual graph MatrixGraph residual = new MatrixGraph(graph.VerticesCount, true, graph.GetEdges()); while (true) { List <Edge> augmentingPath = BFS(residual, source, target); if (augmentingPath == null) { return(maxFlow); } int minWeight = int.MaxValue; foreach (Edge edge in augmentingPath) { if (edge.Weight < minWeight) { minWeight = edge.Weight; } } maxFlow += minWeight; //uptade residual and flow graph foreach (Edge edge in augmentingPath) { if (residual.DoesEdgeExist(edge.From, edge.To)) { residual.UpdateEdgeWeight(edge.From, edge.To, -minWeight); if (residual.GetEdgeWeight(edge.From, edge.To) == 0) { residual.DeleteEdge(edge.From, edge.To); } } if (!residual.DoesEdgeExist(edge.To, edge.From)) { residual.AddEdge(new Edge(edge.To, edge.From)); } residual.UpdateEdgeWeight(edge.To, edge.From, minWeight); flow.UpdateEdgeWeight(edge.From, edge.To, minWeight); flow.UpdateEdgeWeight(edge.To, edge.From, -minWeight); } } }
public static int ChceckConnectivity(IGraph graph) { int connectivity = 0; if (!Algorithms.isConnected(graph)) { return(connectivity); } //create flow network MatrixGraph flowNetworkGraph = new MatrixGraph(graph.VerticesCount, true); foreach (Edge edge in graph.GetEdges()) { flowNetworkGraph.AddEdge(new Edge(edge.From, edge.To, 1)); flowNetworkGraph.AddEdge(new Edge(edge.To, edge.From, 1)); } //pick random vertex Random rand = new Random(); int source = rand.Next((int)flowNetworkGraph.VerticesCount); int minMaxFlow = int.MaxValue; for (int target = 0; target < flowNetworkGraph.VerticesCount; target++) { if (target == source) { continue; } //find max flow between s and t int maxFlow = Algorithms.EdmondsKarp(flowNetworkGraph, source, target); if (maxFlow < minMaxFlow) { minMaxFlow = maxFlow; } } connectivity = minMaxFlow; return(connectivity); }