Index in position 1 exceeds array bounds (must not exceed 1).
조회 수: 2 (최근 30일)
이전 댓글 표시
I have the code below for the bellman function the shortest way, and when i debug, it return "index error" in u = e(2, j), I have tried to change the number several times , still return the same problem, how can I solve it.
function [ v_weight, predecessor ] = bellman_ford( v_num, e_num, source, ...
e, e_weight )
%*****************************************************************************80
%
% Purpose:
%
% BELLMAN_FORD finds shortest paths from a given vertex of a weighted directed graph.
%
% Discussion:
%
% The Bellman-Ford algorithm is used.
%
% Each edge of the graph has a weight, which may be negative. However,
% it should not be the case that there is any negative loop, that is,
% a circuit whose total weight is negative.
%
% Licensing:
%
% This code is distributed under the GNU LGPL license.
%
% Modified:
%
% 12 November 2014
%
% Author:
%
% John Burkardt
%
% Input:
%
% integer V_NUM, the number of vertices.
%
% integer E_NUM, the number of edges.
%
% integer SOURCE, the vertex from which distances will
% be calculated.
%
% integer E(2,E_NUM), the edges, given as pairs of
% vertex indices.
%
% real E_WEIGHT(E_NUM), the weight of each edge.
%
% Output:
%
% real V_WEIGHT(V_NUM), the weight of each node,
% that is, its minimum distance from SOURCE.
%
% integer PREDECESSOR(V_NUM), a list of predecessors,
% which can be used to recover the shortest path from any node back to SOURCE.
%
r8_big = 1.0E+30;
%
% Step 1: initialize the graph.
%
v_weight(1:v_num) = r8_big;
v_weight(source) = 0.0;
predecessor(1:v_num) = -1;
% Step 2: Relax edges repeatedly.
for i = 1 : v_num - 1
for j = 1 : e_num + 1
u = e(2,j);
v = e(1,j);
t = v_weight(u) + e_weight(j);
if ( t < v_weight(v) )
v_weight(v) = t;
predecessor(v) = u;
end
end
end
%
% Step 3: check for negative-weight cycles
%
for j = 1 : e_num
u = e(2,j);
v = e(1,j);
if ( v_weight(u) + e_weight(j) < v_weight(v) )
fprintf ( 1, '\n' );
fprintf ( 1, 'BELLMAN_FORD - Fatal error!\n' );
fprintf ( 1, ' Graph contains a cycle with negative weight.\n' );
error ( 'BELLMAN_FORD - Fatal error!' );
end
end
return
end
댓글 수: 3
답변 (1개)
Bob Thompson
2021년 4월 27일
You're getting the error because your e input is size 1x8, but u = e(2,j) is trying to call the second row of e, which doesn't exist.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Construction에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!