Grafalgo
Library of useful data structures and algorithms
|
00001 // usage: rgraph type n m scram [..] seed 00002 // 00003 // Create a random graph on n vertices and m edges 00004 // and print it. Type specifies what type of graph 00005 // to generate (see below). Span is the max separation 00006 // between vertices in the same edge. Scramble=1 if 00007 // vertex numbers should be randomized, otherwise 0. 00008 // Certain cases require additional arguments that are detailed 00009 // below, along with the string that specifies the 00010 // graph type. 00011 // 00012 // "graph" n m scram seed 00013 // "bigraph" n m scram seed 00014 // "cgraph" n m scram seed 00015 // "tree" n m scram seed 00016 // "wgraph" n m scram lo hi seed 00017 // "wbigraph" n m scram lo hi seed 00018 // "wcgraph" n m scram lo hi seed 00019 // "wtree" n m scram seed 00020 // "digraph" n m scram seed 00021 // "dag" n m scram seed 00022 // "wdigraph" n m scram lo hi seed 00023 // "wdag" n m scram lo hi seed 00024 // "flograph" n m scram mss ecap1 ecap2 seed 00025 // "wflograph" n m scram mss ecap1 ecap2 lo hi seed 00026 // "mflograph" n m scram mss ecap1 ecap2 lo hi seed 00027 // 00028 // For bigraphs, n is the number of vertices in each part. 00029 // For weighted graphs, [lo,hi] is the range of edge 00030 // weights. These are distributed uniformly in the range. 00031 // For flographs, mss is the number of edges from the source 00032 // and to the sink, ecap1 is the average edge capacity of 00033 // source/sink edges and ecap2 is the average edge capacity 00034 // of all other edges. 00035 00036 #include "stdinc.h" 00037 #include "Adt.h" 00038 #include "Wgraph.h" 00039 #include "Wdigraph.h" 00040 #include "Wflograph.h" 00041 #include "Mflograph.h" 00042 00043 //extern void rdigraph(int, int, int, bool, Digraph&); 00044 //extern void randLeng(int, int, Wdigraph&); 00045 //extern void rflograph(int, int, int, bool, Flograph&); 00046 00047 using namespace grafalgo; 00048 00049 int main(int argc, char* argv[]) { 00050 int n, m, mss, scram, lo, hi, ecap1, ecap2, seed; 00051 char *gtyp = argv[1]; 00052 00053 if (argc < 6 || argc > 11 || 00054 sscanf(argv[2],"%d",&n) != 1 || 00055 sscanf(argv[3],"%d",&m) != 1 || 00056 sscanf(argv[4],"%d",&scram) != 1 || 00057 sscanf(argv[argc-3],"%d",&lo) != 1 || 00058 sscanf(argv[argc-2],"%d",&hi) != 1 || 00059 sscanf(argv[argc-1],"%d",&seed) != 1) 00060 Util::fatal("usage: rgraph type n m scram [..] seed"); 00061 if (argc >= 9 && ( 00062 sscanf(argv[5],"%d",&mss) != 1 || 00063 sscanf(argv[6],"%d",&ecap1) != 1 || 00064 sscanf(argv[7],"%d",&ecap2) != 1)) 00065 Util::fatal("usage: rgraph type n m scram [..] seed"); 00066 00067 srandom(seed); string s; 00068 if (strcmp(gtyp,"graph") == 0 && argc == 6) { 00069 Graph g(n,m); 00070 g.rgraph(n,m); 00071 if (scram) g.scramble(); 00072 cout << g; 00073 } else if (strcmp(gtyp,"bigraph") == 0 && argc == 6) { 00074 Graph bg(2*n,m); 00075 bg.rbigraph(n,m); 00076 if (scram) bg.scramble(); 00077 cout << bg; 00078 } else if (strcmp(gtyp,"cgraph") == 0 && argc == 6) { 00079 Graph cg(n,m); 00080 cg.rcgraph(n,m); 00081 if (scram) cg.scramble(); 00082 cout << cg; 00083 } else if (strcmp(gtyp,"tree") == 0 && argc == 6) { 00084 Graph tree(n,n-1); 00085 tree.rtree(n); 00086 if (scram) tree.scramble(); 00087 cout << tree; 00088 } else if (strcmp(gtyp,"wgraph") == 0 && argc == 8) { 00089 Wgraph wg(n,m); 00090 wg.rgraph(n,m); 00091 wg.randWeight(lo,hi); 00092 if (scram) wg.scramble(); 00093 cout << wg; 00094 } else if (strcmp(gtyp,"wbigraph") == 0 && argc == 8) { 00095 Wgraph wbg(2*n,m); 00096 wbg.rbigraph(n,m); 00097 wbg.randWeight(lo,hi); 00098 if (scram) wbg.scramble(); 00099 cout << wbg; 00100 } else if (strcmp(gtyp,"wcgraph") == 0 && argc == 8) { 00101 Wgraph wcg(n,m); 00102 wcg.rcgraph(n,m); 00103 wcg.randWeight(lo,hi); 00104 if (scram) wcg.scramble(); 00105 cout << wcg; 00106 } else if (strcmp(gtyp,"wtree") == 0 && argc == 8) { 00107 Wgraph wtree(n,n-1); 00108 wtree.rtree(n); 00109 wtree.randWeight(lo,hi); 00110 if (scram) wtree.scramble(); 00111 cout << wtree; 00112 } else if (strcmp(gtyp,"digraph") == 0 && argc == 6) { 00113 Digraph dg(n,m); 00114 dg.rgraph(n,m); 00115 if (scram) dg.scramble(); 00116 cout << dg; 00117 } else if (strcmp(gtyp,"dag") == 0 && argc == 6) { 00118 Digraph dag(n,m); 00119 dag.rdag(n,m); 00120 if (scram) dag.scramble(); 00121 cout << dag; 00122 } else if (strcmp(gtyp,"wdigraph") == 0 && argc == 8) { 00123 Wdigraph wD(n,m); 00124 wD.rgraph(n,m); 00125 wD.randLength(lo,hi); 00126 if (scram) wD.scramble(); 00127 cout << wD; 00128 } else if (strcmp(gtyp,"wdag") == 0 && argc == 8) { 00129 Wdigraph waD(n,m); 00130 waD.rdag(n,m); 00131 waD.randLength(lo,hi); 00132 if (scram) waD.scramble(); 00133 cout << waD; 00134 } else if (strcmp(gtyp,"flograph") == 0 && argc == 9) { 00135 Flograph fg(n,m,1,2); 00136 fg.rgraph(n,m,mss); 00137 fg.randCapacity(ecap1,ecap2); 00138 if (scram) fg.scramble(); 00139 cout << fg; 00140 } else if (strcmp(gtyp,"wflograph") == 0 && argc == 11) { 00141 Wflograph wfg(n,m,1,2); 00142 wfg.rgraph(n,m,mss); 00143 wfg.randCapacity(ecap1,ecap2); 00144 wfg.randCost(lo,hi); 00145 if (scram) wfg.scramble(); 00146 cout << wfg; 00147 } else if (strcmp(gtyp,"mflograph") == 0 && argc == 11) { 00148 Mflograph mfg(n,m,1,2); 00149 mfg.rgraph(n,m,mss); 00150 mfg.randCapacity(ecap1,ecap2); 00151 mfg.randMinFlo(lo,hi); 00152 if (scram) mfg.scramble(); 00153 cout << mfg; 00154 } else 00155 Util::fatal("usage: rgraph type n m scram [..] seed"); 00156 exit(0); 00157 }