首页 > 其他分享 >c: Ford - Fulkerson Algorithm

c: Ford - Fulkerson Algorithm

时间:2023-09-26 16:47:24浏览次数:41  
标签:capacity Algorithm int MAX param Ford color Fulkerson NODES

 

FordFulkersonAlgorithm.h  
/**
 * *****************************************************************************
 * @file        FordFulkersonAlgorithm.h
 * @brief       Ford - Fulkerson Algorithm in C Ford-Fulkerson算法(FFA)是一种 贪婪算法 ,用于计算流网络中的最大流量
 * IDE VSCODE C11
 * @author       (geovindu,Geovin Du,涂聚文)
 * @date        2023-09-26
 * @copyright   geovindu
 * *****************************************************************************
 */
#ifndef FORDFULKERSONALGORITHM_H
#define FORDFULKERSONALGORITHM_H



 
#include <stdio.h>
#include <stdlib.h>

#define A 0
#define B 1
#define C 2
#define MAX_NODES 10
#define O 1000000000



int flow[MAX_NODES][MAX_NODES];
int color[MAX_NODES];
int pred[MAX_NODES];

int head, tail;
int q[MAX_NODES + 2];

/**
 * @brief       
 * 
 * @param       x 
 * @param       y 
 * @return      int 
 */
int min(int x, int y);

/**
 * @brief       
 * 
 * @param       x 
 * @param       color 
 */
void enqueue(int x,int color[MAX_NODES]);

/**
 * @brief       
 * 
 * @param       color 
 * @return      int 
 */
int dequeue(int color[MAX_NODES]);

/**
 * @brief       
 * 
 * @param       start 
 * @param       target 
 * @param       color 
 * @param       pred 
 * @param       flow 
 * @param       capacity 
 * @return      int 
 */
int bfs(int start, int target,int color[MAX_NODES],int pred[MAX_NODES],int flow[MAX_NODES][MAX_NODES],int capacity[MAX_NODES][MAX_NODES]);


/**
 * @brief       
 * 
 * @param       source 
 * @param       sink 
 * @param       capacity 
 * @return      int 
 */
int FordFulkerson(int source, int sink,int capacity[MAX_NODES][MAX_NODES]);




#endif 

  

FordFulkersonAlgorithm.c
/**
 * *****************************************************************************
 * @file        FordFulkersonAlgorithm.c
 * @brief       Ford - Fulkerson Algorithm in C Ford-Fulkerson算法(FFA)是一种 贪婪算法 ,用于计算流网络中的最大流量
 * IDE VSCODE C11
 * @author       (geovindu,Geovin Du,涂聚文)
 * @date        2023-09-26
 * @copyright   geovindu
 * *****************************************************************************
 */

#include <stdio.h>
#include "include/FordFulkersonAlgorithm.h"



int fn=6;
int fe=7;
/**
 * @brief       
 * 
 * @param       x 
 * @param       y 
 * @return      int 
 */
int min(int x, int y) {
  return x < y ? x : y;
}



/**
 * @brief       
 * 
 * @param       x 
 * @param       color 
 */
void enqueue(int x,int color[MAX_NODES]) {
  q[tail] = x;
  tail++;
  color[x] = B;
}
/**
 * @brief       
 * 
 * @param       color 
 * @return      int 
 */
int dequeue(int color[MAX_NODES]) {
  int x = q[head];
  head++;
  color[x] = C;
  return x;
}

// Using BFS as a searching algorithm
/**
 * @brief       
 * 
 * @param       start 
 * @param       target 
 * @param       color 
 * @param       pred 
 * @param       flow 
 * @param       capacity 
 * @return      int 
 */
int bfs(int start, int target,int color[MAX_NODES],int pred[MAX_NODES],int flow[MAX_NODES][MAX_NODES],int capacity[MAX_NODES][MAX_NODES]) {
  int u, v;
  for (u = 0; u < fn; u++) {
    color[u] = A;
  }
  head = tail = 0;
  enqueue(start,color);
  pred[start] = -1;
  while (head != tail) {
    u = dequeue(color);
    for (v = 0; v < fn; v++) {
      if (color[v] == A && capacity[u][v] - flow[u][v] > 0) {
        enqueue(v,color);
        pred[v] = u;
      }
    }
  }
  return color[target] == C;
}

// Applying fordfulkerson algorithm
/**
 * @brief       
 * 
 * @param       source 
 * @param       sink 
 * @param       capacity 
 * @return      int 
 */
