一:定义了邻接表的大小和要读取以及写入的文件位置;
package algorithm;
public class StaticVariable {public final static int MAXSIZE = ;
public final static int edgeNum = ;
public final static String filepathread = "";
public final static String filepathwrite = "";
public static int[][] shorestPath = new int[MAXSIZE][MAXSIZE];
}
二:邻接矩阵的定义
package algorithm;
public class MGraph {
public int[][] edges = new int[StaticVariable.MAXSIZE][StaticVariable.MAXSIZE];
public int n,e;
MGraph(){
this.n = StaticVariable.MAXSIZE;
this.e = StaticVariable.edgeNum;
initEdges();
}
public void initEdges(){
for(int i = 1; i < StaticVariable.MAXSIZE; ++i ){
for(int j = 1; j < StaticVariable.MAXSIZE; ++j){
if (j == i)
edges[i][j] = 0;
else
edges[i][j] = -1;
}
}
}
}
三:创建邻接表
package algorithm;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class CreateMGraph {
public static List<String> list;
public MGraph mGraph;
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 MGraph createGraph(String filePath){
List<String> ls = readTxtFile(filePath);
mGraph= new MGraph();
for(String l:ls){
String[] lineArray=l.split("\t",2); //使用limit,最多分割成2个字符串
int i = Integer.valueOf(lineArray[0].trim());
int j = Integer.valueOf(lineArray[1].trim());
mGraph.edges[i][j] = 1;
}
return mGraph;
}
// public static void main(String[] args) {
// CreateMGraph cMGraph = new CreateMGraph();
// try{
//
BufferedWriter writer = new BufferedWriter(new FileWriter(new File("D:\\yuanResult.txt")));
//
MGraph mg = cMGraph.createGraph(StaticVariable.filepath);
//
for(int i = 1; i< StaticVariable.MAXSIZE; ++i){
//
for(int j = 1; j< StaticVariable.MAXSIZE; ++j){
//
writer.write(String.valueOf(mg.edges[i][j]));
//
}
//
writer.newLine();
// }
// writer.close();
// }catch(Exception e){
//
e.printStackTrace();
// }
// }
}
四:广度优先算法实现计算无权无向图
package algorithm;
import java.io.IOException;
public class BFSAlgorithm {
public void bfs(MGraph mGraph, int v) throws IOException{
int[] visit = new int[StaticVariable.MAXSIZE];
int[] que = new int[mGraph.n];
int front = 0, rear = 0;
int node;
visit[v] = 1;
rear = (rear+1)%mGraph.n;
que[rear] = v;
int[] dist = new int[StaticVariable.MAXSIZE];
dist[v] = 0;
while(front != rear){
front = (front+1)%mGraph.n;
node = que[front];
for(int i = 1; i < mGraph.n; ++i){
if(visit[i]==0 && mGraph.edges[node][i] == 1){
rear = (rear+1)%mGraph.n;
que[rear] = i;
visit[i] = 1;
dist[i] = dist[node]+1;
System.out.println(1);
}
}
}
for(int i = 1; i < StaticVariable.MAXSIZE; ++i){
StaticVariable.shorestPath[v][i] = dist[i];
}
}
}
五:main 方法
package algorithm;
import java.io.*;
import java.io.IOException;
public class ShortestPath {
public static void main(String[] args) throws IOException{
CreateMGraph createMGraph = new CreateMGraph();
BFSAlgorithm bfsAlgorithm = new BFSAlgorithm();
MGraph mGraph = createMGraph.createGraph(StaticVariable.filepathread);
BufferedWriter writer = new BufferedWriter(new FileWriter(new File(StaticVariable.filepathwrite)));
for(int i = 1; i < StaticVariable.MAXSIZE; ++i){
try {
bfsAlgorithm.bfs(mGraph,i);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try{
for(int j=1; j < StaticVariable.MAXSIZE; ++j){
for(int k=j+1; k < StaticVariable.MAXSIZE; ++k){
if(StaticVariable.shorestPath[j][k] != 0){
writer.write(j + "
" +k+" "+String.valueOf(StaticVariable.shorestPath[j][k]));
writer.newLine();
System.out.println(0);
}
}
}
writer.close();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
注释:本程序全部拷下来,只需更改静态变量的值即可使用