aboutsummaryrefslogtreecommitdiff
path: root/src/com.example.portfolio3/com/example/portfolio3/AdjMapGraph.java
blob: 8c2c1395772c792d663aacab4f2d672dcf5ec4f4 (plain)
  1. package com.example.portfolio3;
  2. // origin: <https://moodle.ruc.dk/course/section.php?id=211877>
  3. import java.util.Collection;
  4. import java.util.HashMap;
  5. import java.util.HashSet;
  6. import java.util.Map;
  7. import java.util.Set;
  8. /// Adjecency Map Graph - A map from vertices to map of target vertex to edge
  9. class AdjMapGraph extends AbstractGraph {
  10. /// foo
  11. AdjMapGraph() { }
  12. /// foo
  13. private Map<Vertex, Map<Vertex, Edge>> outEdge = new HashMap<>();
  14. /// foo
  15. public void insertEdge(final Vertex v1, final Vertex v2, final int w) {
  16. Edge e = new Edge(v1, v2, w);
  17. if (!outEdge.containsKey(e.from())) {
  18. outEdge.put(e.from(), new HashMap<Vertex, Edge>());
  19. }
  20. outEdge.get(e.from()).put(e.to(), e);
  21. }
  22. /// foo
  23. public Collection<Edge> edges() {
  24. Set<Edge> edges = new HashSet<>();
  25. for (Vertex v : outEdge.keySet()) {
  26. for (Vertex w : outEdge.get(v).keySet()) {
  27. edges.add(outEdge.get(v).get(w));
  28. }
  29. }
  30. return edges;
  31. }
  32. /// foo
  33. public Collection<Edge> outEdge(final Vertex v) {
  34. return outEdge.get(v).values();
  35. }
  36. /// foo
  37. public Integer getWeight(final Vertex v1, final Vertex v2) {
  38. // constant time operation
  39. if (!outEdge.containsKey(v1)) {
  40. return null;
  41. }
  42. if (!outEdge.get(v1).containsKey(v2)) {
  43. return null;
  44. }
  45. return outEdge.get(v1).get(v2).weight();
  46. }
  47. }