int FordFulkerson(int source, int sink,int capacity[MAX_NODES][MAX_NODES]) {
  int i, j, u;
  int max_flow = 0;
  for (i = 0; i < fn; i++) {
    for (j = 0; j < fn; j++) {
      flow[i][j] = 0;
    }
  }

  // Updating the residual values of edges
  while (bfs(source, sink,color,pred,flow,capacity)) {
    int increment = O;
    for (u = fn - 1; pred[u] >= 0; u = pred[u]) {
      increment = min(increment, capacity[pred[u]][u] - flow[pred[u]][u]);
    }
    for (u = fn - 1; pred[u] >= 0; u = pred[u]) {
      flow[pred[u]][u] += increment;
      flow[u][pred[u]] -= increment;
    }
    // Adding the path flows
    max_flow += increment;
  }
  return max_flow;
}

  

调用:

   //14 Ford - Fulkerson algorith 
    /**/

    printf("\n14 Ford - Fulkerson algorith \n");

    int capacity[10][10];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
        capacity[i][j] = 0;
        }
    }
    capacity[0][1] = 8;
    capacity[0][4] = 3;
    capacity[1][2] = 9;
    capacity[2][4] = 7;
    capacity[2][5] = 2;
    capacity[3][5] = 5;
    capacity[4][2] = 7;
    capacity[4][3] = 4;

    int s = 0, t =5;
    printf("\nMax Flow: %d\n", FordFulkerson(s, t,capacity));

  

 

 

标签:capacity,Algorithm,int,MAX,param,Ford,color,Fulkerson,NODES
From: https://www.cnblogs.com/geovindu/p/17730405.html

相关文章

  • c: Kruskal Algorithm
    KruskalAlgorithm.h /*****************************************************************//***\fileKruskalAlgorithm.h*\briefKruskalAlgorithm克鲁斯卡尔算法*IDE:VSCODEc11https://github.com/hustcc/JS-Sorting-Algorithm/blob/master/2.selectio......
  • python: Sorting Algorithms
     #encoding:utf-8#版权所有2023涂聚文有限公司#许可信息查看:PythonSortingAlgorithms#描述:*https://www.programiz.com/dsa/counting-sort#*https://www.geeksforgeeks.org/sorting-algorithms/#Author:geovindu,GeovinDu涂聚文.#IDE:PyC......
  • python: Essential Algorithms
     #encoding:utf-8#版权所有2023涂聚文有限公司#许可信息查看:#描述:#Author:geovindu,GeovinDu涂聚文.#IDE:PyCharm2023.1python311#Datetime:2023/9/2121:28#User:geovindu#Product:PyCharm#Project:EssentialAlgor......
  • c: Sorting Algorithms
    SortAlgorithm.h /*****************************************************************//***\fileSortAlgorithm.h*\brief业务操作方法*VSCODEc11https://github.com/hustcc/JS-Sorting-Algorithm/blob/master/2.selectionSort.md*https://www.progra......
  • 论文阅读: Co-design Hardware and Algorithm for Vector Search
    1.Introduction介绍一下论文背景,向量检索常用于搜索引擎,推荐系统,LLM和科学计算等对应的常用的硬件向量检索方法,IVF-PQ其中IVF:将多个向量聚类,PQ将向量压缩而为了最大化IVF-PQ的效果,也会面临很多的挑战在芯片设计的过程中,会遇到针对六个阶段如何设计合适的微架构?如何将有......
  • Proj. CRR Paper Reading: Optimal Speedup of Las Vegas Algorithms, Adaptive resta
    TitleAdaptiverestartforstochasticsynthesisPLDI2021TaskDistributethepowerbetweenmultiplerunsinstochasticprogramsynthesistoaccelerateHere,astochasticprogramsynthesisprogramcanbesummarizedasfollows:Givenasetof<input,ou......
  • 解决npm ERR! Cannot read properties of null (reading ‘pickAlgorithm‘)报错问题
    转载自:https://www.cnblogs.com/zhyp/p/16920380.html=========解决方法:在终端中运行命令:npmcacheclear--force然后重新运行npmi命令,再次安装安装完成,没有出现报错npmrunserve运行项目,项目可以正常启动了。  安装vueCLI失败后,百度得知在终端执行命令:npmcleanc......
  • Data structure and algorithm-Two
    B树                扩容           找出不含重复字符的最长字串的长度 字母异位词分组   优化用一个长度26的整数数组来标识 ArrayKey的构造方法   判断是否存在重复元素 ......
  • 【MySQL 8.0】新特性:ALTER TABLE … ALGORITHM=INSTANT
    MySQL8.0.29之前,在线DDL操作中即时添加列只能添加在表的最后一列MySQL8.0.29扩展了对ALTERTABLE…ALGORITHM=INSTANT的支持:用户可以在表的任何位置即时添加列、即时删除列、添加列时评估行大小限制(root@node01)>altertablecustomeraddcolumnc_commentvarcha......
  • 基于斑马优化算法Zebra Optimization Algorithm求解单目标优化问题附matlab代码
    ✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。......