aboutsummaryrefslogtreecommitdiff
path: root/src/com.example.portfolio3/com/example/portfolio3/AdjMapGraph.java
blob: f12df6f882fc3fc5599f5b69afd757e482858b76 (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(Vertex v1, Vertex v2, 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. outEdge.get(e.from()).put(e.to(), e);
  20. }
  21. /// foo
  22. public Collection<Edge> edges() {
  23. Set<Edge> edges = new HashSet<>();
  24. for (Vertex v : outEdge.keySet())
  25. for (Vertex w : outEdge.get(v).keySet())
  26. edges.add(outEdge.get(v).get(w));
  27. return edges;
  28. }
  29. /// foo
  30. public Collection<Edge> outEdge(Vertex v) {
  31. return outEdge.get(v).values();
  32. }
  33. /// foo
  34. public Integer getWeight(Vertex v1, Vertex v2) {
  35. // constant time operation
  36. if(!outEdge.containsKey(v1))return null;
  37. if(!outEdge.get(v1).containsKey(v2))return null;
  38. return outEdge.get(v1).get(v2).weight();
  39. }
  40. }