The following function is applied onto a graph and it specifically implements the method of breadth-first search. To visit all nodes connected to node k of a graph, we put the k in a FIFO queue and then go into a loop, during which we get the next node from the queue and, if we have not already visited it, we visit it and push into the queue all nodes belonging to the adjacency list of this node, continuing this process until we empty the queue.

## Tag Archive: graph

The following program reads a set of edges that define a graph and creates a representation of this graph with an adjacency list. The adjacency list of a graph is an array of lists, one for each vertex, where the j-th list contains a linked list of the nodes which are connected with the j-th vertex.

The following program reads a set of edges that define an undirected graph and creates a representation of this graph with an adjacency matrix, giving a[i][j] and a[j][i] the value of 1 if there is an edge from i to j or from j to i in the graph, or the value of 0 if it does not exist. Also, we assume that the number of vertices V is a constant known at compilation time. Otherwise, there should be dynamic memory allocation for the array that represents the adjacency matrix.