一:节点的定义
package tiger.yyh.practice;
import java.util.ArrayList;
import java.util.List;
class Node {
public int name;
public List<Node> adjs = new ArrayList<Node>();
Node(int name) {
this.name = name;
}
public void addArc(Node node) {
adjs.add(node);
}
public int getName() {
return name;
}
public void setName(int name) {
this.name = name;
}
}
二:邻接表的定义
package tiger.yyh.practice;
import java.util.ArrayList;
import java.util.List;
public class AGraph {
public static List<Node> vertexes = new ArrayList<Node>();
public void addVertex(Node vertex) {
vertexes.add(vertex);
}
public void addArc(Node head, Node tail) {
head.addArc(tail);
}
}
三:静态变量的定义
package tiger.yyh.practice;
public class StaticVariable {
public final static String filePathRead = "";
public final static String filePathWrite = "";
public final static String filePathNode ="" ;
public final static int MAXNODE = ;
public final static int MAXSTACK = ;
}
四:广度优先遍历实现无权无向图的最短路径
package tiger.yyh.practice;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class BFSAGraph {
// public Map<Integer,Integer> dist;
// public BFSAGraph() {
// dist = getValue(StaticVariable.filePathNode);
// }
public Map<Integer, Integer> getValue(String filePathNode) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
List<String> list = CreateAGraph.readTxtFile(filePathNode);
for (String str : list) {
map.put(Integer.valueOf(str.trim()), 0);
}
return map;
}
public Map<Integer, Integer> bfs(AGraph aGraph,int v,List<Node> list) throws IOException{
Map<Integer,Integer> visited = getValue(StaticVariable.filePathNode);
Map<Integer,Integer> dist = getValue(StaticVariable.filePathNode);
int[] que = new int[StaticVariable.MAXSTACK];
int front=0,rear=0;
int node;
Node head = null;
visited.put(v,1) ;
rear = (rear+1)%StaticVariable.MAXSTACK;
que[rear] = v;
while(front != rear){
front = (front+1)%StaticVariable.MAXSTACK;
node = que[front];
System.out.println("node = "+ node);
for(int i=0; i<list.size(); ++i){
if(list.get(i).name == node){
head = list.get(i);
break;
}
}
if(head.adjs.size() != 0){
for(Node arc:head.adjs){
if(visited.get(arc.getName())==0){
rear = (rear+1)%StaticVariable.MAXSTACK;
que[rear] = arc.getName();
visited.put(arc.getName(), 1);
dist.put(arc.getName(), dist.get(head.getName())+1);
}
}
}
}
for(Integer key:dist.keySet()){
if(dist.get(key)!=0){
System.out.println("v = " +v+" key = "+key+" value = "+dist.get(key));
}
}
return dist;
}
}
五:构建邻接表
package tiger.yyh.practice;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CreateAGraph {
public static List<String> list;
public AGraph aGraph;
public List<Node> nodelist;
public List<String> ls;
public static List<String> readTxtFile(String filePath){
try {
String encoding="GBK";
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding); //考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
list= new ArrayList<String>();
while((lineTxt = bufferedReader.readLine()) != null){
list.add(lineTxt);
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
return list;
}
public List<Node> getNode(String filePathNode){
AGraph aGraph = new AGraph();
List<String> list = readTxtFile(filePathNode);
for(String str:list){
Node node = new Node(Integer.valueOf(str.trim()));
aGraph.addVertex(node);
}
return aGraph.vertexes;
}
public AGraph createAGraph(String filePathRead,String filePathNode){
nodelist = getNode(filePathNode);
aGraph = new AGraph();
Node head = null;
Node tail = null;
ls = readTxtFile(filePathRead);
for(String l:ls){
String[] lineArray=l.split(",",2); //使用limit,最多分割成2个字符串
int i = Integer.valueOf(lineArray[0].trim());
int j = Integer.valueOf(lineArray[1].trim());
for(int k=0; k<nodelist.size();++k){
if(nodelist.get(k).name == i){
head = nodelist.get(k);
break;
}
}
for(int m=0; m<nodelist.size(); ++m){
if(nodelist.get(m).name == j){
tail = nodelist.get(m);
break;
}
}
aGraph.addArc(head, tail);
aGraph.addArc(tail, head); //加上他就是无向图,删去他就是有向图
}
return aGraph;
}
public void print(List<Node> list) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter(new File(StaticVariable.filePathWrite)));
for(Node head : list) {
if(head.adjs.size()!=0){
bw.write(String.valueOf(head.getName()));
System.out.print(head.getName());
for(Node arc : head.adjs) {
bw.write(" -> ");
bw.write(String.valueOf(arc.getName()));
System.out.print(" -> ");
System.out.print(arc.getName());
}
bw.newLine();
System.out.println();
}
}
bw.close();
}
public static void main(String[] args) throws IOException {
CreateAGraph cAGraph = new CreateAGraph();
AGraph aGraph = cAGraph.createAGraph(StaticVariable.filePathRead, StaticVariable.filePathNode);
cAGraph.print(aGraph.vertexes);
}
}
六:main程序
package tiger.yyh.practice;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Map;
public class ShestPath {
public static void main(String[] args) throws IOException{
CreateAGraph cAGraph = new CreateAGraph();
AGraph aGraph = cAGraph.createAGraph(StaticVariable.filePathRead, StaticVariable.filePathNode);
BufferedWriter bw = new BufferedWriter(new FileWriter(new File(StaticVariable.filePathWrite)));;
BFSAGraph bfsaGraph = new BFSAGraph();
List<String> list = CreateAGraph.readTxtFile(StaticVariable.filePathNode);
for(String str:list){
Map<Integer, Integer> dist = bfsaGraph.bfs(aGraph,Integer.valueOf(str),aGraph.vertexes);
for(Integer key:dist.keySet()){
if(dist.get(key)!=0){
bw.write(Integer.valueOf(str)+"\t"+key+"\t"+dist.get(key));
System.out.println("v = " +Integer.valueOf(str)+" key = "+key+" value = "+dist.get(key));
bw.newLine();
}
}
}
bw.close();
}
}
注释:只需更改静态变量的那个类,本程序即可使用,