Commit 6a7ee74e authored by Leigh B. Stoller's avatar Leigh B. Stoller
Browse files

Commit Jon's dijkstra route calculator that is to be run on the nodes.

parent a235283e
......@@ -1395,7 +1395,7 @@ outfiles="$outfiles Makeconf GNUmakefile \
os/GNUmakefile os/split-image.sh os/imagezip/GNUmakefile \
os/imagezip/ffs/GNUmakefile os/imagezip/extfs/GNUmakefile \
os/frisbee.redux/GNUmakefile os/growdisk/GNUmakefile \
os/syncd/GNUmakefile \
os/syncd/GNUmakefile os/dijkstra/GNUmakefile \
pxe/GNUmakefile pxe/bootinfo.restart \
security/GNUmakefile security/paperbag security/lastlog_daemon \
security/plasticwrap \
......
......@@ -440,7 +440,7 @@ outfiles="$outfiles Makeconf GNUmakefile \
os/GNUmakefile os/split-image.sh os/imagezip/GNUmakefile \
os/imagezip/ffs/GNUmakefile os/imagezip/extfs/GNUmakefile \
os/frisbee.redux/GNUmakefile os/growdisk/GNUmakefile \
os/syncd/GNUmakefile \
os/syncd/GNUmakefile os/dijkstra/GNUmakefile \
pxe/GNUmakefile pxe/bootinfo.restart \
security/GNUmakefile security/paperbag security/lastlog_daemon \
security/plasticwrap \
......
......@@ -14,7 +14,7 @@ SYSTEM := $(shell uname -s)
include $(OBJDIR)/Makeconf
SUBDIRS = imagezip frisbee.redux growdisk syncd
SUBDIRS = imagezip frisbee.redux growdisk syncd dijkstra
all: $(SUBDIRS) split-image.sh
......@@ -29,6 +29,9 @@ frisbee.redux:
syncd:
@$(MAKE) -C syncd all
dijkstra:
@$(MAKE) -C dijkstra all
install: $(INSTALL_SBINDIR)/split-image.sh
@$(MAKE) -C imagezip install
@$(MAKE) -C frisbee.redux install
......@@ -46,6 +49,7 @@ ifneq ($(SYSTEM),Linux)
endif
$(MAKE) -C syncd client-install
$(MAKE) -C growdisk client-install
$(MAKE) -C dijkstra client-install
remote-install:
$(INSTALL) -m 755 -o root -g wheel -d $(LBINDIR)
......
#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2004 University of Utah and the Flux Group.
# All rights reserved.
#
SRCDIR = @srcdir@
TESTBED_SRCDIR = @top_srcdir@
OBJDIR = ../..
SUBDIR = os/dijkstra
include $(OBJDIR)/Makeconf
CFLAGS = -Wall -O2 -g -static
all: dijkstra
include $(TESTBED_SRCDIR)/GNUmakerules
dijkstra: dijkstra.o wgraph.o wgraph.h
$(CXX) $(CFLAGS) dijkstra.o wgraph.o $(LIBS) -o dijkstra
wgraph.o: wgraph.cc wgraph.h
client-install: dijkstra
$(INSTALL_PROGRAM) dijkstra $(DESTDIR)$(CLIENT_BINDIR)/dijkstra
clean:
rm -f *.o dijkstra
/* dijkstra.c
Compute shortest paths in weighted graphs using Dijkstra's algorithm
by: Steven Skiena
date: March 6, 2002
*/
/*
Copyright 2003 by Steven S. Skiena; all rights reserved.
Permission is granted for use in non-commerical applications
provided this copyright notice remains intact and unchanged.
This program appears in my book:
"Programming Challenges: The Programming Contest Training Manual"
by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003.
See our website www.programming-challenges.com for additional information.
This book can be ordered from Amazon.com at
http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/
*/
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include "wgraph.h"
using namespace std;
#define MAXINT 100007
int parent[MAXV]; /* discovery relation */
int firstHop[MAXV];
int distanceList[MAXV]; /* distance vertex is from start */
void dijkstra(graph *g, int start) /* WAS prim(g,start) */
{
int i,j; /* counters */
bool intree[MAXV]; /* is the vertex in the tree yet? */
int v; /* current vertex to process */
int w; /* candidate next vertex */
int weight; /* edge weight */
int dist; /* best current distance from start */
for (i=1; i<=g->nvertices; i++) {
intree[i] = false;
distanceList[i] = MAXINT;
parent[i] = -1;
}
distanceList[start] = 0;
v = start;
firstHop[v] = v;
while (intree[v] == false) {
intree[v] = true;
for (i=0; i<g->degree[v]; i++) {
w = g->edges[v][i].v;
weight = g->edges[v][i].weight;
/* CHANGED */ if (distanceList[w] > (distanceList[v]+weight)) {
/* CHANGED */ distanceList[w] = distanceList[v]+weight;
/* CHANGED */ parent[w] = v;
if (firstHop[v] == start)
{
firstHop[w] = w;
}
else
{
firstHop[w] = firstHop[v];
}
}
}
v = 1;
dist = MAXINT;
for (i=1; i<=g->nvertices; i++)
if ((intree[i] == false) && (dist > distanceList[i])) {
dist = distanceList[i];
v = i;
}
}
/*for (i=1; i<=g->nvertices; i++) printf("%d %d\n",i,distanceList[i]);*/
}
void print_route(graph * g, int source, int dest)
{
std::map< int, pair<string, string> >::iterator pos;
pos = g->ip[dest].begin();
std::map< int, pair<string, string> >::iterator limit;
limit = g->ip[dest].end();
string sourceIp = g->ip[source][firstHop[dest]].first;
string firstHopIp = g->ip[source][firstHop[dest]].second;
for ( ; pos != limit; ++pos)
{
if (pos->second.first != firstHopIp)
{
cout << "ROUTE DEST=" << pos->second.first
<< " DESTTYPE=host DESTMASK=255.255.255.255 NEXTHOP="
<< firstHopIp << " COST=" << distanceList[dest] << " SRC="
<< sourceIp << endl;
}
}
}
int main(int argc, char * argv[])
{
if (argc == 2)
{
string arg(argv[1]);
string firstHost, secondHost;
string firstIp, secondIp;
map<string, int> nameToIndex;
int weight = 0;
graph g;
int i = 0;
int vertexCounter = 1;
int numEdges = 0;
initialize_graph(&g);
cin >> g.nvertices >> numEdges;
for (i = 1; i <= numEdges; ++i)
{
cin >> firstHost >> firstIp >> secondHost >> secondIp >> weight;
map<string,int>::iterator firstPos = nameToIndex.find(firstHost);
if (firstPos == nameToIndex.end())
{
firstPos = nameToIndex.insert(make_pair(firstHost,
vertexCounter)).first;
++vertexCounter;
}
map<string,int>::iterator secondPos = nameToIndex.find(secondHost);
if (secondPos == nameToIndex.end())
{
secondPos = nameToIndex.insert(make_pair(secondHost,
vertexCounter)).first;
++vertexCounter;
}
insert_edge(&g, firstPos->second, secondPos->second, false,
weight);
g.ip[firstPos->second][secondPos->second] = make_pair(firstIp,
secondIp);
g.ip[secondPos->second][firstPos->second] = make_pair(secondIp,
firstIp);
}
map<string,int>::iterator sourcePos = nameToIndex.find(arg);
if (sourcePos == nameToIndex.end())
{
cerr << "Invalid source name in command line" << endl;
return 1;
}
// read_graph(&g,false);
dijkstra(&g,sourcePos->second);
for (i=1; i<=g.nvertices; i++)
{
if (i != sourcePos->second)
{
print_route(&g,sourcePos->second,i);
}
// find_path(source,i,parent);
}
return 0;
}
else
{
cerr << "Incorrect number of arguments" << endl;
return 1;
}
}
/* wgraph.c
A generic weighted graph data type
by Steven Skiena
*/
/*
Copyright 2003 by Steven S. Skiena; all rights reserved.
Permission is granted for use in non-commerical applications
provided this copyright notice remains intact and unchanged.
This program appears in my book:
"Programming Challenges: The Programming Contest Training Manual"
by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003.
See our website www.programming-challenges.com for additional information.
This book can be ordered from Amazon.com at
http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/
*/
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <cstdio>
#include "wgraph.h"
using namespace std;
void initialize_graph(graph * g)
//graph *g; /* graph to initialize */
{
int i; /* counter */
g -> nvertices = 0;
g -> nedges = 0;
for (i=1; i<=MAXV; i++) g->degree[i] = 0;
}
void read_graph(graph * g, bool directed)
//graph *g; /* graph to initialize */
//bool directed; /* is this graph directed? */
{
int i; /* counter */
int m; /* number of edges */
int x,y,w; /* placeholder for edge and weight */
string xIp, yIp;
initialize_graph(g);
// scanf("%d %d\n",&(g->nvertices),&m);
cin >> g->nvertices >> m;
for (i=1; i<=m; i++) {
// scanf("%d %d %d\n",&x,&y,&w);
cin >> x >> xIp >> y >> yIp >> w;
insert_edge(g,x+1,y+1,directed,w);
g->ip[x+1][y+1] = make_pair(xIp, yIp);
g->ip[y+1][x+1] = make_pair(yIp, xIp);
}
}
void insert_edge(graph * g, int x, int y, bool directed, int w)
//graph *g; /* graph to initialize */
//int x, y; /* placeholder for edge (x,y) */
//bool directed; /* is this edge directed? */
//int w; /* edge weight */
{
if (g->degree[x] > MAXDEGREE)
printf("Warning: insertion(%d,%d) exceeds degree bound\n",x,y);
g->edges[x][g->degree[x]].v = y;
g->edges[x][g->degree[x]].weight = w;
/*g->edges[x][g->degree[x]].in = false;*/
g->degree[x] ++;
if (directed == false)
insert_edge(g,y,x,true,w);
else
g->nedges ++;
}
void delete_edge(graph * g, int x, int y, bool directed)
//graph *g; /* graph to initialize */
//int x, y; /* placeholder for edge (x,y) */
//bool directed; /* is this edge directed? */
{
int i; /* counter */
for (i=0; i<g->degree[x]; i++)
if (g->edges[x][i].v == y) {
g->degree[x] --;
g->edges[x][i] = g->edges[x][g->degree[x]];
if (directed == false)
delete_edge(g,y,x,true);
return;
}
printf("Warning: deletion(%d,%d) not found in g.\n",x,y);
}
void print_graph(graph * g)
//graph *g; /* graph to print */
{
int i,j; /* counters */
for (i=1; i<=g->nvertices; i++) {
printf("%d: ",i);
for (j=0; j<g->degree[i]; j++)
printf(" %d",g->edges[i][j].v);
printf("\n");
}
}
//bool processed[MAXV]; /* which vertices have been processed */
//bool discovered[MAXV]; /* which vertices have been found */
//int parent[MAXV]; /* discovery relation */
//void initialize_search(graph * g)
//graph *g; /* graph to traverse */
//{
// int i; /* counter */
// for (i=1; i<=g->nvertices; i++) {
// processed[i] = false;
// discovered[i] = false;
// parent[i] = -1;
// }
//}
//void dfs(graph * g, int v)
//graph *g; /* graph to traverse */
//int v; /* vertex to start searching from */
//{
// int i; /* counter */
// int y; /* successor vertex */
//
// discovered[v] = true;
// process_vertex(v);
//
// for (i=0; i<g->degree[v]; i++) {
// y = g->edges[v][i].v;
// if (discovered[y] == false) {
// parent[y] = v;
// dfs(g,y);
// } else
// if (processed[y] == false)
// process_edge(v,y);
// }
//
// processed[v] = true;
//
//
void process_vertex(int v)
//int v; /* vertex to process */
{
printf(" %d",v);
}
void process_edge(int x, int y)
//int x,y; /* edge to process */
{
}
void find_path(int start, int end, int parents[])
//int start; /* first vertex on path */
//int end; /* last vertex on path */
//int parents[]; /* array of parent pointers */
{
if ((start == end) || (end == -1))
printf("\n%d",start);
else {
find_path(start,parents[end],parents);
printf(" %d",end);
}
}
//void connected_components(graph * g)
//graph *g; /* graph to analyze */
//{
// int c; /* component number */
// int i; /* counter */
//
// initialize_search(g);
//
// c = 0;
// for (i=1; i<=g->nvertices; i++)
// if (discovered[i] == false) {
// c = c+1;
// printf("Component %d:",c);
// dfs(g,i);
// printf("\n");
// }
//}
/* wgraph.h
Header file for weighted graph type
by Steven Skiena
*/
/*
Copyright 2003 by Steven S. Skiena; all rights reserved.
Permission is granted for use in non-commerical applications
provided this copyright notice remains intact and unchanged.
This program appears in my book:
"Programming Challenges: The Programming Contest Training Manual"
by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003.
See our website www.programming-challenges.com for additional information.
This book can be ordered from Amazon.com at
http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/
*/
#define MAXV 20000 /* maximum number of vertices */
#define MAXDEGREE 50 /* maximum outdegree of a vertex */
typedef struct {
int v; /* neighboring vertex */
int weight; /* edge weight */
} edge;
typedef struct {
edge edges[MAXV+1][MAXDEGREE]; /* adjacency info */
int degree[MAXV+1]; /* outdegree of each vertex */
int nvertices; /* number of vertices in the graph */
int nedges; /* number of edges in the graph */
// map from dest edge to (source ip, dest ip)
std::map< int, pair<string, string> > ip[MAXV + 1];
} graph;
void initialize_graph(graph * g);
void read_graph(graph * g, bool directed);
void insert_edge(graph * g, int x, int y, bool directed, int w);
void delete_edge(graph * g, int x, int y, bool directed);
void print_graph(graph * g);
void initialize_search(graph * g);
void dfs(graph * g, int v);
void process_vertex(int v);
void process_edge(int x, int y);
void find_path(int start, int end, int parents[]);
void connected_components(graph * g);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment