Skip to content

Latest commit

 

History

History
101 lines (51 loc) · 4.32 KB

lecture14_Traversal Tree_Traversal Graph_DFS.md

File metadata and controls

101 lines (51 loc) · 4.32 KB

DFS(深度优先搜索)

树的定义

image-20220613141350877

树的遍历顺序(Tree Traversal)

  • Preorder(前序遍历)

image-20220613141713277

  • Inorder(中序遍历)

image-20220613141749324

  • Postorder(后序遍历)

image-20220613141832498

  • Levelorder

image-20220613141924495

Graphs(图)的基本概念和定义

image-20220613144634545

树的定义是:一系列的节点,并且每个节点必须有至少一条边进行连接,其限制条件是:两个节点之间只允许有一种路径通过。

上述中红色的图都符合图的定义:

image-20220613144741621

image-20220613144812536

simple Graph(简单图)定义的限制有:1) no loops 2) no parallel edges

以下显示了一些有关Graph的术语:

image-20220613145258899

image-20220613145309630

Graph Problems

image-20220613150100602

DFS

DFS可用来解决s-t Path问题以及连通性问题(Connectivity)

DFS的核心主旨思想为:

image-20220613152535247

以上图右下角的图为例,以DepthFirstPaths问题为背景,阐释DFS的具体流程如下:

image-20220613152839541

在mark(1)之前首先 set degeTo(1)=0

image-20220613152945458

以此类推

image-20220613152959450

image-20220613153007635

image-20220613153011356

image-20220613153025684

image-20220613153029373

image-20220613153040950

image-20220613153055394

image-20220613153100311

image-20220613153107654

image-20220613153112342

image-20220613153123130

image-20220613153130570

image-20220613153136509

image-20220613153152110

image-20220613153159103

image-20220613153205306

DFS的preorder以及postorder

image-20220613153308582