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.

38 lines
794 B

  1. public class UndirectedGraph extends Graph {
  2. public UndirectedGraph() {
  3. super();
  4. }
  5. @Override
  6. public boolean addEdge(Node node1, Node node2) {
  7. addNode(node1); // only adds if node not already in graph.
  8. addNode(node2);
  9. boolean addEdgeSuccess = (
  10. addEdgeFromTo(node1, node2)
  11. && addEdgeFromTo(node2, node1));
  12. if (addEdgeSuccess) {
  13. numEdges++;
  14. }
  15. return addEdgeSuccess;
  16. }
  17. @Override
  18. public boolean removeEdge(Node node1, Node node2) {
  19. if (!containsNode(node1) || !containsNode(node2)) {
  20. return false;
  21. }
  22. boolean removeEdgeSuccess = (
  23. removeEdgeFromTo(node1, node2)
  24. && removeEdgeFromTo(node2, node1));
  25. if (removeEdgeSuccess) {
  26. numEdges--;
  27. }
  28. return removeEdgeSuccess;
  29. }
  30. }