如何使用C ++将边缘列表从文本文件导入二分图

时间:2015-09-28 14:58:33

标签: c++ parsing

我有256个输入/输出的边缘列表。我需要将这些列表值导入二分图。我是C ++的新手,不知道从哪里开始。我最终需要解析这个列表并转到输入行,该行将是" row"和输出值是"列"并将该值设置为1(对于true)。希望这是有道理的。

示例文本文件数据:

192, 16
120, 134
256, 87
122, 167
142, 97
157, 130
245, 232
223, 63

这是我的程序

// A C++ program to find maximal Bipartite matching.
#include <iostream>
#include <string.h>
#include <fstream>
using namespace std;

// M is number of inputs and N is number of outputs
#define M 256
#define N 256

// A DFS based recursive function that returns true if a
// matching for vertex u is possible
bool bpm(bool bpGraph[M][N], int u, bool seen[], int matchR[])
{
    // Try every output one by one
    for (int v = 0; v < N; v++)
    {
        // If input u is connected to output v and v is
        // not already connected to
        if (bpGraph[u][v] && !seen[v])
        {
            seen[v] = true; // Mark v as connected

            // If output 'v' is not assigned to an input OR
            // previously assigned input for output v (which is matchR[v]) 
            // has an alternate output available. 
            // Since v is marked as visited in the above line, matchR[v] 
            // in the following recursive call will not get output'v' again
            if (matchR[v] < 0 || bpm(bpGraph, matchR[v], seen, matchR))
            {
                matchR[v] = u;
                return true;
            }
        }
    }
    return false;
}

// Returns maximum number of matching from M to N
int maxBPM(bool bpGraph[M][N])
{
    // An array to keep track of the Inputs assigned to
    // Outputs. The value of matchR[i] is the Input number
    // assigned to input i, the value -1 indicates nothing is
    // assigned.
    int matchR[N];

    // Initially all outputs are available
    memset(matchR, -1, sizeof(matchR));

    int result = 0; // Count of matches
    for (int u = 0; u < M; u++)
    {
        // Mark all Inputs as not connected for next output.
        bool seen[N];
        memset(seen, 0, sizeof(seen));

        // Find if the input'u' can connect to an output
        if (bpm(bpGraph, u, seen, matchR))
            result++;
    }
    return result;
}

// Driver program to test above functions
int main()
{
    // Let us create a bpGraph shown in the above example



    bool bpGraph[M][N] = {};

    cout << "Maximum number networks "
         << maxBPM(bpGraph);

    return 0;
}

0 个答案:

没有答案