Question: Please use C and comment, please. Thank you in advance! /* This function constructs an adjacency list for a graph from an adjacency matrix input

Please use C and comment, please. Thank you in advance!Please use C and comment, please. Thank you in advance! /* Thisfunction constructs an adjacency list for a graph from an adjacency matrix

/* This function constructs an adjacency list for a graph from an adjacency matrix input parameters: The following are 'consumed' by this function int** adj_mat 2D array that stores the adjacency matrix int rows # of rows in the matrix int cols # of columns in the matrix The following are 'produced' by this function adj_node_t*** list a linked list of adjacencies return parameters: none adj_node_t*** list is passed in by reference from the main function so that it can be malloc'd via the init_adj_list function. After initializing it go through each row and add its adjacent nodes void construct_adj_list(int ** adj mat, int rows, int cols, adj_node_t*** list), // verify that the adj matrix is correct if(rows != cols) { fprintf(stderr, "Adjacency matrix is not square "); exit(EXIT_FAILURE); fprintf(stdout, "Constructing the adjacency list for this graph ... "); // Initialize the linked list init_adj_list(list, rows); // Since list was passed in by reference, accessing its // elements would require dereferencing it every time. // To make it easier, create a copy of the dereferenced // list and store it in myList for use in this function adj_node_t** myList = *list; // INSERT YOUR CODE HERE // HINT: You will need to use create_node() and add_node() here // Go through each vertex and construct its adjacency list fprintf(stdout, "done "); * This function creates a new adj_node_t node for a vertex and initializes it with node->vid = vid and node->next = NULL (i.e., no neighbor) The function then returns this node input parameters: int vid the ID of the vertex return parameters: adj_node_t newNode the new node for the vertex adj_node_t* create_node(int vid), adj_node_t* newNode = (adj_node_t*) malloc(sizeof(adj_node_t)); assert(newNode); newNode->vid = vid; newNode->next = NULL; return newNode; * Pass in a linked list that represents an adjacnecy list and the row (or vertex) to which you need to add a new node. First check that the adjacency list for the current row is not empty (i.e., NULL). If it is NULL, this is the first adjacent node. Otherwise, traverse the list until you reach the end, and then add the new node. input parameters: adj_node_t** list adjacency list for the current graph int row vertex that has this node as adjacent adj_node_t* node the node that needs to be added return parameters: none void add_node(adj_node_t** list, int row, adj_node_t* node) if(list[row] == NULL) { // empty list - the head points to this node list[row] = node; } else { // otherwise, find the end, and add it to the end adj_node_t* next = list[row]; while(next->next != NULL) { next = next->next; next->next = node; /* This function constructs an adjacency list for a graph from an adjacency matrix input parameters: The following are 'consumed' by this function int** adj_mat 2D array that stores the adjacency matrix int rows # of rows in the matrix int cols # of columns in the matrix The following are 'produced' by this function adj_node_t*** list a linked list of adjacencies return parameters: none adj_node_t*** list is passed in by reference from the main function so that it can be malloc'd via the init_adj_list function. After initializing it go through each row and add its adjacent nodes void construct_adj_list(int ** adj mat, int rows, int cols, adj_node_t*** list), // verify that the adj matrix is correct if(rows != cols) { fprintf(stderr, "Adjacency matrix is not square "); exit(EXIT_FAILURE); fprintf(stdout, "Constructing the adjacency list for this graph ... "); // Initialize the linked list init_adj_list(list, rows); // Since list was passed in by reference, accessing its // elements would require dereferencing it every time. // To make it easier, create a copy of the dereferenced // list and store it in myList for use in this function adj_node_t** myList = *list; // INSERT YOUR CODE HERE // HINT: You will need to use create_node() and add_node() here // Go through each vertex and construct its adjacency list fprintf(stdout, "done "); * This function creates a new adj_node_t node for a vertex and initializes it with node->vid = vid and node->next = NULL (i.e., no neighbor) The function then returns this node input parameters: int vid the ID of the vertex return parameters: adj_node_t newNode the new node for the vertex adj_node_t* create_node(int vid), adj_node_t* newNode = (adj_node_t*) malloc(sizeof(adj_node_t)); assert(newNode); newNode->vid = vid; newNode->next = NULL; return newNode; * Pass in a linked list that represents an adjacnecy list and the row (or vertex) to which you need to add a new node. First check that the adjacency list for the current row is not empty (i.e., NULL). If it is NULL, this is the first adjacent node. Otherwise, traverse the list until you reach the end, and then add the new node. input parameters: adj_node_t** list adjacency list for the current graph int row vertex that has this node as adjacent adj_node_t* node the node that needs to be added return parameters: none void add_node(adj_node_t** list, int row, adj_node_t* node) if(list[row] == NULL) { // empty list - the head points to this node list[row] = node; } else { // otherwise, find the end, and add it to the end adj_node_t* next = list[row]; while(next->next != NULL) { next = next->next; next->next = node

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!