Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Dijkstra's Algorithm implementation

Avatar of nothing8171
nothing8171 asked on
C++
2 Comments1 Solution3702 ViewsLast Modified:
Hello Experts,

I am having a real dilly of a pickle here with implementing this algorithm.  I understand the basic concept on how it tries to find the shortest path from point a to c with the least amount of weight, but I believe I am coding more than I need to.  

If some one could take a peek and show me what i'm doing wrong I would appreciate it.

Stuff to know:
argument s = =  0
size == 10
adj_matrix[s][u] = sees if points are adjacent
distance[s][u] = tells you the weight of edge
precede[s][u] = suppose to move to previous node

all() = function to test if array contains all correct bools = in this case 'false'

If I didn't make this clear enough let me know and thanks for your help!

**************************************************************
void graph::short_path_graph(int s)
{
      bool * permanent;
      permanent = new bool [size];
      
      for(int x = 0; x < size; x++)
      {
            permanent[x] = false;
      }
      
      
      for(int i = 1; i < size; i++)
      {
            distance[s][i] = INT_MAX;
      }
      
      
      distance[s][s] = 0;
      
      while(!all(permanent))
      {      
            int u = 0;
            int v = 0;
            
            for(int i = 1; i < size; ++i)
            {
                  for(int y = 0; y < size; y++)
                  {
                        if(adj_matrix[s][i] && adj_matrix[s][y])
                        {       
                              if(distance[s][i] < distance[s][y] && !permanent[s])
                              {
                                    v = distance[s][i];
                                    y++;
                              }
                              
                        }
                        
                  }
            }
            
      
            permanent[v] = true;
            
            for(u = 0; u < size; ++u)
            {
                  if(adj_matrix[v][u])
                  {
                        if(distance[s][u] > distance[s][v] + distance[v][u])
                        {
                              distance[s][u] = distance[s][v] + distance[v][u];
                                                            
                              precede[s][u] = v;
                                                                                                                        
                        }
                        
                  }
                  
            }
      
      
      }                     
      
}

***************************************

 
ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelseFlag of Germany image

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answers