Duyệt đồ Thị Theo Chiều Sâu Java - Jundat95
Có thể bạn quan tâm
Jundat95 Home
Facebook
System.out.print('Hello world!');
Header Ads
- Knowledge
- _Android
- _React Native
- _IOS
- _Java
- _JavaScript
- _C#
- _HTML
- Operating system
- _Windows
- _Ubuntu
- Tutorial
- Tools
- Ebook
Duyệt đồ thị theo chiều sâu Java
Thuật toán duyệt đồ thị theo chiều sâu DFS, mình không nói về lý thuyết nữa vì lý thuyết có trong quyển sách của thầy Lê Mình Hoàng rồi mọi người tải về xem nhé.
File graph.inp 8 7 1 5 1 2 1 3 2 3 2 4 3 5 4 6 7 8 File Code Duyệt chiều sâu DFS: Cách một duyệt theo chiều sâu DFS: package graph; import java.io.*; public class DFS { private static final String pathIn = "graph.inp"; private static final String pathOut = "graph.out"; private static int n, m, start, end; private static int[][] arrays; private static int[] back; private static PrintWriter printWriter; public static void DFS(int u) { int v; for (v = 0; v < n; v++) { if(arrays[u][v] == 1 && back[v] == 0) { back[v] = u; DFS(v); } } } public static void main(String[] args) { ReadFile(); for (int i = 0; i < n; i++){ back[i] = 0; } back[start] = 1; DFS(start); try { printWriter = new PrintWriter(pathOut, "UTF-8"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } printWriter.print(""+end); while (start != end) { end = back[end]; printWriter.print("-"+end); } printWriter.close(); } public static void ReadFile() { try { FileReader reader = new FileReader(pathIn); BufferedReader bufferedReader = new BufferedReader(reader); String lineStr = bufferedReader.readLine(); n = Integer.parseInt(lineStr.split(" ")[0]); m = Integer.parseInt(lineStr.split(" ")[1]); start = Integer.parseInt(lineStr.split(" ")[2]); end = Integer.parseInt(lineStr.split(" ")[3]); arrays = new int[n+1][n+1]; back = new int[n+1]; int u = 0, v = 0; for(int i = 0; i < m; i++) { lineStr = bufferedReader.readLine(); u = Integer.parseInt(lineStr.split(" ")[0]); v = Integer.parseInt(lineStr.split(" ")[1]); arrays[u][v] = 1; arrays[v][u] = 1; } reader.close(); } catch (IOException e) { e.printStackTrace(); } } } Cách 2 duyệt đồ thị theo chiều sâu DFS: import java.io.*; import java.util.Scanner; public class DepthFirstSearch { private static final String InputFile = "PATH.INP"; private static final String OutputFile = "PATH.OUT"; private static final int Max = 100; private boolean[][] A; private int[] Trace; private int n, Start, Finish; private PrintWriter out; public DepthFirstSearch() { A = new boolean[Max][Max]; Trace = new int[Max]; Enter(); FileOutputStream fo = null; try { fo = new FileOutputStream(OutputFile); if (fo == null) { PrintWriter writer = null; try { writer = new PrintWriter(OutputFile, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } writer.close(); fo = new FileOutputStream(OutputFile); } } catch (FileNotFoundException e) { e.printStackTrace(); } out = new PrintWriter(fo); Trace[Start]= -1; DFS(Start); Result(); out.close(); } private void Enter() { FileInputStream fi = null; try { fi = new FileInputStream(InputFile); } catch (FileNotFoundException e) { e.printStackTrace(); } Scanner inp = new Scanner(fi, "UTF-8"); String[] st = inp.nextLine().split(" "); n = Integer.parseInt(st[0]); int m = Integer.parseInt(st[1]); Start = Integer.parseInt(st[2]); Finish = Integer.parseInt(st[3]); for (int i = 0; i < m; i++) { String[] tmp = inp.nextLine().split(" "); int U = Integer.parseInt(tmp[0]); int V = Integer.parseInt(tmp[1]); A[U][V] = true; A[V][U] = true; } inp.close(); } private void DFS(int U) { for (int V = 0; V < n; V++) { if (A[U][V] && Trace[V] == 0) { Trace[V] = U; DFS(V); } } } private void Result() { out.println("From " + Start + " you can visit: "); for (int V = 0; V < n; V++) { if (Trace[V] != 0) { out.print(V + ", "); } } out.println(); out.println("The path from " + Start + " to " + Finish + " : "); if (Trace[Finish] == 0) { out.println("not found"); } else { while (Finish != Start) { out.print(Finish + " - "); Finish = Trace[Finish]; } out.println(Start); } } public static void main(String args[]){ new DepthFirstSearch(); } } Hãy like và share fanpage để ủng hộ blog nhé :DRelated Posts
Học TậpPost a Comment
No comments
Subscribe to: Post Comments ( Atom )Translate
STAY WITH US
- 114 followers
- 250 followers
- 500 likes
- 0 followers
- 1000 subscribers
- 266 followers
Popular Posts
-
Đề thi trắc nghiệm quản lý dự án công nghệ thông tin Đề thi trắc nghiệm quản lý dự án công nghệ thông tin Cau1 Ai có trách nhiệm chuẩn bị báo cáo đánh giá sau triển khai? A. Ngư... -
[Ebook] Giáo trình lập trình C++ nâng cao Giáo trình lập trình C/C++ nâng cao Giáo trình dành cho các bạn yêu thích tìm tòi học hỏi, và đặc biệt yêu thích ngôn ngữ c++ Yêu... -
Bài Tập Trắc Nghiệm Lập Trình C, Có Đáp Án. Bài Tập Trắc Nghiệm Lập Trình C, Có Đáp Án. L ink tải bài tập trắc nghiệm C. https://mega.co.nz/#!xJMnWCZI!gp3gYnCVqy9UD-cdX...
-
Hướng dẫn cài đặt docker trên ubuntu 19.04 Hướng dẫn cài đặt docker trên ubuntu 19.04 Hướng dẫn cài đặt Docker trên ubuntu 19.04 đơn giản. 1, Chạy các lệnh sau để cài đặt b...
Arquivo do blog
- ► 2024 (1)
- ► April (1)
- ► 2023 (1)
- ► August (1)
- ► 2022 (4)
- ► November (1)
- ► May (1)
- ► April (1)
- ► January (1)
- ► 2021 (26)
- ► December (6)
- ► September (3)
- ► August (1)
- ► July (1)
- ► May (3)
- ► April (3)
- ► March (3)
- ► February (2)
- ► January (4)
- ► 2020 (22)
- ► December (1)
- ► September (1)
- ► August (4)
- ► July (2)
- ► April (3)
- ► March (8)
- ► January (3)
- ► 2019 (42)
- ► December (2)
- ► November (3)
- ► October (2)
- ► September (2)
- ► July (5)
- ► May (3)
- ► April (10)
- ► March (7)
- ► February (7)
- ► January (1)
- ► 2018 (19)
- ► December (1)
- ► November (7)
- ► September (1)
- ► August (1)
- ► July (2)
- ► June (1)
- ► May (1)
- ► February (2)
- ► January (3)
- ► 2016 (15)
- ► December (1)
- ► October (2)
- ► September (1)
- ► August (3)
- ► July (2)
- ► June (1)
- ► May (2)
- ► April (3)
- ► 2015 (75)
- ► December (3)
- ► November (2)
- ► October (8)
- ► September (7)
- ► August (7)
- ► July (4)
- ► June (4)
- ► May (3)
- ► April (5)
- ► March (16)
- ► February (3)
- ► January (13)
- ► 2014 (12)
- ► December (6)
- ► November (2)
- ► September (3)
- ► August (1)
Recent Posts
Recent Comments
Created By Tinh NgoTừ khóa » Duyệt đồ Thị Dfs
-
Cây DFS (Depth-First Search Tree) Và ứng Dụng - VNOI
-
Giải Thuật Tìm Kiếm Theo Chiều Sâu (Depth First Search) - VietTuts
-
Các Giải Thuật Tìm Kiếm Trên đồ Thị - Viblo
-
Thuật Toán DFS – Tìm Kiếm Theo Chiều Sâu Trên đồ Thị - TEK4
-
Duyệt đồ Thị Theo Chiều Sâu (DFS) - VietCodes
-
DEPTH FIRST SEARCH | Duyệt đồ Thị Theo Chiều Sâu | Đa I Tờ
-
DFS - Duyệt Đồ Thị | Code Cùng Bắc | Nhập Môn Lập Trình Thi Đấu #5
-
Cài đặt Thuật Toán Duyệt đồ Thị - DFS, BFS - VN SEEDER
-
Giải Thuật Và Lập Trình - C: IV. Các Phép Duyệt đồ Thị (Traversals Of ...
-
Thuật Toán Về Tìm Kiếm Theo Chiều Sâu DFS Bằng Ngôn Ngữ C/C++
-
Đồ Thị Trong Python: Thuật Toán Tìm Kiếm Theo Chiều Sâu (DFS)
-
Thuật Toán Duyệt Theo Chiều Sâu(Deep-First Search-DFS) Duyệt đồ Thị
-
[PDF] Toán Rời Rạc 2,ngô Xuân Bách,hvcnbcvt