aboutsummaryrefslogtreecommitdiff
path: root/src/com.example.portfolio3/com/example/portfolio3/EdgeGraph.java
blob: 22cfe87921021d0db110a05b1de3935fca8f814f (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(Vertex v1,Vertex v2,int w){
  15. edges.add(new Edge(v1,v2,w));
  16. }
  17. /// foo
  18. public Collection<Edge> edges(){return edges;}
  19. /// foo
  20. public Collection<Edge> outEdge(Vertex v){
  21. ArrayList<Edge> outEdge=new ArrayList<>();
  22. for(Edge e:edges)if(e.from()==v)outEdge.add(e);
  23. return outEdge;
  24. }
  25. /// foo
  26. public Integer getWeight(Vertex v1,Vertex v2){
  27. // linear in number of edges in the graph
  28. for(Edge e:edges){
  29. if(e.from()==v1 && e.to()==v2)return e.weight();
  30. }
  31. return null;
  32. }
  33. }