Computer >> कंप्यूटर >  >> प्रोग्रामिंग >> C++

एडमंड्स-कार्प एल्गोरिथम को लागू करने के लिए C++ प्रोग्राम

स्रोत और सिंक वर्टेक्स के बीच अधिकतम प्रवाह की गणना करने के लिए एडमंड्स-कार्प एल्गोरिदम को लागू करने के लिए यह एक सी ++ प्रोग्राम है।

एल्गोरिदम:

Begin
   function edmondsKarp() :
      initiate flow as 0.
      If there is an augmenting path from source to sink, add the path to flow.
      Return flow.
End

उदाहरण कोड

#include<cstdio>
#include<queue>
#include<cstring>
#include<vector>
#include<iostream>
using namespace std;
int c[10][10];
int flowPassed[10][10];
vector<int> g[10];
int parList[10];
int currentPathC[10];
int bfs(int sNode, int eNode)//breadth first search
{
   memset(parList, -1, sizeof(parList));
   memset(currentPathC, 0, sizeof(currentPathC));
   queue<int> q;//declare queue vector
   q.push(sNode);
   parList[sNode] = -1;//initialize parlist’s source node
   currentPathC[sNode] = 999;//initialize currentpath’s source node
   while(!q.empty())// if q is not empty
   {
      int currNode = q.front();
      q.pop();
      for(int i=0; i<g[currNode].size(); i++)
      {
         int to = g[currNode][i];
         if(parList[to] == -1)
         {
            if(c[currNode][to] - flowPassed[currNode][to] > 0)
            {
               parList[to] = currNode;
               currentPathC[to] = min(currentPathC[currNode],
               c[currNode][to] - flowPassed[currNode][to]);
               if(to == eNode)
               {
                  return currentPathC[eNode];
               }
               q.push(to);
            }
         }
      }
   }
   return 0;
}
int edmondsKarp(int sNode, int eNode)
{
   int maxFlow = 0;
   while(true)
   {
      int flow = bfs(sNode, eNode);
      if (flow == 0)
      {
         break;
      }
      maxFlow += flow;
      int currNode = eNode;
      while(currNode != sNode)
      {
         int prevNode = parList[currNode];
         flowPassed[prevNode][currNode] += flow;
         flowPassed[currNode][prevNode] -= flow;
         currNode = prevNode;
      }
   }
return maxFlow;
}
int main()
{
   int nodCount, edCount;
   cout<<"enter the number of nodes and edges\n";
   cin>>nodCount>>edCount;
   int source, sink;
   cout<<"enter the source and sink\n";
   cin>>source>>sink;
   for(int ed = 0; ed < edCount; ed++)
   {
      cout<<"enter the start and end vertex along with capacity\n";
      int from, to, cap;
      cin>>from>>to>>cap;
      c[from][to] = cap;
      g[from].push_back(to);
      g[to].push_back(from);
   }
   int maxFlow = edmondsKarp(source, sink);
   cout<<endl<<endl<<"Max Flow is:"<<maxFlow<<endl;
}

आउटपुट

enter the number of nodes and edges
6
7
enter the source and sink
0
4
enter the start and end vertex along with capacity
0
1
14
enter the start and end vertex along with capacity
2
4
10
enter the start and end vertex along with capacity
6
7
9
enter the start and end vertex along with capacity
5
2
10
enter the start and end vertex along with capacity
1
4
12
enter the start and end vertex along with capacity
2
0
15
enter the start and end vertex along with capacity
5
3
15
Max Flow is:12

  1. जॉनसन के एल्गोरिथम को लागू करने के लिए C++ प्रोग्राम

    यहां हम दो शीर्षों के बीच सबसे छोटा रास्ता खोजने के लिए जॉनसन का एल्गोरिदम देखेंगे। ग्राफ यहां दिया गया है। किनारों के बीच सबसे छोटा रास्ता नीचे जैसा है। यह कार्यक्रम अपनी लागतों के साथ शीर्षों की संख्या, किनारों की संख्या और किनारों की संख्या लेगा। इनपुट - कार्यक्षेत्र:3 किनारे:5 लागत के स

  1. कडाने के एल्गोरिथम को लागू करने के लिए C++ प्रोग्राम

    कडाने के एल्गोरिथ्म का उपयोग पूर्णांकों की एक सरणी से अधिकतम सबअरे योग का पता लगाने के लिए किया जाता है। यहां हम इस एल्गोरिथम को लागू करने के लिए C++ प्रोग्राम पर चर्चा करेंगे। एल्गोरिदम Begin Function kadanes(int array[], int length):    Initialize    highestMax = 0    

  1. सी ++ प्रोग्राम विगेनियर साइफर को लागू करने के लिए

    Vigenere Cipher, अल्फ़ाबेटिक टेक्स्ट को एन्क्रिप्ट करने की एक तरह की पॉलीअल्फ़ाबेटिक प्रतिस्थापन विधि है। इस विधि में एन्क्रिप्शन और डिक्रिप्शन के लिए Vigenere Cipher Table का उपयोग किया जाता है जिसमें A से Z तक के अक्षर 26 पंक्तियों में लिखे जाते हैं। एन्क्रिप्शन कुंजी: स्वागत है संदेश: यहिस्ट