Movatterモバイル変換


[0]ホーム

URL:


Data Structures & Algorithms Tutorial

What Is an Augmenting Path?



Anaugmenting path in a graph is just a way to get from thesource to thesink where we can push more flow. In a flow network, the goal is to find these paths and increase the flow along them, ultimately making the total flow as big as possible.

These paths are essential in finding themaximum flow from the source to the sink in flow networks.

Augmenting Path Example

Lets break it down with an example. Imagine we have a simple flow network with a source, a sink, and a few intermediate nodes. Each edge between nodes has acapacity (how much it can carry) andflow (how much is currently flowing). Here's how it looks:

In this network, nodeA is thesource and nodeF is thesink. The capacities and flows of the edges are as follows:

  • Edge AB: Capacity = 10, Flow = 5
  • Edge AC: Capacity = 15, Flow = 10
  • Edge BD: Capacity = 5, Flow = 3
  • Edge BE: Capacity = 10, Flow = 7
  • Edge CF: Capacity = 10, Flow = 8

Thisflow network shows howdata flows fromA toF. The key here is to push as muchflow as possible while respecting thecapacities of the edges. In this case, themaximum flow fromA toF is15, which is achieved by sending5 units of flow through edgeAB,10 units through edgeAC, and5 units through edgeCF. But theres still room for improvement by findingaugmenting paths to push more flow through!

Augmenting Path Algorithms

Now, lets talk about some algorithms that help us find these augmenting paths. There are a few options, and they work in different ways to maximize the flow:

  • Ford-Fulkerson Algorithm: This is a basic and simple approach, but it might not be the fastest.
  • Edmonds-Karp Algorithm: This one is more efficient because it uses BFS to find augmenting paths.
  • Dinic's Algorithm: A more advanced method that uses a layered approach for better performance.
  • Push-Relabel Algorithm: Instead of finding paths directly, this method pushes excess flow and adjusts labels to optimize the flow.

Implemention of Augmenting Paths

Now that we know whataugmenting paths are and how they help us increase theflow in anetwork, lets see how we can implement them in code. Heres a simple example of theFord-Fulkerson algorithm -

Algorithm for Augmenting Paths

1. Create a residual graph with the same capacities as the original graph.2. While theres an augmenting path from the source to the sink:   a. Find the path using a search algorithm (like BFS).   b. Determine the maximum flow that can be pushed through the path.   c. Update the residual graph by reducing the capacities of forward edges and increasing the capacities of backward edges.3. Return the maximum flow found.

Code for Augmenting Paths

Heres a simple code snippet that uses the Ford-Fulkerson algorithm to find the maximum flow in a flow network:

#include <stdio.h>#include <limits.h>#include <stdbool.h>#define V 6bool bfs(int rGraph[V][V], int s, int t, int parent[]) {   bool visited[V] = {false};   int queue[V];   int front = 0, rear = 0;   queue[rear++] = s;   visited[s] = true;   parent[s] = -1;   while (front != rear) {      int u = queue[front++];      for (int v = 0; v < V; v++) {         if (!visited[v] && rGraph[u][v] > 0) {            queue[rear++] = v;            parent[v] = u;            visited[v] = true;         }      }   }    return visited[t];}int fordFulkerson(int graph[V][V], int s, int t){   int u, v;   int rGraph[V][V];   for(u = 0; u < V; u++){      for(v = 0; v < V; v++){         rGraph[u][v] = graph[u][v];      }   }   int parent[V];   int max_flow = 0;   while(bfs(rGraph, s, t, parent)){      int path_flow = INT_MAX;      for(v = t; v != s; v = parent[v]){         u = parent[v];         path_flow = path_flow < rGraph[u][v] ? path_flow : rGraph[u][v];      }      for(v = t; v != s; v = parent[v]){         u = parent[v];         rGraph[u][v] -= path_flow;         rGraph[v][u] += path_flow;      }      max_flow += path_flow;   }   return max_flow;}int main(){   int graph[V][V] = {      {0, 10, 15, 0, 0, 0},      {0, 0, 0, 5, 10, 0},      {0, 0, 0, 0, 0, 10},      {0, 0, 0, 0, 0, 5},      {0, 0, 0, 0, 0, 10},      {0, 0, 0, 0, 0, 0}   };   printf("The maximum possible flow is %d\n", fordFulkerson(graph, 0, 5));   return 0;}

Output

Following is the output of the above code:

