answersLogoWhite

0


Best Answer

/* Dijkstra's Algorithm in C */

/* Source: w w w . c o d e w i t h c . c o m / d i j k s t r a s - a l g o r i t h m - i n - c

#include<stdio.h>

#include<conio.h>

#include<process.h>

#include<string.h>

#include<math.h>

#define IN 99

#define N 6

int dijkstra(int cost[][N], int source, int target);

int main()

{

int cost[N][N],i,j,w,ch,co;

int source, target,x,y;

printf("\t The Shortest Path Algorithm ( DIJKSTRA'S ALGORITHM in C \n\n");

for(i=1;i< N;i++)

for(j=1;j< N;j++)

cost[i][j] = IN;

for(x=1;x< N;x++)

{

for(y=x+1;y< N;y++)

{

printf("Enter the weight of the path between nodes %d and %d: ",x,y);

scanf("%d",&w);

cost [x][y] = cost[y][x] = w;

}

printf("\n");

}

printf("\nEnter the source:");

scanf("%d", &source);

printf("\nEnter the target");

scanf("%d", &target);

co = dijsktra(cost,source,target);

printf("\nThe Shortest Path: %d",co);

}

int dijsktra(int cost[][N],int source,int target)

{

int dist[N],prev[N],selected[N]={0},i,m,min,start,d,j;

char path[N];

for(i=1;i< N;i++)

{

dist[i] = IN;

prev[i] = -1;

}

start = source;

selected[start]=1;

dist[start] = 0;

while(selected[target] ==0)

{

min = IN;

m = 0;

for(i=1;i< N;i++)

{

d = dist[start] +cost[start][i];

if(d< dist[i]&&selected[i]==0)

{

dist[i] = d;

prev[i] = start;

}

if(min>dist[i] && selected[i]==0)

{

min = dist[i];

m = i;

}

}

start = m;

selected[start] = 1;

}

start = target;

j = 0;

while(start != -1)

{

path[j++] = start+65;

start = prev[start];

}

path[j]='\0';

strrev(path);

printf("%s", path);

return dist[target];

}

User Avatar

Wiki User

6y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you implement Dijkstra's algorithm in c language?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How can you implement Ricart and Agrawala's algorithm in c?

Ronaldo! 'c' coding of Ricart-agarwala algorithm


How do you write algorith of C programs?

There is no specific Hard and Fast rule for writing algorithm. The normal method is the following: 1. get a problem 2. find or invent an algorithm to solve it 3. implement the algorithm in a programming language (C, for example)


C code to implement the file allocation algorithm?

yes we can do it,in c


Write a program in C language to implement the apriori algorithm?

JavaScript is one program that has been written in C to implement the Apriori algorithm. There are also several other known programs available on the Internet that implement it as well.


How do you write an Algorithm for a C plus plus Program?

You don't write an algorithm for a C++ program, unless you are documenting the C++ program after-the-fact. The normal procedure is to write the algorithm first, in a language independent fashion, and then translate that stated algorithm into C++ code, or into whatever language you wish.


How is c language different from fuzzy logic?

fuzzy logic is a logic which we have to implement in c language


Write a program of binary heap in c or c language?

to implement operations on binary heap in c


Which book is suitable for c language?

1. A partition of a positive integer n is a sequence of positive integers that sum to n. Write an algorithm in psedocode and then implement the algorithm (in C) to print all non-increasing partitions of n. eg. If n=4 4 3 1 2 2 2 1 1 1 1 1 1


Write a program in c language to implement framing methods like character stuffing?

A program in c language to implement framing methods like character stuffing can be grave sizeCRC-32 and the variable c50.


What is the first no for sialkot?

Data structure is related to the algorithm, ie solution of the requirements.whereas Data Type is component of a programming language, which can be used to implement the required data structure (organization of data and format).For example if the requirement say that a LIST is required, and in C programming language , there is not LIST data type BUT LIST CAN BE REALISED(IMPLEMENTED ) in C.


How do you run graphics program in C?

pro c language to implement linear search using pointers


What is the algorithm for determining the maximum of two numbers?

Compare two numbers, a and b. If a is greater than b then return a, otherwise return b. In C, we can implement this algorithm using the ternary operator (?:), as follows: return a&gt;b?a:b;