Question: I need help creating the bfs function: void bfs(adj_node_t** list, int rows, int source, int* color, int* distance, int* parent) { if (source >= rows)
I need help creating the bfs function:
void bfs(adj_node_t** list, int rows, int source, int* color, int* distance, int* parent) {
if (source >= rows) {
exit(EXIT_FAILURE);
}
If (list == NULL) {
exit(EXIT_FAILURE);
}
/*
Initialize the vertices, color should be initialized to 0, distance to -1 for infinity, parent to -1 for NIL.
Initialize the source vertex, distance for the source vertex should be 0, parent to -1, and color to 1.
BFS iteration
Use remove_node (to fetch & dequeue the vertex)
You will also need create_node and add_node
}
Here are the helper functions:
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;
}
void add_node(adj_node_t** list, int row, adj_node_t* node) {
if(list[row] == NULL) {
list[row] = node;
} else {
adj_node_t* next = list[row];
while(next->next != NULL) {
next = next->next;
}
Next->next = node;
}
}
int remove_node(adj_node_t** list) {
int vid = -1;
if(list != NULL) {
adj_node_t* head = *list;
if(head != NULL) {
adj_node_t* tmp = head;
vid = head->vid;
head = head->next;
free(tmp);
}
*list = head;
} else {
exit(EXIT_FAILURE);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
