Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package graphs.freshcode;
- // exported project
- import org.jgrapht.*;
- import org.jgrapht.graph.*;
- import org.jgrapht.traverse.*;
- import java.util.*;
- public class BaseGraph<V,E> extends SimpleDirectedWeightedGraph<V,E> {
- // for mapping vertices to boolean values to indicate whether marked
- HashMap<V, Boolean> marked = new HashMap<V, Boolean>();
- // for mapping vertices to int values to indicate visits
- HashMap<V, Integer> visitMap = new HashMap<V, Integer>();
- // for mapping vertices to Double values to indicate weight
- HashMap<V, Double> weightMap = new HashMap<V, Double>();
- private static final long serialVersionUID = 3545796589454112304L;
- public BaseGraph (EdgeFactory<V, E> ef) {
- super(ef);
- }
- public BaseGraph (Class<? extends E> edgeClass) {
- super(edgeClass);
- }
- @Override
- public boolean addVertex (V v) {
- marked.put(v, false);
- return super.addVertex(v);
- }
- public void print() {
- TreeSet<V> ts = new TreeSet<V>(vertexSet());
- Iterator<V> it = ts.iterator();
- System.out.println("Number of vertices is " + vertexSet().size());
- System.out.println("Number of edges is " + edgeSet().size());
- while (it.hasNext()) {
- V v = it.next();
- System.out.print(v+": ");
- System.out.println(Graphs.neighborListOf(this, v));
- }
- }
- // get access to lowest rank vertex of graph
- public V getFirstVertex() {
- Set<V> vs = vertexSet();
- TreeSet<V> ts = new TreeSet<V>(vs);
- return ts.first();
- }
- // below all for marking vertices
- public void unsetAllMarked() {
- Iterator<V> it = this.vertexSet().iterator();
- while (it.hasNext())
- marked.put(it.next(), false);
- }
- public void setMarked(V v) {
- marked.put(v, true);
- }
- public void unsetMarked(V v) {
- marked.put(v, false);
- }
- public Boolean isMarked(V v) {
- return marked.get(v);
- }
- // ADDED
- public void setVisits(){
- Iterator<V> it = this.vertexSet().iterator();
- while (it.hasNext())
- visitMap.put(it.next(), 0);
- }
- public Integer getVisits(V v) {
- // PRE: vertex V exists
- // POST: returns the value of attribute visits for vertex v
- if(!visitMap.containsKey(v)){
- setVisits();
- }
- return visitMap.get(v);
- }
- public void setWeights(){
- Iterator<V> it = vertexSet().iterator();
- while (it.hasNext()) {
- weightMap.put(it.next(), Double.valueOf(0));
- }
- }
- public Double getWeight(V v) {
- // PRE: vertex V exists
- // POST: returns the value of attribute weight for vertex v
- // TODO
- if(!weightMap.containsKey(v)){
- setWeights();
- }
- return weightMap.get(v);
- // TODO: REPLACE THIS WITH AN APPROPRIATE RETURN VALUE
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement