图的最短路径搜索

1 篇文章 0 订阅


输入:

11
0 10
0 0
0 4
1 1
1 2
1 4
2 2
2 1
2 3
2 5
3 3
3 2
4 4
4 0
4 1
4 5
4 7
5 5
5 2
5 4
5 6
5 8
6 6
6 5
7 7
7 4
8 8
8 5
8 9
9 9
9 8
9 10
10 10
10 9
-1 -1
代码:

/*
 * 基本思想:

 * 1:open表和closed表,open表记录未访问的节点,closed表记录访问过的表

 * 2:移出open表中的第一个结点N放入closed表中,若N是目的节点则退出,否则:

 * 3:遍历N的子节点进入closed表,注意是N的全部子节点,如找到则退出,否则继续
 * 4:最后利用回溯找到最佳路径
 * 
 */


import java.util.Scanner;
import java.util.Stack;

class Arcnode{
private int adjvex;
private Arcnode nextArc;
private int parentArc;

public int getAdjvex(){

return adjvex;

}
public void setAdjvex(int adjvex){

this.adjvex = adjvex;

}
public Arcnode getNextArc(){
return nextArc;
}
public void setNextArc(Arcnode nextArc){
this.nextArc = nextArc;

}
public void setparentArc(int parentArc){
this.parentArc=parentArc;
}
public int getparentArc(){
return parentArc;
}
}
class Vexnode{
private int vexData;
private Arcnode firstArc;

public int getVexData(){
return vexData;
}
public void setVexData(int vexData){

this.vexData=vexData;
}
public Arcnode getFirstArc(){
return firstArc;
}
public void setFirstArc(Arcnode firstArc){
this.firstArc=firstArc;

}

}

public class Theway {

public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
int Number = in.nextInt();
int start = in.nextInt();
int end =in.nextInt();
Vexnode[] graph = new Vexnode[Number];
for (int i = 0; i < graph.length; i++) {
graph[i] = new Vexnode();
graph[i].setVexData(i);
}
creatGraph(graph);


int[] visited = new int[Number];

Stack close = new Stack();
findTheBest(graph,start, visited,close,end);
System.out.println("the best way:");
//showGraph(graph);
if(!close.isEmpty())
getback(graph,start,close);

}
//回溯找最佳路径
private static void getback(Vexnode[] graph, int start, Stack close) {

Arcnode p = close.pop();
Arcnode test =p;

Arcnode temp;
System.out.print(test.getAdjvex()+"<--");
while (!close.isEmpty()){

if(test.getparentArc()<0)
{System.out.println("Theway.getback()");
break;}
temp = close.pop();


if(bijiao(temp.getAdjvex(),test.getparentArc()))//此处多了一个;受死我了
{ System.out.print(temp.getAdjvex()+"<--");
test=temp;
}
//System.out.println("<--"+test.getAdjvex());

}
}
//是否相等
private static boolean bijiao(int adjvex, int getparentArc) {
if(adjvex==getparentArc)
return true;
return false;
}

private static void findTheBest(Vexnode[] graph, int i, int[] visited, Stack close, int end) {
int first;
Queue queue = new Queue();
queue.insert(i);

while(!queue.isEmpty()){
i=queue.remove();

Arcnode p =graph[i].getFirstArc();

if(p!=null){

first=p.getAdjvex();
if(visited[first]==0){
visited[first]=1;
close.add(p);
if(first==end)
return;
}
while(p.getNextArc()!=null)
{p=p.getNextArc();
if(visited[p.getAdjvex()]==0)
{queue.insert(p.getAdjvex());
//p.setparentArc(i);竟然不行;
graph[p.getAdjvex()].getFirstArc().setparentArc(i);//


}
}
}
}

}

private static void showGraph(Vexnode[] graph) {
for (int i = 0; i < graph.length; i++) {
System.out.println("i="+i);
Arcnode pArcnode=graph[i].getFirstArc();
while(pArcnode!=null){
System.out.print(pArcnode.getAdjvex()+"-->");
pArcnode=pArcnode.getNextArc();
}
System.out.println();
}

}
//建图 比较麻烦每个点都要靠输入建立两个相邻的点要输入两次
private static void creatGraph(Vexnode[] graph) throws Exception {
System.out.println("Theway.creatGraph()");
if(graph.length==0){
System.out.println("Theway.creatGraph()");
return;
}
int v1=-1;
int v2=-1;

Arcnode p,q;
Scanner in =new Scanner(System.in);

v1 = in.nextInt();

v2 = in.nextInt();
if(v1==-1&&v2==-1)
return ;
while(v1>=0&&v2>=0){
if(v1>=graph.length||v2>graph.length){
System.out.println("Theway.creatGraph()");
return;
}

q=new Arcnode();
q.setAdjvex(v2);
q.setNextArc(null);

if(graph[v1].getFirstArc()== null){

graph[v1].setFirstArc(q);

}

else {
p=graph[v1].getFirstArc();
while(p.getNextArc()!=null){
p=p.getNextArc();
}
p.setNextArc(q);


}

v1 = in.nextInt();
v2 = in.nextInt(); }

}
}

结果:

the best way:
10<--9<--8<--5<--4<--0<--

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值