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(

            int f =   (  ((3751 * to.getRow() + 6539 * graph[row][col].getRow() ) % 1213 )* ((3751 * graph[row][col].getRow() + 6539 * to.getRow()  ) %1213)
                        +  ((4567 * to.getCol() + 9673 * graph[row][col].getCol() ) % 1213) * (( 4567 * graph[row][col].getCol() + 9673 * to.getCol() )%1213)
                       )
                       + 1 ;

         //int j = (int) f;
            System.out.println(f);
          }
      }




}


above was trying to mod each part to break it smaller but it gave me wird numbers

the original is as follows i tred modding the entire value

      int f =   (  (3751 * to.getRow() + 6539 * graph[row][col].getRow() ) * (3751 * graph[row][col].getRow() + 6539 * to.getRow()  )
                        +  (4567 * to.getCol() + 9673 * graph[row][col].getCol() )  * ( 4567 * graph[row][col].getCol() + 9673 * to.getCol() )
                       ) %1213
                       + 1 ;

i  was told i had to handle the overflow somehow any help would be much appreciated!
tyweed420Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
The only way to prevent overflow in all circumstances is to use BigInteger
0
SlimHealerCommented:
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.

Vertex(r1,c1) to
Vertex(r2,c2) from

 (  (3751 * r1 + 6539 * r2 ) * (3751 * r2 + 6539 * r1 )
                      +  (4567 * c1 + 9673 * c2 )  * ( 4567 * c2 + 9673 * c1 )
                     ) %1213
                     + 1 ;

"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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sciuriwareCommented:
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.
<*>
0
CEHJCommented:
8-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.