0
votes

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");
    }
}
1
If data represents the index of the edge to which the node is connected, consider the inner loop of print_graf and make it a loop over all the nodes j. Instead of printing the value of ptr->data it should print a 0 if j != ptr->data or 1 otherwise (and in this case update ptr). - Bob__
@Bob__ sorry but i didn't quite understand this. i should make a double loop of i and j in which i should include ptr = ad[i][j], then in this loop a should use an if statement to print 0 if j != ptr->data or 1 if otherwise? I tried this, void print_graf2(struct node *ad[], int no_of_nodes) { struct node *ptr = NULL; int i,j; for(i=0; i<no_of_nodes; i++) { for(j=0; j<no_of_nodes; j++) { ptr = ad[i][j]; if(j!=ptr->data) { printf("0"); } else { printf("1"); } } } } - Vlad
@Bob__ but it gave me this |error: incompatible types when assigning to type 'struct node *' from type 'struct node'| - Vlad

1 Answers

1
votes

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