Thread: dijkstra's algorithm in c language, having problems, please help

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    7

    dijkstra's algorithm in c language, having problems, please help

    Does this code look like a proper dijkstra's algorithm and what am I doing wrong?

    Code:
    /* dijkstra's algorithm in in c language */
    
    
    #include <stdio.h>
    #include <cstdlib.h>
    #include <graph.h>
    #include <math.h>
    #define IN=1000000
    #define N 6
    int main() {
    int i, j, w;
    cost[i][j];
    cost[u][j]-cost[i][j]=e;
    cost[i+1][j+1]-cost[i][j]=w
    cost();
    
    
    /*   edge     0-1    */
    cost[0].i= 0
    cost[0].u=1
    cost[0].w=1
    
    
    /*   edge    1-2     */
    cost[1[.i=1
    cost[1].u=2
    cost[1].w=3
    
    
    /*   edge   1-3      */
    cost[2].i=1
    cost[2].u=3
    cost[2].w=4
    
    
    /*   edge   2-4      */
    cost[3].i=2
    cost[3].u=4
    cost[3].w=1
    
    
    /*   edge     3-5    */
    cost[4].i=3
    cost[4].u=5
    cost[4].w=2
    
    
    /*   edge    4-6     */
    cost[5].i=4
    cost[5].u=6
    cost[5].w=6
    
    
    /*   edge    3-4     */
    cost[6].i=3
    cost[6].u=4
    cost[6].w=5
    
    
    return cost;
    }
    
    
    int deletingfromqueue(){
    adjacent();
    if(visited>0){
    u=visited[u];
    visited=visited[i]-visited[u]
    visited[i]=dist[i][j]
    compare();
    show();
    if(cost[u]-cost[i]=w){
     for(i=1; i<=N-1; i++){
      for(j=1; j<=N; j++)
        cost[i][j]=cost[i+1][j+1];
         }
    N--
      }
    }
    typedef struct{
    int i, u
    int w
    }e;
    
    
    bool compare(const void *i, const void *u){
    x=i
    x1=u
    if(i -> w  <  u ->w)
    return -1;
    if(i-> w  <  u-> w)
    return 1;
    return (0);
    }
    
    
    int cost(){
    dist();
    int visited[N], memset(visited, 0, sizeof(visited));
    cost[i][j]=cost[6][6]
    int i, j;
    adjacent();
    printf("Enter the elements\n", i+1, j+1);
    scanf("%d", &cost[i][j])
    printf("Enter number of nodes\n")
    scanf("%d", &N)
    counter=malloc(sizeof(int*) *N);
    visited=malloc(sizeof(int*) *deletingfromqueue);
     for(i=0; i<N; N++){
       for(j=0; j<N; j++){
        scanf("%d", &cost[i][j])
          if(cost[i][j]=0){
             cost[i][j]= IN
             visited[i]=0
                }
          }
    }
    if(N>0; N++){
    counter++
    }
    
    
    int dist(){
    dist[i]= IN
    prior[i]= -1
    printf("Enter starting node\n")
    scanf("%d", &u);
    cost();
    if(cost[i][j]=cost[i+1][j]){
    N= N-1
    }
    startnode=u;
    for(i=0; i<N; i++){
      for(j=0; j<N; j++){
        dist[i]=0
     adjacent();
    cost[i][j]= adjacent(u,i)
    N--
    prio[i]= prior[i+1];
    counter++;
    for (p=0; p<N; p++){
      if(cost[i][p] > cost[i][j]){
         cost[i][p]= cost[i][j] + w 
              }
          }
    }
    
    
    int adjacent(){
    int i=0;
    for(i=0; i<N; i++){
    if(!visited[i]&&dist[i] > dist[u]){
      dist[i]+cost[v][i]=dist[i]
    }
    if(dist>dist[i]&&visited[i]){
      dist=dist[i]
      prior=1
      visited[i]=1
    }
    if(cost[0][i]&&!visited[i]{
      if(dist[v]+dist[i]<dist[i]+w){
        if(!dist[v]=prior++&&dist[i]+w){
          if(dist[v] < dist[w]){
             dist[w]= dist[i]+cost[w];
                dist[w]=dist[i];
               deletingfromqueue();
                qsort();
                             }
                       }
                 }
    }
    
    
    int qsort(cost, N, sizeof(int), (*compare), (const void *i, const void *u)){
    cost[u][j]>cost[i][j]
    e=edge
    h=0
    if(e->i  <  h->i)
    return -1;
    else if(e->i  >  h->i)
    return 1;
    else if(e->u  <  h->u)
    return -1;
    else
    return(e->u  >  h->u)
    show();
    }
    
    
    int show(){
    int i, j;
    for(i=1; i<N=N; i++){
      for(j=1; j<=N; j++){
        printf("%d", cost[i][j]);
          }
       }
    return dist;

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    No, it doesn't look like a proper Djikstra's algorithm. Where did you get it? It's full of all different kinds of errors, invalid code and undefined behavior.

    Apart from that, the formatting is crappy. Next time, please make sure your code is properly indented. Also, please provide all relevant information, such as non-standard headers like cstdlib.h1, graph.h and map.h.

    1There is stdlib.h, which is a standard C header, and cstdlib, which is a C++ header that mimics stdlib.h for C++ compatability with C.

  3. #3
    Registered User
    Join Date
    Jul 2016
    Posts
    7
    Well I looked at Dijkstra's algorithm so I tried to put in the cost matrix and distance matrix and code to find adjacent nodes. Then I added in what was supposed to be a code to enter the weights n to compare different distances. Can you pinpoint where the errors are a d if anything is correct in the code?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I don't mean to be a jerk here, I'm simply being blunt. To be honest, this looks like you copied random pieces of crappy code from around the internet, hoping it would magically work.

    If you wrote this code, you wrote way too much code at once, for a problem you didn't fully understand, using a solution you didn't understand, and a language you don't even know the basics of. A good, high-level approach to solving programming problems

    1. Read and understand the problem itself.
    2. Come up with a high-level approach to solving it. This is often expressed in English, perhaps with some pseudo code.
    3. Repeatedly break those solution steps down into smaller and more well-defined steps.
    4. Implement your solution one small step at a time, testing as you go. Don't move on to the next piece until all previous pieces are written and debugged.


    I'm not going to waste my time finding all your problems, but I'll note a few general categories of errors you have:

    • You are missing semicolons on a lot of statements, and some braces too, I think.
    • You make function calls but don't pass the right number or type of arguments.
    • You define functions that are supposed to return values, but you don't return anything. Then, you call those functions, but don't attempt to capture or use the return value, so you effectively throw it away.
    • You have a number of statements that don't do anything, such as comparing two things, but not as part of an if or loop condition.
    • Many of your assignment statements are backwards.
    • You use variables that you don't declare.


    There's probably lots more.

    Frankly, you lack nearly any understanding of the C programming language, even of the basics. Dijkstra's algorithm is simply too complicated of a problem to start with, it's a "you have to learn to walk before you lean to run" situation. Start with some basic tutorials. Once you have the grammar basics down, and can handle things like arrays to loops without any trouble, then you can start exploring more complex problems like Dijkstra's.
    Last edited by anduril462; 07-29-2016 at 12:35 PM.

  5. #5
    Registered User
    Join Date
    Jul 2016
    Posts
    7
    You are spot on actually. I looked at others code from around the web, learned what strategies the used for certain parts and rewrote it in my own way. I did take some aspects of kruskal's algorithm. And yes I am a beginner when in comes to c, quite fresh actually. You're right I should learn the basics properly instead of rushing into the language

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dijkstra's Algorithm?
    By mmhawk607 in forum C++ Programming
    Replies: 1
    Last Post: 04-15-2013, 12:13 PM
  2. Dijkstra Algorithm - Not Getting it..
    By johnnyzithers in forum C Programming
    Replies: 1
    Last Post: 10-06-2009, 11:18 PM
  3. Help w/Dijkstra's algorithm
    By dudeomanodude in forum C++ Programming
    Replies: 1
    Last Post: 04-22-2008, 02:26 PM
  4. Dijkstra algorithm problem.
    By apacz in forum C++ Programming
    Replies: 5
    Last Post: 05-28-2005, 04:16 PM
  5. Dijkstra algorithm
    By Micko in forum C++ Programming
    Replies: 5
    Last Post: 04-30-2004, 05:21 AM

Tags for this Thread