您的位置:首页 > 其它

图的设计与实现

2013-03-22 16:23 253 查看
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 map
removedFixup(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;
else
return 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 valid
if(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 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<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 v
int 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();
}

@Override
public Object[] toArray() {
// TODO Auto-generated method stub
return null;
}

@Override
public <T> T[] toArray(T[] a) {
// TODO Auto-generated method stub
return null;
}

@O
4000
verride
public boolean add(T e) {
// TODO Auto-generated method stub
return false;
}

public boolean remove(Object item)
{
if(vtxMap.containsKey(item))
{
removeVertex(item);
return true;
}
return false;
}

@Override
public boolean containsAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean addAll(Collection<? extends T> c) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean retainAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean removeAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
}

@Override
public 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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息