Skip to content

Instantly share code, notes, and snippets.

@rigibun
Created May 7, 2014 14:24
Show Gist options
  • Save rigibun/94fd760749c145ac794d to your computer and use it in GitHub Desktop.
Save rigibun/94fd760749c145ac794d to your computer and use it in GitHub Desktop.
#include "ford.h"
static const int32_t INF = 2147483647;
void ford(int32_t *d, size_t V, edge *es, size_t E, size_t s) {
for(int i = 0; i < V; i++)
d[i] = INF;
d[s] = 0;
while(true) {
bool update = false;
for(int i = 0; i < E; i++) {
edge e = es[i];
if(d[e.from] != INF && d[e.from] + e.cost < d[e.to]) {
update = true;
d[e.to] = d[e.from] + e.cost;
}
}
if(!update)
break;
}
}
#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
#ifndef FORD_H
#define FORD_H
typedef struct _edge {
size_t from, to;
int32_t cost;
} edge;
void ford(int32_t *v, size_t V, edge *e, size_t E, size_t s);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment