We are no longer accepting contributions to Documentation. Please see our post on meta.

algorithm

Graph Traversals

This draft deletes the entire topic.

Examples

  • 0

    The function takes the argument of the current node index, adjacency list (stored in vector of vectors in this example), and vector of boolean to keep track of which node has been visited.

    void dfs(int node, vector<vector<int>>* graph, vector<bool>* visited) {
        // check whether node has been visited before
        if((*visited)[node])
            return;
    
        // set as visited to avoid visiting the same node twice
        (*visited)[node] = true;
    
        // perform some action here
        cout << node;
    
        // traverse to the adjacent nodes in depth-first manner
        for(int i = 0; i < (*graph)[node].size(); ++i)
            dfs((*graph)[node][i], graph, visited);
    }
    
Please consider making a request to improve this example.

Syntax

Syntax

Parameters

Parameters

Remarks

Remarks

Still have a question about Graph Traversals? Ask Question

Topic Outline


    We are no longer accepting contributions to Documentation. Drafts cannot be modified.