【问题标题】:comparing two graphs by The degree of vertices通过顶点的度数比较两个图
【发布时间】:2015-07-29 14:05:02
【问题描述】:

我编写了一个代码来显示两个顶点度数相等的图。但我不知道为什么它不能正常工作并给出不正确的答案。例如:对于两个图,例如:

[0 1 1 0 0     [0 1 0 1 0 
 1 0 1 0 0      1 0 1 0 0
 1 0 0 1 0      0 1 0 1 0 
 0 0 1 0 1      0 0 1 0 1
 0 0 0 1 0]     0 0 0 1 0]

但它不应该写“两个图的顶点度数不同”的消息,而是写它。我不知道该怎么办。 如果可能,请指导我。

int temp = 0;
    int temp2 = 0;
    int gg = 1;
    for (int ia = 0; ia < m; ia++)
    {
        for (int ja = 0; ja < m; ja++)
        {
            if (j.f[ia][ja] == 1)
            {

                temp++;
            }

        }
        for (int i = 0; i < m; i++&&gg == 1)
        {
            for (int j = 0; j < m; j++&&gg == 1)
            {
                if (g.f[i][j] == 1)
                {

                    temp2++;
                }
            }
            if (temp == temp2)
                gg = 0;
            else
            {
                cout << "two graphs don't have the same degree of vertices";
                system("pause");
                exit(1);

            }


        }

【问题讨论】:

    标签: c++ graph


    【解决方案1】:

    试试这个:

    int temp=0;
    int temp2=0;
    
    for(int i = 0; i < m; i++)
      for(int j = 0; j < m; j++)
        temp += j.f[i][j];
    
    for(int i = 0; i < m; i++)
      for(int j = 0; j < m; j++)
        temp2 += g.f[i][j];
    
    if(temp!=temp2){
      cout<<"two graphics don't have the same degree of vertices";
      system("pause");
      exit(1);
    }
    

    这段代码的作用是首先检查两个顶点的度数,然后进行比较。您的代码正在做的是将第二个整个图的检查嵌套在第一个图的检查中,并且由于代码结构不佳而发生了奇怪的行为。完成两者的检查,然后进行比较。

    【讨论】:

    • 你到底想在这里做什么?我并不完全理解您所说的“顶点度”是什么意思。您是否检查每一行以查看它们是否有相同数量的 1?
    • 是的。我的意思是。您发送的第一个代码无法正常工作,但这个代码运行良好。谢谢
    • 既然这已经解决了你的问题,你能把它标记为正确答案吗?谢谢!
    猜你喜欢
    • 1970-01-01
    • 2019-09-18
    • 2016-03-29
    • 2011-08-24
    • 1970-01-01
    • 2014-12-30
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    相关资源
    最近更新 更多