From 79e04705c6eeed95992e5753a8328aad90e02f68 Mon Sep 17 00:00:00 2001 From: Jonas Smedegaard Date: Sun, 20 Apr 2025 19:39:42 +0200 Subject: move each auxiliary class to own file, to please javadoc --- com/example/portfolio3/AdjListGraph.java | 47 ++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 com/example/portfolio3/AdjListGraph.java (limited to 'com/example/portfolio3/AdjListGraph.java') diff --git a/com/example/portfolio3/AdjListGraph.java b/com/example/portfolio3/AdjListGraph.java new file mode 100644 index 0000000..416f646 --- /dev/null +++ b/com/example/portfolio3/AdjListGraph.java @@ -0,0 +1,47 @@ +package com.example.portfolio3; + +// origin: + +import java.util.*; + +/// Adjecency List Graph - A map from vertices to set of outedges from the vertex +class AdjListGraph extends AbstractGraph { + + /// foo + AdjListGraph() {} + + /// foo + private Map> 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()); + 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(Vertex v){ + if(!outEdge.containsKey(v)) + return new HashSet(); + 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; + } +} -- cgit v1.2.3