Question: //NEED TO DEAL WITH stronglyConnectedComponent method package graphs; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; import java.util.Set; public
//NEED TO DEAL WITH stronglyConnectedComponent method
package graphs;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
public class AdjacencyListGraph
Map
private class Vertex {
T key;
List
List
Vertex(T key) {
this.key = key;
this.successors = new ArrayList
this.predecessors = new ArrayList
}
}
AdjacencyListGraph(Set
this.keyToVertex = new HashMap
for (T key : keys) {
Vertex v = new Vertex(key);
this.keyToVertex.put(key, v);
}
}
@Override
public int size() {
return this.keyToVertex.size();
}
@Override
public int numEdges() {
int count = 0;
for (Vertex v : this.keyToVertex.values()) {
count += v.successors.size();
}
return count;
}
@Override
public boolean addEdge(T from, T to) {
if (!this.keyToVertex.containsKey(from)) {
throw new NoSuchElementException("Could not find vertex with key " + from.toString());
}
if (!this.keyToVertex.containsKey(to)) {
throw new NoSuchElementException("Could not find vertex with key " + to.toString());
}
Vertex fromV = this.keyToVertex.get(from);
Vertex toV = this.keyToVertex.get(to);
List
List
if (fromSuccs.contains(toV)) {
return false;
}
fromSuccs.add(toV);
toPreds.add(fromV);
return true;
}
@Override
public boolean hasVertex(T key) {
return this.keyToVertex.containsKey(key);
}
@Override
public boolean hasEdge(T from, T to) throws NoSuchElementException {
if (!this.keyToVertex.containsKey(from)) {
throw new NoSuchElementException("Could not find vertex with key " + from.toString());
}
if (!this.keyToVertex.containsKey(to)) {
throw new NoSuchElementException("Could not find vertex with key " + to.toString());
}
Vertex fromV = this.keyToVertex.get(from);
Vertex toV = this.keyToVertex.get(to);
List
List
if (fromSuccs.contains(toV)) {
return true;
}
return false;
}
@Override
public boolean removeEdge(T from, T to) throws NoSuchElementException {
if (!this.keyToVertex.containsKey(from)) {
throw new NoSuchElementException("Could not find vertex with key " + from.toString());
}
if (!this.keyToVertex.containsKey(to)) {
throw new NoSuchElementException("Could not find vertex with key " + to.toString());
}
Vertex fromV = this.keyToVertex.get(from);
Vertex toV = this.keyToVertex.get(to);
if (!this.hasEdge(from, to)) {
return false;
}
List
List
fromSuccs.remove(toV);
toPreds.remove(fromV);
if (!fromSuccs.contains(toV)) {
return true;
}
return false;
}
@Override
public int outDegree(T key) {
return this.keyToVertex.get(key).successors.size();
}
@Override
public int inDegree(T key) {
return this.keyToVertex.get(key).predecessors.size();
}
@Override
public Set
Set
for (T v : this.keyToVertex.keySet()) {
vertexSet.add(v);
}
return vertexSet;
}
@Override
public List
List
for (Vertex v : this.keyToVertex.get(key).successors) {
successorList.add(v.key);
}
return successorList;
}
@Override
public List
List
for (Vertex v : this.keyToVertex.get(key).predecessors) {
predecessorList.add(v.key);
}
return predecessorList;
}
@Override
public Iterator
return new theIterator(this.successorList(key));
}
@Override
public Iterator
return new theIterator(this.predecessorList(key));
}
private class theIterator implements Iterator
List
int index = 0;
public theIterator(List
this.target = theList;
}
@Override
public boolean hasNext() {
return this.index < this.target.size();
}
@Override
public T next() {
if (hasNext()) {
this.index += 1;
return this.target.get(this.index - 1);
}
return null;
}
}
@Override
public Set
this.keyToVertex.get(key);
//TODO
return null;
}
@Override
public List
// TODO
return null;
}
}
//TEST Cases
package graphs;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue;
import java.io.File; import java.io.FileNotFoundException; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Scanner; import java.util.Set;
import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test;
/** * Complete test cases for Milestone 2 * * * @author Nate Chenette * */ public class GraphSurfingMilestone2Test {
// Toggle this to false to speed up early testing process. private static boolean runLivingPeopleGraphTests = true;
// Toggle this to false to suppress output. private static boolean verbose = true;
private static int m2points = 0; private static int m2bonusPoints = 0; private static int m2weight = 1; private static final int MAX_POINTS = 60; private static Graph
@BeforeClass public static void buildLivingPeopleGraph() { if (runLivingPeopleGraphTests) { livingPeopleALGraph = WikiSurfing.wikiLivingPeopleGraphAL(verbose); } }
private Set
private Set
private void addExampleEdges(Graph
private void addExample2Edges(Graph
public Graph
public Graph
public Graph
public Graph
private void helperTestStronglyConnectedComponentExampleGraphs(Graph
Set
@Test public void testALStronglyConnectedComponentExampleGraphs() { helperTestStronglyConnectedComponentExampleGraphs(makeExampleALGraph(), makeExample2ALGraph()); }
@Test public void testAMStronglyConnectedComponentExampleGraphs() { helperTestStronglyConnectedComponentExampleGraphs(makeExampleAMGraph(), makeExample2AMGraph()); }
private void helperTestShortestPathExampleGraphs(Graph
assertEquals(Arrays.asList(0,1), g2.shortestPath(0,1)); assertEquals(Arrays.asList(0,2,4,6), g2.shortestPath(0,6)); assertEquals(Arrays.asList(6,2,3), g2.shortestPath(6,3)); assertEquals(Arrays.asList(4,6,2,3), g2.shortestPath(4,3)); assertEquals(Arrays.asList(1,0,2,4,5), g2.shortestPath(1,5)); m2points += 8*m2weight;
assertEquals(null, g.shortestPath("b","a")); assertEquals(null, g.shortestPath("f","b")); assertEquals(null, g2.shortestPath(2,0)); assertEquals(null, g2.shortestPath(4,1)); m2points += 2*m2weight;
}
@Test public void testALShortestPathExampleGraphs() { helperTestShortestPathExampleGraphs(makeExampleALGraph(), makeExample2ALGraph()); }
@Test public void testAMShortestPathExampleGraphs() { helperTestShortestPathExampleGraphs(makeExampleAMGraph(), makeExample2AMGraph()); }
@Test public void testWikiSurfingShortestPath() { assertTrue(runLivingPeopleGraphTests); List
@Test public void testWikiSurfingStronglyConnectedComponent() { assertTrue(runLivingPeopleGraphTests); List
private class ChallengeAndResult { String from; String to; int solutionLength; ChallengeAndResult(String a, String b, int len) { this.from = a; this.to = b; this.solutionLength = len; } }
@Test public void testWikiSurfingFindChallengePair() { assertTrue(runLivingPeopleGraphTests); Scanner sc = null; String from = ""; String to = ""; String pairFileName = "challenge-pair.txt"; try { sc = new Scanner(new File(pairFileName)); } catch (FileNotFoundException e) { System.err.printf("Could not find file %s%n",pairFileName); } if (sc.hasNextLine()) { from = sc.nextLine(); } if (sc.hasNextLine()) { to = sc.nextLine(); } List
// @Test // public void testWikiSurfingFindChallengePairAlgorithm() { // assertTrue(runLivingPeopleGraphTests); // Pair
@AfterClass public static void printSummary() { if (livingPeopleShortestPathWorking) { m2points += m2bonusPoints; } System.out.print(" =============== "); System.out.print("Total points: "); System.out.print(m2points + "/" + MAX_POINTS); System.out.println(" ==============="); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
