ログインしてさらにmixiを楽しもう

コメントを投稿して情報交換!
更新通知を受け取って、最新情報をゲット!

Javaの課題丸投げコミュの探索プログラム

  • mixiチェック
  • このエントリーをはてなブックマークに追加
連続ですみませんがもう一問お願いします。

以下は探索プログラムの一例である。
(元の問題文が英語で、エキサイト翻訳で和訳にしています)


幅優先探索の例
図:http://members.jcom.home.ne.jp/pokemon_glider/Ex5_1.jpg
アルゴリズム解説:http://members.jcom.home.ne.jp/pokemon_glider/Ex5_1.txt
参照ソースコード:http://members.jcom.home.ne.jp/pokemon_glider/Ex5_1code.txt

深さ優先探索の例
図:http://members.jcom.home.ne.jp/pokemon_glider/Ex5_2.jpg
アルゴリズム解説:http://members.jcom.home.ne.jp/pokemon_glider/Ex5_2.txt
参照ソースコード:http://members.jcom.home.ne.jp/pokemon_glider/Ex5_2code.txt


プログラム作成の手順
1.Nodeクラスの作成:http://members.jcom.home.ne.jp/pokemon_glider/node.txt
2.木かグラフのコンストラクタを作る:http://members.jcom.home.ne.jp/pokemon_glider/tree.txt
3.探索プログラム
幅優先探索プログラム:http://members.jcom.home.ne.jp/pokemon_glider/breadth.txt
★深さ優先探索プログラム:http://members.jcom.home.ne.jp/pokemon_glider/depth.txt
4.メインメソッド:http://members.jcom.home.ne.jp/pokemon_glider/main.txt


出力サンプル
幅優先探索プログラム:http://members.jcom.home.ne.jp/pokemon_glider/breadthsample.txt
深さ優先探索プログラム:http://members.jcom.home.ne.jp/pokemon_glider/depthsample.txt



以上において、★の深さ優先探索プログラムのソースコードを完成させよ、というのが問題です。
長くなってしまってすみませんが、よろしくお願いします。

コメント(4)

エキサイト和訳は意味不明なので、英語の原文でのアルゴリズム解説を貼り付けてください。そうしたら回答できると思います。
すみません、それでは原文のアルゴリズムを貼ります。

幅優先探索プログラムのアルゴリズム
The breadth-first search algorithm searches a state space by constructing a hierarchical tree structure consisting of a set of nodes and links. The algorithm defines a way to move through the tree structure, examining the value at nodes. From the start, it looks at each node one edge away. Then it moves out from those nodes to all two edges away from the start. This continues until either the goal node is found or the entire tree is searched.

The algorithm follows:
1.Create a queue and add the first node to it.
2.Loop:
If the queue is empty, quit.
Remove the first node from the queue.
If the node contains the goal state, then exit with the node as the solution.
For each child of the current node: add the new state to the back of the queue.


深さ優先探索プログラム
The depth-first algorithm follows a single branch of the tree down as many levels as possible until we either reach a solution or a dead end. It searches from the start or root node all the way down to a leaf node. If it does not find the goal node, it backtracks up the tree and searches down the next untested path until it reaches the next leaf.

The algorithm follows:
1.Create a queue and add the first node to it.
2.Loop:
If the queue is empty, quit.
Remove the first node from the queue.
If the node contains the goal state, then exit with the node as the solution.
For each child of the current node: add the new state to the front of the queue.


以上です。よろしくお願いします。
できました。出力結果もサンプルと一致します。

public void depthFirst() {
Vector open = new Vector();
open.addElement(node[0]);
Vector closed = new Vector();
boolean success = false;
int step = 0;
for (;;) {
System.out.println("STEP:" + (step++));
System.out.println("OPEN:" + open.toString());
System.out.println("closed:" + closed.toString());
if (open.size() == 0) {// openは空か?
success = false;
break;
} else {
Node node = (Node) open.elementAt(0);
open.removeElementAt(0);
if (node == goal) {
success = true;
break;
} else {
Vector children = node.getChildren();
closed.addElement(node);
for (int i = children.size() - 1; i >= 0; i--) {
// for(int i = 0 ; i < children.size() ; i++){
Node m = (Node) children.elementAt(i);
if (!open.contains(m) && !closed.contains(m)) {
m.setPointer(node);
open.insertElementAt(m, 0);
if (m == goal) {
success = true;
break;
}
}
}
}
}
}
if (success) {
System.out.println("*** Solution ***");
printSolution(goal);
}
}
ありがとうございます!確認致しました。

ログインすると、みんなのコメントがもっと見れるよ

mixiユーザー
ログインしてコメントしよう!

Javaの課題丸投げ 更新情報

Javaの課題丸投げのメンバーはこんなコミュニティにも参加しています

星印の数は、共通して参加しているメンバーが多いほど増えます。

人気コミュニティランキング