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