The maximum possible flow is 20
#include <iostream>#include <limits.h>#include <queue>using namespace std;#define V 6bool bfs(int rGraph[V][V], int s, int t, int parent[]) {   bool visited[V] = {false};   queue<int> q;   q.push(s);   visited[s] = true;   parent[s] = -1;   while (!q.empty()) {      int u = q.front();      q.pop();      for (int v = 0; v < V; v++) {         if (!visited[v] && rGraph[u][v] > 0) {            q.push(v);            parent[v] = u;            visited[v] = true;         }      }   }    return visited[t];}int fordFulkerson(int graph[V][V], int s, int t){   int u, v;   int rGraph[V][V];   for(u = 0; u < V; u++){      for(v = 0; v < V; v++){         rGraph[u][v] = graph[u][v];      }   }   int parent[V];   int max_flow = 0;   while(bfs(rGraph, s, t, parent)){      int path_flow = INT_MAX;      for(v = t; v != s; v = parent[v]){         u = parent[v];         path_flow = min(path_flow, rGraph[u][v]);      }      for(v = t; v != s; v = parent[v]){         u = parent[v];         rGraph[u][v] -= path_flow;         rGraph[v][u] += path_flow;      }      max_flow += path_flow;   }   return max_flow;}int main(){   int graph[V][V] = {      {0, 10, 15, 0, 0, 0},      {0, 0, 0, 5, 10, 0},      {0, 0, 0, 0, 0, 10},      {0, 0, 0, 0, 0, 5},      {0, 0, 0, 0, 0, 10},      {0, 0, 0, 0, 0, 0}   };   cout << "The maximum possible flow is " << fordFulkerson(graph, 0, 5) << endl;   return 0;}

Output

The maximum possible flow is 20
import java.util.LinkedList;import java.util.Queue;public class Main {   static final int V = 6;   static boolean bfs(int rGraph[][], int s, int t, int parent[]) {      boolean visited[] = new boolean[V];      Queue<Integer> q = new LinkedList<>();      q.add(s);      visited[s] = true;      parent[s] = -1;      while (!q.isEmpty()) {         int u = q.poll();         for (int v = 0; v < V; v++) {            if (!visited[v] && rGraph[u][v] > 0) {               q.add(v);               parent[v] = u;               visited[v] = true;            }         }      }      return visited[t];   }   static int fordFulkerson(int graph[][], int s, int t) {      int u, v;      int rGraph[][] = new int[V][V];      for (u = 0; u < V; u++) {         for (v = 0; v < V; v++) {            rGraph[u][v] = graph[u][v];         }      }      int parent[] = new int[V];      int max_flow = 0;      while (bfs(rGraph, s, t, parent)) {         int path_flow = Integer.MAX_VALUE;         for (v = t; v != s; v = parent[v]) {            u = parent[v];            path_flow = Math.min(path_flow, rGraph[u][v]);         }         for (v = t; v != s; v = parent[v]) {            u = parent[v];            rGraph[u][v] -= path_flow;            rGraph[v][u] += path_flow;         }         max_flow += path_flow;      }      return max_flow;   }   public static void main(String[] args) {      int graph[][] = {         {0, 10, 15, 0, 0, 0},         {0, 0, 0, 5, 10, 0},         {0, 0, 0, 0, 0, 10},         {0, 0, 0, 0, 0, 5},         {0, 0, 0, 0, 0, 10},         {0, 0, 0, 0, 0, 0}};      System.out.println("The maximum possible flow is " + fordFulkerson(graph, 0, 5));   }}

Output

The maximum possible flow is 20
from collections import dequeV = 6def bfs(rGraph, s, t, parent):    visited = [False] * V    q = deque()    q.append(s)    visited[s] = True    parent[s] = -1    while q:        u = q.popleft()        for v in range(V):            if not visited[v] and rGraph[u][v] > 0:                q.append(v)                parent[v] = u                visited[v] = True    return visited[t]def fordFulkerson(graph, s, t):    rGraph = [[0] * V for _ in range(V)]    for u in range(V):        for v in range(V):            rGraph[u][v] = graph[u][v]    parent = [0] * V    max_flow = 0    while bfs(rGraph, s, t, parent):        path_flow = float('inf')        v = t        while v != s:            u = parent[v]            path_flow = min(path_flow, rGraph[u][v])            v = parent[v]        v = t        while v != s:            u = parent[v]            rGraph[u][v] -= path_flow            rGraph[v][u] += path_flow            v = parent[v]        max_flow += path_flow    return max_flowgraph = [    [0, 10, 15, 0, 0, 0],    [0, 0, 0, 5, 10, 0],    [0, 0, 0, 0, 0, 10],    [0, 0, 0, 0, 0, 5],    [0, 0, 0, 0, 0, 10],    [0, 0, 0, 0, 0, 0]]print("The maximum possible flow is", fordFulkerson(graph, 0, 5))

Output

The maximum possible flow is 20

Each of these algorithms helps us find the best paths to push moreflow through thenetwork and, eventually, determine themaximum flow possible.

Real-Life Uses of Augmenting Paths

Augmenting paths are super useful in many real-world situations, especially when we need to manage theflow of things likedata,traffic, ormaterials. Here are some areas where these concepts come in handy:

  • Transportation Networks: Augmenting paths can be used to model howtraffic moves throughroads,railways, and othertransport systems. This helps us optimizetraffic flow.
  • Communication Networks: Incomputer andtelecom networks, augmenting paths can help managedata flow and figure out the most efficient way to routeinformation.
  • Supply Chain Networks: Insupply chains, augmenting paths help track theflow ofgoods andmaterials, ensuring smooth movement fromsuppliers tocustomers.
  • Fluid Systems: Augmenting paths can also be applied in systems that moveliquids orgases, likepipes orwater distribution systems, to make sure everything flows as efficiently as possible.

By using augmenting paths, engineers can better understand how things flow through these systems and find ways to improve them. Whether itstransportation,communication,supply chains, orfluid systems, these techniques help us optimize howresources move and find the most efficient solutions!

Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp