diff options
author | Jonas Smedegaard <dr@jones.dk> | 2025-04-29 07:19:10 +0200 |
---|---|---|
committer | Jonas Smedegaard <dr@jones.dk> | 2025-04-29 07:19:10 +0200 |
commit | 705b7a5a32793f7ed8a24b8b35afe3f9d49348be (patch) | |
tree | 7cffeb0de03a4638b87f04e01e7193eacbeaeede /src/com.example.portfolio3/com/example/portfolio3/AdjListGraph.java | |
parent | a5c3599d7bc7a9ef5583ad2d50a55975f030fbea (diff) |
tidy whitespace
Diffstat (limited to 'src/com.example.portfolio3/com/example/portfolio3/AdjListGraph.java')
-rw-r--r-- | src/com.example.portfolio3/com/example/portfolio3/AdjListGraph.java | 82 |
1 files changed, 44 insertions, 38 deletions
diff --git a/src/com.example.portfolio3/com/example/portfolio3/AdjListGraph.java b/src/com.example.portfolio3/com/example/portfolio3/AdjListGraph.java index 765eeca..88e54d6 100644 --- a/src/com.example.portfolio3/com/example/portfolio3/AdjListGraph.java +++ b/src/com.example.portfolio3/com/example/portfolio3/AdjListGraph.java @@ -8,44 +8,50 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; -/// Adjecency List Graph - A map from vertices to set of outedges from the vertex +/// 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(final Vertex v1, final Vertex v2, final 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(final Vertex v){ - if(!outEdge.containsKey(v)) - return new HashSet<Edge>(); - return outEdge.get(v); - } - - /// foo - public Integer getWeight(final Vertex v1, final 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; - } + /// foo + public AdjListGraph() { } + + /// foo + private Map<Vertex,Set<Edge>> outEdge = new HashMap<>(); + + /// foo + public void insertEdge(final Vertex v1, final Vertex v2, final 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(final Vertex v) { + if (!outEdge.containsKey(v)) + return new HashSet<Edge>(); + + return outEdge.get(v); + } + + /// foo + public Integer getWeight(final Vertex v1, final 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; + } } |