package com.example.portfolio3; // origin: 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> 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()); outEdge.get(e.from()).put(e.to(), e); } /// foo public Collection edges() { Set 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 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(); } }