Don't change the code or add, just complete the methods Don't copy any of the answers posted here because they are wrong
Posted: Mon Jun 06, 2022 6:15 pm
Don't change the code or add, just complete the
methods
Don't copy any of the answers posted here because they
are wrong and will result in a dislike
Thank you.
- Undirected Graph implementation in Java
- Task 4 to 7-1
- Show test cases, if possible
.
.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
public class UndirectedGraph<T extends
Comparable<T>> extends Graph<T> {
private Map<T, Set<T>>
graph;
// the following class variable will be used
by
// UndiretedGraphAlgorithms class
private Map<T, Boolean> visited;
public UndirectedGraph() {
this.graph = new
HashMap<>();
this.visited = new
HashMap<>();
}
public boolean isEmpty() {
// TASK 1: Complete this
method
// Hint: An empty graph
contains zero vertices
return graph.isEmpty();
}
public int getSize() {
// TASK 2: compute the size
(number of vertices)
return graph.size();
}
public void addVertex(T vertex) throws
VertexExistsException {
// TASK 3: Complete this
method
// Hint: A new vertex is by
default not visited.
graph.put(vertex, new
HashSet<>());
}
public List<T> getAdjacent(T vertex)
{
return new
ArrayList<>(graph.get(vertex));
}
public void addEdge(T fromVertex, T toVertex)
{
// TASK 4: Complete this
method
// Hint: Recall, both
vertices already exist. Also,
// our
graphs are not oriented, hence both edges
// need
to be added.
}
@Override
public void reset() {
// TASK 5: This method
resets values
// of visited map to
false
}
@Override
public String toString() {
// TASK 6: Override
toString() method
}
@Override
public List<T> getVertices() {
return
graph.keySet().stream()
.collect(Collectors.toList());
}
@Override
public List<T> getVisited() {
// TASK 7-0: code this
method
}
@Override
public void setVisited(T vertex) {
// TASK 7-1: code this
method
// you may assume vertex T
exists in the graph
}
}
methods
Don't copy any of the answers posted here because they
are wrong and will result in a dislike
Thank you.
- Undirected Graph implementation in Java
- Task 4 to 7-1
- Show test cases, if possible
.
.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
public class UndirectedGraph<T extends
Comparable<T>> extends Graph<T> {
private Map<T, Set<T>>
graph;
// the following class variable will be used
by
// UndiretedGraphAlgorithms class
private Map<T, Boolean> visited;
public UndirectedGraph() {
this.graph = new
HashMap<>();
this.visited = new
HashMap<>();
}
public boolean isEmpty() {
// TASK 1: Complete this
method
// Hint: An empty graph
contains zero vertices
return graph.isEmpty();
}
public int getSize() {
// TASK 2: compute the size
(number of vertices)
return graph.size();
}
public void addVertex(T vertex) throws
VertexExistsException {
// TASK 3: Complete this
method
// Hint: A new vertex is by
default not visited.
graph.put(vertex, new
HashSet<>());
}
public List<T> getAdjacent(T vertex)
{
return new
ArrayList<>(graph.get(vertex));
}
public void addEdge(T fromVertex, T toVertex)
{
// TASK 4: Complete this
method
// Hint: Recall, both
vertices already exist. Also,
// our
graphs are not oriented, hence both edges
// need
to be added.
}
@Override
public void reset() {
// TASK 5: This method
resets values
// of visited map to
false
}
@Override
public String toString() {
// TASK 6: Override
toString() method
}
@Override
public List<T> getVertices() {
return
graph.keySet().stream()
.collect(Collectors.toList());
}
@Override
public List<T> getVisited() {
// TASK 7-0: code this
method
}
@Override
public void setVisited(T vertex) {
// TASK 7-1: code this
method
// you may assume vertex T
exists in the graph
}
}