如何在c编程中找到邻接矩阵中的'1'数?

时间:2018-03-20 18:32:57

标签: c adjacency-matrix

我有一个20 x 20的邻接矩阵,我想计算第i行和第j列中的1的数量,并将其显示为我的输出。我希望我的输出是这样的:

Number of edges for each node: (for example)
Vertex 0: 3
Vertex 1: 4
.
.
.
Vertex 19: 10

这是我的邻接矩阵代码:

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.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");
        }
    }
}

0 个答案:

没有答案