aboutsummaryrefslogtreecommitdiff
path: root/com/example/portfolio3/AdjMapGraph.java
diff options
context:
space:
mode:
authorJonas Smedegaard <dr@jones.dk>2025-04-20 19:39:42 +0200
committerJonas Smedegaard <dr@jones.dk>2025-04-20 19:39:42 +0200
commit79e04705c6eeed95992e5753a8328aad90e02f68 (patch)
treecc75e2753cf09097d60037f7ba44b4055ba19c42 /com/example/portfolio3/AdjMapGraph.java
parentd008224669e9b9cd9264c4779f0c660f32b7458b (diff)
move each auxiliary class to own file, to please javadoc
Diffstat (limited to 'com/example/portfolio3/AdjMapGraph.java')
-rw-r--r--com/example/portfolio3/AdjMapGraph.java45
1 files changed, 45 insertions, 0 deletions
diff --git a/com/example/portfolio3/AdjMapGraph.java b/com/example/portfolio3/AdjMapGraph.java
new file mode 100644
index 0000000..85e5d04
--- /dev/null
+++ b/com/example/portfolio3/AdjMapGraph.java
@@ -0,0 +1,45 @@
+package com.example.portfolio3;
+
+// origin: <https://moodle.ruc.dk/course/section.php?id=211877>
+
+import java.util.*;
+
+/// Adjecency Map Graph - A map from vertices to map of target vertex to edge
+class AdjMapGraph extends AbstractGraph {
+
+ /// foo
+ AdjMapGraph() {}
+
+ /// foo
+ private Map<Vertex, Map<Vertex, 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 HashMap<Vertex, Edge>());
+ outEdge.get(e.from()).put(e.to(), e);
+ }
+
+ /// foo
+ public Collection<Edge> edges() {
+ Set<Edge> edges = new HashSet<>();
+ for (Vertex v : outEdge.keySet())
+ for (Vertex w : outEdge.get(v).keySet())
+ edges.add(outEdge.get(v).get(w));
+ return edges;
+ }
+
+ /// foo
+ public Collection<Edge> outEdge(Vertex v) {
+ return outEdge.get(v).values();
+ }
+
+ /// foo
+ public Integer getWeight(Vertex v1, Vertex v2) {
+ // constant time operation
+ if(!outEdge.containsKey(v1))return null;
+ if(!outEdge.get(v1).containsKey(v2))return null;
+ return outEdge.get(v1).get(v2).weight();
+ }
+}