ALG2012-C.ppt

Similar documents
ALG2012-A.ppt

untitled

アルゴリズムとデータ構造1

ALG2012-F.ppt

8 if switch for while do while 2

class IntCell { private int value ; int getvalue() {return value; private IntCell next; IntCell next() {return next; IntCell(int value) {this.value =

class IntCell { private int value ; int getvalue() {return value; private IntCell next; IntCell next() {return next; IntCell(int value) {this.value =

アルゴリズムとデータ構造1

新・明解Java入門

Microsoft Word - keisankigairon.ch doc

K227 Java 2

解きながら学ぶJava入門編

I java A

やさしいJavaプログラミング -Great Ideas for Java Programming サンプルPDF

明解Javaによるアルゴリズムとデータ構造

r1.dvi

アルゴリズムとデータ構造1

Java (9) 1 Lesson Java System.out.println() 1 Java API 1 Java Java 1

目的 泡立ち法を例に Comparableインターフェイスの実装 抽象クラスの利用 型パラメタの利用 比較 入替 の回数を計測

2

: : : TSTank 2

Exam : 1z1-809-JPN Title : Java SE 8 Programmer II Vendor : Oracle Version : DEMO Get Latest & Valid 1z1-809-JPN Exam's Question and Answers 1 from Ac

明解Javaによるアルゴリズムとデータ構造

Java講座

(Eclipse\202\305\212w\202\324Java2\215\374.pdf)

人工知能入門

10K pdf

3 Java 3.1 Hello World! Hello World public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World");

新・明解Javaで学ぶアルゴリズムとデータ構造

ALG ppt

Java学習教材

Java演習(4) -- 変数と型 --

2

JavaプログラミングⅠ

Java プログラミング Ⅰ 7 回目 switch 文と論理演算子 今日の講義講義で学ぶ内容 switch 文 論理演算子 条件演算子 条件判断文 3 switch 文 switch 文 式が case のラベルと一致する場所から直後の break; まで処理しますどれにも一致致しない場合 def

JavaプログラミングⅠ

問題1 以下に示すプログラムは、次の処理をするプログラムである

JavaプログラミングⅠ

Javaプログラムの実行手順


問 次の Fortran プログラムの説明及びプログラムを読んで、設問に答えよ。

Java Java Java Java Java 4 p * *** ***** *** * Unix p a,b,c,d 100,200,250,500 a*b = a*b+c = a*b+c*d = (a+b)*(c+d) = 225

. IDE JIVE[1][] Eclipse Java ( 1) Java Platform Debugger Architecture [5] 3. Eclipse GUI JIVE 3.1 Eclipse ( ) 1 JIVE Java [3] IDE c 016 Information Pr

新・明解Javaで学ぶアルゴリズムとデータ構造

シミュレーションの簡単な例 GUI 無しのシミュレーションを作る GUI を作る パラメタを設定するデモンストレーションをする 2 オブジェクト指向プログラミング特論

r02.dvi


JavaプログラミングⅠ

問題 01 以下は コンソールより年齢を入力させ その年齢にあった料金を表示するプログラムである 年齢ごとの金額は以下の通りである 年齢の範囲金額 0 歳以上 6 歳以下 120 円 7 歳以上 65 歳未満 200 円 65 歳以上無料 package j1.exam02; import java

JAVA 11.4 PrintWriter 11.5

2 static final int DO NOTHING ON CLOSE static final int HIDE ON CLOSE static final int DISPOSE ON CLOSE static final int EXIT ON CLOSE void setvisible

(Basic Theory of Information Processing) 1

Vector Vector Vector Vector() Vector(int n) n Vector(int n,int delta) n delta

break 文 switch ブロック内の実行中の処理を強制的に終了し ブロックから抜けます switch(i) 強制終了 ソースコード例ソースファイル名 :Sample7_1.java // 入力値の判定 import java.io.*; class Sample7_1 public stati

226

プログラミング入門1

JavaプログラミングⅠ

5 p Point int Java p Point Point p; p = new Point(); Point instance, p Point int 2 Point Point p = new Point(); p.x = 1; p.y = 2;

IE6 2 BMI chapter1 Java 6 chapter2 Java 7 chapter3 for if 8 chapter4 : BMI 9 chapter5 Java GUI 10 chapter6 11 chapter7 BMI 12 chap

問 次の Fortran プログラムの説明及びプログラムを読んで、設問に答えよ。

Java updated


II Java :30 12:00 I. I IV II. III. IV. ( a d) V. : this==null, T == N A ActionListener C class D actionperformed G getsource I implements K

JAVA入門

Transcription:

2012717 (sakai.keiichi@kochi-tech.ac.jp) http://www.info.kochi-tech.ac.jp/k1sakai/lecture/alg/2012/index.html 1

1. 2. 2

.. 3

public class WeightedNode<E> { private E value; // private Map<WeightedNode<E>, Integer> edges; // private boolean visited; private int distance; public WeightedNode(E value, Map<WeightedNode<E>, Integer> edges) { this.value = value; this.edges = edges; reset(); public void reset(){ this.visited = false; this.distance = Integer.MAX_VALUE; this.edges.clear(); public E getvalue() { // return value; public Map<WeightedNode<E>, Integer> getedges(){ return this.edges; // public void connectto(weightednode<e> to, int weight){ this.edges.put(to, weight); // 4

public class Dijkstra { public static <E> void search(collection<weightednode<e>> graph, WeightedNode<E> start){ Set<WeightedNode<E>> visited = new HashSet<WeightedNode<E>>(); Set<WeightedNode<E>> unreached = new HashSet<WeightedNode<E>>(graph); start.setdistance(0); while(!unreached.isempty() ){ int min_distance = Integer.MAX_VALUE; WeightedNode<E> min_node = null; for(weightednode<e> node: unreached){ if(node.getdistance() < min_distance){ min_distance = node.getdistance(); min_node = node; unreached.remove(min_node); visited.add(min_node); for(map.entry<weightednode<e>,integer> edge: min_node.getedges().entryset()){ if(unreached.contains(edge.getkey())){ edge.getkey().setdistance( Math.min(edge.getKey().getDistance(), min_node.getdistance() + edge.getvalue())); 5

private static WeightedNode<Character> nodea = new WeightedNode<Character>('A', new HashMap<WeightedNode<Character>, Integer>()); private static WeightedNode<Character> nodeb = new WeightedNode<Character>('B', new HashMap<WeightedNode<Character>, Integer>()); private static WeightedNode<Character> nodec = new WeightedNode<Character>('C', new HashMap<WeightedNode<Character>, Integer>()); private static WeightedNode<Character> noded = new WeightedNode<Character>('D', new HashMap<WeightedNode<Character>, Integer>()); private static WeightedNode<Character> nodee = new WeightedNode<Character>('E', new HashMap<WeightedNode<Character>, Integer>()); private static WeightedNode<Character> nodef = new WeightedNode<Character>('F', new HashMap<WeightedNode<Character>, Integer>()); private static Collection<WeightedNode<Character>> test_data = new LinkedList<WeightedNode<Character>>(); static { test_data.add(nodea); test_data.add(nodeb); test_data.add(nodec); test_data.add(noded); test_data.add(nodee); test_data.add(nodef); public static void main(string[] args) { System.out.println("4.5.1"); for(weightednode<character> node: test_data){ node.reset(); nodea.connectto(nodeb, 6); nodea.connectto(nodec, 4); nodeb.connectto(noded, 3); nodec.connectto(nodee, 3); nodec.connectto(nodef, 6); nodee.connectto(nodeb, 2); nodee.connectto(noded, 1); search(test_data, nodea); System.out.println("" + nodea.getvalue() + ""); for(weightednode<character> node: test_data){ System.out.println("" + node.getvalue() + ": " + node.getdistance()); 6

ü ü ü ü ü ü 7

public class DijkstraArray { public static <E> void search(weightednode<e>[] nodes, int start, int[][] weights){ nodes[start].setdistance(0); for(int step = 0; step < nodes.length; step++){ int min = Integer.MAX_VALUE; int p = 0; for(int x = 0; x < nodes.length; x++){ if(!nodes[x].isvisited() && (nodes[x].getdistance() < min)){ p = x; min = nodes[x].getdistance(); if(min == Integer.MAX_VALUE){ throw new IllegalArgumentException(""); nodes[p].setvisited(true); for(int x = 0; x < nodes.length; x++){ if(weights[p][x] == Integer.MAX_VALUE){ continue; nodes[x].setdistance( Math.min(nodes[x].getDistance(), nodes[p].getdistance() + weights[p][x])); 8

private static WeightedNode<Character> nodea = new WeightedNode<Character>('A'); private static WeightedNode<Character> nodeb = new WeightedNode<Character>('B'); private static WeightedNode<Character> nodec = new WeightedNode<Character>('C'); private static WeightedNode<Character> noded = new WeightedNode<Character>('D'); private static WeightedNode<Character> nodee = new WeightedNode<Character>('E'); private static WeightedNode<Character> nodef = new WeightedNode<Character>('F'); @SuppressWarnings("unchecked") private static WeightedNode<Character>[] test_data = new WeightedNode[] { nodea, nodeb, nodec, noded, nodee, nodef ; public static void main(string[] args) { System.out.println("4.5.1"); for(weightednode<character> node: test_data){ node.reset(); int[][] weights = new int[test_data.length][test_data.length]; for(int[] from: weights){ for(int i = 0; i < from.length; i++){ from[i] = Integer.MAX_VALUE; weights[0][1] = 6; weights[0][2] = 4; weights[1][3] = 3; weights[2][4] = 3; weights[2][5] = 6; weights[4][1] = 2; weights[4][3] = 1; search(test_data, 0, weights); System.out.println("" + nodea.getvalue() + ""); for(weightednode<character> node: test_data){ System.out.println("" + node.getvalue() + ": " + node.getdistance()); 9

10

public class Floyd { public static <E> void search(int[][] weights, int[][] distances){ for(int i = 0; i < weights.length; i++){ distances[i] = weights[i].clone(); for(int k = 0; k < weights.length; k++){ for(int i = 0; i < weights.length; i++){ if(distances[i][k] == Integer.MAX_VALUE){ continue; // or for(int j = 0; j < weights[i].length; j++){ if(distances[k][j] == Integer.MAX_VALUE){ continue; // or int w = distances[i][k] + distances[k][j]; if(w < distances[i][j]){ distances[i][j] = w; 11

public static void main(string[] args) { System.out.println("4.5.1"); int[][] weights = new int[6][6]; for(int i = 0; i < weights.length; i++){ for(int j = 0; j < weights[i].length; j++){ weights[i][j] = Integer.MAX_VALUE; weights[i][i] = 0; weights[0][1] = 6; weights[0][2] = 4; weights[1][3] = 3; weights[2][4] = 3; weights[2][5] = 6; weights[4][1] = 2; weights[4][3] = 1; int[][] distances = new int[weights.length][]; search(weights, distances); System.out.println(""); for(int i = 0; i < distances.length; i++){ for(int j = 0; j < distances[i].length; j++){ if(distances[i][j] == Integer.MAX_VALUE){ System.out.print("- "); else { System.out.print(distances[i][j] + " "); System.out.println(); 12

a [ i, j] true = false 13

public class Warshall { public static <E> void search(boolean[][] adjacency, boolean[][] reachable){ for(int i = 0; i < adjacency.length; i++){ reachable[i] = adjacency[i].clone(); for(int k = 0; k < adjacency.length; k++){ for(int i = 0; i < adjacency.length; i++){ if(!reachable[i][k] ){ continue; for(int j = 0; j < adjacency[i].length; j++){ reachable[i][j] = reachable[i][j] reachable[k][j]; 14

public static void main(string[] args) { System.out.println("4.5.1"); boolean[][] adjacency = new boolean[6][6]; for(int i = 0; i < adjacency.length; i++){ for(int j = 0; j < adjacency[i].length; j++){ adjacency[i][j] = false; adjacency[i][i] = true; adjacency[0][1] = true; adjacency[0][2] = true; adjacency[1][3] = true; adjacency[2][4] = true; adjacency[2][5] = true; adjacency[4][1] = true; adjacency[4][3] = true; boolean[][] reachable = new boolean[adjacency.length][]; search(adjacency, reachable); System.out.println(""); for(int i = 0; i < reachable.length; i++){ for(int j = 0; j < reachable[i].length; j++){ if(reachable[i][j]){ System.out.print("+ "); else { System.out.print("- "); System.out.println(); 15

: : : : () PHS 16