Something funny is happening need help

Posted on 2002-05-01
Medium Priority
Last Modified: 2010-04-02
I am trying to implement dijkstra's algrithm and this is the code. for some reason it seems to go into infinity loop. it doesn't even excute the cout statemtents.

First initialization of the matrix doesn't work . it prints garbage values in the last line.

It does not execute the cout statement cout <<"\n"<< "Yahoo";

I have been cracking my head for hours together. I hope some body can just help me out. I am trying to run this code on GNU/linux using g++. my skills of debugging with gdb are close to non-existant.


the code is here

include <iostream.h>
#define  HIGH 100000
#define  max 7
#define  start 2

int w[max][max];

// Declaring 2 global functions that we need to use later.

int least_weight(int weight[], int queue[], int *end);

void rearrange(int prev_node, int prev[], int w[max][max], int weight[]);

int no_of_nodes=6;

int main(void)
/* we have to implement a priority queue of the adjusting and calculating the
 * neighbours. Now the weight matrix is already defined before. So we need to
 * just access the matrix and then adjust the weight.
/*      int weight[7][7]={{-1,-1,-1,-1,-1,-1,-1},
                          { -1,0,1,-1,-1,1,-1},
        int i=0,j=0;

        cout <<"\n" << " I am in the main ";

        w[3][6]=1; w[2][3]=w[3][2]=w[4][5]=w[5][4]=2;
                cout <<"\n";
/* 1) No of nodes is got from the main fn. It has to be passed as a parameter.
 * eg. the fn is to invoked as di(int no_of_nodes).
 * 2) 3 arrays would be needed. One to hold temporary weight and another the
 * previous node. Indexing is done from 1 to make it easier to refer to the
 * values. Eg. Prev[9]=8 means that to get to the shortest path to node 9
 * would be through node 8. The third would be the queue.
 * 3) We first initialize the weight array to hold the highest weight possible.
 * and the previous node array is initialized to all zero's.
int prev[max], weight[max],queue[max];

cout <<"\n"<< " I am going to initiazile the prev and weight matrix";
        cout<<"\n I am in the loop and the value of j is "<<j;
        weight[j] = HIGH;
        prev[j] = 0;

cout<<"\n"<<" I have intialized both the prev and the weight matrix";

/* However the weight of the start node to itself is zero and there is no
 * previous node to the start node. So we initialize prev node of the
 * start node to be itself.

cout<< "\n" <<" Yahooo";


/* The number of the nodes in the queue would be the number of nodes created
 * The nodes enter the queue as when they are created.

int end_of_queue = no_of_nodes - 1;
for(int i=0;i<no_of_nodes;i++)

/* Now we attempt to process the elements in the queue */

        int prev_node = least_weight(weight, queue, &end_of_queue);
        rearrange(prev_node, prev, w, weight);
for(int k =1;k<=no_of_nodes;k++)

// Now we define the functions

/* In a priority queue we assume the first element to be the least element
 * until we find a lesser value later on in the queue.

int least_weight(int weight[], int queue[], int *end_of_queue)
  int id_of_node = 0;
  int temp_min_weight = weight[queue[0]];
  int temp = 0;
  for(int loop=1;loop<= *end_of_queue; loop++)
          if(weight[queue[loop]] < temp_min_weight)
                  temp_min_weight = weight[queue[loop]];
                  temp = loop;

// We have now the min_weight_node in the queue.

id_of_node = queue[temp];

// Now we move elements in the queue

for(int loop2=temp; loop2< *end_of_queue; loop2++)
        queue[loop2] = queue[loop2+1];

// The length of the queue is reduced by one


return id_of_node;

/* Now we try to estimate which one should be the previous node that would help
 * in finding out the path to those nodes that are not directly connected.

void rearrange(int prev_node, int prev[], int w[max][max], int weight[])
        int node;
        int temp_min_weight=0;
        int node_weight=0;
        int loop, loop2;        

        for( loop=1; loop<=no_of_nodes;loop++)
// if the weight is less than 0 means that there is no link. If it is zero
// means that it is loop to itself.
                node = loop;
                temp_min_weight = weight[loop];
                node_weight = weight[loop];

// Now we search for the the node of the least weight. For that we calculate
// the weight and look for the least.

                for(loop2=1; loop2<=no_of_nodes; loop2++)
                        node_weight = weight[loop2] + w[loop2][node];
                                temp_min_weight = node_weight;
                                 prev[node] = loop2;
                weight[node] = temp_min_weight;

Question by:bsd_linux
LVL 16

Accepted Solution

Peter Kwan earned 1080 total points
ID: 6983010
The reason for infinity loop because the precedence of the unary operator * is the same as --. However, the associativity is from right to left. Therefore, the statement *end_of_queue-- will execute -- first, and then deference the result.

To solve your problem, you need to add parenthesis, such as (*end_of_queue)--

Hope this helps.
LVL 23

Expert Comment

ID: 6983017
>> It does not execute the cout statement cout <<"\n"<< "Yahoo";

It does, only that cout is a buffered output stream so you need to call

cout << endl;

that should flush all the data to output.

>> *end_of_queue--;

Thats evil, should be


and should take care of infinite loop.

Author Comment

ID: 6983832
Whoa!! A greasy measly pointer. Bah! thanks folks. I really appreciate it.


Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question