c语言如何在邻接矩阵

时间:2018-03-15 05:07:10

标签: c

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define M 20
#define N 20
int main()
{
    int i, j, x,a,b;
    int G[20][20] = { {0} };
    /*creaate random adjaceney matrix*/
    printf("==================================================\n");
    printf("Welcome to my Graph Processing tool!\n\n");

srand(time(NULL));
for (i = 0; i < M; i++) {
    for (j = 0; j < N; j++) {
        if (i == j) {
            G[i][j] = 0;
        }
        else {
            G[i][j] = rand() % 2;
            G[j][i] = G[i][j];
        }
    }
}
/*check whether the whole row equals to 0*/
for (j = 0; j < N; j++) {
    if (G[j] == 0) {
        x = rand() % 20 + 1;
        G[x][j] = G[j][x] = 1;
    }
    /*print the matrix G*/
    else
    {

        printf("The adjacency for graph G is\n");
        for (i = 0; i < M; i++) {
            for (j = 0; j < N; j++) {
                printf("%d ", G[i][j]);
            }
            printf("\n");
        }
    }
}

    /*all one-hop neighbors*/
    printf("\nList of one-hop neighbors:");
    for (i = 0; i < M;i++) {
        printf("\nVertex %d: ", i);
        for (j = 0; j < N; j++) {
            if (G[i][j] == 1) {

                printf("%d ",j);
            }
        }
    }
    printf("\n===================================\n\n");


    /*two-hop neighbors*/
    printf("List of two-hop neighbors:");
    /*for (i = 0; i < M; i++) {
        printf("\nVertex %d: ", i);
        for (j = 0; j < N; j++) {

            if (G[i][j] == 1) {                     
                    for (a = 0; a < 20; a++) {
                        if (G[a][j] == 1 && G[a][i] != 1 /*&& G[a][j] == 1 && G[i][a] != 1) {

                            printf("%d ", a);
                        }
                    }
                }
            }
        }
    printf("\n=================================================\n");*/

    for (i = 0; i < M; i++) {
        printf("\nVertex %d: ", i);
        for (j = 0; j < N; j++)/*; for(a=0;a<j;a++)*/ {
            for (a = 0; a < 20; a++) {
                if (G[i][j] == 1 && i != j) {
                    if (G[a][j] == 1 && G[a][i] != 1 && a != i && a != j && i != j) {
                        printf("%d ", a);
                    }

                }
                /*else {
                    printf("NONE");
                }*/
            }
        }

        }

    printf("\n============================================\n");


    system("pause");
    return 0;
}

第1个代码是生成20x20随机邻接矩阵 矩阵是G [i] [j] 它是对称的,矩阵中只有0和1 矩阵的主对角线都是0

然后我找到一跳邻居 单跳邻居部分我确定它是正确的

2跳邻居部分的答案是错误的

我的想法是,如果G [i] [j] = 1,G [a] [j] = 1且G [a] [i]!= 1,那么i和a将是两跳邻居

但如何循环? 如何使用for循环找到两跳邻居?

我在这里输出了图像 enter image description here

1 个答案:

答案 0 :(得分:0)

你的想法是正确的。为了避免过多的嵌套for循环,让我们编写一个函数来测试两个节点是否是两跳邻居:

#include <stdbool.h>

bool distance_is_two (int i, int j)
{
    if ((i == j) || (G[i][j] == 1)) {
        return false;
    }

    for (int k = 0; k < N; k++) {
        if ((k == i) || (k == j)) {
            continue;
        }

        if ((G[i][k] == 1) && (G[k][j] == 1)) {
            return true;
        }
    }

    return false;
}