1 / 25

103 資訊學科培訓 圖論 - B FS & D FS & 應用

103 資訊學科培訓 圖論 - B FS & D FS & 應用. 講者 : 林庭宇. 前情提要. 各位認識圖嗎 ?. 基本概念. B FS & D FS 就是圖形的「搜索」 實作 時分別需要用到 Queue & Stack 可求取距離、連通等基礎資料 應用極廣. Yes. No. 知道 Stack 和 Queue 嗎 ?. BFS. 廣度優先搜索 ( Breadth-first Search ) 先搜索完叉路再往下走 由近而遠的搜索 把走過的路連 起來會變成一棵 tree You need a Queue. 範例. DFS.

rose-stokes
Download Presentation

103 資訊學科培訓 圖論 - B FS & D FS & 應用

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 103資訊學科培訓圖論 -BFS&DFS& 應用 講者:林庭宇

  2. 前情提要 • 各位認識圖嗎?

  3. 基本概念 • BFS&DFS 就是圖形的「搜索」 • 實作時分別需要用到 Queue & Stack • 可求取距離、連通等基礎資料 • 應用極廣 Yes No 知道Stack和Queue嗎?

  4. BFS • 廣度優先搜索(Breadth-first Search) • 先搜索完叉路再往下走 • 由近而遠的搜索 • 把走過的路連起來會變成一棵tree • You need a Queue

  5. 範例

  6. DFS • 深度優先搜索(Depth-first Search) • 先搜索完整條路再去走叉路 • 把走過的路連起來也會變成一棵tree • You need a Stack

  7. 範例again

  8. 時間複雜度 • 看資料型態而定 Adjacency matrix -> O(V^2) Adjacency lists -> O(V+E) • BUT 要記得剪枝!!! • 記下走過的點,以免重覆走浪費時間

  9. 練習時間 ZJ a290 b224 a753 d908

  10. 讓我們繼續看下去 • DFS延伸概念 • 判斷DAG(Direct Acylic Graph) • 拓樸排序(Topological Sort) • 連通分量(Connected Components) • 關節點(割點,Cut Point) OAO~~~

  11. DFS延伸概念 • Discovery time Finishing time 1 5 7 6 2 4 3 2 4 1 8 7 5 3 6 8

  12. DFS延伸2 Tree Edges: not yet discovered Back Edges: discovered but not yet finished Forward/Cross Edges: finished

  13. DAG(Direct Acylic Graph) • 有向無環圖 • 判斷方式:DFS • 他沒有Back Edges!!!

  14. 拓樸排序 • Topological Sort • 將圖上的點進行排序 • 假如A->B,則A要排在B之前 • 前提:圖必須是一個DAG • 用途: 排大學課程、解決DAG圖各種問題的利器

  15. 做法: 1.先找出第一點 – 什麼點一定可以排在最前面? 2.進行拓樸排序 – 想想DFS延伸! => 1.任意一個沒有「入邊」的點 2.Finishing time!

  16. 拓樸範例 • 一個可行的拓樸排序: 9205637814 • Finishing time反排!!! 8 2 4 5 6 7 3 10 9 1

  17. 來個題目 ZJ a552 a454 d917

  18. 連通分量 • 連通關係(就有向圖來討論) • 強連通-任兩點均有路來回 • 弱連通-化為無向圖後為相連圖形 • 單向連通-任兩點至少有單向路可走 • 強連通>單向連通>弱連通

  19. 強連通分量 • strongly connected components(SCC) • 用處:收縮圖形 • 怎麼找? -> 還是 DFS

  20. 關節點(割點,Cut Point) • 無向圖中,少了就會使圖不連通的點

  21. 有圖有真相

  22. 怎麼找割點? ->DFSagain!!! • 用DFS tree來想

  23. THE END P.S. 蒙地卡羅 is good!

  24. 補-Stack • 就像堆盤子一樣 • 遞迴就是一種Stack • 陣列或STL

  25. 補 -Queue • 就像排隊一樣(不考慮插隊) • 陣列或STL Back

More Related