jeudi 4 novembre 2021

DFS using 1D like 2D vector

When the adjacency list is created in a 1D vector, how can it be used like a 2D vector in the for loop for dfs.

int dfs(int node) {
    int visCount = 1;
    visited[node] = true;
//this part!
    for (auto neighbour: graph[node]) {
        if (!visited[neighbour]) {
            visCount += dfs(neighbour);
        }
    }
    return visCount;
}

Aucun commentaire:

Enregistrer un commentaire