图的设计与实现

来源:互联网 发布:黑客论坛源码 编辑:程序博客网 时间:2024/05/16 06:24
package deno.Graphics;import java.awt.Color;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.util.ArrayList;import java.util.Collection;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Scanner;import java.util.Set;import practice.AStack;import practice.LinkedQueue;public class DiGraph<T> implements Graph<T>{private HashMap<T,Integer> vtxMap;private ArrayList<VertexInfo<T>> vInfo;private int numEdges;private AStack<Integer> availStack;private class IteratorImpl implements Iterator<T>{Iterator<T> iter;T lastValue=null;public IteratorImpl(){iter=vtxMap.keySet().iterator();}public boolean hasNext() {return iter.hasNext();}public T next(){lastValue=iter.next();return lastValue;}public void remove() {if(lastValue==null)throw new IllegalStateException("Graph vertex set iterator call to next()"+"required before calling remove()");int index=getVInfoIndex(lastValue);iter.remove();//remove the current vertex from the mapremovedFixup(index);//remove all edges that terminate at lastValue and update the in-degree// each neighbor of lastValue}}public DiGraph(){vtxMap=new HashMap();availStack=new AStack();vInfo=new ArrayList<VertexInfo<T>>();numEdges=0;}public String toString(){String gStr="";Iterator<Edge> iter=null;Edge e=null;for(int i=0;i<vInfo.size();i++){VertexInfo<T> v=vInfo.get(i);if(vInfo.get(i).occupied){gStr+=v.vertex+":"+"in-degree"+v.inDegree+" out-degree "+v.edgeList.size()+"\n";gStr+="Edges:";iter=v.edgeList.iterator();while(iter.hasNext()){e=iter.next();gStr+=vInfo.get(e.dest).vertex+"("+e.weight+")";}gStr+="\n";}}return gStr;}public void colorWhite(){for(int i=0;i<vInfo.size();i++){if(vInfo.get(i).occupied==true){vInfo.get(i).Color=VertexColor.WHITE;}//System.out.println(vInfo.get(i).Color);}}private Edge findEdge(LinkedList<Edge> edgeList,int dest){return null;}public VertexColor getColor(T v){int index=getVInfoIndex(v);if(index==-1)throw new IllegalArgumentException("DiGraph getNeighbors():vertex not in graph");VertexInfo<T> vtxInfo=vInfo.get(index);return vtxInfo.Color;}public int getData(T v){int index=getVInfoIndex(v);if(index==-1)throw new IllegalArgumentException("DiGraph getNeighbors():vertex not in graph");VertexInfo<T> vtxInfo=vInfo.get(index);return vtxInfo.dataValue;}public T getParent(){//int index=getVInfoIndex();//if(index==-1)throw new IllegalArgumentException("DiGraph getNeighbors():vertex not in graph");//VertexInfo<T> vtxInfo=vInfo.get(index);//return vtxInfo.parent;}private int getVInfoIndex(Object v){Integer indexObj=vtxMap.get(v);if(indexObj==null)return -1;elsereturn indexObj;}public int inDegree(T v){int index=getVInfoIndex(v);if(index==-1)throw new IllegalArgumentException("DiGraph getNeighbors():vertex not in graph");VertexInfo<T> vtxInfo=vInfo.get(index);return vtxInfo.inDegree;}public void initData(){for(int i=0;i<vInfo.size();i++){if(vInfo.get(i).occupied){vInfo.get(i).dataValue=Integer.MAX_VALUE;}}}public int outDegree(T v){int index=getVInfoIndex(v);if(index==-1)throw new IllegalArgumentException("DiGraph getNeighbors():vertex not in graph");VertexInfo<T> vtxInfo=vInfo.get(index);return vtxInfo.edgeList.size();}public DiGraph<String> readGraph(String filename){if(filename==null)return null;File file=new File(filename);try {Scanner inputStream=new Scanner(new FileInputStream(file));while(inputStream.hasNext()){inputStream.nextLine();}} catch (FileNotFoundException e){e.printStackTrace();}return null;}private void removedFixup(int index){Iterator<Edge> iter=null;Edge e=null;VertexInfo<T> vtxInfo=vInfo.get(index),edgeVtxInfo;vtxInfo.occupied=false;availStack.push(index);for(int i=0;i<vInfo.size();i++){edgeVtxInfo=vInfo.get(i);//check if vertex is validif(edgeVtxInfo.occupied){iter=edgeVtxInfo.edgeList.iterator();while(iter.hasNext()){e=iter.next();if(e.dest==index){iter.remove();numEdges--;break;}}}}numEdges-=vtxInfo.edgeList.size();iter=vtxInfo.edgeList.iterator();while(iter.hasNext()){e=iter.next();edgeVtxInfo=vInfo.get(e.dest);iter.remove();edgeVtxInfo.inDegree--;}}public boolean removeVertex(Object v){int index=getVInfoIndex(v);if(index==-1){return false;}vtxMap.remove(v);removedFixup(index);return true;}public VertexColor setColor(T v,VertexColor c){int index=getVInfoIndex(v);if(index==-1){throw new IllegalArgumentException("setColor():Vetex is not exsit");}VertexInfo<T> vtxInfo=vInfo.get(index);VertexColor oldColor=vtxInfo.Color;vtxInfo.Color=c;return oldColor;}public int setData(T v,int value){return 0;}public T setParent(T v, T p){int pos1=getVInfoIndex(v),pos2=getVInfoIndex(p);VertexInfo<T> vtxInfo;T oldParent=null;if(pos1!=-1&&pos2!=-1){vtxInfo=vInfo.get(pos1);oldParent=vtxInfo.parent;vtxInfo.parent=p;}else{throw new IllegalArgumentException("DiGraph setParent():vertex not in graph");}return oldParent;}public boolean addEdge(T v1, T v2,int w){int pos1=getVInfoIndex(v1);int pos2=getVInfoIndex(v2);if(pos1==pos2||pos1==-1||pos2==-2)throw new IllegalArgumentException("Vetex is not exsit or them are the same vertex!");VertexInfo<T> vtxInfo1=vInfo.get(pos1);VertexInfo<T> vtxInfo2=vInfo.get(pos2);Edge e=new Edge(pos2,w);boolean returnValue=true;//try to add an Edge reference v1-v2//if it already exists,just returnif(!vtxInfo1.edgeList.contains(e)){vtxInfo1.edgeList.add(e);//increment inDegree for vertex v2 and numver of edgesvtxInfo2.inDegree++;numEdges++;}elsereturnValue=false;return returnValue;}public boolean addVertex(T v) {VertexInfo<T> vt=null;if(!vtxMap.containsKey(v)){vt=new VertexInfo<T>(v);vt.occupied=true;vInfo.add(vt);vtxMap.put(v,vInfo.indexOf(vt));//System.out.print();return true;}return false;}public void clean() {}public boolean containsEdge(T v1, T v2) {return false;}public boolean containsVertex(Object v) {return vtxMap.containsKey(v);}public Set<T> getNeighbors(T v) {//find the VertexFoIndex object for index vint index=getVInfoIndex(v);if(index==-1)throw new IllegalArgumentException("DiGraph getNeighbors():vertex not in graph");HashSet<T> edgeSet=new HashSet<T>();VertexInfo<T> vtxInfo=vInfo.get(index);Iterator<Edge> iter=vtxInfo.edgeList.iterator();Edge e=null;while(iter.hasNext()){e=iter.next();edgeSet.add(vInfo.get(e.dest).vertex);}return edgeSet;}public int getWeight(T v1, T v2) {return 0;}public boolean isEmpty() {return vtxMap.isEmpty();}public int numberOfEdge() {return this.numEdges;}public int numberOfVertex() {return vtxMap.size();}public boolean removeEdge(T v1, T v2){return false;}public int setWeight(T v1, T v2){return 0;}private Set<T> vSet=null;public Set<T> vertexSet() {if(vSet==null){vSet=new Set<T>(){public int size(){return vtxMap.size();}public boolean isEmpty() {return vtxMap.isEmpty();}public boolean contains(Object o){return vtxMap.containsKey((T)o);}public Iterator<T> iterator() {return new IteratorImpl();}@Overridepublic Object[] toArray() {// TODO Auto-generated method stubreturn null;}@Overridepublic <T> T[] toArray(T[] a) {// TODO Auto-generated method stubreturn null;}@Overridepublic boolean add(T e) {// TODO Auto-generated method stubreturn false;}public boolean remove(Object item) {if(vtxMap.containsKey(item)){removeVertex(item);return true;}return false;}@Overridepublic boolean containsAll(Collection<?> c) {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean addAll(Collection<? extends T> c) {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean retainAll(Collection<?> c) {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean removeAll(Collection<?> c) {// TODO Auto-generated method stubreturn false;}@Overridepublic void clear() {// TODO Auto-generated method stub}};}return vSet;}}
package deno.Graphics;import java.util.LinkedList;public class VertexInfo<T>{public T vertex;public LinkedList<Edge> edgeList;public int inDegree;public boolean occupied;public VertexColor Color;public int dataValue;public T parent;public VertexInfo(T v){vertex=v;edgeList=new LinkedList<Edge>();inDegree=0;occupied=false;}}

package deno.Graphics;public enum VertexColor {WHITE,GRAY,BLACK}


package deno.Graphics;public class Edge {public int dest;public int weight;public Edge(int dest,int weight){this.dest=dest;this.weight=weight;}public boolean equals(Object obj){return this.dest==((Edge)obj).dest;}}
package deno.Graphics;import java.util.Set;public interface Graph<T> {public boolean addEdge(T v1,T v2,int w);public boolean addVertex(T v);public void clean();public boolean containsEdge(T v1,T v2);public boolean containsVertex(Object v);public Set<T> getNeighbors(T v);public int getWeight(T v1,T v2);public boolean isEmpty();public int numberOfEdge();public int numberOfVertex();public boolean removeEdge(T v1,T v2);public int setWeight(T v1,T v2);public Set<T> vertexSet();}

原创粉丝点击