有没有一种方法可以创建图并对其进行操作

时间:2018-12-30 13:24:21

标签: c++

想做什么!

  1. 用户可以输入任意数量的对。
  2. 然后我必须从成对的图中创建一个图形,其中顶点是个人,并且通过边与其他人相连,他与之成对。
  3. 然后,我希望用顶点替换边,反之亦然。

我做了什么!

  1. 用于未知数量的输入。我用向量来存储对。同时输入成对输入。我实现了一个循环,使我在整个配对列表中具有唯一的人数。
  2. 我的问题是要用它制作一个图形。我已经尝试过使用向量数组来达到这个目的。但是它显示访问冲突错误。我不明白。
  3. 我的代码是否错误,还是应该使用其他数据类型(如列表或其他类型)?但是我必须确保它的长度不是固定的(例如数组)。

示例 Desired process, i want to implement

我的代码尚未

      //function to add edge in graph
      void addEdge(std::vector<char> adj[], char u, char v)
      {
       adj[u].push_back(v);
       adj[v].push_back(u);
      }

       //function to print graph
       void printGraph(std::vector<char> adj[], int V)
         {
       for (int v = 0; v < V; ++v)
       {
        std::cout << "\n Adjacency list of vertex "
        << v << "\n head ";
        for (auto x : adj[v])
            std::cout << "-> " << x;
        printf("\n");
        } 
           }

         int main()
       {    
       int numberOfUniquePeople = 0, numberOfPairs = 0, index = 0, newIndex 
       = 1;
         std::vector<char> pair; //original pair of list
         std::vector<char> uniqueElements; //unique number of people in 
             entire list
         char c = '1';
        while (c != '0')
       {
    int flag = 0;
    std::cout << "Enter the pair : ";
    std::cin >> c; //input for first person in pair
    if (c == '0')
        break;
    else
        pair.push_back(c);

    //start check for unique people
    for (auto i = uniqueElements.begin(); i != uniqueElements.end(); i++)
    {
        if(*i == c)
            flag = 1;
    }
    if (flag != 1)
    {
        uniqueElements.push_back(c);
        numberOfUniquePeople++;
    }
    //end check for unique people

    flag = 0;
    std::cin >> c; //input for second person in the pair
    pair.push_back(c);

    //start check for unique people
    for (auto i = uniqueElements.begin(); i != uniqueElements.end(); i++)
    {
        if (*i == c)
            flag = 1;
    }
    if (flag != 1)
    {
        uniqueElements.push_back(c);
        numberOfUniquePeople++;
    }
    //end check for unique people
    numberOfPairs++;
    }
    //creating graph
    std::vector<char> adj[50];
    for (auto i = pair.begin(); i != pair.end(); i++)
    {
            char c = *i;
            char d = *(i+1);
            addEdge(adj,c,d);
    }

     printGraph(adj, 50);

     system("PAUSE");
        }     

0 个答案:

没有答案
相关问题