Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 给出分段错误的welsh-powell算法代码 我在C++中找到了著名的威尔士鲍威尔算法代码的实现,只是为了检查它的工作,我编译了代码并执行它。不幸的是,这给了我一个分割错误,我无法解决这个问题。任何帮助都将不胜感激 #include <iostre_Algorithm_C++11_Segmentation Fault - Fatal编程技术网

Algorithm 给出分段错误的welsh-powell算法代码 我在C++中找到了著名的威尔士鲍威尔算法代码的实现,只是为了检查它的工作,我编译了代码并执行它。不幸的是,这给了我一个分割错误,我无法解决这个问题。任何帮助都将不胜感激 #include <iostre

Algorithm 给出分段错误的welsh-powell算法代码 我在C++中找到了著名的威尔士鲍威尔算法代码的实现,只是为了检查它的工作,我编译了代码并执行它。不幸的是,这给了我一个分割错误,我无法解决这个问题。任何帮助都将不胜感激 #include <iostre,algorithm,c++11,segmentation-fault,Algorithm,C++11,Segmentation Fault,给出分段错误的welsh-powell算法代码 我在C++中找到了著名的威尔士鲍威尔算法代码的实现,只是为了检查它的工作,我编译了代码并执行它。不幸的是,这给了我一个分割错误,我无法解决这个问题。任何帮助都将不胜感激 #include <iostream> #include <list> #include <algorithm> using namespace std; // A class that represents an undirected gra

给出分段错误的welsh-powell算法代码 我在C++中找到了著名的威尔士鲍威尔算法代码的实现,只是为了检查它的工作,我编译了代码并执行它。不幸的是,这给了我一个分割错误,我无法解决这个问题。任何帮助都将不胜感激

#include <iostream>
#include <list>
#include <algorithm>
using namespace std;

// A class that represents an undirected graph
class Graph
{
    int V;    // No. of vertices
    list<int> *adj;    // A dynamic array of adjacency lists
public:
    // Constructor and destructor
    Graph(int V)
    {
        this->V = V;
        adj = new list<int>[V];
    }
    ~Graph()
    {
        delete [] adj;
    }

    // function to add an edge to graph
    void addEdge(int v, int w);

    // Prints greedy coloring of the vertices
    void greedyColoring();
};

void Graph::addEdge(int v, int w)
{
    adj[v].push_back(w);
    adj[w].push_back(v);  // Note: the graph is undirected
}

struct vertexDegree
{
    int v;
    int degree;
};

bool compareByDegree(const vertexDegree& x, const vertexDegree& y)
{
    return x.degree > y.degree;
}

// Assigns colors (starting from 0) to all vertices and prints
// the assignment of colors
void Graph::greedyColoring()
{
    int result[V];

    // Initialize remaining V-1 vertices as unassigned
    for (int u = 1; u < V; u++)
        result[u] = -1;  // no color is assigned to u

    // A temporary array to store the available colors. True
    // value of available[cr] would mean that the color cr is
    // assigned to one of its adjacent vertices
    bool available[V];

    for (int cr = 0; cr < V; cr++)
        available[cr] = false;

    vertexDegree arr[V];
    for (int i = 0; i < V; i++)
    {
        arr[i].v = i;
        arr[i].degree = adj[i].size();
    }


    sort(arr, arr+V, compareByDegree);

    cout << "Sorted vertices \n";
    for (int i = 0; i <V; i++)
    {
        cout << arr[i].v << " ";
    }
    cout << "\n";

    // Assign the first color to first vertex in sorted array
    result[arr[0].v]  = 0;


    // Assign colors to remaining V-1 vertices
    for (int x = 1; x < V; x++)
    {
        int u = arr[x].v;

        // Process all adjacent vertices and flag their colors
        // as unavailable
        list<int>::iterator i;
        for (i = adj[u].begin(); i != adj[u].end(); ++i)
            if (result[*i] != -1)
                available[result[*i]] = true;

        // Find the first available color
        int cr;
        for (cr = 0; cr < V; cr++)
            if (available[cr] == false)
                break;

        result[u] = cr; // Assign the found color

        // Reset the values back to false for the next iteration
        for (i = adj[u].begin(); i != adj[u].end(); ++i)
            if (result[*i] != -1)
                available[result[*i]] = false;
    }

    // print the result
    for (int u = 0; u < V; u++)
        cout << "Vertex " << u << " --->  Color "
             << result[u] << endl;
}

// Driver program to test above function
int main()
{
    Graph g1(5);
    g1.addEdge(0, 1);
    g1.addEdge(0, 2);
    g1.addEdge(1, 2);
    g1.addEdge(1, 3);
    g1.addEdge(2, 3);
    g1.addEdge(3, 4);
    cout << "Coloring of Graph 1 \n";
    g1.greedyColoring();

    Graph g2(5);
    g2.addEdge(0, 1);
    g2.addEdge(0, 2);
    g2.addEdge(1, 2);
    g2.addEdge(1, 4);
    g2.addEdge(2, 4);
    g2.addEdge(4, 3);
    cout << "\nColoring of Graph 2 \n";
    g2.greedyColoring();

    return 0;
}   
#包括
#包括
#包括
使用名称空间std;
//表示无向图的类
类图
{
int V;//顶点数
list*adj;//邻接列表的动态数组
公众:
//构造函数和析构函数
图形(INTV)
{
这个->V=V;
adj=新列表[V];
}
~Graph()
{
删除[]形容词;
}
//函数将边添加到图形中
无效补遗(整数v,整数w);
//打印顶点的贪婪着色
void greedyColoring();
};
void图::addEdge(int v,int w)
{
形容词[v]。推回(w);
adj[w].向后推(v);//注意:图是无向的
}
结构顶点度
{
INTV;
智力度;
};
布尔比较度(常数顶点度和x,常数顶点度和y)
{
返回x度>y度;
}
//为所有顶点和打印指定颜色(从0开始)
//颜色的分配
void图::greedyColor()
{
int结果[V];
//将其余V-1顶点初始化为未指定顶点
对于(int u=1;u它对我来说行得通吗。你能试着隔离这个问题,看看你的系统上为什么会出现这个问题吗?好的,我会试试。我使用的是Ubuntu 14.04。它会为我打印排序的顶点,然后给出分段错误。@IulianPopescuI正在一个
macOS Sierra
上使用你的代码。这个过程运行时返回退出代码0,所以我认为这个问题与操作系统有关。