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.

123 lines
2.8 KiB

  1. import java.util.HashMap;
  2. import java.util.HashSet;
  3. import java.util.Iterator;
  4. import java.util.Map;
  5. import java.util.Set;
  6. public abstract class Graph {
  7. protected Map<Node, Set<Edge>> adjacencySets;
  8. protected int numNodes;
  9. protected int numEdges;
  10. protected Map<String, Node> elementToNode;
  11. /*
  12. * These methods need to be overridden by subclasses.
  13. */
  14. public abstract boolean addEdge(Node node1, Node node2);
  15. public abstract boolean removeEdge(Node node1, Node node2);
  16. public Graph() {
  17. adjacencySets = new HashMap<Node, Set<Edge>>();
  18. elementToNode = new HashMap<String, Node>();
  19. numNodes = 0;
  20. numEdges = 0;
  21. }
  22. public boolean addNode(Node newNode) {
  23. if (newNode == null || containsNode(newNode)) {
  24. return false;
  25. }
  26. Set<Edge> newAdjacencySet = new HashSet<Edge>();
  27. adjacencySets.put(newNode, newAdjacencySet);
  28. elementToNode.put(newNode.getElement(), newNode);
  29. numNodes++;
  30. return true;
  31. }
  32. public Set<Node> getNodeNeighbors(Node node) {
  33. if (!containsNode(node)) {
  34. return null;
  35. }
  36. Set<Edge> nodeEdges = adjacencySets.get(node);
  37. Set<Node> nodeNeighbors= new HashSet<Node>();
  38. for (Edge e : nodeEdges) {
  39. Node neighbor = e.getDestination();
  40. nodeNeighbors.add(neighbor);
  41. }
  42. return nodeNeighbors;
  43. }
  44. protected boolean addEdgeFromTo(Node source,
  45. Node destination) {
  46. Edge newEdge = new Edge(source, destination);
  47. Set<Edge> sourceEdges = adjacencySets.get(source);
  48. if (!sourceEdges.contains(newEdge)) {
  49. sourceEdges.add(newEdge);
  50. return true;
  51. }
  52. return false;
  53. }
  54. protected boolean removeEdgeFromTo(Node source,
  55. Node destination) {
  56. Edge toRemove = new Edge(source, destination);
  57. Set<Edge> sourceEdges = adjacencySets.get(source);
  58. if (sourceEdges.contains(toRemove)) {
  59. sourceEdges.remove(toRemove);
  60. return true;
  61. }
  62. return false;
  63. }
  64. public int getNumNodes() {
  65. return numNodes;
  66. }
  67. public int getNumEdges() {
  68. return numEdges;
  69. }
  70. public Node getStartingNode() {
  71. Iterator<Node> iter = adjacencySets.keySet().iterator();
  72. if (iter.hasNext()) {
  73. return iter.next();
  74. }
  75. return null;
  76. }
  77. public Set<Node> getAllNodes() {
  78. return adjacencySets.keySet();
  79. }
  80. public Set<Edge> getNodeEdges(Node node) {
  81. if (!containsNode(node)) {
  82. return null;
  83. }
  84. return adjacencySets.get(node);
  85. }
  86. public boolean containsNode(Node node) {
  87. return adjacencySets.containsKey(node);
  88. }
  89. public Node getNode(String element) {
  90. if (!elementToNode.containsKey(element)) {
  91. Node newNode = new Node(element);
  92. elementToNode.put(element, newNode);
  93. return newNode;
  94. }
  95. return elementToNode.get(element);
  96. }
  97. public boolean containsElement(String element) {
  98. return elementToNode.containsKey(element);
  99. }
  100. }