BFS和DFS的java實現

<pre name="code" class="java">
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;

/*廣度遍歷是遍歷到某個頂點,而後訪問其鏈接點a,b。接着訪問a的鏈接表,
 很是天然的,這種數據結構就是HashMap,以頂點爲key。保存每個頂點的鏈接表
 */
public class BFS {
	static int count=0;
	/*
	 * HashMap<Character,LinkedList<Character>> graph 這個HashMap是用於存放圖中每個node的鄰接表
	 * 表示此映射所維護的鍵的類型爲Character。此映射值的類型爲LinkedList<Character> graph
	 * 表示將映射關係存放在graph此映射中
	 * 
	 * LinkedList<Character> 表示在此Collection中保持元素類型爲Character
	 * 
	 * HashMap<Character,Integer> dist 這個HashMap 是用於存放每個node與距離頂點s的距離的映射關係
	 * 表示此映射所維護的鍵的類型爲Character 此映射所維護的值的類型爲Integer。dist表示將映射關係存放到dist此映射中
	 */
	private void bfs(HashMap<Character, LinkedList<Character>> graph,
			HashMap<Character, Integer> dist, char start) {
		// Queue<Character> 表示在此Collection中所保存的元素的類型爲Character
		Queue<Character> q = new LinkedList<Character>();
		q.add(start);// 將指定元素s插入隊列,成功時返回true,假設沒有可用空間。則返回illegalStateException
		//put(start,0) start爲指定值將要關聯的鍵,0爲指定值將要關聯的值。 假設start與0的映射關係已存在,則返回並替換舊值0
		//假設 start與0的映射關係不存在,則返回null
		dist.put(start, 0);
		int i = 0;
		while (!q.isEmpty())//
		{
			char top = q.poll();// 獲取並移除隊列的頭,返回隊列的頭,假設隊列爲空。返回null
			i++;
			// dist.get(top) 返回指定鍵top所映射的值
			System.out.println("The " + i + "th element:" + top+ " Distance from s is:" + dist.get(top));
			int d = dist.get(top) + 1;// 得出其周邊還未被訪問的節點的距離
			/*
			 * graph.get(top)假設此映射包括一個知足 (key==null ? k==null : key.equals(k))
			 * 的從 k 鍵到 v 值的映射關係,則此方法返回 v;不然返回 null。(最多僅僅能有一個這種映射關係。

) * for(元素變量:元素集合),假設元素集合中所有元素都已遍歷過,則結束此循環。 不然運行for循環裏的程序塊 */ for (Character c : graph.get(top)) { // containskey(key) 假設此映射包括對於指定鍵key的映射關係,則返回true if (!dist.containsKey(c))// 假設dist中尚未該元素說明尚未被訪問 { //關聯指定鍵c與指定值d。假設關聯關係已存在。則替換舊值d。返回舊值d, 假設無映射關係,則返回null dist.put(c, d); q.add(c); // 將指定元素c插入隊列,成功時返回true。假設沒有可用空間,則返回illegalStateException } } } } private static void dfs(HashMap<Character , LinkedList<Character>> graph,HashMap<Character, Boolean> visited) { visit(graph, visited, 's'); } private static void visit(HashMap<Character , LinkedList<Character>> graph,HashMap<Character, Boolean> visited,char start) { if (!visited.containsKey(start)) { count++; System.out.println("The time into element " + start + ":" + count);// 記錄進入該節點的時間 visited.put(start, true); for (Character c : graph.get(start)) { if (!visited.containsKey(c)) { visit(graph, visited, c);// 遞歸訪問其鄰近節點 } } count++; System.out.println("The time out element " + start + ":" + count);// 記錄離開該節點的時間 } } public static void main(String args[]) { BFS bb = new BFS(); // s頂點的鄰接表 LinkedList<Character> list_s = new LinkedList<Character>(); list_s.add('w'); list_s.add('r'); LinkedList<Character> list_w = new LinkedList<Character>(); list_w.add('s'); list_w.add('x'); list_w.add('i'); LinkedList<Character> list_r = new LinkedList<Character>(); list_r.add('s'); list_r.add('v'); LinkedList<Character> list_x = new LinkedList<Character>(); list_x.add('w'); list_x.add('y'); list_x.add('u'); LinkedList<Character> list_v = new LinkedList<Character>(); list_v.add('r'); LinkedList<Character> list_i = new LinkedList<Character>(); list_i.add('w'); LinkedList<Character> list_u = new LinkedList<Character>(); list_u.add('x'); LinkedList<Character> list_y = new LinkedList<Character>(); list_y.add('x'); HashMap<Character, LinkedList<Character>> graph = new HashMap<Character, LinkedList<Character>>(); graph.put('s', list_s); graph.put('w', list_w); graph.put('r', list_r); graph.put('x', list_x); graph.put('v', list_v); graph.put('i', list_i); graph.put('y', list_y); graph.put('u', list_u); System.out.println("BFS starts:"); HashMap<Character, Integer> dist = new HashMap<Character, Integer>(); char start = 's'; bb.bfs(graph, dist, start); System.out.println("DFS starts:"); HashMap<Character, Boolean> visited=new HashMap<Character, Boolean>(); bb.dfs(graph, visited); } }java

相關文章
相關標籤/搜索