Reputation: 47
I'm trying to implement Dijsktra's algorithm in C#.
My code has an error in the function to find the vertex with minimum distance value, from the set of vertices not yet included in shortest path tree.
The error says Use of unassigned local variable 'min_index'. and it occurs on return min_index;
Can you tell me what i did wrong please ?
Here is the code of the function =
// Utility function to find the vertex with minimum distance value, from
// the set of vertices not yet included in shortest path tree
int minDistance (int[] dist, bool[] sptSet)
{
// Initialize min value
int min = INT_MAX, min_index;
for (int v = 0; v < V; v++)
{
if (sptSet[v] == false && dist[v] <= min)
{
min = dist[v];
min_index = v;
}
}
return min_index;
}
Thank you.
Upvotes: 0
Views: 298
Reputation: 3915
I think that in case your if condition fails every time, you're trying to return a value that is unassigned.
Upvotes: 0
Reputation: 14059
Your method may return value of the min_index
that has never been set.
You should initialize it before:
int min = INT_MAX;
int min_index = -1;
Upvotes: 3