aboutsummaryrefslogtreecommitdiff
path: root/src/com.example.portfolio3/com/example/portfolio3/EdgeGraph.java
blob: 4fe393075ea025681c106ccf56ac098949ac8776 (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(
  15. final Vertex v1, final Vertex v2, final int w
  16. ) {
  17. edges.add(new Edge(v1, v2, w));
  18. }
  19. /// foo
  20. public Collection<Edge> edges() {
  21. return edges;
  22. }
  23. /// foo
  24. public Collection<Edge> outEdge(final Vertex v) {
  25. ArrayList<Edge> outEdge = new ArrayList<>();
  26. for (Edge e: edges) {
  27. if (e.from() == v) {
  28. outEdge.add(e);
  29. }
  30. }
  31. return outEdge;
  32. }
  33. /// foo
  34. public Integer getWeight(final Vertex v1, final Vertex v2) {
  35. // linear in number of edges in the graph
  36. for (Edge e: edges) {
  37. if (e.from() == v1 && e.to() == v2) {
  38. return e.weight();
  39. }
  40. }
  41. return null;
  42. }
  43. }