aboutsummaryrefslogtreecommitdiff
path: root/src/com.example.portfolio3/com/example/portfolio3/EdgeGraph.java
blob: 2f3b036c4a8b71bdb9df234d629040079d5a9a3b (plain)
  1. package com.example.portfolio3;
  2. // origin: <https://moodle.ruc.dk/course/section.php?id=211877>
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.HashSet;
  6. import java.util.Set;
  7. /// EdgeGraph - One big set of all edges in the graph
  8. class EdgeGraph extends AbstractGraph {
  9. /// foo
  10. EdgeGraph() { }
  11. /// foo
  12. Set<Edge> edges = new HashSet<>();
  13. /// foo
  14. public void insertEdge(final Vertex v1, final Vertex v2, final int w) {
  15. edges.add(new Edge(v1, v2, w));
  16. }
  17. /// foo
  18. public Collection<Edge> edges() {
  19. return edges;
  20. }
  21. /// foo
  22. public Collection<Edge> outEdge(final Vertex v) {
  23. ArrayList<Edge> outEdge = new ArrayList<>();
  24. for (Edge e: edges) {
  25. if (e.from() == v) {
  26. outEdge.add(e);
  27. }
  28. }
  29. return outEdge;
  30. }
  31. /// foo
  32. public Integer getWeight(final Vertex v1, final Vertex v2) {
  33. // linear in number of edges in the graph
  34. for (Edge e: edges) {
  35. if (e.from() == v1 && e.to() == v2) {
  36. return e.weight();
  37. }
  38. }
  39. return null;
  40. }
  41. }