图的设计与实现(三)
olor():Vetex is not exsit");
}
VertexInfo 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 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 vtxInfo1=vInfo.get(pos1);
VertexInfo 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 return
if(!vtxInfo1.edgeList.contains(e))
{
vtxInfo1.edgeList.add(e);
//increment inDegree for vertex v2 and numver of edges
vtxInfo2.inDegree++;
numEdges++;
}
else
returnValue=false;
return returnValue;
}
public boolean addVertex(T v)
{
VertexInfo vt=null;
if(!vtxMap.containsKey(v))
{
vt=new VertexInfo(v);
vt.occupied=true;
vInfo.add(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 getNeighbors(T v)
{
//find the VertexFoIndex object for index v
int index=getVInfoIndex(v);
if(index==-1)
throw new IllegalArgumentException("DiGraph getNeighbors():vertex not in graph");
HashSet edgeSet=new HashSet();
VertexInfo vtxInfo=vInfo.get(index);
Iterator 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)