| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1671 人关注过本帖
标题:求教:关于无向图的Bellman-Ford算法
只看楼主 加入收藏
UAPOPPING
Rank: 1
等 级:新手上路
帖 子:13
专家分:5
注 册:2015-1-21
结帖率:100%
收藏
已结贴  问题点数:50 回复次数:2 
求教:关于无向图的Bellman-Ford算法
我用java实现bellman-ford算法,但书里面的例子关于bellman-ford的算法只用于向图。然而我的作业里面却给了一个无向图。
一下是读取的文本,第一行是总顶点数,第二行是源点 source, 接下来是图的邻接矩阵表达式。
10                                       
0
  0  12   0  17   0   7   0  12   0  -7  
 12   0   0  -4   3  19   0   0  10  15  
  0   0   0   3   2   2   0  -3  20   0  
 17  -4   3   0   0   0  13   0   0   0  
  0   3   2   0   0   9   0  12   0  12  
  7  19   2   0   9   0   0   4   0  15  
  0   0   0  13   0   0   0  18  14  16  
 12   0  -3   0  12   4  18   0  18   0  
  0  10  20   0   0   0  14  18   0   0  
 -7  15   0   0  12  15  16   0   0   0  
我运行出来似乎有错,请哪位大高手帮我看看我的代码哪里出错了。

源点为0, 总共10个顶点。

程序代码:

import *;
import java.util.*;

public class BellmanFord  {
    
    LinkedList<Edge> edges;
    int distances[];
    //int path[];
    int numberOfVertices;
    int edge;
    int source;
    final static int INFINITY=999;

    private static class Edge  {
        int u,v,w;
        public Edge(int u, int v, int w)     {
            this.u=u;
            this.v=v;
            this.w=w;
        }
    }
    
    BellmanFord () throws IOException {
              
        InputResult BellmanFordInput = readInput("BellmanFordinput.txt");
        
        source = BellmanFordInput.sourceVertex;
        numberOfVertices = BellmanFordInput.numberOfVertice;
        
        int[][] inputGraph = BellmanFordInput.adjacencyMatrix;
        edges = new LinkedList<Edge>();
        for(int i=0; i<numberOfVertices; i++) {
            for(int j =0; j< numberOfVertices; j++) {
                if(inputGraph[i][j] != 0)
                    edges.add(new Edge(i, j, inputGraph[i][j]));
            }
        }
        edge = edges.size();
        distances = new int[numberOfVertices];
    }

    void relax() { //the relax operation
        int i, j;
        for(i=0;i<numberOfVertices;++i) {
            distances[i]=INFINITY;
        }
        distances[source] = 0;
        
        for (i = 0; i < numberOfVertices - 1; ++i) {
            for (j = 0; j < edge; ++j) {                             //calculate the shortest path
                if (distances[edges.get(j).u] + edges.get(j).w < distances[edges.get(j).v]) {
                    distances[edges.get(j).v] = distances[edges.get(j).u] + edges.get(j).w;
                }
             }
         }
    }

    boolean NoCycle() {
        int j;
        for (j = 0; j < edge; ++j)
            if (distances[edges.get(j).u] + edges.get(j).w < distances[edges.get(j).v])
                 return false;
        return true;
    }
    

    
    /* The following method is going read the BellmanFordinput.txt as input and returns an object that contains
        1) total number of vertices, 2)  source vertex and 3) the adjacency matrix  */
    private static InputResult readInput(String txtfile) throws IOException{
        String pathname=txtfile; 
        File filename=new File(pathname);
        InputStreamReader reader = new InputStreamReader( new FileInputStream(filename));
        BufferedReader br = new BufferedReader(reader);
        StringBuffer buffer = new StringBuffer();
        String line = br.readLine();
        while(line!=null){
            buffer.append(" "+line);        
            line = br.readLine();
        }
        String temp[]=buffer.toString().replaceFirst(" ", "").split("\\s+");
        InputResult inputResult= new InputResult();
        inputResult.numberOfVertice=Integer.parseInt(temp[0]);
        inputResult.sourceVertex=Integer.parseInt(temp[1]);
        inputResult.adjacencyMatrix=new int[inputResult.numberOfVertice][inputResult.numberOfVertice];
        for(int i=0; i<inputResult.numberOfVertice; i++) { //line 
                for(int j=0; j<inputResult.numberOfVertice; j++){ //column
                    inputResult.adjacencyMatrix[ i ] [ j ] = Integer.parseInt(temp[j+10*i+2]);
                }
        }
        return inputResult;
    }
   
