Data structure--diagram (top)--Traversal of graphs

Source: Internet
Author: User

The traversal of graphs

Two typical ways to traverse

    • Deep priority Search (Depth first search, DFS)
    • Breadth Priority search (breadth First search, BFS)
Depth-First Search

Pseudo-code description of depth-first search

void DFS (Vertex V) {    true;      for (Each adjacency point of v W)         if (! Isvisited[w])            DFS (W);}

The first sequence traversal of a tree is a generalization of a tree's ordinal traversal

What is the time complexity of Dfs if there are n vertices and e-bars? Not sure!

    • Using the adjacency table to store the graph, there are O (n+2e) each point accessed once per edge visited once
    • Using the adjacency matrix to store the graph, there is O (N2) We have to access the N row n columns, so N2
Breadth First Search

The equivalent of the sequence traversal in the tree (from top to bottom, from left to right layer of access) to the queue, pop-up, it's about the children.

A tree is a special kind of diagram,

Pseudo code Description of BFS

void BFS (Vertex V) {    true;    Enqueue (V, Q);      while (! IsEmpty (Q))         = Dequeue (Q);            for (each adjacency point of v W)             if (! Visited[w]) {                true;                Enqueue[w, Q];            }}

What is the time complexity of Dfs if there are n vertices and e-bars? Not sure!

    • Using the adjacency table to store the graph, there are O (n+2e) each point accessed once per edge visited once
    • Using the adjacency matrix to store the graph, there is O (N2) We have to access the N row n columns, so N2

Data structure--diagram (top)--Traversal of graphs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.