diff options
author | Jonas Smedegaard <dr@jones.dk> | 2025-04-27 16:43:17 +0200 |
---|---|---|
committer | Jonas Smedegaard <dr@jones.dk> | 2025-04-27 16:43:22 +0200 |
commit | 8535a9ca92539bf12ec00cac7a4e47be604f0283 (patch) | |
tree | 7a1fb082d5218e18a0186d97a8ed1f7dd87fd41f /src/com.example.portfolio3/com/example/portfolio3/AdjListGraph.java | |
parent | d104247b8bcdb2a38b680ac54e7ceb2bba155c0e (diff) |
simplify path structure
Diffstat (limited to 'src/com.example.portfolio3/com/example/portfolio3/AdjListGraph.java')
-rw-r--r-- | src/com.example.portfolio3/com/example/portfolio3/AdjListGraph.java | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/com.example.portfolio3/com/example/portfolio3/AdjListGraph.java b/src/com.example.portfolio3/com/example/portfolio3/AdjListGraph.java new file mode 100644 index 0000000..a677d3e --- /dev/null +++ b/src/com.example.portfolio3/com/example/portfolio3/AdjListGraph.java @@ -0,0 +1,47 @@ +package com.example.portfolio3; + +// origin: <https://moodle.ruc.dk/course/section.php?id=211877> + +import java.util.*; + +/// Adjecency List Graph - A map from vertices to set of outedges from the vertex +public class AdjListGraph extends AbstractGraph { + + /// foo + public AdjListGraph() {} + + /// foo + private Map<Vertex,Set<Edge>> outEdge= new HashMap<>(); + + /// foo + public void insertEdge(Vertex v1,Vertex v2,int w){ + Edge e=new Edge(v1,v2,w); + if(!outEdge.containsKey(e.from())) + outEdge.put(e.from(),new HashSet<Edge>()); + outEdge.get(e.from()).add(e); + } + + /// foo + public Collection<Edge> edges(){ + Set<Edge> edges=new HashSet<>(); + for(Vertex v:outEdge.keySet())edges.addAll(outEdge.get(v)); + return edges; + } + + /// foo + public Collection<Edge> outEdge(Vertex v){ + if(!outEdge.containsKey(v)) + return new HashSet<Edge>(); + return outEdge.get(v); + } + + /// foo + public Integer getWeight(Vertex v1,Vertex v2){ + // linear in number of outedges from vertices + if(!outEdge.containsKey(v1))return null; + for(Edge e:outEdge.get(v1)){ + if(e.to()==v2)return e.weight(); + } + return null; + } +} |