Grafalgo
Library of useful data structures and algorithms
/Users/jst/src/grafalgo/cpp/graphAlgorithms/sPath/badCase.cpp
00001 // usage: badCase n
00002 //
00003 // BadCase generates a weighted digraph that causes Dijkstra's
00004 // shortest path algorithm to perform poorly, when
00005 // started from vertex 1.
00006 //
00007 // The parameter n is the number of vertices.
00008 //
00009 // This program is not bullet-proof. Caveat emptor.
00010 
00011 #include "stdinc.h"
00012 #include "Wdigraph.h"
00013 
00014 using namespace grafalgo;
00015 
00016 int main(int argc, char* argv[]) {
00017         vertex u,v; edge e; int n;
00018 
00019         if (argc != 2 || sscanf(argv[1],"%d",&n) != 1)
00020                 Util::fatal("usage badCase n");
00021 
00022         Wdigraph dig(n,n*n/2);
00023 
00024         for (u = 1; u <= n-1; u++) {
00025                 e = dig.join(u,u+1); dig.setLength(e,1);
00026                 for (v = u+2; v <= n; v++) {
00027                         e = dig.join(u,v); dig.setLength(e,2*(n-u));
00028                 }
00029         }
00030         dig.sortAdjLists();
00031         string s;
00032         cout << dig.toString(s);
00033 }
 All Classes Files Functions Variables Typedefs Friends