简体   繁体   中英

Adjacency list representation of graph is not printing adjacent nodes. Why?

In this adjacency list program, the nodes adjacent are not getting displayed properly. There could be a mistake in the insert function or in the display function. Each node has an edge list but the edge list isn't printed while displaying the graph. It could have an error somewhere.
I don't know where is it. It could be a semantic error.

//implementation of adjacency list representation

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

#define maxnodes 50

typedef struct node
{
    int data;
    struct node *link;    
}graph;

//inserting edge list for each node
void insert_node(graph **node_p,int i,int j)
{
    graph *temp;
    temp=(graph *)malloc(sizeof(graph));
    temp->data=j;
    temp->link=node_p[j];
    node_p[j]=temp;
}

void create_graph(graph **nodep)
{
    int i,j;
    while(1)
    {
        printf("enter source and destination nodes");
        scanf("%d %d",&i,&j);
        if(i==0 && j==0)
            break;
        insert_node(nodep,i,j);   
    }
}

void display(graph **nodep,int n)
{
    int i;
    graph *cur;
    for(i=1;i<=n;i++)
    {
        cur=nodep[i];
        printf("\n the nodes adjacent to node %d are=",i);
        while(cur!=NULL)
        {
          printf("\t%d",cur->data); 
          cur=cur->link; 
        }
    }
}

int main()
{
    int i;
    int n,ch;
    graph *nodelist[maxnodes];
    printf("enter number of number of nodes in the graph");
    scanf("%d",&n);

    for(i=1;i<=n;i++)
        nodelist[i]=NULL;

    while(1)
    {
        printf("\nenter option 1.create graph 2.display 3.exit");
        scanf("%d",&ch);
        switch(ch)
        {
           case 1:create_graph(nodelist);
                  break;
           case 2:display(nodelist,n);
                  break;
           case 3:exit(0); 
                   break; 
          default:
                 break;
        }
    }    
}

The logic inside your insert_node() function is completely wrong.

Assuming, you are building an undirected graph, if the user enters the pair i, j , you have to push node i to the adjacency list of j and also node j to that of node i .

Another major mistake is, you are always appending the newly created node, which you denote by temp to the first element of the adjacency list of the other node, which is true only in case the other node's neighbors' list was empty. You first have to check if(node_p[i] == NULL) . If that is true, you can directly append the temp to that node_p[i] , otherwise you have to follow link s to get to the last element of the adjacency list and append temp there.

The modified code looks like this

void insert_node(graph **node_p,int i,int j)
    {
        /* Add j to adj list of i */
        graph *temp;
        temp=(graph *)malloc(sizeof(graph));
        temp->data=j;
        temp->link=NULL;

        if(node_p[i] == NULL) /* First Node */
            node_p[i]=temp;
        else
        {
            graph *loc;
            loc = node_p[i];
            while(loc->link != NULL)
                loc = loc->link;
            loc->link = temp;
        }
        
        /*** COMMENT THIS PORTION IF THE GRAPH IS DIRECTED ***/
        /* Add i to adj list of j */
        graph *temp1;
        temp1 = (graph *)malloc(sizeof(graph));
        temp1->data = i;
        temp1->link = NULL;

        if(node_p[j] == NULL) /* First Node */
            node_p[j]=temp1;
        else
        {
            graph *loc;
            loc = node_p[j];
            while(loc->link != NULL)
                loc = loc->link;
            loc->link = temp1;
        }
    }

In case you are building a directed graph ie, i -> j and not j -> i , comment out the second half of the code that says /* Add i to adj list of j */ .

Using this. I got this output on the following graph of 5 nodes whose edges are = {1-2, 1-5, 1-3, 2-4}

enter number of number of nodes in the graph5

enter option 1.create graph 2.display 3.exit 1
enter source and destination nodes 1 2
enter source and destination nodes 1 5
enter source and destination nodes 1 3
enter source and destination nodes 2 4
enter source and destination nodes 0 0

enter option 1.create graph 2.display 3.exit 2

 the nodes adjacent to node 1 are=  2   5   3
 the nodes adjacent to node 2 are=  1   4
 the nodes adjacent to node 3 are=  1
 the nodes adjacent to node 4 are=  2
 the nodes adjacent to node 5 are=  1
enter option 1.create graph 2.display 3.exit3

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