15パズルの問題を理解するために、深度最初の検索と幅最初の検索について学びました。まず、深さfirst検索(DFS)について説明しましょう。深さ第一の目的は、開始頂点から最も遠いパスの検索に優先順位を付けることです。一方、幅検索は、最初に開始頂点に最も近いパスを最初に検索することです。深度最初の検索とバックトラッキングの違いは何だろうか? Baiduでは、バックトラッキングは一種の深い検索であると言われていますが、違いはバックトラッキングが検索ツリーを保持しないことです。では、Browness-First Search(BFS)はどうですか?そのアプリケーションは何ですか?回答:最短経路、ワイン部門の問題、8つのデジタル問題など。ポイントに戻りましょう。ここでは、Javaを使用して幅広い検索と深い検索を実装しました。その中で、グラフ +スタックを使用してディープ検索が実装され、グラフ +キューを使用して幅広い検索が実装されます。コードは次のとおりです。
1.「無向グラフ」を表す新しいクラスNodirectionGraphを作成する
パッケージcom.wly.algorithmbase.dataStructure;/** *無向グラフ * @author wly * */public class nodirectiongraph {private int mmaxsize; //グラフに含まれるグラフに含まれる頂点の最大数[] vertexlist; Private int nvertex; //現在保存されている頂点の数public nodirectiongraph(int maxsize){mmaxsize = maxsize; vertexlist = new graphvertex [mmaxsize]; indicatormat = new int [mmaxsize] [mmaxsize]; nvertex = 0; // j = 0; j <mmaxsize; j ++){for(int k = 0; k <mmaxsize; k ++){indicatormat [j] = 0;}}} public void addvertex(graphvertex v){if(nvertex <mmaxsize){vertexlist [nevertex ++ v;} {system.out.println( "---挿入が失敗し、頂点の数が上限に達しました!");}/***隣接するマトリックスを変更し、@param start* @param end*/public void addedge(int start、int end){indicatormat [start] = 1; indicatormat [in indicatormat] = 1; indicatormat隣接Matrix*/public void printindicatormat(){for(int [] line:indicatormat){for(int i:line){system.out.out.out.out.out.println();}}/**** deptexindex index index index index index index index index index index indexグラフのマトリックス */public void dfs(int vertexindex){arraystack stack = new arraystack(); // 1。検索要素をstack vertexlist [vertexindex] .setVisited(true); stack.push(vertexindex); int nextexexindex = getNextVertExIndex(vertexIndex); while(!stack.isempty()){//スタックを継続的に押し、スタックが空になるまでスタックを連続して押して(検索要素がスタックをポップアップしない)if(nextexexindex!= -1){vertexlist [nextextexindex] .setvisted(true); stack.push(nextexindex); stack.printelems(); stack.printelems( {stack.pop();} //現在の上部要素に他の非トラバースノードが含まれているかどうかを取得if(!stack.isempty()){nextexingindex = getNextVertExIndex(stack.peek());}}}/** *列){for(int i = 0; i <indicatormat [column] .length; i ++){if(indicatormat [i] == 1 &&!vertexlist [i] .isvisited()){return i;}} return -1;}/*** bedsals raversal* @param vertexIndexの通りには、numbersal* bedsal*グラフの隣接マトリックスの行*/public void bfs(int vertexIndex){chainqueue queue = new Chainqueue(); vertexlist [vertexindex] .setVisited(true); queue.insert(new Queenode(vertexindex)); while(!queue.isempty()){if(nextExtExIndex!= -1){vertexList [nextExtExIndex] .setVisited(true); queue.insert(nextExtExindex));} else {queue.remove();} if(! getNextVertExIndex(queue.peek()。data); queue.printelems();}}}}}}}}}}}}2。次に、配列でシミュレートされる配列スタックがあります
パッケージcom.wly.algorithmbase.dataStructure;/***arrayを使用してスタック構造*@author wly**/public class arraystack {private int [] tarray; private topindex = -1; //スタックの現在のトップ要素のインデックス位置を示します。 array ***/tarray = new int [capulation_step];}/***top top要素をポップアップするメソッド*@return*/public int pop(){if(isempty()){system.out.println( "エラー、スタックの要素は空です、ポップできません"); return -1;} else {int i = tarray [topindex]; tarray [topindex-] = -1; // pop element Return i int [tarray.length + caperation_step]; for(int i = 0; i <tarray.length; i ++){temparray [i] = tarray [i];} tarray = temparray = null;} else {topindex ++; tarray [t;}}/** * {if(isempty()){system.out.println( "エラー、スタックの要素が空である、覗くことはできません"); return -1;} else {return tarray [topindex];}}/** printeLems(){for(int i = 0; i <= topindex; i ++){system.out.print(tarray [i]+"");} system.out.println();}}}3。リンクされたリストでシミュレートされたキュー内のチェーンキュー
パッケージcom.wly.algorithmbase.datastructure;/** *リンクリストを使用してキュー * * @author wly * */public class chainqueue {private queenode head;キューのテールへのノード*/public void insert(queenode node){//もちろん、Tail.prev = node if(head = node){head = node; tael = head;} else {node.next = tail; tail.prev = node;ノード;} size ++;}/***キューヘッドノードを削除*/public queenode remove(){if(!isempty()){queuenode temp = head = head.prev; size - ; return temp;} else {system.out.out.println(return操作}; empty * * @return */public boolean isempty(){if(size> 0){return false;} else {return true;}}/** * queueの最初のノードを返しますが、削除されませんが、public queenode peek(){){){)現在のキューは空です! "); return null;}}/*** queue size** @return*/public int size(){return size;}/*** coeue*/public void printelems(){queuenode tempnode = head; while! "; tempnode = tempnode.prev;} system.out.println();}}/** * node class * * @author wly * */class queenode {queenode prev; setData(int data){this.data = data;}@override public string toString(){// todo auto-fenerated method stub super.tostring(); return data + "";}}}4。TEST_BFS_DFSをテストします
パッケージcom.wly.algorithmbase.search; import com.wly.algorithmbase.dataStructure.grapbertex; Import com.wly.algorithmbase.datastructure.nodirectiongraph; {//テストデータnodirectiongraph graph = new nodirectiongraph(7); graph.addvertex(new graphvertex( "a")); graph.addvertex(new graphvertex( "b")); graph.addvertex(new graphvertex( "c")); graphvertex( "e")); graph.addvertex(new graphvertex( "f")); graph.addvertex(new graphvertex( "g")); graph.addedge(0、1); graph.addedge(0、2); graph.addedge(1、3); graph.addedge(1、4); graph.addedge(3、6); 5); system.out.println( " - グラフの隣接するマトリックス - "); graph.printindicatormat(); // test deep search system.out.out.println( " - 深い優先度検索 - "); graph.dfs(0); graph = new nodirectiongraph(7); graph.addervertex(new graphex(new graphex(a graph); graphvertex( "b")); graph.addvertex(new graphvertex( "c")); graph.addvertex(new graphvertex( "d")); graph.addvertex(new graphvertex( "e"); graph.addvertex(new graphvertex( "f")); 1); graph.addedge(0、2); graph.addedge(1、3); graph.addedge(1、4); graph.addedge(3、6); graph.addedge(2、5); system.out.println( " - 幅広い優先度検索 - "); graph.bfs(0);}}}ここでテストするグラフ構造は次のとおりです。
操作結果は次のとおりです。
-ADJACENT GRAPHのAdjacentマトリックス - 0 1 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ここでは、上記の深い検索と幅広い検索の実行結果を説明する必要があります。ここで、0、1、2、3 ...それぞれA、B、C、Dに対応しています...それは少し混乱しています、私を許してください~~
ああ~~~
要約します
上記は、グラフベースの深度検索と幅最初の検索完全コードのJavaプログラミングの実装に関するこの記事のすべての内容です。私はそれが誰にでも役立つことを願っています。興味のある友人は、このサイトの他の関連トピックを引き続き参照できます。欠点がある場合は、それを指摘するためにメッセージを残してください。このサイトへのご支援をありがとうございました!