| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1671 人关注过本帖
标题:求教:关于无向图的Bellman-Ford算法
取消只看楼主 加入收藏
UAPOPPING
Rank: 1
等 级:新手上路
帖 子:13
专家分:5
注 册:2015-1-21
结帖率:100%
收藏
已结贴  问题点数:50 回复次数:1 
求教:关于无向图的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
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.029931 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved