From 705b7a5a32793f7ed8a24b8b35afe3f9d49348be Mon Sep 17 00:00:00 2001 From: Jonas Smedegaard Date: Tue, 29 Apr 2025 07:19:10 +0200 Subject: tidy whitespace --- .../com/example/portfolio3/AdjListGraph.java | 82 ++++++++++++---------- 1 file changed, 44 insertions(+), 38 deletions(-) (limited to 'src/com.example.portfolio3/com/example/portfolio3/AdjListGraph.java') 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> 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()); - outEdge.get(e.from()).add(e); - } - - /// foo - public Collection edges(){ - Set edges=new HashSet<>(); - for(Vertex v:outEdge.keySet())edges.addAll(outEdge.get(v)); - return edges; - } - - /// foo - public Collection outEdge(final Vertex v){ - if(!outEdge.containsKey(v)) - return new HashSet(); - 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> 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()); + outEdge.get(e.from()).add(e); + } + + /// foo + public Collection edges() { + Set edges = new HashSet<>(); + for (Vertex v: outEdge.keySet())edges.addAll(outEdge.get(v)); + + return edges; + } + + /// foo + public Collection outEdge(final Vertex v) { + if (!outEdge.containsKey(v)) + return new HashSet(); + + 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; + } } -- cgit v1.2.3