【问题标题】:Graphs: for an adjacency matrix图:用于邻接矩阵
【发布时间】:2021-02-02 14:47:35
【问题描述】:

我要写一个程序,它将以邻接表和邻接矩阵的形式显示图形结果。我已经通过 YT 上的教程指导自己如何实现邻接列表,并使用当前存储的数据(用户正在介绍自己,例如节点数、边数、边连接的边数以及哪一个)我想知道/了解如何使用已经存储的数据来构建邻接矩阵。

#include <stdio.h>
#include <stdlib.h>

struct node
{
    int data;
    struct node *next;
};
void read_graf(struct node *ad[], int no_of_nodes);

void print_graf(struct node *ad[], int no_of_nodes);

int main()
{
   int op;
   int i,j,k, nodes;
   begin:
   printf("Type in the number of edges: ");
   scanf("%d", &nodes);
   struct node *adj[nodes];
   for (i=0;i<nodes;i++)
        adj[i] = NULL;
   read_graf(adj,nodes);
   print_graf(adj,nodes);

   printf("\nDo you want to try again? 1 for yes and 0 for no: ");
   scanf("%d", &op);
   if (op==1)
    goto begin;
   else
    {
    printf("Thank you for visiting! :)");
    exit(0);
    }
   return 0;
}

void read_graf(struct node *ad[], int no_of_nodes)
{
    struct node *new_node;
    int i,j,k, val;
    for (i=0;i<no_of_nodes;i++)
    {
        struct node *last= NULL;
        printf("\n To how many edges is edge %d connected to: ", i+1);
        scanf("%d", &k);
        for (j=0;j<k;j++)
        {
            printf("To which edges is it connected : ");
            scanf("%d", &val);
            new_node = (struct node *)malloc(sizeof(struct node*));
            new_node->data = val;
            new_node->next = NULL;
            if(ad[i]==NULL)
                ad[i]= new_node;
            else
            last->next = new_node;
            last = new_node;
        }
    }
}

void print_graf(struct node *ad[], int no_of_nodes)
{
    struct node *ptr = NULL;
    int i,j;
    for(i=0;i<no_of_nodes;i++)
    {
        ptr = ad[i];
        printf("\n x%d : ", i+1);
        while(ptr != NULL)
        {
            printf("%d,\t", ptr->data);
            ptr = ptr->next;
        }
        printf("0");
    }
}

【问题讨论】:

  • 如果data代表节点连接的边的索引,考虑print_graf的内循环,使其成为所有节点j的循环。如果j != ptr-&gt;data1 否则应该打印0 而不是打印ptr-&gt;data 的值(在这种情况下更新ptr)。
  • @Bob__ 抱歉,但我不太明白这一点。 i 应该创建一个 i 和 j 的双循环,其中 i 应该包含 ptr = ad[i][j],然后在这个循环中,如果 j != ptr->data 则 a 应该使用 if 语句打印 0,否则打印 1 ?我试过这个,void print_graf2(struct node *ad[], int no_of_nodes) { struct node *ptr = NULL; int i,j; for(i=0; i&lt;no_of_nodes; i++) { for(j=0; j&lt;no_of_nodes; j++) { ptr = ad[i][j]; if(j!=ptr-&gt;data) { printf("0"); } else { printf("1"); } } } }
  • @Bob__ 但它给了我这个 |error: incompatible types when assignment to type 'struct node *' from type 'struct node'|

标签: c pointers matrix graph


【解决方案1】:

如果您只需要打印出邻接矩阵并假设1 将表示节点 i 和节点 j 之间的连接,则只需稍微修改print_graf函数内部的循环。

void print_as_mat(struct node *ad[], int no_of_nodes)
{
    struct node *ptr = NULL;
    
    for(int i = 0; i < no_of_nodes; ++i)
    {
        ptr = ad[i];

        // Loop over all the possible nodes
        for(int j = 0; j < no_of_nodes; ++j)
        {
            if ( ptr  &&  ptr->data == j ) {
                //        ^^^^^^^^^^^^^^ Check if the index correspond to the 
                //                       current node in the adjacency list.
                printf(" 1");

                // update the current node in the list, but only here.
                ptr = ptr->next;
            }
            else {
                printf(" 0");
            }
        }
        // The row is completed.
        putchar('\n');
    }
}

【讨论】:

  • 我现在试过了,但是当矩阵看起来像这样时:F(x1)={x2,x3};F(x2)={x3};F(x3)={x1} x1 x2 x3 x1:0 1 1 x2:0 0 1 x3:1 0 0 It ends up looking like this: 0 0 1 0 0 0 0 1 0
  • 啊,我想我明白了,它会从它打印 1 到 j+1 的位置,它应该是
  • 但是,tbh,我真的不明白为什么会这样(xd),我尝试了不同的方法,但没有帮助
  • @Vlad 如果您从 1 开始计算边数(这与 C 的从零开始的索引规则相冲突),您需要 change the condition。我希望“算法”足够清楚,它只是扫描每个可能的索引并将其与相邻节点列表中的当前节点进行比较。如果存在连接,则打印 1 并且 移动到列表的下一个元素,否则打印 0。当检查所有可能的索引(连接)时,数组的以下元素(下一个列表节点)被考虑。
  • 天哪。我现在觉得自己像个白痴,我什么都试过了,但是“j + 1”,我猜天才隐藏在简单中:)。感谢您的帮助,非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-24
  • 2016-04-06
  • 1970-01-01
  • 2018-12-02
  • 1970-01-01
相关资源
最近更新 更多