Can't stop this integer overflow from giving me a negative number
I have a formula below which gives me weights to every other vertex so that i can use dijkstra's algorithm to find the shortest path. The problem is that when i do the calculation some overflow and create negative numbers. I was told if i mod each step it would stop the overflow vs mod the entire value , however neither are working. I'v tried starting with a long then casting to a int still get negatives. If someone could help me that be great!
public void setweights()
{
for(int row=0; row < 20 ;row++ )
{
for( int col=0 ; col < 20; col++)
{
// graph[row][col].setDistance(
The variable "to" is undefined in the code snippet you provide. What is "to" declared to be?
Also, in a less programmatic sense, what is the weight function from one vertex of the graph to another? You can't arbitrarily insert a mod operation into the middle of a function without dramatically changing said function, so it sounds like you are heading down the wrong path altogether.
As an asside, I seem to recall that dijkstra's algorithm requires that you set the initial value of each node of the graph to "infinity". We don't have all of your code, but it's possible that some initialization operation iis messing you up.
0
tyweed420Author Commented:
Big integer how do you implement that? is that a long?
ok, yeah sorry i didn't leave you much info here is the formula.
"to" is the first vertex
graph[row][col] are all others
I initially set the distances which is ok for dikstra's algorithm I just need to do this calculation without it going over the max integer value because it gioves me a negative value. And as we know negative values cause prblems with dijkstras algorithm.
0
Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.
If you only started by replacing int by long
half your problem was solved already.
The other half goes when you secure all intermediate results to long.
<*>