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

public class UndirectedGraph extends Graph {
public UndirectedGraph() {
super();
}
@Override
public boolean addEdge(Node node1, Node node2) {
addNode(node1); // only adds if node not already in graph.
addNode(node2);
boolean addEdgeSuccess = (
addEdgeFromTo(node1, node2)
&& addEdgeFromTo(node2, node1));
if (addEdgeSuccess) {
numEdges++;
}
return addEdgeSuccess;
}
@Override
public boolean removeEdge(Node node1, Node node2) {
if (!containsNode(node1) || !containsNode(node2)) {
return false;
}
boolean removeEdgeSuccess = (
removeEdgeFromTo(node1, node2)
&& removeEdgeFromTo(node2, node1));
if (removeEdgeSuccess) {
numEdges--;
}
return removeEdgeSuccess;
}
}