Clear Filters
Clear Filters

Index in position 1 exceeds array bounds (must not exceed 1).

2 views (last 30 days)
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 Comments
Lamour Haithem Firass
Lamour Haithem Firass on 27 Apr 2021
[ v_weight, predecessor ] = bellman_ford( 6, 8, [1 1 2 3 4 5 5 6 ], [2 6 4 2 3 2 4 5 ], [10 8 2 1 -2 -4 -1 1 ])

Sign in to comment.

Answers (1)

Bob Thompson
Bob Thompson on 27 Apr 2021
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.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!