Graph algorithm (a)--basic graph algorithm (BFS,DFS and its application) (2)

Source: Internet
Author: User

2) DFS

The depth-first search always searches for the starting edge of the recently discovered node v until all the departure edges of that node are discovered

Once all the starting edges of node v are found, the search is traced back to the V's precursor node.

Implementation Details: Timestamp

Each node has a discovery time and a completion time.

The precursor of a DFS rear image forms a depth-first forest

1#include <iostream>2 using namespacestd;3 #defineNIL-14 intg[ +][ +];5 intN;6 structNode7 {8     intcolor;9     intD;Ten     intF; One     intPi; A}t[ +]; - intTime ; -  the voidDFS (); - voidDfs_visit (intu); -  - voidDFS () + { -      for(intI=0; i<n;i++) +     { AT[i].color =0; atT[i].pi =NIL; -     } -Time=1; -      for(intI=0; i<n;i++) -     { -         if(T[i].color = =0) in dfs_visit (i); -     } to } + voidDfs_visit (intu) - { theT[U].D = time++; *T[u].color =1;/*****/ $      for(intv=0; v<n;v++)Panax Notoginseng     { -         if((g[u][v] = =1) && (T[v].color = =0)) the         { +T[v].pi =u; A Dfs_visit (v); the         } +     } -T[U].F = time++; $ } $ /* - int main () - { the While (cin>>n) -     {Wuyi for (int i=0;i<n;i++) the         { - for (int j=0;j<n;j++) Wu             { - cin>>g[i][j]; About             } $         } - DFS (); - for (int i=0;i<n;i++) - cout<<i<< "<<t[i].d<<" "<<t[i].f<<" "<<endl; A     } + return 0; the } - */

Graph algorithm (a)--basic graph algorithm (BFS,DFS and its application) (2)

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.