diff --git a/.gitignore b/.gitignore index 2434886..120106a 100644 --- a/.gitignore +++ b/.gitignore @@ -53,3 +53,4 @@ gradle-app.setting # gimp image file to make pngs of graphs for tests *.xcf +.idea diff --git a/.idea/compiler.xml b/.idea/compiler.xml deleted file mode 100644 index 52adc4f..0000000 --- a/.idea/compiler.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml deleted file mode 100644 index e7bedf3..0000000 --- a/.idea/copyright/profiles_settings.xml +++ /dev/null @@ -1,3 +0,0 @@ - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 9271fca..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - - - - - - - - - - - Android - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index fd3e04d..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules/1748581304/coding-exercises.iml b/.idea/modules/1748581304/coding-exercises.iml deleted file mode 100644 index 100c00b..0000000 --- a/.idea/modules/1748581304/coding-exercises.iml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules/1748581304/coding-exercises_main.iml b/.idea/modules/1748581304/coding-exercises_main.iml deleted file mode 100644 index cf38f7a..0000000 --- a/.idea/modules/1748581304/coding-exercises_main.iml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - - - - - diff --git a/.idea/modules/1748581304/coding-exercises_test.iml b/.idea/modules/1748581304/coding-exercises_test.iml deleted file mode 100644 index 31fda24..0000000 --- a/.idea/modules/1748581304/coding-exercises_test.iml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - - - - - - - diff --git a/src/main/java/graphs/Edge.java b/src/main/java/graphs/Edge.java new file mode 100644 index 0000000..69a174b --- /dev/null +++ b/src/main/java/graphs/Edge.java @@ -0,0 +1,73 @@ +package graphs; + +public class Edge implements Comparable { + private Node leftNode; + private Node rightNode; + private double weight; + + public Edge(Node leftNode, Node rightNode, double weight) { + this.leftNode = leftNode; + this.rightNode = rightNode; + setWeight(weight); + } + + public Edge(Node leftNode, Node rightNode) { + this.leftNode = leftNode; + this.rightNode = rightNode; + setWeight(1); + } + + public Edge(Edge edge) { + this.leftNode = edge.getLeftNode(); + this.rightNode = edge.getRightNode(); + this.weight = edge.getWeight(); + } + + public double getWeight() { + return weight; + } + + public Edge setWeight(double weight) { + this.weight = weight; + return this; + } + + public Node getLeftNode() { + return leftNode; + } + + public Node getRightNode() { + return rightNode; + } + + public void setLeftNode(Node leftNode) { + this.leftNode = leftNode; + } + + public void setRightNode(Node rightNode) { + this.rightNode = rightNode; + } + + public boolean contains(Node node) { + return leftNode.equals(node) || rightNode.equals(node); + } + + // will return null if node is not in edge, else returns opposite node + public Node getOtherNode(Node node) { + if(contains(node)) { + if(node.equals(getRightNode())) { + return getLeftNode(); + } else { + return getRightNode(); + } + } else { + return null; + } + } + + // "Note: this class has a natural ordering that is inconsistent with equals." + public int compareTo(Edge edge) { + return Double.compare(this.getWeight(), edge.getWeight()); + } + +} diff --git a/src/main/java/graphs/GraphClass.java b/src/main/java/graphs/Graph.java similarity index 50% rename from src/main/java/graphs/GraphClass.java rename to src/main/java/graphs/Graph.java index a3075a8..279f0ad 100644 --- a/src/main/java/graphs/GraphClass.java +++ b/src/main/java/graphs/Graph.java @@ -1,60 +1,82 @@ package graphs; -import java.util.*; -public class GraphClass { - private List edges; - private List nodes; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.LinkedList; +import java.util.Collections; + + +public class Graph { + private List edges; + private List nodes; private boolean isDirected; private boolean isWeighted; - public GraphClass() { - edges = new ArrayList(); - nodes = new ArrayList(); + public Graph() { + edges = new ArrayList(); + nodes = new ArrayList(); setDirected(false); setWeighted(false); } - public GraphClass(List nodes, List edges) { - this.edges = edges; + public Graph(List nodes, List edges) { + // if no flag for isDirected, default false + this(nodes, edges, false); + } + + public Graph(List nodes, List edges, boolean isDirected) { this.nodes = nodes; - setDirected(false); + setDirected(isDirected); + if(isDirected()) { + this.edges = edges; + } else { + this.edges = new ArrayList(); + for(Edge edge : edges) { + addEdge(edge); + } + } setWeighted(false); } - public GraphClass(GraphClass graph) { + public Graph(Graph graph) { edges = graph.getEdges(); nodes = graph.getNodes(); isDirected = graph.isDirected(); isWeighted = graph.isWeighted(); } - public List getEdges() { + public List getEdges() { return edges; } - public List getNodes() { + public List getNodes() { return nodes; } - public void addNode(GraphNode node) { + public void addNode(Node node) { nodes.add(node); } - public void addEdge(GraphEdge edge) { + public void addEdge(Edge edge) { edges.add(edge); + if(! isDirected()) { + edges.add(new Edge(edge.getRightNode(), edge.getLeftNode(), edge.getWeight())); + } } -// public void addEdge(GraphNode leftNode, GraphNode rightNode) { + // public void addEdge(Node leftNode, Node rightNode) { //// weight is the sqrt( ( left.x - right.x) ^ 2 + (left.y - right.y) ^ 2) // double weight = Math.sqrt(Math.pow(leftNode.getX() - rightNode.getX(), 2) + Math.pow((leftNode.getY() - rightNode.getY()),2)); -// edges.add(new GraphEdge(leftNode, rightNode, weight)); +// edges.add(new Edge(leftNode, rightNode, weight)); // } // returns removed node for chaining - public GraphNode removeNode(GraphNode node) { - Set toRemove = new HashSet(); - for(GraphEdge edge : edges) { + public Node removeNode(Node node) { + Set toRemove = new HashSet(); + for(Edge edge : edges) { if(edge.contains(node)) { toRemove.add(edge); } @@ -64,16 +86,13 @@ public GraphNode removeNode(GraphNode node) { return node; } - public void removeEdge(GraphEdge edge) { + public void removeEdge(Edge edge) { edges.remove(edge); } // set directed will work on graph and all edges so can be done at the end - public void setDirected(boolean directed) { - isDirected = directed; - for(GraphEdge edge : edges) { - edge.setDirected(directed); - } + public void setDirected(boolean isDirected) { + this.isDirected = isDirected; } public boolean isDirected() { @@ -89,10 +108,10 @@ public void setWeighted(boolean weighted) { isWeighted = weighted; } - public List getIncidentEdges(GraphNode node) { - List incidentEdges = new LinkedList(); - for(GraphEdge edge : edges) { - if(edge.getLeftNode().equals(node) || (edge.getRightNode().equals(node) && ! isDirected)) { + public List getIncidentEdges(Node node) { + List incidentEdges = new LinkedList(); + for(Edge edge : edges) { + if(edge.getLeftNode().equals(node)) { incidentEdges.add(edge); } } diff --git a/src/main/java/graphs/GraphEdge.java b/src/main/java/graphs/GraphEdge.java deleted file mode 100644 index 47004d7..0000000 --- a/src/main/java/graphs/GraphEdge.java +++ /dev/null @@ -1,110 +0,0 @@ -package graphs; - -public class GraphEdge implements Comparable { - private GraphNode leftNode; - private GraphNode rightNode; - private double weight; - private boolean isWeighted; - private boolean isDirected; - - public GraphEdge(GraphNode leftNode, GraphNode rightNode, double weight) { - this.leftNode = leftNode; - this.rightNode = rightNode; - setWeight(weight); - setWeighted(true); - setDirected(false); - } - - public GraphEdge(GraphNode leftNode, GraphNode rightNode) { - this.leftNode = leftNode; - this.rightNode = rightNode; - this.isWeighted = false; - setDirected(false); - } - - public GraphEdge(GraphEdge edge) { - this.leftNode = edge.getLeftNode(); - this.rightNode = edge.getRightNode(); - this.weight = edge.getWeight(); - } - - public double getWeight() { - if (!isWeighted) { - // i should throw an exception here? - } - return weight; - } - - // the way this is written, taking an GraphEdge that is unweighted - // first need to setWeighted(true) then setWeight(weight) - public GraphEdge setWeight(double weight) { - if (isWeighted) { - this.weight = weight; - } - return this; - } - - public boolean isWeighted() { - return isWeighted; - } - - public GraphEdge setWeighted(boolean weighted) { - this.isWeighted = weighted; - return this; - } - - public boolean isDirected() { - return isDirected; - } - - public void setDirected(boolean directed) { - this.isDirected = directed; - } - - public GraphNode getLeftNode() { - return leftNode; - } - - public GraphNode getRightNode() { - return rightNode; - } - - public void setLeftNode(GraphNode leftNode) { - this.leftNode = leftNode; - } - - public void setRightNode(GraphNode rightNode) { - this.rightNode = rightNode; - } - - public boolean contains(GraphNode node) { - return leftNode.equals(node) || rightNode.equals(node); - } - - // will return null if node is not in edge, else returns opposite node - public GraphNode getOtherNode(GraphNode node) { - if(contains(node)) { - if(node.equals(getRightNode())) { - return getLeftNode(); - } else { - return getRightNode(); - } - } else { - return null; - } - } - - // "Note: this class has a natural ordering that is inconsistent with equals." - public int compareTo(GraphEdge edge) { - if (isWeighted && edge.isWeighted()) { - // is it necessary to specify "this." ? - if(edge.getWeight() > this.getWeight()) { - return -1; - } else if (this.getWeight() > edge.getWeight()){ - return 1; - } - } - return 0; - } - -} diff --git a/src/main/java/graphs/GraphSearch.java b/src/main/java/graphs/GraphSearch.java index 5414bf3..12c017a 100644 --- a/src/main/java/graphs/GraphSearch.java +++ b/src/main/java/graphs/GraphSearch.java @@ -1,10 +1,11 @@ package graphs; import java.util.*; +import java.util.stream.Collectors; public class GraphSearch { - public static List depthFirstSearch(GraphClass graph, GraphNode start, GraphNode finish) { + public static List depthFirstSearch(Graph graph, Node start, Node finish) { if (graph == null || graph.isEmpty()) { throw new IllegalArgumentException("empty or null graph"); @@ -13,21 +14,21 @@ public static List depthFirstSearch(GraphClass graph, GraphNode start throw new IllegalArgumentException("null start node or target node"); } if(! (graph.getNodes().contains(start) && graph.getNodes().contains(finish))) { - return new LinkedList(); + return new LinkedList(); } // DFS uses a stack of nodes visited // stack is push pop peek : peek to see - Stack location = new Stack(); + Stack location = new Stack(); start.visit(); location.push(start); boolean searching = true; while (searching) { - GraphNode currentNode = location.peek(); - List incidentEdges = graph.getIncidentEdges(currentNode); + Node currentNode = location.peek(); + List incidentEdges = graph.getIncidentEdges(currentNode); // getNextNode can return null - GraphNode nextNode = getNextNode(incidentEdges, currentNode); + Node nextNode = getNextNode(incidentEdges, currentNode); if(nextNode == null) { location.pop(); if (location.isEmpty()) { @@ -35,16 +36,16 @@ public static List depthFirstSearch(GraphClass graph, GraphNode start } } else if (nextNode.equals(finish)) { location.push(nextNode); - return new LinkedList(location); + return new LinkedList(location); } else { nextNode.visit(); location.push(nextNode); } } - return new LinkedList(); + return new LinkedList(); } - public static List depthFirstSearchRecursive(GraphClass graph, GraphNode start, GraphNode finish) { + public static List depthFirstSearchRecursive(Graph graph, Node start, Node finish) { if (graph == null || graph.isEmpty()) { throw new IllegalArgumentException("empty or null graph"); } @@ -52,32 +53,32 @@ public static List depthFirstSearchRecursive(GraphClass graph, GraphN throw new IllegalArgumentException("null start node or target node"); } if(! (graph.getNodes().contains(start) && graph.getNodes().contains(finish))) { - return new LinkedList(); + return new LinkedList(); } // DFS uses a stack of nodes visited // stack is push pop peek : peek to see - Stack location = new Stack(); + Stack location = new Stack(); start.visit(); location.push(start); return depthFirstSearchRecursive(graph, start, finish, location); } - public static List depthFirstSearchRecursive(GraphClass graph, GraphNode start, GraphNode finish, Stack location) { + public static List depthFirstSearchRecursive(Graph graph, Node start, Node finish, Stack location) { - GraphNode currentNode = location.peek(); - List incidentEdges = graph.getIncidentEdges(currentNode); + Node currentNode = location.peek(); + List incidentEdges = graph.getIncidentEdges(currentNode); // getNextNode can return null - GraphNode nextNode = getNextNode(incidentEdges, currentNode); + Node nextNode = getNextNode(incidentEdges, currentNode); if (nextNode == null) { location.pop(); if (location.isEmpty()) { // if search has not found finish, because stack has been filled and emptied - return new LinkedList(); + return new LinkedList(); } } else if (nextNode.equals(finish)) { location.push(nextNode); - return new LinkedList(location); + return new LinkedList(location); } else { nextNode.visit(); location.push(nextNode); @@ -85,21 +86,21 @@ public static List depthFirstSearchRecursive(GraphClass graph, GraphN return depthFirstSearchRecursive(graph, start, finish, location); } - private static GraphNode getNextNode(List incidentEdges, GraphNode startingNode) { + private static Node getNextNode(List incidentEdges, Node startingNode) { if (incidentEdges.isEmpty() || incidentEdges == null || startingNode == null) { return null; } else { - for (GraphEdge edge : incidentEdges) { - GraphNode node = edge.getOtherNode(startingNode); + for (Edge edge : incidentEdges) { + Node node = edge.getOtherNode(startingNode); if (!node.isVisited()) { - return edge.getOtherNode(startingNode); + return node; } } return null; } } - public static List breadthFirstSearch(GraphClass graph, GraphNode start, GraphNode finish) { + public static List breadthFirstSearch(Graph graph, Node start, Node finish) { if (graph == null || graph.isEmpty()) { throw new IllegalArgumentException("empty or null graph"); } @@ -107,26 +108,26 @@ public static List breadthFirstSearch(GraphClass graph, GraphNode st throw new IllegalArgumentException("null start node or target node"); } if(! (graph.getNodes().contains(start) && graph.getNodes().contains(finish))) { - return new LinkedList(); + return new LinkedList(); } // BFS uses a queue of nodes to visit // queue is add poll to get and remove peek to see - Queue nodeQueue = new LinkedList(); + Queue nodeQueue = new LinkedList(); start.visit(); nodeQueue.add(start); // list of nodes in order to be returned - LinkedList transitions = new LinkedList(); + LinkedList transitions = new LinkedList(); - GraphNode currentNode = nodeQueue.poll(); + Node currentNode = nodeQueue.poll(); double distance = 0; while (currentNode != null) { - List incidentEdges = graph.getIncidentEdges(currentNode); + List incidentEdges = graph.getIncidentEdges(currentNode); // getNextNode can return null - GraphNode nextNode = getNextNode(incidentEdges, currentNode); + Node nextNode = getNextNode(incidentEdges, currentNode); while (nextNode != null) { - transitions.add(new GraphEdge(currentNode, nextNode)); + transitions.add(new Edge(currentNode, nextNode)); if(nextNode.equals(finish)) { return getPathHome(transitions, start, finish); } else { @@ -138,18 +139,18 @@ public static List breadthFirstSearch(GraphClass graph, GraphNode st currentNode = nodeQueue.poll(); } // if target was never found, return empty list - return new LinkedList(); + return new LinkedList(); } // start node is unused, as this helper assumes the front of transitions is start - private static List getPathHome(List transitions, GraphNode start, GraphNode finish) { - GraphNode to = finish; - GraphNode from; - LinkedList nodeList = new LinkedList(); + private static List getPathHome(List transitions, Node start, Node finish) { + Node to = finish; + Node from; + LinkedList nodeList = new LinkedList(); nodeList.addFirst(to); while(! transitions.isEmpty()) { - GraphEdge edge = transitions.remove(transitions.size() - 1); + Edge edge = transitions.remove(transitions.size() - 1); if(edge.getRightNode().equals(to)) { from = edge.getLeftNode(); nodeList.addFirst(from); @@ -158,4 +159,102 @@ private static List getPathHome(List transitions, GraphNod } return nodeList; } + + + /* + djikstras algo: + takes: graph, source, target + + map of V to distance from source + - instantiate at MAX_INT + map of V's previous node + + set of V + + // set source distance to zero + distanceMap.put(source, 0) + while(! setOfV.isEmpty()) + + Node currentNode = setOfV.pop() + update distance values for all nodes from nextNode + ie get incident edges and + update distance for each node = min(distance(node), distance(currentNode) + edge(currentNode, nextNode).getWeight()) + if distance < current distance + edge weight + update distance + update previous node + + // sort algo that takes list of nodes and map to their current distances and sorts nodes + find next node that is ! in settled and shortest distance (ie distance(currentNode) + edge(currentNode, nextNode).getWeight()) + shortest distance nextNode gets added to settled + + eventually every node will have a non infinite distance from source + + + */ + + public static List djikstraShortestPath(Graph graph, Node start, Node finish) { + if (graph == null || graph.isEmpty()) { + throw new IllegalArgumentException("empty or null graph"); + } + if(start == null || finish == null) { + throw new IllegalArgumentException("null start node or target node"); + } + if(! (graph.getNodes().contains(start) && graph.getNodes().contains(finish))) { + throw new IllegalArgumentException("either start node or target node are not contained in graph"); + } + + LinkedList nodes = new LinkedList<>(graph.getNodes()); + + HashMap distanceMap = new HashMap<>(); + for(Node node : nodes) { + distanceMap.put(node, Double.valueOf(Double.MAX_VALUE)); + } + HashMap previousNodeMap = new HashMap<>(); + + distanceMap.put(start, Double.valueOf(0)); + while(! distanceMap.isEmpty()) { + Node currentNode = getShortestDistanceNode(distanceMap); + if(currentNode.equals(finish)) { + break; + } + double currentDistance = distanceMap.get(currentNode); + distanceMap.remove(currentNode); + List incidentEdges = graph.getIncidentEdges(currentNode); + for(Edge edge : incidentEdges) { + Node nextNode = edge.getRightNode(); + if(distanceMap.containsKey(nextNode)){ + double newDistance = currentDistance + edge.getWeight(); + if(newDistance < distanceMap.get(nextNode)) { + distanceMap.put(nextNode, newDistance); + previousNodeMap.put(nextNode, currentNode); + } + } + } + } + if(! previousNodeMap.containsKey(finish)) { + return new LinkedList(); + } + LinkedList transitions = new LinkedList(); + HashSet keys = new HashSet(previousNodeMap.keySet()); + Node nextNode = finish; + keys.remove(nextNode); + while(! keys.isEmpty()) { + Node currentNode = previousNodeMap.get(nextNode); + transitions.addFirst(new Edge(currentNode, nextNode)); + if(currentNode.equals(start)) { + break; + } + nextNode = currentNode; + keys.remove(nextNode); + } + return getPathHome(transitions, start, finish); + } + + private static Node getShortestDistanceNode(HashMap nodeMap) { + return nodeMap.entrySet() + .stream() + .sorted(Map.Entry.comparingByValue()) + .findFirst().get().getKey(); + + } } diff --git a/src/main/java/graphs/GraphNode.java b/src/main/java/graphs/Node.java similarity index 84% rename from src/main/java/graphs/GraphNode.java rename to src/main/java/graphs/Node.java index 323462a..a313602 100644 --- a/src/main/java/graphs/GraphNode.java +++ b/src/main/java/graphs/Node.java @@ -1,25 +1,25 @@ package graphs; -public class GraphNode { +public class Node { private float x; private float y; private T value; private boolean visited; - public GraphNode(GraphNode node) { + public Node(Node node) { setX(node.getX()); setY(node.getY()); setValue(node.getValue()); } - public GraphNode(float x, float y, T value) { + public Node(float x, float y, T value) { this.x = x; this.y = y; this.value = value; } - public GraphNode(T value) { + public Node(T value) { this.value = value; } diff --git a/src/test/java/graphs/DirectedUnweightedGraphTest.java b/src/test/java/graphs/DirectedUnweightedGraphTest.java new file mode 100644 index 0000000..9ed7f52 --- /dev/null +++ b/src/test/java/graphs/DirectedUnweightedGraphTest.java @@ -0,0 +1,340 @@ +package graphs; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + +public class DirectedUnweightedGraphTest { + + private Node nodeA; + private Node nodeB; + private Node nodeC; + private Node nodeD; + private Node nodeE; + private Node nodeF; + private Node nodeG; + private Node nodeH; + private Node nodeZ; + + private Graph DirectedUnweightedGraph8, + DirectedUnweightedGraph9, + DirectedUnweightedGraph10; + + + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Before + public void setUp() throws Exception { + nodeA = new Node("A"); + nodeB = new Node("B"); + nodeC = new Node("C"); + nodeD = new Node("D"); + nodeE = new Node("E"); + nodeF = new Node("F"); + nodeG = new Node("G"); + nodeH = new Node("H"); + nodeZ = new Node("Z"); + + List nodesForUndirectedUnweightedGraph1 = new LinkedList(Arrays.asList( + nodeA, + nodeB, + nodeC, + nodeD, + nodeE, + nodeF + )); + + + List nodesForUndirectedUnweightedGraph2 = new LinkedList(Arrays.asList( + nodeA, + nodeB, + nodeC, + nodeD, + nodeE, + nodeF, + nodeG, + nodeH, + nodeZ + )); + + Edge edgeBetweenAandB = new Edge(nodeA, nodeB); + Edge edgeBetweenBandC = new Edge(nodeB, nodeC); + Edge edgeBetweenHandZ = new Edge(nodeH, nodeZ); + Edge edgeBetweenEandZ = new Edge(nodeE, nodeZ); + Edge edgeBetweenDandF = new Edge(nodeD, nodeF); + Edge edgeBetweenBandD = new Edge(nodeB, nodeD); + Edge edgeBetweenAandD = new Edge(nodeA, nodeD); + Edge edgeBetweenBandH = new Edge(nodeB, nodeH); + Edge edgeBetweenCandE = new Edge(nodeC, nodeE); + Edge edgeBetweenBandF = new Edge(nodeB, nodeF); + Edge edgeBetweenAandC = new Edge(nodeA, nodeC); + Edge edgeBetweenFandH = new Edge(nodeF, nodeH); + + /* + * graph 8 + * 12 edges + * a-d ~ + * b-d ~ + * g-d + * z-g + * z-d + * h-z ~ + * b-h ~ + * f-b + * c-e ~ + * d-c + * e-d + * d-f + */ + + + Edge edgeBetweenGandD = new Edge(nodeG,nodeD); + Edge edgeBetweenZandG = new Edge(nodeZ,nodeG); + Edge edgeBetweenZandD = new Edge(nodeZ,nodeD); + Edge edgeBetweenFandB = new Edge(nodeF,nodeB); + Edge edgeBetweenDandC = new Edge(nodeD,nodeC); + Edge edgeBetweenEandD = new Edge(nodeE,nodeD); + + List edgesForGraph8 = new LinkedList(); + edgesForGraph8.add(edgeBetweenAandD); + edgesForGraph8.add(edgeBetweenBandD); + edgesForGraph8.add(edgeBetweenHandZ); + edgesForGraph8.add(edgeBetweenBandH); + edgesForGraph8.add(edgeBetweenCandE); + edgesForGraph8.add(edgeBetweenGandD); + edgesForGraph8.add(edgeBetweenZandG); + edgesForGraph8.add(edgeBetweenZandD); + edgesForGraph8.add(edgeBetweenFandB); + edgesForGraph8.add(edgeBetweenDandC); + edgesForGraph8.add(edgeBetweenEandD); + edgesForGraph8.add(edgeBetweenDandF); + + + // use nodes from graph 2 + DirectedUnweightedGraph8 = new Graph(nodesForUndirectedUnweightedGraph2, edgesForGraph8, true); + // + // Graph #8: directed and unweighted + // + + + /* + * graph 9 + * 12 edges + * a-b + * b-c + * c-e + * e-d + * g-d + * g-a + * z-g + * h-z + * f-h + * d-f + * e-z + * f-a + * + */ + + Edge edgeBetweenGandA = new Edge(nodeG, nodeA); + Edge edgeBetweenFandA = new Edge(nodeF, nodeA); + + List edgesForGraph9 = new LinkedList(); + edgesForGraph9.add(edgeBetweenAandB); + edgesForGraph9.add(edgeBetweenBandC); + edgesForGraph9.add(edgeBetweenCandE); + edgesForGraph9.add(edgeBetweenEandZ); + edgesForGraph9.add(edgeBetweenEandD); + edgesForGraph9.add(edgeBetweenGandD); + edgesForGraph9.add(edgeBetweenZandG); + edgesForGraph9.add(edgeBetweenHandZ); + edgesForGraph9.add(edgeBetweenFandH); + edgesForGraph9.add(edgeBetweenDandF); + edgesForGraph9.add(edgeBetweenGandA); + edgesForGraph9.add(edgeBetweenFandA); + + + DirectedUnweightedGraph9 = new Graph(nodesForUndirectedUnweightedGraph2, edgesForGraph9, true); + // + // Graph #9: directed and unweighted + // + + + /* + * graph 10 + * 10 edges + * a-d + * a-c + * d-c + * z-c + * g-e + * e-b + * b-f + * f-h + * d-z + * g-a + */ + + Edge edgeBetweenZandC = new Edge(nodeZ, nodeC); + Edge edgeBetweenGandE = new Edge(nodeG, nodeE); + Edge edgeBetweenEandB = new Edge(nodeE, nodeB); + Edge edgeBetweenDandZ = new Edge(nodeD, nodeZ); + List edgesForGraph10 = new LinkedList(); + edgesForGraph10.add(edgeBetweenAandD); + edgesForGraph10.add(edgeBetweenAandC); + edgesForGraph10.add(edgeBetweenDandC); + edgesForGraph10.add(edgeBetweenBandF); + edgesForGraph10.add(edgeBetweenFandH); + edgesForGraph10.add(edgeBetweenZandC); + edgesForGraph10.add(edgeBetweenGandE); + edgesForGraph10.add(edgeBetweenEandB); + edgesForGraph10.add(edgeBetweenDandZ); + edgesForGraph10.add(edgeBetweenGandA); + + DirectedUnweightedGraph10 = new Graph(nodesForUndirectedUnweightedGraph2, edgesForGraph10, true); + // + // Graph #10: directed and unweighted + // + + + + } + + @Test + public void depthFirstSearchDirectedUnweightedGraph08() throws Exception { + List path = GraphSearch.depthFirstSearch(DirectedUnweightedGraph8, nodeH, nodeB); + Assert.assertEquals(path.size(), 6); + Assert.assertTrue(path.get(0).getValue().equals("H")); + Assert.assertTrue(path.get(1).getValue().equals("Z")); + Assert.assertTrue(path.get(2).getValue().equals("G")); + Assert.assertTrue(path.get(3).getValue().equals("D")); + Assert.assertTrue(path.get(4).getValue().equals("F")); + Assert.assertTrue(path.get(5).getValue().equals("B")); + } + + @Test + public void depthFirstSearchRecursiveDirectedUnweightedGraph08() throws Exception { + List path = GraphSearch.depthFirstSearchRecursive(DirectedUnweightedGraph8, nodeH, nodeB); + Assert.assertEquals(path.size(), 6); + Assert.assertTrue(path.get(0).getValue().equals("H")); + Assert.assertTrue(path.get(1).getValue().equals("Z")); + Assert.assertTrue(path.get(2).getValue().equals("G")); + Assert.assertTrue(path.get(3).getValue().equals("D")); + Assert.assertTrue(path.get(4).getValue().equals("F")); + Assert.assertTrue(path.get(5).getValue().equals("B")); + } + + @Test + public void breadthFirstSearchDirectedUnweightedGraph08() throws Exception { + List path = GraphSearch.breadthFirstSearch(DirectedUnweightedGraph8, nodeH, nodeB); + Assert.assertEquals(path.size(), 5); + Assert.assertTrue(path.get(0).getValue().equals("H")); + Assert.assertTrue(path.get(1).getValue().equals("Z")); + Assert.assertTrue(path.get(2).getValue().equals("D")); + Assert.assertTrue(path.get(3).getValue().equals("F")); + Assert.assertTrue(path.get(4).getValue().equals("B")); + } + + @Test + public void djikstraShortestPathDirectedWeightedGraph08() throws Exception { + List path = GraphSearch.djikstraShortestPath(DirectedUnweightedGraph8, nodeH, nodeB); + Assert.assertEquals(path.size(), 5); + Assert.assertTrue(path.get(0).getValue().equals("H")); + Assert.assertTrue(path.get(1).getValue().equals("Z")); + Assert.assertTrue(path.get(2).getValue().equals("D")); + Assert.assertTrue(path.get(3).getValue().equals("F")); + Assert.assertTrue(path.get(4).getValue().equals("B")); + } + + @Test + public void depthFirstSearchDirectedUnweightedGraph09() throws Exception { + List path = GraphSearch.depthFirstSearch(DirectedUnweightedGraph9, nodeE, nodeF); + Assert.assertEquals(path.size(), 5); + Assert.assertTrue(path.get(0).getValue().equals("E")); + Assert.assertTrue(path.get(1).getValue().equals("Z")); + Assert.assertTrue(path.get(2).getValue().equals("G")); + Assert.assertTrue(path.get(3).getValue().equals("D")); + Assert.assertTrue(path.get(4).getValue().equals("F")); + } + + @Test + public void depthFirstSearchRecursiveDirectedUnweightedGraph09() throws Exception { + List path = GraphSearch.depthFirstSearchRecursive(DirectedUnweightedGraph9, nodeE, nodeF); + Assert.assertEquals(path.size(), 5); + Assert.assertTrue(path.get(0).getValue().equals("E")); + Assert.assertTrue(path.get(1).getValue().equals("Z")); + Assert.assertTrue(path.get(2).getValue().equals("G")); + Assert.assertTrue(path.get(3).getValue().equals("D")); + Assert.assertTrue(path.get(4).getValue().equals("F")); + } + + @Test + public void breadthFirstSearchDirectedUnweightedGraph09() throws Exception { + List path = GraphSearch.breadthFirstSearch(DirectedUnweightedGraph9, nodeE, nodeF); + Assert.assertEquals(path.size(), 3); + Assert.assertTrue(path.get(0).getValue().equals("E")); + Assert.assertTrue(path.get(1).getValue().equals("D")); + Assert.assertTrue(path.get(2).getValue().equals("F")); + } + + @Test + public void djikstraShortestPathDirectedWeightedGraph09() throws Exception { + List path = GraphSearch.djikstraShortestPath(DirectedUnweightedGraph9, nodeE, nodeF); + Assert.assertEquals(path.size(), 3); + Assert.assertTrue(path.get(0).getValue().equals("E")); + Assert.assertTrue(path.get(1).getValue().equals("D")); + Assert.assertTrue(path.get(2).getValue().equals("F")); + } + + @Test + public void depthFirstSearchDirectedUnweightedGraph10() throws Exception { + List path = GraphSearch.depthFirstSearch(DirectedUnweightedGraph10, nodeG, nodeH); + Assert.assertEquals(path.size(), 5); + Assert.assertTrue(path.get(0).getValue().equals("G")); + Assert.assertTrue(path.get(1).getValue().equals("E")); + Assert.assertTrue(path.get(2).getValue().equals("B")); + Assert.assertTrue(path.get(3).getValue().equals("F")); + Assert.assertTrue(path.get(4).getValue().equals("H")); + } + + @Test + public void depthFirstSearchRecursiveDirectedUnweightedGraph10() throws Exception { + List path = GraphSearch.depthFirstSearchRecursive(DirectedUnweightedGraph10, nodeG, nodeH); + Assert.assertEquals(path.size(), 5); + Assert.assertTrue(path.get(0).getValue().equals("G")); + Assert.assertTrue(path.get(1).getValue().equals("E")); + Assert.assertTrue(path.get(2).getValue().equals("B")); + Assert.assertTrue(path.get(3).getValue().equals("F")); + Assert.assertTrue(path.get(4).getValue().equals("H")); + } + + @Test + public void breadthFirstSearchDirectedUnweightedGraph10() throws Exception { + List path = GraphSearch.breadthFirstSearch(DirectedUnweightedGraph10, nodeG, nodeH); + Assert.assertEquals(path.size(), 5); + Assert.assertTrue(path.get(0).getValue().equals("G")); + Assert.assertTrue(path.get(1).getValue().equals("E")); + Assert.assertTrue(path.get(2).getValue().equals("B")); + Assert.assertTrue(path.get(3).getValue().equals("F")); + Assert.assertTrue(path.get(4).getValue().equals("H")); + } + + @Test + public void djikstraShortestPathDirectedWeightedGraph10() throws Exception { + List path = GraphSearch.djikstraShortestPath(DirectedUnweightedGraph10, nodeG, nodeH); + Assert.assertEquals(path.size(), 5); + Assert.assertTrue(path.get(0).getValue().equals("G")); + Assert.assertTrue(path.get(1).getValue().equals("E")); + Assert.assertTrue(path.get(2).getValue().equals("B")); + Assert.assertTrue(path.get(3).getValue().equals("F")); + Assert.assertTrue(path.get(4).getValue().equals("H")); + } + +} \ No newline at end of file diff --git a/src/test/java/graphs/DirectedWeightedGraphTest.java b/src/test/java/graphs/DirectedWeightedGraphTest.java new file mode 100644 index 0000000..3b26b07 --- /dev/null +++ b/src/test/java/graphs/DirectedWeightedGraphTest.java @@ -0,0 +1,317 @@ +package graphs; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + +public class DirectedWeightedGraphTest { + + private Node nodeA; + private Node nodeB; + private Node nodeC; + private Node nodeD; + private Node nodeE; + private Node nodeF; + private Node nodeG; + private Node nodeH; + private Node nodeZ; + + private Graph DirectedWeightedGraph11, + DirectedWeightedGraph12, + DirectedWeightedGraph13; + + + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Before + public void setUp() throws Exception { + nodeA = new Node("A"); + nodeB = new Node("B"); + nodeC = new Node("C"); + nodeD = new Node("D"); + nodeE = new Node("E"); + nodeF = new Node("F"); + nodeG = new Node("G"); + nodeH = new Node("H"); + nodeZ = new Node("Z"); + + + List nodesForUndirectedUnweightedGraph2 = new LinkedList(Arrays.asList( + nodeA, + nodeB, + nodeC, + nodeD, + nodeE, + nodeF, + nodeG, + nodeH, + nodeZ + )); + Edge edgeBetweenAandB = new Edge(nodeA,nodeB); + Edge edgeBetweenBandC = new Edge(nodeB,nodeC); + Edge edgeBetweenHandZ = new Edge(nodeH,nodeZ); + Edge edgeBetweenEandZ = new Edge(nodeE,nodeZ); + Edge edgeBetweenDandF = new Edge(nodeD,nodeF); + Edge edgeBetweenBandD = new Edge(nodeB,nodeD); + Edge edgeBetweenAandD = new Edge(nodeA,nodeD); + Edge edgeBetweenBandH = new Edge(nodeB,nodeH); + Edge edgeBetweenCandE = new Edge(nodeC,nodeE); + Edge edgeBetweenBandF = new Edge(nodeB,nodeF); + Edge edgeBetweenAandC = new Edge(nodeA,nodeC); + Edge edgeBetweenFandH = new Edge(nodeF,nodeH); + Edge edgeBetweenGandD = new Edge(nodeG,nodeD); + Edge edgeBetweenZandG = new Edge(nodeZ,nodeG); + Edge edgeBetweenZandD = new Edge(nodeZ,nodeD); + Edge edgeBetweenFandB = new Edge(nodeF,nodeB); + Edge edgeBetweenDandC = new Edge(nodeD,nodeC); + Edge edgeBetweenEandD = new Edge(nodeE,nodeD); + Edge edgeBetweenGandA = new Edge(nodeG,nodeA); + Edge edgeBetweenFandA = new Edge(nodeF,nodeA); + Edge edgeBetweenZandC = new Edge(nodeZ,nodeC); + Edge edgeBetweenGandE = new Edge(nodeG,nodeE); + Edge edgeBetweenEandB = new Edge(nodeE,nodeB); + Edge edgeBetweenDandZ = new Edge(nodeD,nodeZ); + + + /* + * graph 8 but weighted + * 12 edges + * a-d + * b-d + * g-d + * z-g + * z-d 2, rest 1 + * h-z + * b-h + * f-b + * c-e + * d-c + * e-d + * d-f + * + * + */ + List edgesForGraph11 = new LinkedList(); + edgesForGraph11.add(edgeBetweenAandD.setWeight(1)); + edgesForGraph11.add(edgeBetweenZandD.setWeight(2)); + edgesForGraph11.add(edgeBetweenBandD.setWeight(1)); + edgesForGraph11.add(edgeBetweenHandZ.setWeight(1)); + edgesForGraph11.add(edgeBetweenBandH.setWeight(1)); + edgesForGraph11.add(edgeBetweenCandE.setWeight(1)); + edgesForGraph11.add(edgeBetweenGandD.setWeight(1)); + edgesForGraph11.add(edgeBetweenZandG.setWeight(1)); + edgesForGraph11.add(edgeBetweenFandB.setWeight(1)); + edgesForGraph11.add(edgeBetweenDandC.setWeight(1)); + edgesForGraph11.add(edgeBetweenEandD.setWeight(1)); + edgesForGraph11.add(edgeBetweenDandF.setWeight(1)); + DirectedWeightedGraph11 = new Graph(nodesForUndirectedUnweightedGraph2, edgesForGraph11, true); + DirectedWeightedGraph11.setWeighted(true); + + // + // Graph #11: directed and weighted + // + + /* + * graph 9 but weighted + * 12 edges + * a-b + * b-c + * c-e + * e-d + * g-d + * g-a + * z-g + * h-z + * f-h + * d-f + * e-z + * f-a + * + */ + List edgesForGraph12 = new LinkedList(); + edgesForGraph12.add(edgeBetweenAandB.setWeight(1)); + edgesForGraph12.add(edgeBetweenBandC.setWeight(1)); + edgesForGraph12.add(edgeBetweenCandE.setWeight(1)); + edgesForGraph12.add(edgeBetweenEandZ.setWeight(1)); + edgesForGraph12.add(edgeBetweenEandD.setWeight(2)); + edgesForGraph12.add(edgeBetweenGandD.setWeight(2)); + edgesForGraph12.add(edgeBetweenZandG.setWeight(1)); + edgesForGraph12.add(edgeBetweenHandZ.setWeight(1)); + edgesForGraph12.add(edgeBetweenFandH.setWeight(1)); + edgesForGraph12.add(edgeBetweenDandF.setWeight(1)); + edgesForGraph12.add(edgeBetweenGandA.setWeight(1)); + edgesForGraph12.add(edgeBetweenFandA.setWeight(1)); + DirectedWeightedGraph12 = new Graph(nodesForUndirectedUnweightedGraph2, edgesForGraph12, true); + DirectedWeightedGraph12.setWeighted(true); + + // + // Graph #12: directed and weighted + // + + + /* + * graph 10 but weighted + * 10 edges + * a-d + * a-c + * d-c + * z-c + * g-e + * e-b + * b-f + * f-h + * d-z + * g-a + * + */ + + List edgesForGraph13 = new LinkedList(); + edgesForGraph13.add(edgeBetweenAandD.setWeight(1)); + edgesForGraph13.add(edgeBetweenAandC.setWeight(1)); + edgesForGraph13.add(edgeBetweenDandC.setWeight(1)); + edgesForGraph13.add(edgeBetweenBandF.setWeight(1)); + edgesForGraph13.add(edgeBetweenFandH.setWeight(1)); + edgesForGraph13.add(edgeBetweenZandC.setWeight(1)); + edgesForGraph13.add(edgeBetweenGandE.setWeight(2)); + edgesForGraph13.add(edgeBetweenEandB.setWeight(1)); + edgesForGraph13.add(edgeBetweenDandZ.setWeight(1)); + edgesForGraph13.add(edgeBetweenGandA.setWeight(1)); + + + DirectedWeightedGraph13 = new Graph(nodesForUndirectedUnweightedGraph2, edgesForGraph13, true); + DirectedWeightedGraph13.setWeighted(true); + + // + // Graph #13: directed and weighted + // + + + } + + @Test + public void depthFirstSearchDirectedWeightedGraph11() throws Exception { + List path = GraphSearch.depthFirstSearch(DirectedWeightedGraph11, nodeH, nodeB); + Assert.assertEquals(path.size(), 6); + Assert.assertTrue(path.get(0).getValue().equals("H")); + Assert.assertTrue(path.get(1).getValue().equals("Z")); + Assert.assertTrue(path.get(2).getValue().equals("G")); + Assert.assertTrue(path.get(3).getValue().equals("D")); + Assert.assertTrue(path.get(4).getValue().equals("F")); + Assert.assertTrue(path.get(5).getValue().equals("B")); + } + + @Test + public void depthFirstSearchRecursiveDirectedWeightedGraph11() throws Exception { + List path = GraphSearch.depthFirstSearchRecursive(DirectedWeightedGraph11, nodeH, nodeB); + Assert.assertEquals(path.size(), 6); + Assert.assertTrue(path.get(0).getValue().equals("H")); + Assert.assertTrue(path.get(1).getValue().equals("Z")); + Assert.assertTrue(path.get(2).getValue().equals("G")); + Assert.assertTrue(path.get(3).getValue().equals("D")); + Assert.assertTrue(path.get(4).getValue().equals("F")); + Assert.assertTrue(path.get(5).getValue().equals("B")); + } + + @Test + public void breadthFirstSearchDirectedWeightedGraph11() throws Exception { + List path = GraphSearch.breadthFirstSearch(DirectedWeightedGraph11, nodeH, nodeB); + Assert.assertEquals(path.size(), 5); + Assert.assertTrue(path.get(0).getValue().equals("H")); + Assert.assertTrue(path.get(1).getValue().equals("Z")); + Assert.assertTrue(path.get(2).getValue().equals("D")); + Assert.assertTrue(path.get(3).getValue().equals("F")); + Assert.assertTrue(path.get(4).getValue().equals("B")); + } + + @Test + public void djikstraShortestPathDirectedWeightedGraph11() throws Exception { + List path = GraphSearch.djikstraShortestPath(DirectedWeightedGraph11, nodeH, nodeB); + Assert.assertEquals(path.size(), 5); + } + + @Test + public void depthFirstSearchDirectedWeightedGraph12() throws Exception { + List path = GraphSearch.depthFirstSearch(DirectedWeightedGraph12, nodeE, nodeF); + Assert.assertEquals(path.size(), 5); + Assert.assertTrue(path.get(0).getValue().equals("E")); + Assert.assertTrue(path.get(1).getValue().equals("Z")); + Assert.assertTrue(path.get(2).getValue().equals("G")); + Assert.assertTrue(path.get(3).getValue().equals("D")); + Assert.assertTrue(path.get(4).getValue().equals("F")); + } + + @Test + public void depthFirstSearchRecursiveDirectedWeightedGraph12() throws Exception { + List path = GraphSearch.depthFirstSearchRecursive(DirectedWeightedGraph12, nodeE, nodeF); + Assert.assertEquals(path.size(), 5); + Assert.assertTrue(path.get(0).getValue().equals("E")); + Assert.assertTrue(path.get(1).getValue().equals("Z")); + Assert.assertTrue(path.get(2).getValue().equals("G")); + Assert.assertTrue(path.get(3).getValue().equals("D")); + Assert.assertTrue(path.get(4).getValue().equals("F")); + } + + @Test + public void breadthFirstSearchDirectedWeightedGraph12() throws Exception { + List path = GraphSearch.breadthFirstSearch(DirectedWeightedGraph12, nodeE, nodeF); + Assert.assertEquals(path.size(), 3); + Assert.assertTrue(path.get(0).getValue().equals("E")); + Assert.assertTrue(path.get(1).getValue().equals("D")); + Assert.assertTrue(path.get(2).getValue().equals("F")); + } + + @Test + public void djikstraShortestPathDirectedWeightedGraph12() throws Exception { + List path = GraphSearch.djikstraShortestPath(DirectedWeightedGraph12, nodeE, nodeF); + Assert.assertEquals(path.size(), 3); + } + + @Test + public void depthFirstSearchDirectedWeightedGraph13() throws Exception { + List path = GraphSearch.depthFirstSearch(DirectedWeightedGraph13, nodeG, nodeH); + Assert.assertEquals(path.size(), 5); + Assert.assertTrue(path.get(0).getValue().equals("G")); + Assert.assertTrue(path.get(1).getValue().equals("E")); + Assert.assertTrue(path.get(2).getValue().equals("B")); + Assert.assertTrue(path.get(3).getValue().equals("F")); + Assert.assertTrue(path.get(4).getValue().equals("H")); + } + + @Test + public void depthFirstSearchRecursiveDirectedWeightedGraph13() throws Exception { + List path = GraphSearch.depthFirstSearchRecursive(DirectedWeightedGraph13, nodeG, nodeH); + Assert.assertEquals(path.size(), 5); + Assert.assertTrue(path.get(0).getValue().equals("G")); + Assert.assertTrue(path.get(1).getValue().equals("E")); + Assert.assertTrue(path.get(2).getValue().equals("B")); + Assert.assertTrue(path.get(3).getValue().equals("F")); + Assert.assertTrue(path.get(4).getValue().equals("H")); + } + + @Test + public void breadthFirstSearchDirectedWeightedGraph13() throws Exception { + List path = GraphSearch.breadthFirstSearch(DirectedWeightedGraph13, nodeG, nodeH); + Assert.assertEquals(path.size(), 5); + Assert.assertTrue(path.get(0).getValue().equals("G")); + Assert.assertTrue(path.get(1).getValue().equals("E")); + Assert.assertTrue(path.get(2).getValue().equals("B")); + Assert.assertTrue(path.get(3).getValue().equals("F")); + Assert.assertTrue(path.get(4).getValue().equals("H")); + } + + @Test + public void djikstraShortestPathDirectedWeightedGraph13() throws Exception { + List path = GraphSearch.djikstraShortestPath(DirectedWeightedGraph13, nodeG, nodeH); + Assert.assertEquals(path.size(), 5); + } + + +} \ No newline at end of file diff --git a/src/test/java/graphs/GraphSearchTest.java b/src/test/java/graphs/GraphSearchTest.java deleted file mode 100644 index e6f7eb7..0000000 --- a/src/test/java/graphs/GraphSearchTest.java +++ /dev/null @@ -1,1116 +0,0 @@ -package graphs; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - -import java.util.Arrays; -import java.util.LinkedList; -import java.util.List; - -/** - * Created by shalka on 8/14/16. - */ -public class GraphSearchTest { - - private GraphNode nodeA; - private GraphNode nodeB; - private GraphNode nodeC; - private GraphNode nodeD; - private GraphNode nodeE; - private GraphNode nodeF; - private GraphNode nodeG; - private GraphNode nodeH; - private GraphNode nodeZ; - - private GraphClass UndirectedUnweightedgraph1, - UndirectedUnweightedgraph2, - UndirectedUnweightedgraph3, - UndirectedUnweightedgraph4, - UndirectedUnweightedgraph5, - UndirectedUnweightedgraph6, - UndirectedUnweightedgraph7, - DirectedUnweightedGraph8, - DirectedUnweightedGraph9, - DirectedUnweightedGraph10, - DirectedWeightedGraph11, - DirectedWeightedGraph12, - DirectedWeightedGraph13; - - - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Before - public void setUp() throws Exception { - nodeA = new GraphNode("A"); - nodeB = new GraphNode("B"); - nodeC = new GraphNode("C"); - nodeD = new GraphNode("D"); - nodeE = new GraphNode("E"); - nodeF = new GraphNode("F"); - nodeG = new GraphNode("G"); - nodeH = new GraphNode("H"); - nodeZ = new GraphNode("Z"); - - List nodesForUndirectedUnweightedGraph1 = new LinkedList(Arrays.asList( - nodeA, - nodeB, - nodeC, - nodeD, - nodeE, - nodeF - )); - - GraphEdge edgeBetweenAandB = new GraphEdge(nodeA, nodeB); - GraphEdge edgeBetweenBandC = new GraphEdge(nodeB, nodeC); - GraphEdge edgeBetweenCandD = new GraphEdge(nodeC, nodeD); - GraphEdge edgeBetweenDandE = new GraphEdge(nodeD, nodeE); - GraphEdge edgeBetweenEandF = new GraphEdge(nodeE, nodeF); - - List edgesForUndirectedUnweightedGraph1 = new LinkedList(); - edgesForUndirectedUnweightedGraph1.add(edgeBetweenAandB); - edgesForUndirectedUnweightedGraph1.add(edgeBetweenBandC); - edgesForUndirectedUnweightedGraph1.add(edgeBetweenCandD); - edgesForUndirectedUnweightedGraph1.add(edgeBetweenDandE); - edgesForUndirectedUnweightedGraph1.add(edgeBetweenEandF); - // - // Graph #1: Straight line (unweighted, undirected) - // A -> B -> C -> D -> E -> F - // - UndirectedUnweightedgraph1 = new GraphClass(nodesForUndirectedUnweightedGraph1, edgesForUndirectedUnweightedGraph1); - - List nodesForUndirectedUnweightedGraph2 = new LinkedList(Arrays.asList( - nodeA, - nodeB, - nodeC, - nodeD, - nodeE, - nodeF, - nodeG, - nodeH, - nodeZ - )); - - GraphEdge edgeBetweenFandG = new GraphEdge(nodeF, nodeG); - GraphEdge edgeBetweenGandH = new GraphEdge(nodeG, nodeH); - GraphEdge edgeBetweenAandZ = new GraphEdge(nodeA, nodeZ); - GraphEdge edgeBetweenHandZ = new GraphEdge(nodeH, nodeZ); - - List edgesForUndirectedUnweightedGraph2 = new LinkedList(); - edgesForUndirectedUnweightedGraph2.add(edgeBetweenAandB); - edgesForUndirectedUnweightedGraph2.add(edgeBetweenBandC); - edgesForUndirectedUnweightedGraph2.add(edgeBetweenCandD); - edgesForUndirectedUnweightedGraph2.add(edgeBetweenDandE); - edgesForUndirectedUnweightedGraph2.add(edgeBetweenEandF); - edgesForUndirectedUnweightedGraph2.add(edgeBetweenFandG); - edgesForUndirectedUnweightedGraph2.add(edgeBetweenGandH); - edgesForUndirectedUnweightedGraph2.add(edgeBetweenAandZ); - edgesForUndirectedUnweightedGraph2.add(edgeBetweenHandZ); - - // - // Graph #2: Different solutions for DFS and BFS (unweighted, undirected) - // A -> B -> C -> D -> E -> F -> G -> H - // \ / - // \------------> Z -------------/ - // - UndirectedUnweightedgraph2 = new GraphClass(nodesForUndirectedUnweightedGraph2, edgesForUndirectedUnweightedGraph2); - - // graph 2 has all nodes A - H & Z - List nodesForUndirectedUnweightedGraph3 = nodesForUndirectedUnweightedGraph2; - -// GraphEdge edgeBetweenHandZ = new GraphEdge(nodeZ, nodeH); - /* - * make edges, then make edge list: - * graph 3 has edges: - * A - D - * B - D - * B - C ~ - * C - D ~ - * D - F - * D - H - * H - Z ~ - * E - Z - * E - G - */ - - GraphEdge edgeBetweenEandZ = new GraphEdge(nodeE, nodeZ); - GraphEdge edgeBetweenEandG = new GraphEdge(nodeE, nodeG); - GraphEdge edgeBetweenDandH = new GraphEdge(nodeD, nodeH); - GraphEdge edgeBetweenDandF = new GraphEdge(nodeD, nodeF); - GraphEdge edgeBetweenBandD = new GraphEdge(nodeB, nodeD); - GraphEdge edgeBetweenAandD = new GraphEdge(nodeA, nodeD); - - List edgesForUndirectedUnweightedGraph3 = new LinkedList(); - edgesForUndirectedUnweightedGraph3.add(edgeBetweenBandC); - edgesForUndirectedUnweightedGraph3.add(edgeBetweenCandD); - edgesForUndirectedUnweightedGraph3.add(edgeBetweenHandZ); - edgesForUndirectedUnweightedGraph3.add(edgeBetweenEandZ); - edgesForUndirectedUnweightedGraph3.add(edgeBetweenEandG); - edgesForUndirectedUnweightedGraph3.add(edgeBetweenDandH); - edgesForUndirectedUnweightedGraph3.add(edgeBetweenDandF); - edgesForUndirectedUnweightedGraph3.add(edgeBetweenBandD); - edgesForUndirectedUnweightedGraph3.add(edgeBetweenAandD); - - // - // Graph #3: see UndirectedUnweightedgraph3.png - // - UndirectedUnweightedgraph3 = new GraphClass(nodesForUndirectedUnweightedGraph3, edgesForUndirectedUnweightedGraph3); - - // graph 2 has all nodes A - H & Z - List nodesForUndirectedUnweightedGraph4 = nodesForUndirectedUnweightedGraph2; - -// GraphEdge edgeBetweenHandZ = new GraphEdge(nodeZ, nodeH); - /* - * make edges, then make edge list: - * graph 4 has edges: - * a-b ~ - * b-e - * b-h - * e-g ~ - * g-h ~ - * h-z ~ - * h-d ~ - * d-f ~ - * d-c ~ - * c-f - * - */ - GraphEdge edgeBetweenBandE = new GraphEdge(nodeB, nodeE); - GraphEdge edgeBetweenBandH = new GraphEdge(nodeB, nodeH); - GraphEdge edgeBetweenCandF = new GraphEdge(nodeC, nodeF); - - - List edgesForUndirectedUnweightedGraph4 = new LinkedList(); - edgesForUndirectedUnweightedGraph4.add(edgeBetweenCandD); - edgesForUndirectedUnweightedGraph4.add(edgeBetweenHandZ); - edgesForUndirectedUnweightedGraph4.add(edgeBetweenEandG); - edgesForUndirectedUnweightedGraph4.add(edgeBetweenDandH); - edgesForUndirectedUnweightedGraph4.add(edgeBetweenDandF); - edgesForUndirectedUnweightedGraph4.add(edgeBetweenAandB); - edgesForUndirectedUnweightedGraph4.add(edgeBetweenGandH); - edgesForUndirectedUnweightedGraph4.add(edgeBetweenBandE); - edgesForUndirectedUnweightedGraph4.add(edgeBetweenBandH); - edgesForUndirectedUnweightedGraph4.add(edgeBetweenCandF); - - - // - // Graph #4: see UndirectedUnweightedgraph4.png - // - UndirectedUnweightedgraph4 = new GraphClass(nodesForUndirectedUnweightedGraph4, edgesForUndirectedUnweightedGraph4); - - - // graph 2 has all nodes A - H & Z - List nodesForUndirectedUnweightedGraph5 = nodesForUndirectedUnweightedGraph2; - // - // Graph #5: graph with nodes from graph 2 and edges from graph 1 - // - // start and target have no path, will return empty list - UndirectedUnweightedgraph5 = new GraphClass(nodesForUndirectedUnweightedGraph5, edgesForUndirectedUnweightedGraph1); - - - - // graph 2 has all nodes A - H & Z - List nodesForUndirectedUnweightedGraph6 = nodesForUndirectedUnweightedGraph2; - /* - * make edges, then make edge list: - * graph 6 nodes A-F, and all edges (15) - * a-b ~ - * a-c - * a-d ~ - * a-e - * a-f - * b-c ~ - * b-d ~ - * b-e ~ - * b-f - * c-d ~ - * c-e - * c-f ~ - * d-e ~ - * d-f ~ - * e-f ~ - * - */ - - GraphEdge edgeBetweenCandE = new GraphEdge(nodeC, nodeE); - GraphEdge edgeBetweenBandF = new GraphEdge(nodeB, nodeF); - GraphEdge edgeBetweenAandF = new GraphEdge(nodeA, nodeF); - GraphEdge edgeBetweenAandE = new GraphEdge(nodeA, nodeE); - GraphEdge edgeBetweenAandC = new GraphEdge(nodeA, nodeC); - - - List edgesForUndirectedUnweightedGraph6 = new LinkedList(edgesForUndirectedUnweightedGraph1); - edgesForUndirectedUnweightedGraph6.add(edgeBetweenCandF); - edgesForUndirectedUnweightedGraph6.add(edgeBetweenAandB); - edgesForUndirectedUnweightedGraph6.add(edgeBetweenAandD); - edgesForUndirectedUnweightedGraph6.add(edgeBetweenBandC); - edgesForUndirectedUnweightedGraph6.add(edgeBetweenBandD); - edgesForUndirectedUnweightedGraph6.add(edgeBetweenBandE); - edgesForUndirectedUnweightedGraph6.add(edgeBetweenCandD); - edgesForUndirectedUnweightedGraph6.add(edgeBetweenCandE); - edgesForUndirectedUnweightedGraph6.add(edgeBetweenDandE); - edgesForUndirectedUnweightedGraph6.add(edgeBetweenDandF); - edgesForUndirectedUnweightedGraph6.add(edgeBetweenEandF); - edgesForUndirectedUnweightedGraph6.add(edgeBetweenBandF); - edgesForUndirectedUnweightedGraph6.add(edgeBetweenAandF); - edgesForUndirectedUnweightedGraph6.add(edgeBetweenAandE); - edgesForUndirectedUnweightedGraph6.add(edgeBetweenAandC); - - // - // Graph #6: graph with nodes from graph 1, fully connected - // - UndirectedUnweightedgraph6 = new GraphClass(nodesForUndirectedUnweightedGraph6, edgesForUndirectedUnweightedGraph6); - - // graph 2 has all nodes A - H & Z - List nodesForUndirectedUnweightedGraph7 = nodesForUndirectedUnweightedGraph2; - // edges needed (17): - /* - * a-b ~ - * a-e ~ - * a-z ~ - * a-g - * b-g - * b-e ~ - * e-g ~ - * g-z - * e-z ~ - * d-e ~ - * h-z ~ - * d-h ~ - * c-d ~ - * d-f ~ - * c-h - * c-f ~ - * f-h - * - */ - - - GraphEdge edgeBetweenCandH = new GraphEdge(nodeC, nodeH); - GraphEdge edgeBetweenBandG = new GraphEdge(nodeB, nodeG); - GraphEdge edgeBetweenAandG = new GraphEdge(nodeA, nodeG); - GraphEdge edgeBetweenGandZ = new GraphEdge(nodeG, nodeZ); - GraphEdge edgeBetweenFandH = new GraphEdge(nodeF, nodeH); - - List edgesForUndirectedUnweightedGraph7 = new LinkedList(); - edgesForUndirectedUnweightedGraph7.add(edgeBetweenAandB); - edgesForUndirectedUnweightedGraph7.add(edgeBetweenAandE); - edgesForUndirectedUnweightedGraph7.add(edgeBetweenAandZ); - edgesForUndirectedUnweightedGraph7.add(edgeBetweenBandE); - edgesForUndirectedUnweightedGraph7.add(edgeBetweenCandD); - edgesForUndirectedUnweightedGraph7.add(edgeBetweenCandF); - edgesForUndirectedUnweightedGraph7.add(edgeBetweenDandE); - edgesForUndirectedUnweightedGraph7.add(edgeBetweenDandF); - edgesForUndirectedUnweightedGraph7.add(edgeBetweenDandH); - edgesForUndirectedUnweightedGraph7.add(edgeBetweenEandG); - edgesForUndirectedUnweightedGraph7.add(edgeBetweenEandZ); - edgesForUndirectedUnweightedGraph7.add(edgeBetweenHandZ); - edgesForUndirectedUnweightedGraph7.add(edgeBetweenCandH); - edgesForUndirectedUnweightedGraph7.add(edgeBetweenBandG); - edgesForUndirectedUnweightedGraph7.add(edgeBetweenAandG); - edgesForUndirectedUnweightedGraph7.add(edgeBetweenGandZ); - edgesForUndirectedUnweightedGraph7.add(edgeBetweenFandH); - // - // Graph #7: graph7.png - // - UndirectedUnweightedgraph7 = new GraphClass(nodesForUndirectedUnweightedGraph7, edgesForUndirectedUnweightedGraph7); - - - - /* - * graph 8 - * 12 edges - * a-d ~ - * b-d ~ - * g-d - * z-g - * z-d - * h-z ~ - * b-h ~ - * f-b - * c-e ~ - * d-c - * e-d - * d-f - */ - - - GraphEdge edgeBetweenGandD = new GraphEdge(nodeG,nodeD); - GraphEdge edgeBetweenZandG = new GraphEdge(nodeZ,nodeG); - GraphEdge edgeBetweenZandD = new GraphEdge(nodeZ,nodeD); - GraphEdge edgeBetweenFandB = new GraphEdge(nodeF,nodeB); - GraphEdge edgeBetweenDandC = new GraphEdge(nodeD,nodeC); - GraphEdge edgeBetweenEandD = new GraphEdge(nodeE,nodeD); - - List edgesForGraph8 = new LinkedList(); - edgesForGraph8.add(edgeBetweenAandD); - edgesForGraph8.add(edgeBetweenBandD); - edgesForGraph8.add(edgeBetweenHandZ); - edgesForGraph8.add(edgeBetweenBandH); - edgesForGraph8.add(edgeBetweenCandE); - edgesForGraph8.add(edgeBetweenGandD); - edgesForGraph8.add(edgeBetweenZandG); - edgesForGraph8.add(edgeBetweenZandD); - edgesForGraph8.add(edgeBetweenFandB); - edgesForGraph8.add(edgeBetweenDandC); - edgesForGraph8.add(edgeBetweenEandD); - edgesForGraph8.add(edgeBetweenDandF); - - - // use nodes from graph 2 - DirectedUnweightedGraph8 = new GraphClass(nodesForUndirectedUnweightedGraph2, edgesForGraph8); - DirectedUnweightedGraph8.setDirected(true); - // - // Graph #8: directed and unweighted - // - - - /* - * graph 9 - * 12 edges - * a-b - * b-c - * c-e - * e-d - * g-d - * g-a - * z-g - * h-z - * f-h - * d-f - * e-z - * f-a - * - */ - - GraphEdge edgeBetweenGandA = new GraphEdge(nodeG, nodeA); - GraphEdge edgeBetweenFandA = new GraphEdge(nodeF, nodeA); - - List edgesForGraph9 = new LinkedList(); - edgesForGraph9.add(edgeBetweenAandB); - edgesForGraph9.add(edgeBetweenBandC); - edgesForGraph9.add(edgeBetweenCandE); - edgesForGraph9.add(edgeBetweenEandZ); - edgesForGraph9.add(edgeBetweenEandD); - edgesForGraph9.add(edgeBetweenGandD); - edgesForGraph9.add(edgeBetweenZandG); - edgesForGraph9.add(edgeBetweenHandZ); - edgesForGraph9.add(edgeBetweenFandH); - edgesForGraph9.add(edgeBetweenDandF); - edgesForGraph9.add(edgeBetweenGandA); - edgesForGraph9.add(edgeBetweenFandA); - - - DirectedUnweightedGraph9 = new GraphClass(nodesForUndirectedUnweightedGraph2, edgesForGraph9); - DirectedUnweightedGraph9.setDirected(true); - // - // Graph #9: directed and unweighted - // - - - /* - * graph 10 - * 10 edges - * a-d - * a-c - * d-c - * z-c - * g-e - * e-b - * b-f - * f-h - * d-z - * g-a - */ - - GraphEdge edgeBetweenZandC = new GraphEdge(nodeZ, nodeC); - GraphEdge edgeBetweenGandE = new GraphEdge(nodeG, nodeE); - GraphEdge edgeBetweenEandB = new GraphEdge(nodeE, nodeB); - GraphEdge edgeBetweenDandZ = new GraphEdge(nodeD, nodeZ); - List edgesForGraph10 = new LinkedList(); - edgesForGraph10.add(edgeBetweenAandD); - edgesForGraph10.add(edgeBetweenAandC); - edgesForGraph10.add(edgeBetweenDandC); - edgesForGraph10.add(edgeBetweenBandF); - edgesForGraph10.add(edgeBetweenFandH); - edgesForGraph10.add(edgeBetweenZandC); - edgesForGraph10.add(edgeBetweenGandE); - edgesForGraph10.add(edgeBetweenEandB); - edgesForGraph10.add(edgeBetweenDandZ); - edgesForGraph10.add(edgeBetweenGandA); - - DirectedUnweightedGraph10 = new GraphClass(nodesForUndirectedUnweightedGraph2, edgesForGraph10); - DirectedUnweightedGraph10.setDirected(true); - // - // Graph #10: directed and unweighted - // - - - /* - * graph 8 but weighted - * 12 edges - * a-d - * b-d - * g-d - * z-g - * z-d 2, rest 1 - * h-z - * b-h - * f-b - * c-e - * d-c - * e-d - * d-f - * - * - */ - List edgesForGraph11 = new LinkedList(); - edgesForGraph11.add(edgeBetweenAandD.setWeighted(true).setWeight(1)); - edgesForGraph11.add(edgeBetweenZandD.setWeighted(true).setWeight(2)); - edgesForGraph11.add(edgeBetweenBandD.setWeighted(true).setWeight(1)); - edgesForGraph11.add(edgeBetweenHandZ.setWeighted(true).setWeight(1)); - edgesForGraph11.add(edgeBetweenBandH.setWeighted(true).setWeight(1)); - edgesForGraph11.add(edgeBetweenCandE.setWeighted(true).setWeight(1)); - edgesForGraph11.add(edgeBetweenGandD.setWeighted(true).setWeight(1)); - edgesForGraph11.add(edgeBetweenZandG.setWeighted(true).setWeight(1)); - edgesForGraph11.add(edgeBetweenFandB.setWeighted(true).setWeight(1)); - edgesForGraph11.add(edgeBetweenDandC.setWeighted(true).setWeight(1)); - edgesForGraph11.add(edgeBetweenEandD.setWeighted(true).setWeight(1)); - edgesForGraph11.add(edgeBetweenDandF.setWeighted(true).setWeight(1)); - DirectedWeightedGraph11 = new GraphClass(nodesForUndirectedUnweightedGraph2, edgesForGraph11); - DirectedWeightedGraph11.setDirected(true); - DirectedWeightedGraph11.setWeighted(true); - - // - // Graph #11: directed and weighted - // - - /* - * graph 9 but weighted - * 12 edges - * a-b - * b-c - * c-e - * e-d - * g-d - * g-a - * z-g - * h-z - * f-h - * d-f - * e-z - * f-a - * - */ - List edgesForGraph12 = new LinkedList(); - edgesForGraph12.add(edgeBetweenAandB.setWeighted(true).setWeight(1)); - edgesForGraph12.add(edgeBetweenBandC.setWeighted(true).setWeight(1)); - edgesForGraph12.add(edgeBetweenCandE.setWeighted(true).setWeight(1)); - edgesForGraph12.add(edgeBetweenEandZ.setWeighted(true).setWeight(1)); - edgesForGraph12.add(edgeBetweenEandD.setWeighted(true).setWeight(2)); - edgesForGraph12.add(edgeBetweenGandD.setWeighted(true).setWeight(2)); - edgesForGraph12.add(edgeBetweenZandG.setWeighted(true).setWeight(1)); - edgesForGraph12.add(edgeBetweenHandZ.setWeighted(true).setWeight(1)); - edgesForGraph12.add(edgeBetweenFandH.setWeighted(true).setWeight(1)); - edgesForGraph12.add(edgeBetweenDandF.setWeighted(true).setWeight(1)); - edgesForGraph12.add(edgeBetweenGandA.setWeighted(true).setWeight(1)); - edgesForGraph12.add(edgeBetweenFandA.setWeighted(true).setWeight(1)); - DirectedWeightedGraph12 = new GraphClass(nodesForUndirectedUnweightedGraph2, edgesForGraph12); - DirectedWeightedGraph12.setDirected(true); - DirectedWeightedGraph12.setWeighted(true); - - // - // Graph #12: directed and weighted - // - - - /* - * graph 10 but weighted - * 10 edges - * a-d - * a-c - * d-c - * z-c - * g-e - * e-b - * b-f - * f-h - * d-z - * g-a - * - */ - - List edgesForGraph13 = new LinkedList(); - edgesForGraph13.add(edgeBetweenAandD.setWeighted(true).setWeight(1)); - edgesForGraph13.add(edgeBetweenAandC.setWeighted(true).setWeight(1)); - edgesForGraph13.add(edgeBetweenDandC.setWeighted(true).setWeight(1)); - edgesForGraph13.add(edgeBetweenBandF.setWeighted(true).setWeight(1)); - edgesForGraph13.add(edgeBetweenFandH.setWeighted(true).setWeight(1)); - edgesForGraph13.add(edgeBetweenZandC.setWeighted(true).setWeight(1)); - edgesForGraph13.add(edgeBetweenGandE.setWeighted(true).setWeight(2)); - edgesForGraph13.add(edgeBetweenEandB.setWeighted(true).setWeight(1)); - edgesForGraph13.add(edgeBetweenDandZ.setWeighted(true).setWeight(1)); - edgesForGraph13.add(edgeBetweenGandA.setWeighted(true).setWeight(1)); - - - DirectedWeightedGraph13 = new GraphClass(nodesForUndirectedUnweightedGraph2, edgesForGraph13); - DirectedWeightedGraph13.setDirected(true); - DirectedWeightedGraph13.setWeighted(true); - - // - // Graph #13: directed and weighted - // - - - } - - @Test - public void depthFirstSearchUndirectedUnweightedGraph01() throws Exception { - List path = GraphSearch.depthFirstSearch(UndirectedUnweightedgraph1, nodeA, nodeF); - Assert.assertEquals(path.size(), 6); - Assert.assertTrue(path.get(0).getValue().equals("A")); - Assert.assertTrue(path.get(1).getValue().equals("B")); - Assert.assertTrue(path.get(2).getValue().equals("C")); - Assert.assertTrue(path.get(3).getValue().equals("D")); - Assert.assertTrue(path.get(4).getValue().equals("E")); - Assert.assertTrue(path.get(5).getValue().equals("F")); - } - - @Test - public void depthFirstSearchRecursiveUndirectedUnweightedGraph01() throws Exception { - List path = GraphSearch.depthFirstSearchRecursive(UndirectedUnweightedgraph1, nodeA, nodeF); - Assert.assertEquals(path.size(), 6); - Assert.assertTrue(path.get(0).getValue().equals("A")); - Assert.assertTrue(path.get(1).getValue().equals("B")); - Assert.assertTrue(path.get(2).getValue().equals("C")); - Assert.assertTrue(path.get(3).getValue().equals("D")); - Assert.assertTrue(path.get(4).getValue().equals("E")); - Assert.assertTrue(path.get(5).getValue().equals("F")); - } - - @Test - public void breadthFirstSearchUndirectedUnweightedGraph01() throws Exception { - List path = GraphSearch.breadthFirstSearch(UndirectedUnweightedgraph1, nodeA, nodeF); - Assert.assertEquals(path.size(), 6); - Assert.assertTrue(path.get(0).getValue().equals("A")); - Assert.assertTrue(path.get(1).getValue().equals("B")); - Assert.assertTrue(path.get(2).getValue().equals("C")); - Assert.assertTrue(path.get(3).getValue().equals("D")); - Assert.assertTrue(path.get(4).getValue().equals("E")); - Assert.assertTrue(path.get(5).getValue().equals("F")); - } - - @Test - public void depthFirstSearchUndirectedUnweightedGraph02() throws Exception { - List path = GraphSearch.depthFirstSearch(UndirectedUnweightedgraph2, nodeA, nodeH); - Assert.assertEquals(path.size(), 8); - Assert.assertTrue(path.get(0).getValue().equals("A")); - Assert.assertTrue(path.get(1).getValue().equals("B")); - Assert.assertTrue(path.get(2).getValue().equals("C")); - Assert.assertTrue(path.get(3).getValue().equals("D")); - Assert.assertTrue(path.get(4).getValue().equals("E")); - Assert.assertTrue(path.get(5).getValue().equals("F")); - Assert.assertTrue(path.get(6).getValue().equals("G")); - Assert.assertTrue(path.get(7).getValue().equals("H")); - } - - @Test - public void depthFirstSearchRecursiveUndirectedUnweightedGraph02() throws Exception { - List path = GraphSearch.depthFirstSearchRecursive(UndirectedUnweightedgraph2, nodeA, nodeH); - Assert.assertEquals(path.size(), 8); - Assert.assertTrue(path.get(0).getValue().equals("A")); - Assert.assertTrue(path.get(1).getValue().equals("B")); - Assert.assertTrue(path.get(2).getValue().equals("C")); - Assert.assertTrue(path.get(3).getValue().equals("D")); - Assert.assertTrue(path.get(4).getValue().equals("E")); - Assert.assertTrue(path.get(5).getValue().equals("F")); - Assert.assertTrue(path.get(6).getValue().equals("G")); - Assert.assertTrue(path.get(7).getValue().equals("H")); - } - - @Test - public void breadthFirstSearchUndirectedUnweightedGraph02() throws Exception { - List path = GraphSearch.breadthFirstSearch(UndirectedUnweightedgraph2, nodeA, nodeH); - Assert.assertEquals(path.size(), 3); - Assert.assertTrue(path.get(0).getValue().equals("A")); - Assert.assertTrue(path.get(1).getValue().equals("Z")); - Assert.assertTrue(path.get(2).getValue().equals("H")); - } - - @Test - public void depthFirstSearchUndirectedUnweightedGraph03() throws Exception { - List path = GraphSearch.depthFirstSearch(UndirectedUnweightedgraph3, nodeB, nodeE); - Assert.assertEquals(path.size(), 6); - Assert.assertTrue(path.get(0).getValue().equals("B")); - Assert.assertTrue(path.get(1).getValue().equals("C")); - Assert.assertTrue(path.get(2).getValue().equals("D")); - Assert.assertTrue(path.get(3).getValue().equals("H")); - Assert.assertTrue(path.get(4).getValue().equals("Z")); - Assert.assertTrue(path.get(5).getValue().equals("E")); - } - - @Test - public void depthFirstSearchRecursiveUndirectedUnweightedGraph03() throws Exception { - List path = GraphSearch.depthFirstSearchRecursive(UndirectedUnweightedgraph3, nodeB, nodeE); - Assert.assertEquals(path.size(), 6); - Assert.assertTrue(path.get(0).getValue().equals("B")); - Assert.assertTrue(path.get(1).getValue().equals("C")); - Assert.assertTrue(path.get(2).getValue().equals("D")); - Assert.assertTrue(path.get(3).getValue().equals("H")); - Assert.assertTrue(path.get(4).getValue().equals("Z")); - Assert.assertTrue(path.get(5).getValue().equals("E")); - } - - @Test - public void breadthFirstSearchUndirectedUnweightedGraph03() throws Exception { - List path = GraphSearch.breadthFirstSearch(UndirectedUnweightedgraph3, nodeB, nodeE); - Assert.assertEquals(path.size(), 5); - Assert.assertTrue(path.get(0).getValue().equals("B")); - Assert.assertTrue(path.get(1).getValue().equals("D")); - Assert.assertTrue(path.get(2).getValue().equals("H")); - Assert.assertTrue(path.get(3).getValue().equals("Z")); - Assert.assertTrue(path.get(4).getValue().equals("E")); - } - - @Test - public void depthFirstSearchUndirectedUnweightedGraph04() throws Exception { - List path = GraphSearch.depthFirstSearch(UndirectedUnweightedgraph4, nodeB, nodeF); - Assert.assertEquals(path.size(), 7); - Assert.assertTrue(path.get(0).getValue().equals("B")); - Assert.assertTrue(path.get(1).getValue().equals("E")); - Assert.assertTrue(path.get(2).getValue().equals("G")); - Assert.assertTrue(path.get(3).getValue().equals("H")); - Assert.assertTrue(path.get(4).getValue().equals("D")); - Assert.assertTrue(path.get(5).getValue().equals("C")); - Assert.assertTrue(path.get(6).getValue().equals("F")); - } - - @Test - public void depthFirstSearchRecursiveUndirectedUnweightedGraph04() throws Exception { - List path = GraphSearch.depthFirstSearchRecursive(UndirectedUnweightedgraph4, nodeB, nodeF); - Assert.assertEquals(path.size(), 7); - Assert.assertTrue(path.get(0).getValue().equals("B")); - Assert.assertTrue(path.get(1).getValue().equals("E")); - Assert.assertTrue(path.get(2).getValue().equals("G")); - Assert.assertTrue(path.get(3).getValue().equals("H")); - Assert.assertTrue(path.get(4).getValue().equals("D")); - Assert.assertTrue(path.get(5).getValue().equals("C")); - Assert.assertTrue(path.get(6).getValue().equals("F")); - } - - @Test - public void breadthFirstSearchUndirectedUnweightedGraph04() throws Exception { - List path = GraphSearch.breadthFirstSearch(UndirectedUnweightedgraph4, nodeB, nodeF); - Assert.assertEquals(path.size(), 4); - Assert.assertTrue(path.get(0).getValue().equals("B")); - Assert.assertTrue(path.get(1).getValue().equals("H")); - Assert.assertTrue(path.get(2).getValue().equals("D")); - Assert.assertTrue(path.get(3).getValue().equals("F")); - } - - @Test - public void depthFirstSearchUndirectedUnweightedGraph05() throws Exception { - List path = GraphSearch.depthFirstSearch(UndirectedUnweightedgraph5, nodeA, nodeZ); - Assert.assertEquals(path.size(), 0); - } - - @Test - public void depthFirstSearchRecursiveUndirectedUnweightedGraph05() throws Exception { - List path = GraphSearch.depthFirstSearchRecursive(UndirectedUnweightedgraph5, nodeA, nodeZ); - Assert.assertEquals(path.size(), 0); - } - - @Test - public void breadthFirstSearchUndirectedUnweightedGraph05() throws Exception { - List path = GraphSearch.breadthFirstSearch(UndirectedUnweightedgraph5, nodeA, nodeZ); - Assert.assertEquals(path.size(), 0); - } - - @Test - public void depthFirstSearchUndirectedUnweightedGraph06() throws Exception { - List path = GraphSearch.depthFirstSearch(UndirectedUnweightedgraph6, nodeA, nodeF); - Assert.assertEquals(path.size(), 6); - } - - @Test - public void depthFirstSearchRecursiveUndirectedUnweightedGraph06() throws Exception { - List path = GraphSearch.depthFirstSearchRecursive(UndirectedUnweightedgraph6, nodeA, nodeF); - Assert.assertEquals(path.size(), 6); - } - - @Test - public void breadthFirstSearchUndirectedUnweightedGraph06() throws Exception { - List path = GraphSearch.breadthFirstSearch(UndirectedUnweightedgraph6, nodeA, nodeF); - Assert.assertEquals(path.size(), 2); - Assert.assertTrue(path.get(0).getValue().equals("A")); - Assert.assertTrue(path.get(1).getValue().equals("F")); - } - - @Test - public void depthFirstSearchUndirectedUnweightedGraph07() throws Exception { - List path = GraphSearch.depthFirstSearch(UndirectedUnweightedgraph7, nodeA, nodeF); - Assert.assertEquals(path.size(), 6); - } - - @Test - public void depthFirstSearchRecursiveUndirectedUnweightedGraph07() throws Exception { - List path = GraphSearch.depthFirstSearchRecursive(UndirectedUnweightedgraph7, nodeA, nodeF); - Assert.assertEquals(path.size(), 6); - } - - @Test - public void breadthFirstSearchUndirectedUnweightedGraph07() throws Exception { - List path = GraphSearch.breadthFirstSearch(UndirectedUnweightedgraph7, nodeA, nodeF); - Assert.assertEquals(path.size(), 4); - Assert.assertTrue(path.get(0).getValue().equals("A")); - Assert.assertTrue(path.get(1).getValue().equals("E")); - Assert.assertTrue(path.get(2).getValue().equals("D")); - Assert.assertTrue(path.get(3).getValue().equals("F")); - } - - @Test - public void depthFirstSearchNullStartNode() throws Exception { - thrown.expect(IllegalArgumentException.class); - List path = GraphSearch.depthFirstSearch(UndirectedUnweightedgraph5, null, nodeZ); - // the expect message is thrown if the thrown var doesnt catch it's appropriate exception? - // ie this message doesn't need to match the message of the thrown exception in GraphSearch.java, correct? - thrown.expectMessage("expect message"); - } - - @Test - public void depthFirstSearchRecursiveNullStartNode() throws Exception { - thrown.expect(IllegalArgumentException.class); - List path = GraphSearch.depthFirstSearchRecursive(UndirectedUnweightedgraph5, null, nodeZ); - thrown.expectMessage("expect message"); - } - - @Test - public void breadthFirstSearchNullStartNode() throws Exception { - thrown.expect(IllegalArgumentException.class); - List path = GraphSearch.breadthFirstSearch(UndirectedUnweightedgraph5, null, nodeZ); - thrown.expectMessage("expect message"); - } - - @Test - public void depthFirstSearchNullFinishNode() throws Exception { - thrown.expect(IllegalArgumentException.class); - List path = GraphSearch.depthFirstSearch(UndirectedUnweightedgraph5, nodeA, null); - // the expect message is thrown if the thrown var doesnt catch it's appropriate exception? - // ie this message doesn't need to match the message of the thrown exception in GraphSearch.java, correct? - thrown.expectMessage("expect message"); - } - - @Test - public void depthFirstSearchRecursiveNullFinishNode() throws Exception { - thrown.expect(IllegalArgumentException.class); - List path = GraphSearch.depthFirstSearchRecursive(UndirectedUnweightedgraph5, nodeA, null); - thrown.expectMessage("expect message"); - } - - @Test - public void breadthFirstSearchNullFinishNode() throws Exception { - thrown.expect(IllegalArgumentException.class); - List path = GraphSearch.breadthFirstSearch(UndirectedUnweightedgraph5, nodeA, null); - thrown.expectMessage("expect message"); - } - - @Test - public void depthFirstSearchOutFinishNode() throws Exception { - List path = GraphSearch.depthFirstSearch(UndirectedUnweightedgraph1, nodeA, nodeZ); - Assert.assertEquals(path.size(), 0); - } - - @Test - public void depthFirstSearchRecursiveOutFinishNode() throws Exception { - List path = GraphSearch.depthFirstSearchRecursive(UndirectedUnweightedgraph1, nodeA, nodeZ); - Assert.assertEquals(path.size(), 0); - } - - @Test - public void breadthFirstSearchOutFinishNode() throws Exception { - List path = GraphSearch.breadthFirstSearch(UndirectedUnweightedgraph1, nodeA, nodeZ); - Assert.assertEquals(path.size(), 0); - } - - @Test - public void depthFirstSearchOutStartNode() throws Exception { - List path = GraphSearch.depthFirstSearch(UndirectedUnweightedgraph1, nodeZ, nodeA); - Assert.assertEquals(path.size(), 0); - } - - @Test - public void depthFirstSearchRecursiveOutStartNode() throws Exception { - List path = GraphSearch.depthFirstSearchRecursive(UndirectedUnweightedgraph1, nodeZ, nodeA); - Assert.assertEquals(path.size(), 0); - } - - @Test - public void breadthFirstSearchOutStartNode() throws Exception { - List path = GraphSearch.breadthFirstSearch(UndirectedUnweightedgraph1, nodeZ, nodeA); - Assert.assertEquals(path.size(), 0); - } - - @Test - public void depthFirstSearchNullGraph() throws Exception { - thrown.expect(IllegalArgumentException.class); - List path = GraphSearch.depthFirstSearch(null, nodeA, nodeZ); - thrown.expectMessage("expect message"); - } - - @Test - public void depthFirstSearchRecursiveNullGraph() throws Exception { - thrown.expect(IllegalArgumentException.class); - List path = GraphSearch.depthFirstSearchRecursive(null, nodeA, nodeZ); - thrown.expectMessage("expect message"); - } - - @Test - public void breadthFirstSearchNullGraph() throws Exception { - thrown.expect(IllegalArgumentException.class); - List path = GraphSearch.breadthFirstSearch(null, nodeA, nodeZ); - thrown.expectMessage("expect message"); - } - - @Test - public void depthFirstSearchEmptyGraph() throws Exception { - thrown.expect(IllegalArgumentException.class); - List path = GraphSearch.depthFirstSearch(new GraphClass(), nodeA, nodeZ); - thrown.expectMessage("expect message"); - } - - @Test - public void depthFirstSearchRecursiveEmptyGraph() throws Exception { - thrown.expect(IllegalArgumentException.class); - List path = GraphSearch.depthFirstSearchRecursive(new GraphClass(), nodeA, nodeZ); - thrown.expectMessage("expect message"); - } - - @Test - public void breadthFirstSearchEmptyGraph() throws Exception { - thrown.expect(IllegalArgumentException.class); - List path = GraphSearch.breadthFirstSearch(new GraphClass(), nodeA, nodeZ); - thrown.expectMessage("expect message"); - } - - @Test - public void depthFirstSearchDirectedUnweightedGraph08() throws Exception { - List path = GraphSearch.depthFirstSearch(DirectedUnweightedGraph8, nodeH, nodeB); - Assert.assertEquals(path.size(), 6); - Assert.assertTrue(path.get(0).getValue().equals("H")); - Assert.assertTrue(path.get(1).getValue().equals("Z")); - Assert.assertTrue(path.get(2).getValue().equals("G")); - Assert.assertTrue(path.get(3).getValue().equals("D")); - Assert.assertTrue(path.get(4).getValue().equals("F")); - Assert.assertTrue(path.get(5).getValue().equals("B")); - } - - @Test - public void depthFirstSearchRecursiveDirectedUnweightedGraph08() throws Exception { - List path = GraphSearch.depthFirstSearchRecursive(DirectedUnweightedGraph8, nodeH, nodeB); - Assert.assertEquals(path.size(), 6); - Assert.assertTrue(path.get(0).getValue().equals("H")); - Assert.assertTrue(path.get(1).getValue().equals("Z")); - Assert.assertTrue(path.get(2).getValue().equals("G")); - Assert.assertTrue(path.get(3).getValue().equals("D")); - Assert.assertTrue(path.get(4).getValue().equals("F")); - Assert.assertTrue(path.get(5).getValue().equals("B")); - } - - @Test - public void breadthFirstSearchDirectedUnweightedGraph08() throws Exception { - List path = GraphSearch.breadthFirstSearch(DirectedUnweightedGraph8, nodeH, nodeB); - Assert.assertEquals(path.size(), 5); - Assert.assertTrue(path.get(0).getValue().equals("H")); - Assert.assertTrue(path.get(1).getValue().equals("Z")); - Assert.assertTrue(path.get(2).getValue().equals("D")); - Assert.assertTrue(path.get(3).getValue().equals("F")); - Assert.assertTrue(path.get(4).getValue().equals("B")); - } - - @Test - public void depthFirstSearchDirectedUnweightedGraph09() throws Exception { - List path = GraphSearch.depthFirstSearch(DirectedUnweightedGraph9, nodeE, nodeF); - Assert.assertEquals(path.size(), 5); - Assert.assertTrue(path.get(0).getValue().equals("E")); - Assert.assertTrue(path.get(1).getValue().equals("Z")); - Assert.assertTrue(path.get(2).getValue().equals("G")); - Assert.assertTrue(path.get(3).getValue().equals("D")); - Assert.assertTrue(path.get(4).getValue().equals("F")); - } - - @Test - public void depthFirstSearchRecursiveDirectedUnweightedGraph09() throws Exception { - List path = GraphSearch.depthFirstSearchRecursive(DirectedUnweightedGraph9, nodeE, nodeF); - Assert.assertEquals(path.size(), 5); - Assert.assertTrue(path.get(0).getValue().equals("E")); - Assert.assertTrue(path.get(1).getValue().equals("Z")); - Assert.assertTrue(path.get(2).getValue().equals("G")); - Assert.assertTrue(path.get(3).getValue().equals("D")); - Assert.assertTrue(path.get(4).getValue().equals("F")); - } - - @Test - public void breadthFirstSearchDirectedUnweightedGraph09() throws Exception { - List path = GraphSearch.breadthFirstSearch(DirectedUnweightedGraph9, nodeE, nodeF); - Assert.assertEquals(path.size(), 3); - Assert.assertTrue(path.get(0).getValue().equals("E")); - Assert.assertTrue(path.get(1).getValue().equals("D")); - Assert.assertTrue(path.get(2).getValue().equals("F")); - } - - @Test - public void depthFirstSearchDirectedUnweightedGraph10() throws Exception { - List path = GraphSearch.depthFirstSearch(DirectedUnweightedGraph10, nodeG, nodeH); - Assert.assertEquals(path.size(), 5); - Assert.assertTrue(path.get(0).getValue().equals("G")); - Assert.assertTrue(path.get(1).getValue().equals("E")); - Assert.assertTrue(path.get(2).getValue().equals("B")); - Assert.assertTrue(path.get(3).getValue().equals("F")); - Assert.assertTrue(path.get(4).getValue().equals("H")); - } - - @Test - public void depthFirstSearchRecursiveDirectedUnweightedGraph10() throws Exception { - List path = GraphSearch.depthFirstSearchRecursive(DirectedUnweightedGraph10, nodeG, nodeH); - Assert.assertEquals(path.size(), 5); - Assert.assertTrue(path.get(0).getValue().equals("G")); - Assert.assertTrue(path.get(1).getValue().equals("E")); - Assert.assertTrue(path.get(2).getValue().equals("B")); - Assert.assertTrue(path.get(3).getValue().equals("F")); - Assert.assertTrue(path.get(4).getValue().equals("H")); - } - - @Test - public void breadthFirstSearchDirectedUnweightedGraph10() throws Exception { - List path = GraphSearch.breadthFirstSearch(DirectedUnweightedGraph10, nodeG, nodeH); - Assert.assertEquals(path.size(), 5); - Assert.assertTrue(path.get(0).getValue().equals("G")); - Assert.assertTrue(path.get(1).getValue().equals("E")); - Assert.assertTrue(path.get(2).getValue().equals("B")); - Assert.assertTrue(path.get(3).getValue().equals("F")); - Assert.assertTrue(path.get(4).getValue().equals("H")); - } - - @Test - public void depthFirstSearchDirectedWeightedGraph11() throws Exception { - List path = GraphSearch.depthFirstSearch(DirectedWeightedGraph11, nodeH, nodeB); - Assert.assertEquals(path.size(), 6); - Assert.assertTrue(path.get(0).getValue().equals("H")); - Assert.assertTrue(path.get(1).getValue().equals("Z")); - Assert.assertTrue(path.get(2).getValue().equals("G")); - Assert.assertTrue(path.get(3).getValue().equals("D")); - Assert.assertTrue(path.get(4).getValue().equals("F")); - Assert.assertTrue(path.get(5).getValue().equals("B")); - } - - @Test - public void depthFirstSearchRecursiveDirectedWeightedGraph11() throws Exception { - List path = GraphSearch.depthFirstSearchRecursive(DirectedWeightedGraph11, nodeH, nodeB); - Assert.assertEquals(path.size(), 6); - Assert.assertTrue(path.get(0).getValue().equals("H")); - Assert.assertTrue(path.get(1).getValue().equals("Z")); - Assert.assertTrue(path.get(2).getValue().equals("G")); - Assert.assertTrue(path.get(3).getValue().equals("D")); - Assert.assertTrue(path.get(4).getValue().equals("F")); - Assert.assertTrue(path.get(5).getValue().equals("B")); - } - - @Test - public void breadthFirstSearchDirectedWeightedGraph11() throws Exception { - List path = GraphSearch.breadthFirstSearch(DirectedWeightedGraph11, nodeH, nodeB); - Assert.assertEquals(path.size(), 5); - Assert.assertTrue(path.get(0).getValue().equals("H")); - Assert.assertTrue(path.get(1).getValue().equals("Z")); - Assert.assertTrue(path.get(2).getValue().equals("D")); - Assert.assertTrue(path.get(3).getValue().equals("F")); - Assert.assertTrue(path.get(4).getValue().equals("B")); - } - - @Test - public void depthFirstSearchDirectedWeightedGraph12() throws Exception { - List path = GraphSearch.depthFirstSearch(DirectedWeightedGraph12, nodeE, nodeF); - Assert.assertEquals(path.size(), 5); - Assert.assertTrue(path.get(0).getValue().equals("E")); - Assert.assertTrue(path.get(1).getValue().equals("Z")); - Assert.assertTrue(path.get(2).getValue().equals("G")); - Assert.assertTrue(path.get(3).getValue().equals("D")); - Assert.assertTrue(path.get(4).getValue().equals("F")); - } - - @Test - public void depthFirstSearchRecursiveDirectedWeightedGraph12() throws Exception { - List path = GraphSearch.depthFirstSearchRecursive(DirectedWeightedGraph12, nodeE, nodeF); - Assert.assertEquals(path.size(), 5); - Assert.assertTrue(path.get(0).getValue().equals("E")); - Assert.assertTrue(path.get(1).getValue().equals("Z")); - Assert.assertTrue(path.get(2).getValue().equals("G")); - Assert.assertTrue(path.get(3).getValue().equals("D")); - Assert.assertTrue(path.get(4).getValue().equals("F")); - } - - @Test - public void breadthFirstSearchDirectedWeightedGraph12() throws Exception { - List path = GraphSearch.breadthFirstSearch(DirectedWeightedGraph12, nodeE, nodeF); - Assert.assertEquals(path.size(), 3); - Assert.assertTrue(path.get(0).getValue().equals("E")); - Assert.assertTrue(path.get(1).getValue().equals("D")); - Assert.assertTrue(path.get(2).getValue().equals("F")); - } - - @Test - public void depthFirstSearchDirectedWeightedGraph13() throws Exception { - List path = GraphSearch.depthFirstSearch(DirectedWeightedGraph13, nodeG, nodeH); - Assert.assertEquals(path.size(), 5); - Assert.assertTrue(path.get(0).getValue().equals("G")); - Assert.assertTrue(path.get(1).getValue().equals("E")); - Assert.assertTrue(path.get(2).getValue().equals("B")); - Assert.assertTrue(path.get(3).getValue().equals("F")); - Assert.assertTrue(path.get(4).getValue().equals("H")); - } - - @Test - public void depthFirstSearchRecursiveDirectedWeightedGraph13() throws Exception { - List path = GraphSearch.depthFirstSearchRecursive(DirectedWeightedGraph13, nodeG, nodeH); - Assert.assertEquals(path.size(), 5); - Assert.assertTrue(path.get(0).getValue().equals("G")); - Assert.assertTrue(path.get(1).getValue().equals("E")); - Assert.assertTrue(path.get(2).getValue().equals("B")); - Assert.assertTrue(path.get(3).getValue().equals("F")); - Assert.assertTrue(path.get(4).getValue().equals("H")); - } - - @Test - public void breadthFirstSearchDirectedWeightedGraph13() throws Exception { - List path = GraphSearch.breadthFirstSearch(DirectedWeightedGraph13, nodeG, nodeH); - Assert.assertEquals(path.size(), 5); - Assert.assertTrue(path.get(0).getValue().equals("G")); - Assert.assertTrue(path.get(1).getValue().equals("E")); - Assert.assertTrue(path.get(2).getValue().equals("B")); - Assert.assertTrue(path.get(3).getValue().equals("F")); - Assert.assertTrue(path.get(4).getValue().equals("H")); - } - - // same as graph 13, except start at e and look for c, un connected, will need to restart - @Test - public void depthFirstSearchDirectedWeightedGraph14() throws Exception { - List path = GraphSearch.depthFirstSearch(DirectedWeightedGraph13, nodeE, nodeC); - Assert.assertEquals(path.size(), 0); - } - - @Test - public void depthFirstSearchRecursiveDirectedWeightedGraph14() throws Exception { - List path = GraphSearch.depthFirstSearchRecursive(DirectedWeightedGraph13, nodeE, nodeC); - Assert.assertEquals(path.size(), 0); - } - - @Test - public void breadthFirstSearchDirectedWeightedGraph14() throws Exception { - List path = GraphSearch.breadthFirstSearch(DirectedWeightedGraph13, nodeE, nodeC); - Assert.assertEquals(path.size(), 0); - } - -} \ No newline at end of file diff --git a/src/test/java/graphs/GraphTestSuite.java b/src/test/java/graphs/GraphTestSuite.java new file mode 100644 index 0000000..50a3266 --- /dev/null +++ b/src/test/java/graphs/GraphTestSuite.java @@ -0,0 +1,18 @@ +package graphs; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + +@RunWith(Suite.class) +@SuiteClasses({ + SpecialGraphTest.class, + UndirectedUnweightedGraphTest.class, + UndirectedWeightedGraphTest.class, + DirectedUnweightedGraphTest.class, + DirectedWeightedGraphTest.class +}) + +public class GraphTestSuite { + +} diff --git a/src/test/java/graphs/SpecialGraphTest.java b/src/test/java/graphs/SpecialGraphTest.java new file mode 100644 index 0000000..a672d2c --- /dev/null +++ b/src/test/java/graphs/SpecialGraphTest.java @@ -0,0 +1,219 @@ +package graphs; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + +public class SpecialGraphTest { + + private Node nodeA; + private Node nodeB; + private Node nodeC; + private Node nodeD; + private Node nodeE; + private Node nodeF; + private Node nodeG; + private Node nodeH; + private Node nodeZ; + + private Graph UndirectedUnweightedgraph1, + UndirectedUnweightedgraph5; + + + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Before + public void setUp() throws Exception { + nodeA = new Node("A"); + nodeB = new Node("B"); + nodeC = new Node("C"); + nodeD = new Node("D"); + nodeE = new Node("E"); + nodeF = new Node("F"); + nodeG = new Node("G"); + nodeH = new Node("H"); + nodeZ = new Node("Z"); + + List nodesForUndirectedUnweightedGraph1 = new LinkedList(Arrays.asList( + nodeA, + nodeB, + nodeC, + nodeD, + nodeE, + nodeF + )); + + Edge edgeBetweenAandB = new Edge(nodeA, nodeB); + Edge edgeBetweenBandC = new Edge(nodeB, nodeC); + Edge edgeBetweenCandD = new Edge(nodeC, nodeD); + Edge edgeBetweenDandE = new Edge(nodeD, nodeE); + Edge edgeBetweenEandF = new Edge(nodeE, nodeF); + + List edgesForUndirectedUnweightedGraph1 = new LinkedList(); + edgesForUndirectedUnweightedGraph1.add(edgeBetweenAandB); + edgesForUndirectedUnweightedGraph1.add(edgeBetweenBandC); + edgesForUndirectedUnweightedGraph1.add(edgeBetweenCandD); + edgesForUndirectedUnweightedGraph1.add(edgeBetweenDandE); + edgesForUndirectedUnweightedGraph1.add(edgeBetweenEandF); + // + // Graph #1: Straight line (unweighted, undirected) + // A -> B -> C -> D -> E -> F + // + UndirectedUnweightedgraph1 = new Graph(nodesForUndirectedUnweightedGraph1, edgesForUndirectedUnweightedGraph1); + + + List nodesForUndirectedUnweightedGraph5 = new LinkedList(Arrays.asList( + nodeA, + nodeB, + nodeC, + nodeD, + nodeE, + nodeF, + nodeG, + nodeH, + nodeZ + )); + + // + // Graph #5: graph with nodes from graph 2 and edges from graph 1 + // + // start and target have no path, will return empty list + UndirectedUnweightedgraph5 = new Graph(nodesForUndirectedUnweightedGraph5, edgesForUndirectedUnweightedGraph1); + + + + } + + @Test + public void depthFirstSearchNullStartNode() throws Exception { + thrown.expect(IllegalArgumentException.class); + List path = GraphSearch.depthFirstSearch(UndirectedUnweightedgraph5, null, nodeZ); + // the expect message is thrown if the thrown var doesnt catch it's appropriate exception? + // ie this message doesn't need to match the message of the thrown exception in GraphSearch.java, correct? + thrown.expectMessage("expect message"); + } + + @Test + public void depthFirstSearchRecursiveNullStartNode() throws Exception { + thrown.expect(IllegalArgumentException.class); + List path = GraphSearch.depthFirstSearchRecursive(UndirectedUnweightedgraph5, null, nodeZ); + thrown.expectMessage("expect message"); + } + + @Test + public void breadthFirstSearchNullStartNode() throws Exception { + thrown.expect(IllegalArgumentException.class); + List path = GraphSearch.breadthFirstSearch(UndirectedUnweightedgraph5, null, nodeZ); + thrown.expectMessage("expect message"); + } + + @Test + public void depthFirstSearchNullFinishNode() throws Exception { + thrown.expect(IllegalArgumentException.class); + List path = GraphSearch.depthFirstSearch(UndirectedUnweightedgraph5, nodeA, null); + // the expect message is thrown if the thrown var doesnt catch it's appropriate exception? + // ie this message doesn't need to match the message of the thrown exception in GraphSearch.java, correct? + thrown.expectMessage("expect message"); + } + + @Test + public void depthFirstSearchRecursiveNullFinishNode() throws Exception { + thrown.expect(IllegalArgumentException.class); + List path = GraphSearch.depthFirstSearchRecursive(UndirectedUnweightedgraph5, nodeA, null); + thrown.expectMessage("expect message"); + } + + @Test + public void breadthFirstSearchNullFinishNode() throws Exception { + thrown.expect(IllegalArgumentException.class); + List path = GraphSearch.breadthFirstSearch(UndirectedUnweightedgraph5, nodeA, null); + thrown.expectMessage("expect message"); + } + + @Test + public void depthFirstSearchOutFinishNode() throws Exception { + List path = GraphSearch.depthFirstSearch(UndirectedUnweightedgraph1, nodeA, nodeZ); + Assert.assertEquals(path.size(), 0); + } + + @Test + public void depthFirstSearchRecursiveOutFinishNode() throws Exception { + List path = GraphSearch.depthFirstSearchRecursive(UndirectedUnweightedgraph1, nodeA, nodeZ); + Assert.assertEquals(path.size(), 0); + } + + @Test + public void breadthFirstSearchOutFinishNode() throws Exception { + List path = GraphSearch.breadthFirstSearch(UndirectedUnweightedgraph1, nodeA, nodeZ); + Assert.assertEquals(path.size(), 0); + } + + @Test + public void depthFirstSearchOutStartNode() throws Exception { + List path = GraphSearch.depthFirstSearch(UndirectedUnweightedgraph1, nodeZ, nodeA); + Assert.assertEquals(path.size(), 0); + } + + @Test + public void depthFirstSearchRecursiveOutStartNode() throws Exception { + List path = GraphSearch.depthFirstSearchRecursive(UndirectedUnweightedgraph1, nodeZ, nodeA); + Assert.assertEquals(path.size(), 0); + } + + @Test + public void breadthFirstSearchOutStartNode() throws Exception { + List path = GraphSearch.breadthFirstSearch(UndirectedUnweightedgraph1, nodeZ, nodeA); + Assert.assertEquals(path.size(), 0); + } + + @Test + public void depthFirstSearchNullGraph() throws Exception { + thrown.expect(IllegalArgumentException.class); + List path = GraphSearch.depthFirstSearch(null, nodeA, nodeZ); + thrown.expectMessage("expect message"); + } + + @Test + public void depthFirstSearchRecursiveNullGraph() throws Exception { + thrown.expect(IllegalArgumentException.class); + List path = GraphSearch.depthFirstSearchRecursive(null, nodeA, nodeZ); + thrown.expectMessage("expect message"); + } + + @Test + public void breadthFirstSearchNullGraph() throws Exception { + thrown.expect(IllegalArgumentException.class); + List path = GraphSearch.breadthFirstSearch(null, nodeA, nodeZ); + thrown.expectMessage("expect message"); + } + + @Test + public void depthFirstSearchEmptyGraph() throws Exception { + thrown.expect(IllegalArgumentException.class); + List path = GraphSearch.depthFirstSearch(new Graph(), nodeA, nodeZ); + thrown.expectMessage("expect message"); + } + + @Test + public void depthFirstSearchRecursiveEmptyGraph() throws Exception { + thrown.expect(IllegalArgumentException.class); + List path = GraphSearch.depthFirstSearchRecursive(new Graph(), nodeA, nodeZ); + thrown.expectMessage("expect message"); + } + + @Test + public void breadthFirstSearchEmptyGraph() throws Exception { + thrown.expect(IllegalArgumentException.class); + List path = GraphSearch.breadthFirstSearch(new Graph(), nodeA, nodeZ); + thrown.expectMessage("expect message"); + } + +} \ No newline at end of file diff --git a/src/test/java/graphs/UndirectedUnweightedGraphTest.java b/src/test/java/graphs/UndirectedUnweightedGraphTest.java new file mode 100644 index 0000000..0823866 --- /dev/null +++ b/src/test/java/graphs/UndirectedUnweightedGraphTest.java @@ -0,0 +1,574 @@ +package graphs; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + +public class UndirectedUnweightedGraphTest { + + private Node nodeA; + private Node nodeB; + private Node nodeC; + private Node nodeD; + private Node nodeE; + private Node nodeF; + private Node nodeG; + private Node nodeH; + private Node nodeZ; + + private Graph UndirectedUnweightedGraph1, + UndirectedUnweightedGraph2, + UndirectedUnweightedGraph3, + UndirectedUnweightedGraph4, + UndirectedUnweightedGraph5, + UndirectedUnweightedGraph6, + UndirectedUnweightedGraph7; + + + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Before + public void setUp() throws Exception { + nodeA = new Node("A"); + nodeB = new Node("B"); + nodeC = new Node("C"); + nodeD = new Node("D"); + nodeE = new Node("E"); + nodeF = new Node("F"); + nodeG = new Node("G"); + nodeH = new Node("H"); + nodeZ = new Node("Z"); + + List nodesForUndirectedUnweightedGraph1 = new LinkedList(Arrays.asList( + nodeA, + nodeB, + nodeC, + nodeD, + nodeE, + nodeF + )); + + Edge edgeBetweenAandB = new Edge(nodeA, nodeB); + Edge edgeBetweenBandC = new Edge(nodeB, nodeC); + Edge edgeBetweenCandD = new Edge(nodeC, nodeD); + Edge edgeBetweenDandE = new Edge(nodeD, nodeE); + Edge edgeBetweenEandF = new Edge(nodeE, nodeF); + + List edgesForUndirectedUnweightedGraph1 = new LinkedList(); + edgesForUndirectedUnweightedGraph1.add(edgeBetweenAandB); + edgesForUndirectedUnweightedGraph1.add(edgeBetweenBandC); + edgesForUndirectedUnweightedGraph1.add(edgeBetweenCandD); + edgesForUndirectedUnweightedGraph1.add(edgeBetweenDandE); + edgesForUndirectedUnweightedGraph1.add(edgeBetweenEandF); + // + // Graph #1: Straight line (unweighted, undirected) + // A -> B -> C -> D -> E -> F + // + UndirectedUnweightedGraph1 = new Graph(nodesForUndirectedUnweightedGraph1, edgesForUndirectedUnweightedGraph1); + + + List nodesForUndirectedUnweightedGraph2 = new LinkedList(Arrays.asList( + nodeA, + nodeB, + nodeC, + nodeD, + nodeE, + nodeF, + nodeG, + nodeH, + nodeZ + )); + + Edge edgeBetweenFandG = new Edge(nodeF, nodeG); + Edge edgeBetweenGandH = new Edge(nodeG, nodeH); + Edge edgeBetweenAandZ = new Edge(nodeA, nodeZ); + Edge edgeBetweenHandZ = new Edge(nodeH, nodeZ); + + List edgesForUndirectedUnweightedGraph2 = new LinkedList(); + edgesForUndirectedUnweightedGraph2.add(edgeBetweenAandB); + edgesForUndirectedUnweightedGraph2.add(edgeBetweenBandC); + edgesForUndirectedUnweightedGraph2.add(edgeBetweenCandD); + edgesForUndirectedUnweightedGraph2.add(edgeBetweenDandE); + edgesForUndirectedUnweightedGraph2.add(edgeBetweenEandF); + edgesForUndirectedUnweightedGraph2.add(edgeBetweenFandG); + edgesForUndirectedUnweightedGraph2.add(edgeBetweenGandH); + edgesForUndirectedUnweightedGraph2.add(edgeBetweenAandZ); + edgesForUndirectedUnweightedGraph2.add(edgeBetweenHandZ); + + // + // Graph #2: Different solutions for DFS and BFS (unweighted, undirected) + // A -> B -> C -> D -> E -> F -> G -> H + // \ / + // \------------> Z -------------/ + // + UndirectedUnweightedGraph2 = new Graph(nodesForUndirectedUnweightedGraph2, edgesForUndirectedUnweightedGraph2); + + + + // graph 2 has all nodes A - H & Z + List nodesForUndirectedUnweightedGraph3 = nodesForUndirectedUnweightedGraph2; + +// Edge edgeBetweenHandZ = new Edge(nodeZ, nodeH); + /* + * make edges, then make edge list: + * graph 3 has edges: + * A - D + * B - D + * B - C ~ + * C - D ~ + * D - F + * D - H + * H - Z ~ + * E - Z + * E - G + */ + + Edge edgeBetweenEandZ = new Edge(nodeE, nodeZ); + Edge edgeBetweenEandG = new Edge(nodeE, nodeG); + Edge edgeBetweenDandH = new Edge(nodeD, nodeH); + Edge edgeBetweenDandF = new Edge(nodeD, nodeF); + Edge edgeBetweenBandD = new Edge(nodeB, nodeD); + Edge edgeBetweenAandD = new Edge(nodeA, nodeD); + + List edgesForUndirectedUnweightedGraph3 = new LinkedList(); + edgesForUndirectedUnweightedGraph3.add(edgeBetweenBandC); + edgesForUndirectedUnweightedGraph3.add(edgeBetweenCandD); + edgesForUndirectedUnweightedGraph3.add(edgeBetweenHandZ); + edgesForUndirectedUnweightedGraph3.add(edgeBetweenEandZ); + edgesForUndirectedUnweightedGraph3.add(edgeBetweenEandG); + edgesForUndirectedUnweightedGraph3.add(edgeBetweenDandH); + edgesForUndirectedUnweightedGraph3.add(edgeBetweenDandF); + edgesForUndirectedUnweightedGraph3.add(edgeBetweenBandD); + edgesForUndirectedUnweightedGraph3.add(edgeBetweenAandD); + + // + // Graph #3: see UndirectedUnweightedGraph3.png + // + UndirectedUnweightedGraph3 = new Graph(nodesForUndirectedUnweightedGraph3, edgesForUndirectedUnweightedGraph3); + + + + // graph 2 has all nodes A - H & Z + List nodesForUndirectedUnweightedGraph4 = nodesForUndirectedUnweightedGraph2; + +// Edge edgeBetweenHandZ = new Edge(nodeZ, nodeH); + /* + * make edges, then make edge list: + * graph 4 has edges: + * a-b ~ + * b-e + * b-h + * e-g ~ + * g-h ~ + * h-z ~ + * h-d ~ + * d-f ~ + * d-c ~ + * c-f + * + */ + Edge edgeBetweenBandE = new Edge(nodeB, nodeE); + Edge edgeBetweenBandH = new Edge(nodeB, nodeH); + Edge edgeBetweenCandF = new Edge(nodeC, nodeF); + + + List edgesForUndirectedUnweightedGraph4 = new LinkedList(); + edgesForUndirectedUnweightedGraph4.add(edgeBetweenCandD); + edgesForUndirectedUnweightedGraph4.add(edgeBetweenHandZ); + edgesForUndirectedUnweightedGraph4.add(edgeBetweenEandG); + edgesForUndirectedUnweightedGraph4.add(edgeBetweenDandH); + edgesForUndirectedUnweightedGraph4.add(edgeBetweenDandF); + edgesForUndirectedUnweightedGraph4.add(edgeBetweenAandB); + edgesForUndirectedUnweightedGraph4.add(edgeBetweenGandH); + edgesForUndirectedUnweightedGraph4.add(edgeBetweenBandE); + edgesForUndirectedUnweightedGraph4.add(edgeBetweenBandH); + edgesForUndirectedUnweightedGraph4.add(edgeBetweenCandF); + + + // + // Graph #4: see UndirectedUnweightedGraph4.png + // + UndirectedUnweightedGraph4 = new Graph(nodesForUndirectedUnweightedGraph4, edgesForUndirectedUnweightedGraph4); + + + // graph 2 has all nodes A - H & Z + List nodesForUndirectedUnweightedGraph5 = nodesForUndirectedUnweightedGraph2; + // + // Graph #5: graph with nodes from graph 2 and edges from graph 1 + // + // start and target have no path, will return empty list + UndirectedUnweightedGraph5 = new Graph(nodesForUndirectedUnweightedGraph5, edgesForUndirectedUnweightedGraph1); + + + + // graph 2 has all nodes A - H & Z + List nodesForUndirectedUnweightedGraph6 = nodesForUndirectedUnweightedGraph2; + /* + * make edges, then make edge list: + * graph 6 nodes A-F, and all edges (15) + * a-b ~ + * a-c + * a-d ~ + * a-e + * a-f + * b-c ~ + * b-d ~ + * b-e ~ + * b-f + * c-d ~ + * c-e + * c-f ~ + * d-e ~ + * d-f ~ + * e-f ~ + * + */ + + Edge edgeBetweenCandE = new Edge(nodeC, nodeE); + Edge edgeBetweenBandF = new Edge(nodeB, nodeF); + Edge edgeBetweenAandF = new Edge(nodeA, nodeF); + Edge edgeBetweenAandE = new Edge(nodeA, nodeE); + Edge edgeBetweenAandC = new Edge(nodeA, nodeC); + + + List edgesForUndirectedUnweightedGraph6 = new LinkedList(edgesForUndirectedUnweightedGraph1); + edgesForUndirectedUnweightedGraph6.add(edgeBetweenCandF); + edgesForUndirectedUnweightedGraph6.add(edgeBetweenAandB); + edgesForUndirectedUnweightedGraph6.add(edgeBetweenAandD); + edgesForUndirectedUnweightedGraph6.add(edgeBetweenBandC); + edgesForUndirectedUnweightedGraph6.add(edgeBetweenBandD); + edgesForUndirectedUnweightedGraph6.add(edgeBetweenBandE); + edgesForUndirectedUnweightedGraph6.add(edgeBetweenCandD); + edgesForUndirectedUnweightedGraph6.add(edgeBetweenCandE); + edgesForUndirectedUnweightedGraph6.add(edgeBetweenDandE); + edgesForUndirectedUnweightedGraph6.add(edgeBetweenDandF); + edgesForUndirectedUnweightedGraph6.add(edgeBetweenEandF); + edgesForUndirectedUnweightedGraph6.add(edgeBetweenBandF); + edgesForUndirectedUnweightedGraph6.add(edgeBetweenAandF); + edgesForUndirectedUnweightedGraph6.add(edgeBetweenAandE); + edgesForUndirectedUnweightedGraph6.add(edgeBetweenAandC); + + // + // Graph #6: graph with nodes from graph 1, fully connected + // + UndirectedUnweightedGraph6 = new Graph(nodesForUndirectedUnweightedGraph6, edgesForUndirectedUnweightedGraph6); + + + + // graph 2 has all nodes A - H & Z + List nodesForUndirectedUnweightedGraph7 = nodesForUndirectedUnweightedGraph2; + // edges needed (17): + /* + * a-b ~ + * a-e ~ + * a-z ~ + * a-g + * b-g + * b-e ~ + * e-g ~ + * g-z + * e-z ~ + * d-e ~ + * h-z ~ + * d-h ~ + * c-d ~ + * d-f ~ + * c-h + * c-f ~ + * f-h + * + */ + + + Edge edgeBetweenCandH = new Edge(nodeC, nodeH); + Edge edgeBetweenBandG = new Edge(nodeB, nodeG); + Edge edgeBetweenAandG = new Edge(nodeA, nodeG); + Edge edgeBetweenGandZ = new Edge(nodeG, nodeZ); + Edge edgeBetweenFandH = new Edge(nodeF, nodeH); + + List edgesForUndirectedUnweightedGraph7 = new LinkedList(); + edgesForUndirectedUnweightedGraph7.add(edgeBetweenAandB); + edgesForUndirectedUnweightedGraph7.add(edgeBetweenAandE); + edgesForUndirectedUnweightedGraph7.add(edgeBetweenAandZ); + edgesForUndirectedUnweightedGraph7.add(edgeBetweenBandE); + edgesForUndirectedUnweightedGraph7.add(edgeBetweenCandD); + edgesForUndirectedUnweightedGraph7.add(edgeBetweenCandF); + edgesForUndirectedUnweightedGraph7.add(edgeBetweenDandE); + edgesForUndirectedUnweightedGraph7.add(edgeBetweenDandF); + edgesForUndirectedUnweightedGraph7.add(edgeBetweenDandH); + edgesForUndirectedUnweightedGraph7.add(edgeBetweenEandG); + edgesForUndirectedUnweightedGraph7.add(edgeBetweenEandZ); + edgesForUndirectedUnweightedGraph7.add(edgeBetweenHandZ); + edgesForUndirectedUnweightedGraph7.add(edgeBetweenCandH); + edgesForUndirectedUnweightedGraph7.add(edgeBetweenBandG); + edgesForUndirectedUnweightedGraph7.add(edgeBetweenAandG); + edgesForUndirectedUnweightedGraph7.add(edgeBetweenGandZ); + edgesForUndirectedUnweightedGraph7.add(edgeBetweenFandH); + // + // Graph #7: graph7.png + // + UndirectedUnweightedGraph7 = new Graph(nodesForUndirectedUnweightedGraph7, edgesForUndirectedUnweightedGraph7); + + + + } + + @Test + public void depthFirstSearchUndirectedUnweightedGraph01() throws Exception { + List path = GraphSearch.depthFirstSearch(UndirectedUnweightedGraph1, nodeA, nodeF); + Assert.assertEquals(path.size(), 6); + Assert.assertTrue(path.get(0).getValue().equals("A")); + Assert.assertTrue(path.get(1).getValue().equals("B")); + Assert.assertTrue(path.get(2).getValue().equals("C")); + Assert.assertTrue(path.get(3).getValue().equals("D")); + Assert.assertTrue(path.get(4).getValue().equals("E")); + Assert.assertTrue(path.get(5).getValue().equals("F")); + } + + @Test + public void depthFirstSearchRecursiveUndirectedUnweightedGraph01() throws Exception { + List path = GraphSearch.depthFirstSearchRecursive(UndirectedUnweightedGraph1, nodeA, nodeF); + Assert.assertEquals(path.size(), 6); + Assert.assertTrue(path.get(0).getValue().equals("A")); + Assert.assertTrue(path.get(1).getValue().equals("B")); + Assert.assertTrue(path.get(2).getValue().equals("C")); + Assert.assertTrue(path.get(3).getValue().equals("D")); + Assert.assertTrue(path.get(4).getValue().equals("E")); + Assert.assertTrue(path.get(5).getValue().equals("F")); + } + + @Test + public void djikstraShortestPathUndirectedUnweightedGraph01() throws Exception { + List path = GraphSearch.djikstraShortestPath(UndirectedUnweightedGraph1, nodeA, nodeF); + Assert.assertEquals(path.size(), 5); + } + + @Test + public void breadthFirstSearchUndirectedUnweightedGraph01() throws Exception { + List path = GraphSearch.breadthFirstSearch(UndirectedUnweightedGraph1, nodeA, nodeF); + Assert.assertEquals(path.size(), 6); + Assert.assertTrue(path.get(0).getValue().equals("A")); + Assert.assertTrue(path.get(1).getValue().equals("B")); + Assert.assertTrue(path.get(2).getValue().equals("C")); + Assert.assertTrue(path.get(3).getValue().equals("D")); + Assert.assertTrue(path.get(4).getValue().equals("E")); + Assert.assertTrue(path.get(5).getValue().equals("F")); + } + + @Test + public void depthFirstSearchUndirectedUnweightedGraph02() throws Exception { + List path = GraphSearch.depthFirstSearch(UndirectedUnweightedGraph2, nodeA, nodeH); + Assert.assertEquals(path.size(), 8); + Assert.assertTrue(path.get(0).getValue().equals("A")); + Assert.assertTrue(path.get(1).getValue().equals("B")); + Assert.assertTrue(path.get(2).getValue().equals("C")); + Assert.assertTrue(path.get(3).getValue().equals("D")); + Assert.assertTrue(path.get(4).getValue().equals("E")); + Assert.assertTrue(path.get(5).getValue().equals("F")); + Assert.assertTrue(path.get(6).getValue().equals("G")); + Assert.assertTrue(path.get(7).getValue().equals("H")); + } + + @Test + public void depthFirstSearchRecursiveUndirectedUnweightedGraph02() throws Exception { + List path = GraphSearch.depthFirstSearchRecursive(UndirectedUnweightedGraph2, nodeA, nodeH); + Assert.assertEquals(path.size(), 8); + Assert.assertTrue(path.get(0).getValue().equals("A")); + Assert.assertTrue(path.get(1).getValue().equals("B")); + Assert.assertTrue(path.get(2).getValue().equals("C")); + Assert.assertTrue(path.get(3).getValue().equals("D")); + Assert.assertTrue(path.get(4).getValue().equals("E")); + Assert.assertTrue(path.get(5).getValue().equals("F")); + Assert.assertTrue(path.get(6).getValue().equals("G")); + Assert.assertTrue(path.get(7).getValue().equals("H")); + } + + @Test + public void breadthFirstSearchUndirectedUnweightedGraph02() throws Exception { + List path = GraphSearch.breadthFirstSearch(UndirectedUnweightedGraph2, nodeA, nodeH); + Assert.assertEquals(path.size(), 3); + Assert.assertTrue(path.get(0).getValue().equals("A")); + Assert.assertTrue(path.get(1).getValue().equals("Z")); + Assert.assertTrue(path.get(2).getValue().equals("H")); + } + + @Test + public void djikstraShortestPathUndirectedUnweightedGraph02() throws Exception { + List path = GraphSearch.djikstraShortestPath(UndirectedUnweightedGraph2, nodeA, nodeH); + Assert.assertEquals(path.size(), 3); + } + + + @Test + public void depthFirstSearchUndirectedUnweightedGraph03() throws Exception { + List path = GraphSearch.depthFirstSearch(UndirectedUnweightedGraph3, nodeB, nodeE); + Assert.assertEquals(path.size(), 6); + Assert.assertTrue(path.get(0).getValue().equals("B")); + Assert.assertTrue(path.get(1).getValue().equals("C")); + Assert.assertTrue(path.get(2).getValue().equals("D")); + Assert.assertTrue(path.get(3).getValue().equals("H")); + Assert.assertTrue(path.get(4).getValue().equals("Z")); + Assert.assertTrue(path.get(5).getValue().equals("E")); + } + + @Test + public void depthFirstSearchRecursiveUndirectedUnweightedGraph03() throws Exception { + List path = GraphSearch.depthFirstSearchRecursive(UndirectedUnweightedGraph3, nodeB, nodeE); + Assert.assertEquals(path.size(), 6); + Assert.assertTrue(path.get(0).getValue().equals("B")); + Assert.assertTrue(path.get(1).getValue().equals("C")); + Assert.assertTrue(path.get(2).getValue().equals("D")); + Assert.assertTrue(path.get(3).getValue().equals("H")); + Assert.assertTrue(path.get(4).getValue().equals("Z")); + Assert.assertTrue(path.get(5).getValue().equals("E")); + } + + @Test + public void breadthFirstSearchUndirectedUnweightedGraph03() throws Exception { + List path = GraphSearch.breadthFirstSearch(UndirectedUnweightedGraph3, nodeB, nodeE); + Assert.assertEquals(path.size(), 5); + Assert.assertTrue(path.get(0).getValue().equals("B")); + Assert.assertTrue(path.get(1).getValue().equals("D")); + Assert.assertTrue(path.get(2).getValue().equals("H")); + Assert.assertTrue(path.get(3).getValue().equals("Z")); + Assert.assertTrue(path.get(4).getValue().equals("E")); + } + + @Test + public void djikstraShortestPathUndirectedUnweightedGraph03() throws Exception { + List path = GraphSearch.djikstraShortestPath(UndirectedUnweightedGraph3, nodeB, nodeE); + Assert.assertEquals(path.size(), 5); + } + + + @Test + public void depthFirstSearchUndirectedUnweightedGraph04() throws Exception { + List path = GraphSearch.depthFirstSearch(UndirectedUnweightedGraph4, nodeB, nodeF); + Assert.assertEquals(path.size(), 7); + Assert.assertTrue(path.get(0).getValue().equals("B")); + Assert.assertTrue(path.get(1).getValue().equals("E")); + Assert.assertTrue(path.get(2).getValue().equals("G")); + Assert.assertTrue(path.get(3).getValue().equals("H")); + Assert.assertTrue(path.get(4).getValue().equals("D")); + Assert.assertTrue(path.get(5).getValue().equals("C")); + Assert.assertTrue(path.get(6).getValue().equals("F")); + } + + @Test + public void depthFirstSearchRecursiveUndirectedUnweightedGraph04() throws Exception { + List path = GraphSearch.depthFirstSearchRecursive(UndirectedUnweightedGraph4, nodeB, nodeF); + Assert.assertEquals(path.size(), 7); + Assert.assertTrue(path.get(0).getValue().equals("B")); + Assert.assertTrue(path.get(1).getValue().equals("E")); + Assert.assertTrue(path.get(2).getValue().equals("G")); + Assert.assertTrue(path.get(3).getValue().equals("H")); + Assert.assertTrue(path.get(4).getValue().equals("D")); + Assert.assertTrue(path.get(5).getValue().equals("C")); + Assert.assertTrue(path.get(6).getValue().equals("F")); + } + + @Test + public void breadthFirstSearchUndirectedUnweightedGraph04() throws Exception { + List path = GraphSearch.breadthFirstSearch(UndirectedUnweightedGraph4, nodeB, nodeF); + Assert.assertEquals(path.size(), 4); + Assert.assertTrue(path.get(0).getValue().equals("B")); + Assert.assertTrue(path.get(1).getValue().equals("H")); + Assert.assertTrue(path.get(2).getValue().equals("D")); + Assert.assertTrue(path.get(3).getValue().equals("F")); + } + + @Test + public void djikstraShortestPathUndirectedUnweightedGraph04() throws Exception { + List path = GraphSearch.djikstraShortestPath(UndirectedUnweightedGraph4, nodeB, nodeF); + Assert.assertEquals(path.size(), 4); + } + + @Test + public void depthFirstSearchUndirectedUnweightedGraph05() throws Exception { + List path = GraphSearch.depthFirstSearch(UndirectedUnweightedGraph5, nodeA, nodeZ); + Assert.assertEquals(path.size(), 0); + } + + @Test + public void depthFirstSearchRecursiveUndirectedUnweightedGraph05() throws Exception { + List path = GraphSearch.depthFirstSearchRecursive(UndirectedUnweightedGraph5, nodeA, nodeZ); + Assert.assertEquals(path.size(), 0); + } + + @Test + public void breadthFirstSearchUndirectedUnweightedGraph05() throws Exception { + List path = GraphSearch.breadthFirstSearch(UndirectedUnweightedGraph5, nodeA, nodeZ); + Assert.assertEquals(path.size(), 0); + } + + @Test + public void djikstraShortestPathUndirectedUnweightedGraph05() throws Exception { + List path = GraphSearch.djikstraShortestPath(UndirectedUnweightedGraph5, nodeA, nodeZ); + Assert.assertEquals(path.size(), 0); + } + + @Test + public void depthFirstSearchUndirectedUnweightedGraph06() throws Exception { + List path = GraphSearch.depthFirstSearch(UndirectedUnweightedGraph6, nodeA, nodeF); + Assert.assertEquals(path.size(), 6); + } + + @Test + public void depthFirstSearchRecursiveUndirectedUnweightedGraph06() throws Exception { + List path = GraphSearch.depthFirstSearchRecursive(UndirectedUnweightedGraph6, nodeA, nodeF); + Assert.assertEquals(path.size(), 6); + } + + @Test + public void djikstraShortestPathUndirectedUnweightedGraph06() throws Exception { + List path = GraphSearch.djikstraShortestPath(UndirectedUnweightedGraph6, nodeA, nodeF); + Assert.assertEquals(path.size(), 2); + } + + @Test + public void breadthFirstSearchUndirectedUnweightedGraph06() throws Exception { + List path = GraphSearch.breadthFirstSearch(UndirectedUnweightedGraph6, nodeA, nodeF); + Assert.assertEquals(path.size(), 2); + Assert.assertTrue(path.get(0).getValue().equals("A")); + Assert.assertTrue(path.get(1).getValue().equals("F")); + } + + @Test + public void depthFirstSearchUndirectedUnweightedGraph07() throws Exception { + List path = GraphSearch.depthFirstSearch(UndirectedUnweightedGraph7, nodeA, nodeF); + Assert.assertEquals(path.size(), 6); + } + + @Test + public void depthFirstSearchRecursiveUndirectedUnweightedGraph07() throws Exception { + List path = GraphSearch.depthFirstSearchRecursive(UndirectedUnweightedGraph7, nodeA, nodeF); + Assert.assertEquals(path.size(), 6); + } + + @Test + public void breadthFirstSearchUndirectedUnweightedGraph07() throws Exception { + List path = GraphSearch.breadthFirstSearch(UndirectedUnweightedGraph7, nodeA, nodeF); + Assert.assertEquals(path.size(), 4); + Assert.assertTrue(path.get(0).getValue().equals("A")); + Assert.assertTrue(path.get(1).getValue().equals("E")); + Assert.assertTrue(path.get(2).getValue().equals("D")); + Assert.assertTrue(path.get(3).getValue().equals("F")); + } + + @Test + public void djikstraShortestPathUndirectedUnweightedGraph07() throws Exception { + List path = GraphSearch.djikstraShortestPath(UndirectedUnweightedGraph7, nodeA, nodeF); + Assert.assertEquals(path.size(), 4); + } + + + +} \ No newline at end of file diff --git a/src/test/java/graphs/UndirectedWeightedGraphTest.java b/src/test/java/graphs/UndirectedWeightedGraphTest.java new file mode 100644 index 0000000..22d4e76 --- /dev/null +++ b/src/test/java/graphs/UndirectedWeightedGraphTest.java @@ -0,0 +1,354 @@ +package graphs; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + +public class UndirectedWeightedGraphTest { + + private Node nodeA; + private Node nodeB; + private Node nodeC; + private Node nodeD; + private Node nodeE; + private Node nodeF; + private Node nodeG; + private Node nodeH; + private Node nodeZ; + + private Graph UndirectedWeightedGraph11, + UndirectedWeightedGraph12, + UndirectedWeightedGraph13; + + + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Before + public void setUp() throws Exception { + nodeA = new Node("A"); + nodeB = new Node("B"); + nodeC = new Node("C"); + nodeD = new Node("D"); + nodeE = new Node("E"); + nodeF = new Node("F"); + nodeG = new Node("G"); + nodeH = new Node("H"); + nodeZ = new Node("Z"); + + Edge edgeBetweenAandB = new Edge(nodeA, nodeB); + Edge edgeBetweenBandC = new Edge(nodeB, nodeC); + + List nodesForUndirectedUnweightedGraph2 = new LinkedList(Arrays.asList( + nodeA, + nodeB, + nodeC, + nodeD, + nodeE, + nodeF, + nodeG, + nodeH, + nodeZ + )); + Edge edgeBetweenHandZ = new Edge(nodeH, nodeZ); + Edge edgeBetweenEandZ = new Edge(nodeE, nodeZ); + Edge edgeBetweenDandF = new Edge(nodeD, nodeF); + Edge edgeBetweenBandD = new Edge(nodeB, nodeD); + Edge edgeBetweenAandD = new Edge(nodeA, nodeD); + Edge edgeBetweenBandH = new Edge(nodeB, nodeH); + Edge edgeBetweenCandE = new Edge(nodeC, nodeE); + Edge edgeBetweenBandF = new Edge(nodeB, nodeF); + Edge edgeBetweenAandC = new Edge(nodeA, nodeC); + Edge edgeBetweenFandH = new Edge(nodeF, nodeH); + Edge edgeBetweenGandD = new Edge(nodeG,nodeD); + Edge edgeBetweenZandG = new Edge(nodeZ,nodeG); + Edge edgeBetweenZandD = new Edge(nodeZ,nodeD); + Edge edgeBetweenFandB = new Edge(nodeF,nodeB); + Edge edgeBetweenDandC = new Edge(nodeD,nodeC); + Edge edgeBetweenEandD = new Edge(nodeE,nodeD); + Edge edgeBetweenGandA = new Edge(nodeG, nodeA); + Edge edgeBetweenFandA = new Edge(nodeF, nodeA); + Edge edgeBetweenZandC = new Edge(nodeZ, nodeC); + Edge edgeBetweenGandE = new Edge(nodeG, nodeE); + Edge edgeBetweenEandB = new Edge(nodeE, nodeB); + Edge edgeBetweenDandZ = new Edge(nodeD, nodeZ); + + + /* + * graph 8 but weighted + * 12 edges + * a-d + * b-d + * g-d + * z-g + * z-d 2, rest 1 + * h-z + * b-h + * f-b + * c-e + * d-c + * e-d + * d-f + * + * + */ + List edgesForGraph11 = new LinkedList(); + edgesForGraph11.add(edgeBetweenAandD.setWeight(1)); + edgesForGraph11.add(edgeBetweenZandD.setWeight(2)); + edgesForGraph11.add(edgeBetweenBandD.setWeight(1)); + edgesForGraph11.add(edgeBetweenHandZ.setWeight(1)); + edgesForGraph11.add(edgeBetweenBandH.setWeight(1)); + edgesForGraph11.add(edgeBetweenCandE.setWeight(1)); + edgesForGraph11.add(edgeBetweenGandD.setWeight(1)); + edgesForGraph11.add(edgeBetweenZandG.setWeight(1)); + edgesForGraph11.add(edgeBetweenFandB.setWeight(1)); + edgesForGraph11.add(edgeBetweenDandC.setWeight(1)); + edgesForGraph11.add(edgeBetweenEandD.setWeight(1)); + edgesForGraph11.add(edgeBetweenDandF.setWeight(1)); + UndirectedWeightedGraph11 = new Graph(nodesForUndirectedUnweightedGraph2, edgesForGraph11, true); + UndirectedWeightedGraph11.setWeighted(true); + + // + // Graph #11: directed and weighted + // + + /* + * graph 9 but weighted + * 12 edges + * a-b + * b-c + * c-e + * e-d + * g-d + * g-a + * z-g + * h-z + * f-h + * d-f + * e-z + * f-a + * + */ + List edgesForGraph12 = new LinkedList(); + edgesForGraph12.add(edgeBetweenAandB.setWeight(1)); + edgesForGraph12.add(edgeBetweenBandC.setWeight(1)); + edgesForGraph12.add(edgeBetweenCandE.setWeight(1)); + edgesForGraph12.add(edgeBetweenEandZ.setWeight(1)); + edgesForGraph12.add(edgeBetweenEandD.setWeight(2)); + edgesForGraph12.add(edgeBetweenGandD.setWeight(2)); + edgesForGraph12.add(edgeBetweenZandG.setWeight(1)); + edgesForGraph12.add(edgeBetweenHandZ.setWeight(1)); + edgesForGraph12.add(edgeBetweenFandH.setWeight(1)); + edgesForGraph12.add(edgeBetweenDandF.setWeight(1)); + edgesForGraph12.add(edgeBetweenGandA.setWeight(1)); + edgesForGraph12.add(edgeBetweenFandA.setWeight(1)); + UndirectedWeightedGraph12 = new Graph(nodesForUndirectedUnweightedGraph2, edgesForGraph12, true); + UndirectedWeightedGraph12.setWeighted(true); + + // + // Graph #12: directed and weighted + // + + + /* + * graph 10 but weighted + * 10 edges + * a-d + * a-c + * d-c + * z-c + * g-e + * e-b + * b-f + * f-h + * d-z + * g-a + * + */ + + List edgesForGraph13 = new LinkedList(); + edgesForGraph13.add(edgeBetweenAandD.setWeight(1)); + edgesForGraph13.add(edgeBetweenAandC.setWeight(1)); + edgesForGraph13.add(edgeBetweenDandC.setWeight(1)); + edgesForGraph13.add(edgeBetweenBandF.setWeight(1)); + edgesForGraph13.add(edgeBetweenFandH.setWeight(1)); + edgesForGraph13.add(edgeBetweenZandC.setWeight(1)); + edgesForGraph13.add(edgeBetweenGandE.setWeight(2)); + edgesForGraph13.add(edgeBetweenEandB.setWeight(1)); + edgesForGraph13.add(edgeBetweenDandZ.setWeight(1)); + edgesForGraph13.add(edgeBetweenGandA.setWeight(1)); + + + UndirectedWeightedGraph13 = new Graph(nodesForUndirectedUnweightedGraph2, edgesForGraph13, true); + UndirectedWeightedGraph13.setWeighted(true); + + // + // Graph #13: directed and weighted + // + + + } + + @Test + public void depthFirstSearchDirectedWeightedGraph11() throws Exception { + List path = GraphSearch.depthFirstSearch(UndirectedWeightedGraph11, nodeH, nodeB); + Assert.assertEquals(path.size(), 6); + Assert.assertTrue(path.get(0).getValue().equals("H")); + Assert.assertTrue(path.get(1).getValue().equals("Z")); + Assert.assertTrue(path.get(2).getValue().equals("G")); + Assert.assertTrue(path.get(3).getValue().equals("D")); + Assert.assertTrue(path.get(4).getValue().equals("F")); + Assert.assertTrue(path.get(5).getValue().equals("B")); + } + + @Test + public void depthFirstSearchRecursiveDirectedWeightedGraph11() throws Exception { + List path = GraphSearch.depthFirstSearchRecursive(UndirectedWeightedGraph11, nodeH, nodeB); + Assert.assertEquals(path.size(), 6); + Assert.assertTrue(path.get(0).getValue().equals("H")); + Assert.assertTrue(path.get(1).getValue().equals("Z")); + Assert.assertTrue(path.get(2).getValue().equals("G")); + Assert.assertTrue(path.get(3).getValue().equals("D")); + Assert.assertTrue(path.get(4).getValue().equals("F")); + Assert.assertTrue(path.get(5).getValue().equals("B")); + } + + @Test + public void breadthFirstSearchDirectedWeightedGraph11() throws Exception { + List path = GraphSearch.breadthFirstSearch(UndirectedWeightedGraph11, nodeH, nodeB); + Assert.assertEquals(path.size(), 5); + Assert.assertTrue(path.get(0).getValue().equals("H")); + Assert.assertTrue(path.get(1).getValue().equals("Z")); + Assert.assertTrue(path.get(2).getValue().equals("D")); + Assert.assertTrue(path.get(3).getValue().equals("F")); + Assert.assertTrue(path.get(4).getValue().equals("B")); + } + + @Test + public void djikstraShortestPathUndirectedUnweightedGraph11() throws Exception { + List path = GraphSearch.djikstraShortestPath(UndirectedWeightedGraph11, nodeH, nodeB); + Assert.assertEquals(path.size(), 5); + Assert.assertTrue(path.get(0).getValue().equals("H")); + Assert.assertTrue(path.get(1).getValue().equals("Z")); + Assert.assertTrue(path.get(2).getValue().equals("D")); + Assert.assertTrue(path.get(3).getValue().equals("F")); + Assert.assertTrue(path.get(4).getValue().equals("B")); + } + + @Test + public void depthFirstSearchDirectedWeightedGraph12() throws Exception { + List path = GraphSearch.depthFirstSearch(UndirectedWeightedGraph12, nodeE, nodeF); + Assert.assertEquals(path.size(), 5); + Assert.assertTrue(path.get(0).getValue().equals("E")); + Assert.assertTrue(path.get(1).getValue().equals("Z")); + Assert.assertTrue(path.get(2).getValue().equals("G")); + Assert.assertTrue(path.get(3).getValue().equals("D")); + Assert.assertTrue(path.get(4).getValue().equals("F")); + } + + @Test + public void depthFirstSearchRecursiveDirectedWeightedGraph12() throws Exception { + List path = GraphSearch.depthFirstSearchRecursive(UndirectedWeightedGraph12, nodeE, nodeF); + Assert.assertEquals(path.size(), 5); + Assert.assertTrue(path.get(0).getValue().equals("E")); + Assert.assertTrue(path.get(1).getValue().equals("Z")); + Assert.assertTrue(path.get(2).getValue().equals("G")); + Assert.assertTrue(path.get(3).getValue().equals("D")); + Assert.assertTrue(path.get(4).getValue().equals("F")); + } + + @Test + public void breadthFirstSearchDirectedWeightedGraph12() throws Exception { + List path = GraphSearch.breadthFirstSearch(UndirectedWeightedGraph12, nodeE, nodeF); + Assert.assertEquals(path.size(), 3); + Assert.assertTrue(path.get(0).getValue().equals("E")); + Assert.assertTrue(path.get(1).getValue().equals("D")); + Assert.assertTrue(path.get(2).getValue().equals("F")); + } + + @Test + public void djikstraShortestPathUndirectedUnweightedGraph12() throws Exception { + List path = GraphSearch.djikstraShortestPath(UndirectedWeightedGraph12, nodeE, nodeF); + Assert.assertEquals(path.size(), 3); + Assert.assertTrue(path.get(0).getValue().equals("E")); + Assert.assertTrue(path.get(1).getValue().equals("D")); + Assert.assertTrue(path.get(2).getValue().equals("F")); + } + + @Test + public void depthFirstSearchDirectedWeightedGraph13() throws Exception { + List path = GraphSearch.depthFirstSearch(UndirectedWeightedGraph13, nodeG, nodeH); + Assert.assertEquals(path.size(), 5); + Assert.assertTrue(path.get(0).getValue().equals("G")); + Assert.assertTrue(path.get(1).getValue().equals("E")); + Assert.assertTrue(path.get(2).getValue().equals("B")); + Assert.assertTrue(path.get(3).getValue().equals("F")); + Assert.assertTrue(path.get(4).getValue().equals("H")); + } + + @Test + public void depthFirstSearchRecursiveDirectedWeightedGraph13() throws Exception { + List path = GraphSearch.depthFirstSearchRecursive(UndirectedWeightedGraph13, nodeG, nodeH); + Assert.assertEquals(path.size(), 5); + Assert.assertTrue(path.get(0).getValue().equals("G")); + Assert.assertTrue(path.get(1).getValue().equals("E")); + Assert.assertTrue(path.get(2).getValue().equals("B")); + Assert.assertTrue(path.get(3).getValue().equals("F")); + Assert.assertTrue(path.get(4).getValue().equals("H")); + } + + @Test + public void djikstraShortestPathUndirectedUnweightedGraph13() throws Exception { + List path = GraphSearch.djikstraShortestPath(UndirectedWeightedGraph13, nodeG, nodeH); + Assert.assertEquals(path.size(), 5); + Assert.assertTrue(path.get(0).getValue().equals("G")); + Assert.assertTrue(path.get(1).getValue().equals("E")); + Assert.assertTrue(path.get(2).getValue().equals("B")); + Assert.assertTrue(path.get(3).getValue().equals("F")); + Assert.assertTrue(path.get(4).getValue().equals("H")); + } + + @Test + public void breadthFirstSearchDirectedWeightedGraph13() throws Exception { + List path = GraphSearch.breadthFirstSearch(UndirectedWeightedGraph13, nodeG, nodeH); + Assert.assertEquals(path.size(), 5); + Assert.assertTrue(path.get(0).getValue().equals("G")); + Assert.assertTrue(path.get(1).getValue().equals("E")); + Assert.assertTrue(path.get(2).getValue().equals("B")); + Assert.assertTrue(path.get(3).getValue().equals("F")); + Assert.assertTrue(path.get(4).getValue().equals("H")); + } + + // same as graph 13, except start at e and look for c, un connected, will need to restart + @Test + public void depthFirstSearchDirectedWeightedGraph14() throws Exception { + List path = GraphSearch.depthFirstSearch(UndirectedWeightedGraph13, nodeE, nodeC); + Assert.assertEquals(path.size(), 0); + } + + @Test + public void depthFirstSearchRecursiveDirectedWeightedGraph14() throws Exception { + List path = GraphSearch.depthFirstSearchRecursive(UndirectedWeightedGraph13, nodeE, nodeC); + Assert.assertEquals(path.size(), 0); + } + + @Test + public void breadthFirstSearchDirectedWeightedGraph14() throws Exception { + List path = GraphSearch.breadthFirstSearch(UndirectedWeightedGraph13, nodeE, nodeC); + Assert.assertEquals(path.size(), 0); + } + + @Test + public void djikstraShortestPathUndirectedUnweightedGraph14() throws Exception { + List path = GraphSearch.djikstraShortestPath(UndirectedWeightedGraph13, nodeE, nodeC); + Assert.assertEquals(path.size(), 0); + } + +} \ No newline at end of file diff --git a/src/test/java/graphs/graph5.png b/src/test/java/graphs/graph5.png new file mode 100644 index 0000000..e77b4f9 Binary files /dev/null and b/src/test/java/graphs/graph5.png differ diff --git a/src/test/java/graphs/graph6.png b/src/test/java/graphs/graph6.png new file mode 100644 index 0000000..49fe6ae Binary files /dev/null and b/src/test/java/graphs/graph6.png differ diff --git a/src/test/java/graphs/graph7.png b/src/test/java/graphs/graph7.png new file mode 100644 index 0000000..388d870 Binary files /dev/null and b/src/test/java/graphs/graph7.png differ