    //This inner auxiliary class is for storing the BellmanFordinput.txt input result
    private static class InputResult{ 
        int numberOfVertice;
        int sourceVertex;
        int[][] adjacencyMatrix;
    }  

    public static void main(String args[]) throws IOException   {
        BellmanFord  bellmanFord = new BellmanFord ();
        bellmanFord.relax();
        if(bellmanFord.NoCycle()) {
            System.out.println("Start from the source vertex: " + bellmanFord.source + " to " + "destination vertex: i");
            for(int i=0;i<bellmanFord.numberOfVertices;i++)
                if(bellmanFord.distances[i]!=INFINITY){
                    System.out.println(bellmanFord.source+" ==> "+ i +",  shortest distance: " +bellmanFord.distances[i]);
                    }
                else
                    System.out.println(bellmanFord.source+" ==> "+ i +",  shortest distance: INIFINITY" );
        } 
        else {
            System.out.println(" There is a negative edge cycle ");
        } 
    }
        

}



[ 本帖最后由 UAPOPPING 于 2015-6-5 10:58 编辑 ]
搜索更多相关主题的帖子: java 表达式 source 
2015-06-05 10:56
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:35 
Start from the source vertex: 0 to destination vertex: i
0 ==> 0,  shortest distance: 0
0 ==> 1,  shortest distance: 12
0 ==> 2,  shortest distance: INIFINITY
0 ==> 3,  shortest distance: 8
0 ==> 4,  shortest distance: 15
0 ==> 5,  shortest distance: 7
0 ==> 6,  shortest distance: 21
0 ==> 7,  shortest distance: 11
0 ==> 8,  shortest distance: 22
0 ==> 9,  shortest distance: -7
这样吗?

剑栈风樯各苦辛,别时冰雪到时春
2015-06-05 11:50
UAPOPPING
Rank: 1
等 级:新手上路
帖 子:13
专家分:5
注 册:2015-1-21
收藏
得分:0 
回复 2楼 林月儿
当只取邻接矩阵对角线右则一般元素的时候得到的是你这个运算结果,但如果我取整个矩阵进行运算的话就不是了。
程序代码:
    BellmanFord () throws IOException {
              
        InputResult BellmanFordInput = readInput("BellmanFordinput.txt");
        
        source = BellmanFordInput.sourceVertex;
        numberOfVertices = BellmanFordInput.numberOfVertice;
        
        int[][] inputGraph = BellmanFordInput.adjacencyMatrix;
        edges = new LinkedList<Edge>();
        for(int i=0; i<numberOfVertices; i++) {
            for(int j =0; j< numberOfVertices; j++) {
                if(inputGraph[i][j] != 0)
                    edges.add(new Edge(i, j, inputGraph[i][j]));
            }
        }
        edge = edges.size();
        distances = new int[numberOfVertices];
    }


你把这段做一个小小的修改试试,就知道了。我就是不确定到底那种做法是对的。
程序代码:
//表示只取矩阵的对角线右则的元素(矩阵的一半)
for(int i=0; i<numberOfVertices; i++) {
            for(int j = i+1; j< numberOfVertices; j++) {
                if(inputGraph[i][j] != 0)


变成
程序代码:
//表示取整个邻接矩阵
for(int i=0; i<numberOfVertices; i++) {
            for(int j =0; j< numberOfVertices; j++) {
                if(inputGraph[i][j] != 0)


[ 本帖最后由 UAPOPPING 于 2015-6-5 23:36 编辑 ]
2015-06-05 23:33
快速回复:求教:关于无向图的Bellman-Ford算法
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027372 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved