aboutsummaryrefslogtreecommitdiff
path: root/com/example/portfolio3/AdjListGraph.java
blob: a677d3e45168cbeb10321cae409e89b5eaff2f71 (plain)
  1. package com.example.portfolio3;
  2. // origin: <https://moodle.ruc.dk/course/section.php?id=211877>
  3. import java.util.*;
  4. /// Adjecency List Graph - A map from vertices to set of outedges from the vertex
  5. public class AdjListGraph extends AbstractGraph {
  6. /// foo
  7. public AdjListGraph() {}
  8. /// foo
  9. private Map<Vertex,Set<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 HashSet<Edge>());
  15. outEdge.get(e.from()).add(e);
  16. }
  17. /// foo
  18. public Collection<Edge> edges(){
  19. Set<Edge> edges=new HashSet<>();
  20. for(Vertex v:outEdge.keySet())edges.addAll(outEdge.get(v));
  21. return edges;
  22. }
  23. /// foo
  24. public Collection<Edge> outEdge(Vertex v){
  25. if(!outEdge.containsKey(v))
  26. return new HashSet<Edge>();
  27. return outEdge.get(v);
  28. }
  29. /// foo
  30. public Integer getWeight(Vertex v1,Vertex v2){
  31. // linear in number of outedges from vertices
  32. if(!outEdge.containsKey(v1))return null;
  33. for(Edge e:outEdge.get(v1)){
  34. if(e.to()==v2)return e.weight();
  35. }
  36. return null;
  37. }
  38. }