summaryrefslogtreecommitdiff
path: root/src/com.example.portfolio3/com/example/portfolio3/EdgeGraph.java
blob: 8bd925934b5960bd3f97944b6e003fdf2bd04bf2 (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. return outEdge;
  28. }
  29. /// foo
  30. public Integer getWeight(final Vertex v1, final Vertex v2) {
  31. // linear in number of edges in the graph
  32. for (Edge e:edges) {
  33. if (e.from() == v1 && e.to() == v2)
  34. return e.weight();
  35. }
  36. return null;
  37. }
  38. }