求利用邻接矩阵求多有节点最短路径的java程序 可运行
来源:互联网 发布:华为交换机mac过滤 编辑:程序博客网 时间:2024/05/16 10:11
一:定义了邻接表的大小和要读取以及写入的文件位置;
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();
}
}
}
注释:本程序全部拷下来,只需更改静态变量的值即可使用
- 求利用邻接矩阵求多有节点最短路径的java程序 可运行
- 【C++】求当前exe的运行路径
- 求程序运行所用的时间
- 【Python】求程序的运行时间
- 求以邻接矩阵存储的有向无环图中的最长路径
- 已知邻接矩阵,求不重复且经过各景点仅一次而回到起点的路径
- 利用邻接表求解所有节点的最短路径 java实现 可运行
- 利用程序求周周率
- 设计一个可容纳40位数的求n!程序
- matlab求程序运行时间
- java程序题:利用递归方法求5!
- 根据无向图的边邻接矩阵求任意一点到其他所有点之间的最短路径。
- 关于c++与java程序运行的效率困惑求解释
- java程序求最大公约数
- 求校园最短路径的C++语言程序
- 求一个小程序,windows下运行的
- matlab里求程序运行时间的函数
- 分享如何求matlab程序的运行时间
- 告别14
- js截取两个字符串之间的内容(正则表达式).
- 【Python】Python在文本分析中将中文和非中文进行分割
- SVN还原已删除文件
- 什么是“沙箱”技术?
- 求利用邻接矩阵求多有节点最短路径的java程序 可运行
- Hadoop 默认排序
- android EditText软键盘的隐藏和显示
- 数据库 执行计划的基本名词解释
- spring的context:property-placeholder属性
- 如何使用Guava的缓存管理
- (精)软件架构设计
- Java-泛型编程-使用通配符? extends 和 ? super
- chromium flash player issue