Pages

Find the number of components in the Graph

Given a graph using adjacency list representation, find the number of components in the graph. The input should be read from a file. A simple input file can be found here.

Program Code 
/*Finding Number of Components in a graph.*/
#include<stdio.h>
#include<stdlib.h>

#include<string.h>

/*Defining the Macros*/
#define TRUE 1
#define FALSE 0

/*Defining the edge structer for the Adj link list*/
typedef struct adj_list_edge{
    int vertex;
    struct adj_list_edge* next;
} adj_list_edge;

/*Defining the structer for the Adj link list*/
typedef struct adj_list{
    struct adj_list_edge* head;
}adj_list;

/*Defining the structer for the Graph*/
typedef struct graph
{
    int no_vertex;
    struct adj_list* array;
}graph;


/****** Declaration of utility functions for the adjacency list Manupulation******/
struct graph* create_graph(int vertex);
void insert_edge(struct graph* graph, int node, int vertex);
void display_adj_list(struct graph* graph);
void compute_graph_components(struct graph* graph);
int is_vertex_exists(int *vertex_array, int nodes);
int is_in_R(int *comp_elements, int element, int nodes);
void display_comp_elements(int *comp_elements, int nodes, int count);
void remove_from_vertex_array(int *vertex_array, int *comp_elements, int nodes);


int main()
{
    const char filename[] = "Graph_adj_list.csv";
    size_t i, j, k, node_count, node_src, node_dest ;
    char buffer[BUFSIZ], *ptr;
    int arr[100] = {0};
   
    /*File Pointers to read the Adjcency List file.*/   
    FILE *file_pointer;
   
    /* Count number of lines in Adj List CVS file.*/
    file_pointer = fopen(filename, "r");
    if ( file_pointer )
    {
         for ( node_count = 0; fgets(buffer, sizeof buffer, file_pointer); ++node_count )
         {  }
    }
    else /* fopen() returned NULL */
    {
         perror(filename);
    }
    fclose(file_pointer);


    printf("\n\nNumber of Nodes in the Graph:  %d\n", node_count);
    struct graph* graph = create_graph(node_count);

    /* Reading Preferences from the student.CVS file and storing it in "student_pref_list".*/
    file_pointer = fopen(filename, "r");
    if ( file_pointer )
    {

        /*Read each token/vertex value from the line*/
         for ( i = 0; fgets(buffer, sizeof buffer, file_pointer); ++i )
         {
        j=0;
        ptr = (char *)strtok(buffer, "->");

        node_src = strtol(ptr, (char **)NULL, 10);
        node_src--;

        ptr = (char *)strtok(NULL,"->");

                 while(ptr!=NULL)
                 {
            node_dest = strtol(ptr, (char **)NULL, 10);
                        arr[j] = node_dest;
            j++;
            //insert_edge( graph, node_src, node_dest);

            ptr = (char *)strtok(NULL,"->");
        }

        while(j>0)
        {  
            if(arr[j-1]!=0)
                insert_edge( graph, node_src, arr[j-1]);
            j--;  
        }
         }
    }
    else /* fopen() returned NULL */
    {
         perror(filename);
    }
    fclose(file_pointer);
   

    /*Display the Graph Adj list*/
    printf("\n******************************\n\tGraph Adj List \n******************************");
    display_adj_list(graph);
    printf("\n\n");
   
    /*To Compute the Number of Components in the graph*/
    compute_graph_components( graph );

    return 0;
}
/*---------------------*/
/*End of Main Function*/
/*---------------------*/



/*Function defination to create a graph with ajd lists*/
struct graph* create_graph(int vertex)
{  
      
       struct graph* g = (struct graph*) malloc(sizeof(struct graph));

       g->no_vertex = vertex;

       /*Allocate memory for the adj lists*/      
       g->array = (struct adj_list*) malloc(vertex * sizeof(struct adj_list));
      

       /*Init the head of each ajd list head with NULL*/
       while(vertex>0)
       {
          
              g->array[vertex-1].head = NULL;
              vertex--;
       }

