1. 程式人生 > >BFS和DFS的java實現

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);
	}
}