Content deleted Content added
Jellyworld (talk | contribs) No edit summary |
Jellyworld (talk | contribs) No edit summary |
||
Line 40:
==Implementations==
===[[C programming language|C]]===
#define INFINITY ((int) pow(2, sizeof(int)*8-2)-1)▼
typedef struct▼
{▼
}Edge;▼
void BellmanFord(Edge edges[], size_t edgecount, size_t nodecount, size_t source) // nodecount = number of vertices▼
▲#define INFINITY ((int) pow(2, sizeof(int)*8-2)-1)
{▼
▲typedef struct
▲ int source;
{
▲ int dest;
if(i == source) distance[i] = 0;▼
▲ int weight;
else distance[i] = INFINITY;▼
▲}Edge;
▲void BellmanFord(Edge edges[], size_t edgecount, size_t nodecount, size_t source) // nodecount = number of vertices
{
▲ int* distance = (int*) malloc(nodecount*sizeof(int));
{
▲ for(int i = 0; i < nodecount; i++)
▲ {
{
▲ if(i == source) distance[i] = 0;
distance[edges[j].dest] = distance[edges[j].source] + edges[j].weight;
▲ else distance[i] = INFINITY;
}
}▼
}
▲ for(int i = 0; i < nodecount; i++)
}
▲ {
printf("Error occured. Negative edge weight cycles detected");▼
▲ distance[edges[j].dest] = distance[edges[j].source] + edges[j].weight;
}
▲ }
▲ printf("Error occured. Negative edge weight cycles detected");
▲ }
▲ for(int i = 0; i < nodecount; i++)
== Applications in routing ==
|