/** * BFS algorith * * @param: print boolean if to print node during traversal * @return List of nodes in order visited */ public ArrayList<String> bfs(boolean print) { ArrayList<String> ops = new ArrayList<>(); GraphNode v = source; ArrayList<GraphNode> Q = new ArrayList<>(); Q.add(v); v.setVisited(true); while (Q.size() != 0) { v = Q.remove(0); if (print) System.out.println(v); ops.add(v.toString()); // if(checkStoppingCondition(v, getN())) { // return; // } List<Integer> edgeTarget = matrix.get(v.getID()); for (int i = 0; i < edgeTarget.size(); ++i) { if (edgeTarget.get(i) == 1) { GraphNode w = info.get(i + 1); if (!w.getVisited()) { Q.add(w); w.setVisited(true); } } } } return ops; }
/** * Recursive DFS algorthm * * @param matrix Map of lists representing adj matrix * @param info Map of nodes representing node info * @param v Node source * @param n int number of nodes in graph * @param: print boolean if to print node during traversal * @return List of nodes in order visited */ public ArrayList<String> dfs_rec( Map<Integer, List<Integer>> matrix, Map<Integer, GraphNode> info, GraphNode v, Integer n, boolean print, ArrayList<String> ops) { v.setVisited(true); if (print) System.out.println(v); ops.add(v.toString()); // if(checkStoppingCondition(v, getN())) { // return; // } List<Integer> edgeTarget = matrix.get(v.getID()); for (int i = 0; i < edgeTarget.size(); ++i) { if (edgeTarget.get(i) == 1) { GraphNode node = info.get(i + 1); if (!node.getVisited()) { return dfs_rec(matrix, info, node, n, print, ops); } } } return ops; }
/** * @param fname String file name to read info from * @return Map indexed by node id of Node classes */ public Map<Integer, GraphNode> getNodeInfo(String fname) { try { BufferedReader br = new BufferedReader(new FileReader(fname)); String line; boolean firstLine = true; Map<Integer, GraphNode> nodes = new HashMap<>(); while ((line = br.readLine()) != null) { if (firstLine) { firstLine = false; continue; } List<String> words = Arrays.asList(line.split("\\s")); ArrayList<String> realWords = new ArrayList<>(); for (String w : words) { if (w.equals("")) continue; else { realWords.add(w); } } if (realWords.size() != 4) { continue; } GraphNode node = new GraphNode( Integer.parseInt(realWords.get(0)), realWords.get(1), realWords.get(2), realWords.get(3)); nodes.put(node.getID(), node); } br.close(); return nodes; } catch (FileNotFoundException e) { e.printStackTrace(); return null; } catch (IOException e) { e.printStackTrace(); return null; } }
/** * Iterative DFS algorithm * * @return List of nodes in order visited */ public ArrayList<String> dfs_iter(boolean print) { ArrayList<String> ops = new ArrayList<>(); GraphNode v = source; List<GraphNode> stack = new ArrayList<>(); stack.add(v); while (stack.size() != 0) { v = stack.remove(stack.size() - 1); if (v.getVisited() == false) { if (print) System.out.println(v); ops.add(v.toString()); v.setVisited(true); List<Integer> edgeTarget = matrix.get(v.getID()); for (int i = 0; i < edgeTarget.size(); ++i) { if (edgeTarget.get(i) == 1) { GraphNode w = info.get(i + 1); stack.add(w); } } } } return ops; }