Question
//Need to Find the strongly connected component and the shortest path 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;
//Need to Find the strongly connected component and the shortest path
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;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started