blob: 2f3b036c4a8b71bdb9df234d629040079d5a9a3b (
plain)
- package com.example.portfolio3;
- // origin: <https://moodle.ruc.dk/course/section.php?id=211877>
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.HashSet;
- import java.util.Set;
- /// EdgeGraph - One big set of all edges in the graph
- class EdgeGraph extends AbstractGraph {
- /// foo
- EdgeGraph() { }
- /// foo
- Set<Edge> edges = new HashSet<>();
- /// foo
- public void insertEdge(final Vertex v1, final Vertex v2, final int w) {
- edges.add(new Edge(v1, v2, w));
- }
- /// foo
- public Collection<Edge> edges() {
- return edges;
- }
- /// foo
- public Collection<Edge> outEdge(final Vertex v) {
- ArrayList<Edge> outEdge = new ArrayList<>();
- for (Edge e: edges) {
- if (e.from() == v) {
- outEdge.add(e);
- }
- }
- return outEdge;
- }
- /// foo
- public Integer getWeight(final Vertex v1, final Vertex v2) {
- // linear in number of edges in the graph
- for (Edge e: edges) {
- if (e.from() == v1 && e.to() == v2) {
- return e.weight();
- }
- }
- return null;
- }
- }
|