Java 实现广度优先搜索和深度优先搜索

Java 实现广度优先搜索和深度优先搜索

1. 综述

  复习算法数据结构,用Java 实现广度优先搜索和深度优先搜索。

 

2. 代码

要搜索的图为:

Java 实现广度优先搜索和深度优先搜索_第1张图片

Java代码:

 1 package cn.edu.tju.scs;
 2 
 3 import java.util.LinkedList;
 4 import java.util.Queue;
 5 
 6 public class Search {
 7     public static boolean [] accessed = new boolean[8];
 8     public static boolean [] accessed2 = new boolean[8];
 9     public static ArcNode [] heads = new ArcNode[8];
10     public static Queue queue = new LinkedList();
11     
12     public static void main(String [] args) {        
13         for (int i = 0; i < heads.length; i++) {
14             heads[i] = new ArcNode(i + 1);    // 如果节点编号也从0开始就好了,少了很多加一减一的操作
15         }
16         for (int i = 0; i < accessed.length; i++) {
17             accessed[i] = false;            
18         }
19         heads[0].nextArc = new ArcNode(2);
20         heads[0].nextArc.nextArc = new ArcNode(3);
21         
22         heads[1].nextArc = new ArcNode(1);
23         heads[1].nextArc.nextArc = new ArcNode(4);
24         heads[1].nextArc.nextArc.nextArc = new ArcNode(5);
25         
26         heads[2].nextArc = new ArcNode(1);
27         heads[2].nextArc.nextArc = new ArcNode(6);
28         heads[2].nextArc.nextArc.nextArc = new ArcNode(7);
29         
30         heads[3].nextArc = new ArcNode(2);
31         heads[3].nextArc.nextArc = new ArcNode(8);
32         
33         heads[4].nextArc = new ArcNode(2);
34         heads[4].nextArc.nextArc = new ArcNode(8);
35         
36         heads[5].nextArc = new ArcNode(3);
37         
38         heads[6].nextArc = new ArcNode(3);
39         
40         heads[7].nextArc = new ArcNode(4);
41         heads[7].nextArc.nextArc = new ArcNode(5);
42         
43         BroadFirstSearch(heads);
44         DepthFirstSearch(heads);
45     }
46 
47     private static void BroadFirstSearch(ArcNode[] heads) {
48         BroadFirstSearch(heads[0]);
49     }
50 
51     private static void BroadFirstSearch(ArcNode arcNode) {
52         queue.add(arcNode.adjvex);
53         accessed2[arcNode.adjvex - 1] = true;
54         while (queue.size() > 0) {
55             Integer vex = queue.remove();
56             ArcNode curNode = heads[vex - 1].nextArc;
57             while(curNode != null) {
58                 if (!accessed2[curNode.adjvex - 1]) {
59                     queue.add(curNode.adjvex);
60                     accessed2[curNode.adjvex - 1] = true;
61                 }
62                 curNode = curNode.nextArc;
63             }
64             System.out.print(vex);
65             if (queue.size() > 0)
66                 System.out.print(" -> ");
67             else
68                 System.out.println("");
69         }
70     }
71 
72     private static void DepthFirstSearch(ArcNode[] heads) {
73         DepthFirstSearch(heads[0]);
74     }
75 
76     private static void DepthFirstSearch(ArcNode arcNode) {
77         ArcNode curNode = heads[arcNode.adjvex-1];
78         System.out.print(curNode);
79         accessed[curNode.adjvex -1] = true;
80         
81         curNode = curNode.nextArc;
82         while(curNode != null) {
83             if (!accessed[curNode.adjvex - 1]){
84                 System.out.print(" -> ");
85                 DepthFirstSearch(curNode);
86             }
87             curNode = curNode.nextArc;
88         }
89     }
90 }

输出结果:

(广搜:)1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8
(深搜:)1 -> 2 -> 4 -> 8 -> 5 -> 3 -> 6 -> 7

转载于:https://www.cnblogs.com/yongheng20/p/5749957.html

你可能感兴趣的:(Java 实现广度优先搜索和深度优先搜索)