This repository acts as a personal archive for my solutions to EdX course *Data Structures and Software Design* from PennX.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.9 KiB

  1. import java.util.List;
  2. import java.util.LinkedList;
  3. import java.util.HashMap;
  4. import java.util.Queue;
  5. import java.util.Stack;
  6. import java.util.Set;
  7. import java.util.HashSet;
  8. /*
  9. * SD2x Homework #6
  10. * Implement the methods below according to the specification in the assignment description.
  11. * Please be sure not to change the signature of any of the methods!
  12. */
  13. public class GraphUtils {
  14. public static int minDistance(Graph graph, String src, String dest) {
  15. if(graph == null || src == null || dest == null) return -1;
  16. if(!graph.containsElement(src) || !graph.containsElement(dest)) return -1;
  17. if(src.equals(dest)) return 0;
  18. Queue<Node> toExplore = new LinkedList<Node>();
  19. HashMap<Node,Integer> explored = new HashMap<Node, Integer>();
  20. toExplore.add(graph.getNode(src));
  21. explored.put(graph.getNode(src),0);
  22. while(!toExplore.isEmpty()){
  23. for(Edge e : graph.getNodeEdges(toExplore.poll())){
  24. if(e.getDestination().getElement().equals(dest)) return explored.get(e.getSource()) + 1;
  25. if(explored.containsKey(e.getDestination())) continue;
  26. toExplore.add(e.getDestination());
  27. explored.put(e.getDestination(), explored.get(e.getSource()) + 1);
  28. }
  29. }
  30. return -1;
  31. }
  32. public static Set<String> nodesWithinDistance(Graph graph, String src, int distance) {
  33. HashMap<String, Integer> nodes = new HashMap<String, Integer>();
  34. Queue<Node> toExplore = new LinkedList<Node>();
  35. if(graph == null || src == null) return null;
  36. if(!graph.containsElement(src)) return null;
  37. if(distance < 1) return null;
  38. nodes.put(src, 0);
  39. toExplore.add(graph.getNode(src));
  40. while(!toExplore.isEmpty()){
  41. for(Edge e : graph.getNodeEdges(toExplore.poll())){
  42. if(nodes.containsKey(e.getDestination().getElement())) continue;
  43. if(nodes.get(e.getSource().getElement()) == distance) continue;
  44. nodes.put(e.getDestination().getElement(), nodes.get(e.getSource().getElement()) + 1);
  45. toExplore.add(e.getDestination());
  46. }
  47. }
  48. nodes.remove(src);
  49. return nodes.keySet();
  50. }
  51. public static boolean isHamiltonianPath(Graph g, List<String> values) {
  52. if(g == null || values == null) return false;
  53. if(values.isEmpty()) return false;
  54. if(!values.get(0).equals(values.get(values.size() - 1))) return false;
  55. if(g.getNumNodes() != values.size() - 1) return false;
  56. HashSet<String> visited = new HashSet<String>();
  57. visited.add(values.get(0));
  58. for(int i = 1; i < values.size() - 1; i++){
  59. if(visited.contains(values.get(i))) return false;
  60. if(!g.getNodeNeighbors(g.getNode(values.get(i))).contains(g.getNode(values.get(i + 1)))) return false;
  61. }
  62. return true;
  63. }
  64. }