blob: f12df6f882fc3fc5599f5b69afd757e482858b76 (
plain)
- package com.example.portfolio3;
- // origin: <https://moodle.ruc.dk/course/section.php?id=211877>
- import java.util.Collection;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Map;
- import java.util.Set;
- /// 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();
- }
- }
|