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