Độ sâu đầu tiên tìm kiếm
lượt xem 12
download
Tham khảo tài liệu 'độ sâu đầu tiên tìm kiếm', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: Độ sâu đầu tiên tìm kiếm
- DepthFirst Search A B D E C DepthFirst Search 1
- Outline and Reading Definitions (§6.1) Subgraph Connectivity Spanning trees and forests Depthfirst search (§6.3.1) Algorithm Example Properties Analysis Applications of DFS (§6.5) Path finding Cycle finding Depth-First Search 2
- Subgraphs A subgraph S of a graph G is a graph such that The vertices of S are a subset of the vertices of G The edges of S are a Subgraph subset of the edges of G A spanning subgraph of G is a subgraph that contains all the vertices of G Spanning subgraph Depth-First Search 3
- Connectivity A graph is connected if there is a path between every pair of Connected graph vertices A connected component of a graph G is a maximal connected subgraph of G Non connected graph with two connected components Depth-First Search 4
- Trees and Forests A (free) tree is an undirected graph T such that T is connected T has no cycles Tree This definition of tree is different from the one of a rooted tree A forest is an undirected graph without cycles The connected components of a forest are trees Forest Depth-First Search 5
- Spanning Trees and Forests A spanning tree of a connected graph is a spanning subgraph that is a tree A spanning tree is not unique unless the graph is a tree Graph Spanning trees have applications to the design of communication networks A spanning forest of a graph is a spanning subgraph that is a forest Spanning tree Depth-First Search 6
- DepthFirst Search Depthfirst search DFS on a graph with n (DFS) is a general vertices and m edges technique for traversing takes O(n + m ) time a graph DFS can be further A DFS traversal of a extended to solve other graph G graph problems Visits all the vertices and Find and report a path edges of G between two given Determines whether G is vertices connected Find a cycle in the graph Computes the connected Depthfirst search is to components of G graphs what Euler tour Computes a spanning is to binary trees forest of G Depth-First Search 7
- DFS Algorithm The algorithm uses a mechanism for setting and getting “labels” of Algorithm DFS(G, v) vertices and edges Input graph G and a start vertex v of G Algorithm DFS(G) Output labeling of the edges of G Input graph G in the connected component of v Output labeling of the edges of G as discovery edges and back edges as discovery edges and setLabel(v, VISITED) back edges for all e ∈ G.incidentEdges(v) for all u ∈ G.vertices() if getLabel(e) = UNEXPLORED setLabel(u, UNEXPLORED) w ← opposite(v,e) for all e ∈ G.edges() if getLabel(w) setLabel(e, UNEXPLORED) = UNEXPLORED for all v ∈ G.vertices() setLabel(e, DISCOVERY) if getLabel(v) DFS(G, w) = UNEXPLORED else DFS(G, v) setLabel(e, BACK) Depth-First Search 8
- Example A A unexplored vertex A visited vertex B D E unexplored edge discovery edge C back edge A A B D E B D E C C Depth-First Search 9
- Example (cont.) A A B D E B D E C C A A B D E B D E C C Depth-First Search 10
- DFS and Maze Traversal The DFS algorithm is similar to a classic strategy for exploring a maze We mark each intersection, corner and dead end (vertex) visited We mark each corridor (edge ) traversed We keep track of the path back to the entrance (start vertex) by means of a rope (recursion stack) Depth-First Search 11
- Properties of DFS Property 1 DFS(G, v) visits all the vertices and edges in the connected A component of v Property 2 B D E The discovery edges labeled by DFS(G, v) form a spanning tree of C the connected component of v Depth-First Search 12
- Analysis of DFS Setting/getting a vertex/edge label takes O(1) time Each vertex is labeled twice once as UNEXPLORED once as VISITED Each edge is labeled twice once as UNEXPLORED once as DISCOVERY or BACK Method incidentEdges is called once for each vertex DFS runs in O(n + m) time provided the graph is represented by the adjacency list structure Recall that Σ v deg(v) = 2m Depth-First Search 13
- Path Finding We can specialize the DFS algorithm to find a Algorithm pathDFS(G, v, z) path between two given setLabel(v, VISITED) vertices u and z using the S.push(v) template method pattern if v = z return S.elements() We call DFS(G, u) with u for all e ∈ G.incidentEdges(v) as the start vertex if getLabel(e) = UNEXPLORED We use a stack S to keep w ← opposite(v,e) track of the path between if getLabel(w) the start vertex and the = UNEXPLORED current vertex setLabel(e, DISCOVERY) As soon as destination S.push(e) vertex z is encountered, pathDFS(G, w, z) we return the path as the S.pop(e) contents of the stack else setLabel(e, BACK) Depth-First S.pop(v) Search 14
- Cycle Finding Algorithm cycleDFS(G, v, z) We can specialize the setLabel(v, VISITED) DFS algorithm to find a S.push(v) simple cycle using the for all e ∈ G.incidentEdges(v) if getLabel(e) = UNEXPLORED template method pattern w ← opposite(v,e) We use a stack S to S.push(e) keep track of the path if getLabel(w) = UNEXPLORED between the start vertex setLabel(e, DISCOVERY) pathDFS(G, w, z) and the current vertex S.pop(e) As soon as a back edge else T ← new empty stack (v, w) is encountered, repeat we return the cycle as o ← S.pop() the portion of the stack T.push(o) until o = w from the top to vertex w return T.elements() S.pop(v) Depth-First Search 15
CÓ THỂ BẠN MUỐN DOWNLOAD
-
Trí Tuệ Nhân Tạo – Cải Tiến Thuật Toán Tìm Kiếm Sâu Lặp
5 p | 310 | 93
-
Loại bỏ category trong URL của Wordpress blog
5 p | 53 | 21
-
Để bán được hàng trên website
4 p | 144 | 21
-
10 thủ thuật SEO để tăng tần suất của bộ tìm kiếm
6 p | 85 | 9
-
Các dịch vụ đặc biệt của Google
3 p | 62 | 9
-
9 THỦ THUẬT VỀ SEO GOOGLE PLUS
14 p | 59 | 9
-
Cách khắc phục lỗi yahoo chat không hiển thị chữ
4 p | 176 | 9
-
Chuyển e-mail từ tài khoản Gmail cũ sang tài khoản mới
5 p | 104 | 9
-
Forensic hoạt động như thế nào- P1
5 p | 86 | 9
-
Có ai đấy đang tìm kiếm bạn!
3 p | 55 | 7
-
Hướng dẫn tạo tài khoản Google + (Google plus)
4 p | 232 | 7
-
Download hàng loạt hình ảnh trên trang tìm kiếm Google Images
3 p | 80 | 7
-
Các thủ thuật tăng tốc và thay đổi giao diện cho Firefox 4
9 p | 92 | 6
-
Sáu điều bạn cần biết về SEO
8 p | 60 | 6
-
Tránh các lỗi vi phạm cơ chế tìm kiếm của Search Engine
5 p | 66 | 5
-
Chơi game bắn súng ngay trên công cụ tìm kiếm Google
3 p | 86 | 5
-
Không để lại dấu vết tìm kiếm
5 p | 81 | 4
Chịu trách nhiệm nội dung:
Nguyễn Công Hà - Giám đốc Công ty TNHH TÀI LIỆU TRỰC TUYẾN VI NA
LIÊN HỆ
Địa chỉ: P402, 54A Nơ Trang Long, Phường 14, Q.Bình Thạnh, TP.HCM
Hotline: 093 303 0098
Email: support@tailieu.vn