       return g;      
}

/*Insert edge node in to a given adj list in the graph structure*/
void insert_edge(struct graph* graph, int node, int vertex)
{
    struct adj_list_edge* new_edge = (struct adj_list_edge*) malloc(sizeof(struct adj_list_edge));
    new_edge->vertex = vertex;
    new_edge->next = NULL;

    new_edge->next = graph->array[node].head;
    graph->array[node].head = new_edge;
}

/*Function Defination for displaying the Graph adj list */
void display_adj_list(struct graph* graph)
{
     int i;
     for (i=0; i<graph->no_vertex; ++i)
     {
         struct adj_list_edge* temp;
         temp = graph->array[i].head;
         printf("\n Adjacency list of vertex %d\n head ", i+1);
         while(temp)
         {
               printf("-> %d", temp->vertex);
               temp = temp->next;
         }
         printf("\n");
     }
}

void compute_graph_components(struct graph* graph)
{
    int i=0, j, count=0, nodes;
         nodes = graph->no_vertex;

         int *vertex_array, *comp_elements;
    
         vertex_array = (int *)malloc(nodes * sizeof(int));
        comp_elements = (int **)malloc(nodes * sizeof(int *));
  

         for (i=1; i<=nodes; ++i)
         {
            vertex_array[i] = 1;
        comp_elements[i] = 0;
         }

    comp_elements[0] = 1;  
    while(is_vertex_exists(vertex_array, nodes))
         {
        count++;
        j=1;
   

        for(i=0; comp_elements[i]!= 0; ++i)
        {
            struct adj_list_edge* temp;
                       temp = graph->array[comp_elements[i]-1].head;

            while(temp)
            {
                if( (is_in_R(comp_elements, comp_elements[i], nodes)==TRUE) && (is_in_R(comp_elements, temp->vertex, nodes)==FALSE) )
                {
                    comp_elements[j] = temp->vertex;
                                     j++;
                }
                temp = temp->next;
            }
        }

        /*Function to display the componemt elements*/

        display_comp_elements(comp_elements, j, count);

remove_from_vertex_array(vertex_array, comp_elements, nodes);
        /*Remove the done verties from the vertex array */      
    /*    for(i=0; comp_elements[i]!=0 ; ++i)
             {
        vertex_array[comp_elements[i]-1] = 0;
        }

        /*Init all the elements of Comp_element araay to zero (0) */      
        for(i=0; i<nodes ; ++i)
             {
        comp_elements[i] = 0;
        }

        /*Finding new element from the Comp_element araay */
        for(i=1; i<nodes; ++i)
        {
                     if(vertex_array[i]!=0)
                     {
                comp_elements[0] = (i+1);
                break;
            }
        }


         }   
    printf("\nThe total number of components in the graph: %d\n\n", count);
}



int is_vertex_exists(int *vertex_array, int nodes)
{
    int i=0;
    for(i=0; i<nodes; ++i)
    {
             if(vertex_array[i]!=0)
             { return TRUE; }
    }  
    return FALSE;
}

int is_in_R(int *comp_elements, int element, int nodes)
{
    int i=0;
    for(i=0; i<nodes; ++i)
    {
             if (comp_elements[i] == element)
             { return TRUE; }
    }
    return FALSE;;
}


void display_comp_elements(int *comp_elements, int nodes, int count)
{
         int i;
         printf("Elements in Componenet %d:\n{ %d ",count, comp_elements[0]);
         for(i=1; i<nodes;++i)
         {
              if(comp_elements[i] != 0)
              {
                   printf(",%d ",comp_elements[i]);
              }
         }
         printf("}\n\n");
}

void remove_from_vertex_array(int *vertex_array, int *comp_elements, int nodes)
{
    int i,j;
    for(i=0; comp_elements[i]!=0 ; ++i)
    {
    vertex_array[comp_elements[i]-1] = 0;
    }   
}

/*End of the Program*/

No comments:

Post a Comment