简体   繁体   中英

Graphs: for an adjacency matrix

I have to write a program, whitch will show out graph results in the form of an adjacency list and adjacency matrix. I've guided myself by a tutorial on YT on how to implement the adjency list, and with the current stored data (whitch the user is introducing himself, like the number of nodes, edges, to how many edges a edge is connected and to which one) and I want to know/understand how, with the already stored data, to build a adjacency matrix.

#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");
    }
}

If you only need to print out the adjacency matrix and assuming that a 1 would represent a connection between node i and node j , you just have to slightly modify the inner loop inside the print_graf function.

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');
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM