import java.util.*; class ListGraph> implements Graph{ private Map>> nodes = new TreeMap>>(); public void addNode(E node){ if(!(nodes.get(node)==null)){ return; }else{ nodes.put(node, new ArrayList>()); } }//addNode public void connect(E from, E to, String name, int weight){ List> toList = nodes.get(to); List> fromList = nodes.get(from); if(nodes.get(from)==null || nodes.get(to)==null){ throw new NoSuchElementException("connect failure. node missing"); } for(ListEdge tmp : fromList){ if(tmp.getDest().toString().equals(to.toString())){ throw new IllegalStateException("connect failure. connection already present"); } } ListEdge fromEdge = new ListEdge(to, name, weight); fromList.add(fromEdge); ListEdge toEdge = new ListEdge(from, name, weight); toList.add(toEdge); }//connect public Map>> getNodes(){ Map>> temp = nodes; return temp; }//getNodes public List> getEdgesFrom(E e){ List> tmp = nodes.get(e); return tmp; }//getEdgesFrom public ListEdge getEdge(E e1, E e2){ ListEdge rtrn=null; if(nodes.get(e1)==null || nodes.get(e2)==null){ throw new NoSuchElementException("getEdge failure. node missing"); } for(ListEdge cmp : nodes.get(e1)){ if(cmp.getDest().compareTo(e2)==0){ rtrn = cmp; } } return rtrn; }//getEdge public String getEdgeName(E e1, E e2){ String rtrn=null; if(nodes.get(e1)==null || nodes.get(e2)==null){ throw new NoSuchElementException("getEdgeName failure. node missing"); } for(ListEdge cmp : nodes.get(e1)){ if(cmp.getDest().compareTo(e2)==0){ rtrn = cmp.getName(); } } return rtrn; }//getEdgeName public int getEdgeWeight(E e1, E e2){ int rtrn=-1; if(nodes.get(e1)==null || nodes.get(e2)==null){ throw new NoSuchElementException("getEdgeWeight failure. node missing"); } for(ListEdge cmp : nodes.get(e1)){ if(cmp.getDest().compareTo(e2)==0){ rtrn = cmp.getWeight(); } } return rtrn; }//getEdgeWeight public void setEdgeWeight(E e1, E e2, int i){ ListEdge temp=null; boolean ok = false; if(nodes.get(e1)==null || nodes.get(e2)==null){ throw new NoSuchElementException("setEdgeWeight failure. node missing"); } if(i<0){ throw new IllegalArgumentException("setEdgeWeight failure. int i < 0"); } for(ListEdge cmp : nodes.get(e1)){ if(cmp.getDest().compareTo(e2)==0){ cmp.setWeight(i); temp=cmp; for(ListEdge cmp2 : nodes.get(e2)){ if(cmp2.getDest().compareTo(e1)==0){ cmp2.setWeight(i); } }//for }//if }//for if(temp==null){ throw new NoSuchElementException("setEdgeWeight failure. connection not present"); } }//setEdgeWeight public String toString(){ String temp = ""; for(Map.Entry>> entry : nodes.entrySet()){ temp+="from "+entry.getKey()+": "; for(ListEdge cmp : entry.getValue()){ temp+=cmp.toString()+", "; } temp+="\n"; } return temp; }//toString }