Dowemo
0 0 0 0

/**
 * 无向图
 * @author yuli
 *
 */publicclassUndirectedGraph {privateint vertex;//顶点privateint edge;//边private LinkedList<Integer>[] adj;//邻接表/**
 * 内部创建
 * @param v 顶点
 */@SuppressWarnings("unchecked")
 publicUndirectedGraph(int v){
 this.vertex = v;
 edge = 0;
 adj = new LinkedList[v];
 for(int i=0;i<v;i++){
 adj[i] = new LinkedList<>();
 }
 }
 /**
 * 通过加载文件创建
 * @param file
 * @throws IOException
 */@SuppressWarnings("unchecked")
 publicUndirectedGraph(File file) throws IOException{
 FileReader fr = new FileReader(file);
 BufferedReader br = new BufferedReader(fr);
 int vertex = Integer.parseInt(br.readLine());//读取顶点数this.vertex = vertex;//读取顶点数//创建有顶点数的邻接表 adj = new LinkedList[vertex];
 for(int i=0;i<vertex;i++){
 adj[i] = new LinkedList<>();
 }
 int edge = Integer.parseInt(br.readLine());//读取边for(int i=0;i<edge;i++){
 //获取顶点对 String[] vertexs = br.readLine().split("");
 int v = Integer.parseInt(vertexs[0]);
 int m = Integer.parseInt(vertexs[1]);
 //将顶点对添加成一条边 addEdge(v, m);
 }
 if(br!= null){
 br.close();
 }
 }
 /**
 * 添加 一条双向边
 * @param v
 * @param m
 */publicvoidaddEdge(int v,int m){
 adj[v].add(m);
 adj[m].add(v);
 //边数+1 edge++;
 }
 /**
 * 获取顶点数
 * @return */publicintvertexNum(){
 return vertex;
 }
 /**
 * 边数
 * @return */publicintedgeNum(){
 return edge;
 }
 /**
 * 返回临界链表迭代器
 * @param v
 * @return */public Iterable<Integer> adj(int v){
 return adj[v];
 }
}



Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs