/** * @param table_list lista de tabelas * @param visited_tables lista de tabelas visitadas * @param final_table tabela final/pretendida */ public static Node iterativeDepthFirstSearch( int[][] initial_table, int[][] final_table, int max_depth) { List table_list = new List(); table_list.addFirst(initial_table, 0, ""); List visited_tables = new List(); int[][] current_table = new int[3][3]; int depth = 0; String path = new String(); while (!table_list.isEmpty()) { List child_nodes = new List(); path = table_list.getPath(); depth = table_list.getDepth(); current_table = table_list.remove(); if (isSolution(current_table, final_table)) return new Node(current_table, depth, path, null); visited_tables.addFirst(current_table, depth, path); if (depth < max_depth) { child_nodes = playDFS(child_nodes, current_table, depth, path); // verifica se algum dos filhos já foi visitado while (!child_nodes.isEmpty()) { int d = child_nodes.getDepth(); String s = child_nodes.getPath(); int[][] child_table = child_nodes.remove(); if (!visited_tables.contains(child_table)) table_list.addFirst(child_table, d, s); } } } return iterativeDepthFirstSearch(initial_table, final_table, max_depth + 1); }
/** * pesquisa A* * * @param table_list lista de tabelas * @param visited_tables lista de tabelas visitadas * @param final_table tabela final/pretendida * @return */ public static Node aStarSearch(PriorityQueue<Node> table_list, int[][] final_table) { int[][] current_table = new int[3][3]; int depth = 0; String path = new String(); while (!table_list.isEmpty()) { List child_nodes = new List(); Node current_node = table_list.poll(); path = current_node.getPath(); depth = current_node.getDepth(); current_table = current_node.getTable(); /* System.out.println("custo: "+cost+"depth: "+depth); System.out.println("Tabela: "); for(int i=0;i<3;i++){ for(int j=0;j<3;j++) System.out.print(current_table[i][j]); System.out.println(); } */ if (isSolution(current_table, final_table)) return new Node(current_table, depth, path, null); child_nodes = playBFS(child_nodes, current_table, depth, path); // verifica se algum dos filhos já foi visitado while (!child_nodes.isEmpty()) { int d = child_nodes.getDepth(); String s = child_nodes.getPath(); int[][] child_table = child_nodes.remove(); int c = d + getDistanceTo(child_table, final_table); Node child = new Node(child_table, d, s, c, null); table_list.add(child); } } throw new Error("Nao encontrou solucao"); }
/** * pesquisa gulosa * * @param table_list lista de tabelas * @param visited_tables lista de tabelas visitadas * @param final_table tabela final/pretendida */ public static Node greedySearch( PriorityQueue<Node> table_list, List visited_tables, int[][] final_table) { int[][] current_table = new int[3][3]; int depth = 0; String path = new String(); while (!table_list.isEmpty()) { List child_nodes = new List(); Node current_node = table_list.poll(); path = current_node.getPath(); depth = current_node.getDepth(); current_table = current_node.getTable(); if (isSolution(current_table, final_table)) return new Node(current_table, depth, path, null); visited_tables.addFirst(current_table, depth, path); child_nodes = playBFS(child_nodes, current_table, depth, path); // verifica se algum dos filhos já foi visitado while (!child_nodes.isEmpty()) { int d = child_nodes.getDepth(); String s = child_nodes.getPath(); int[][] child_table = child_nodes.remove(); if (!visited_tables.contains(child_table)) { int c = getDistanceTo(child_table, final_table); Node child = new Node(child_table, d, s, c, null); table_list.add(child); } } } throw new Error("Nao encontrou solucao"); }
/** * pesquisa em largura * * @param table_list lista de tabelas * @param visited_tables lista de tabelas visitadas * @param final_table tabela final/pretendida * @return depth profundidade da solução */ public static Node breadthFirstSearch(List table_list, List visited_tables, int[][] final_table) { int[][] current_table = new int[3][3]; int depth = 0; String path = new String(); while (!table_list.isEmpty()) { List child_nodes = new List(); path = table_list.getPath(); depth = table_list.getDepth(); current_table = table_list.remove(); if (isSolution(current_table, final_table)) return new Node(current_table, depth, path, null); visited_tables.addFirst(current_table, depth, path); child_nodes = playBFS(child_nodes, current_table, depth, path); // verifica se algum dos filhos já foi visitado while (!child_nodes.isEmpty()) { int d = child_nodes.getDepth(); String s = child_nodes.getPath(); int[][] child_table = child_nodes.remove(); if (!visited_tables.contains(child_table)) table_list.addLast(child_table, d, s); } } throw new Error("Nao encontrou solucao"); }