source: doc/proposals/modules-alvin/examples/graph/5_tweaking/main__impl.c@ 7ea4073

Last change on this file since 7ea4073 was 7640ff5, checked in by Alvin Zhang <alvin.zhang@…>, 2 months ago

add graph example for modules proposal

  • Property mode set to 100644
File size: 2.8 KB
Line 
1// Graph searching algorithm testing script
2
3#include "graph__export.h"
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <unistd.h>
8
9void print_usage() {
10 printf("Usage: program_name [-n num_iter] [-s seed] [-N num_nodes] [-E num_edges] [-c num_checks] [-h]\n");
11 printf("Options:\n");
12 printf(" -n num_iter Number of graphs to run on\n");
13 printf(" -s seed Seed for random number generator\n");
14 printf(" -N num_nodes Number of nodes in generated graph\n");
15 printf(" -E num_edges Number of edges in generated graph\n");
16 printf(" -c num_checks Number of times to check graph for paths\n");
17 printf(" -h Display this help message\n");
18}
19
20struct Arg {
21 int num_iter, seed, num_nodes, num_edges, num_checks;
22};
23
24struct Arg process_args(int argc, char *argv[]) {
25 int opt;
26 struct Arg args;
27 args.num_iter = 10;
28 args.seed = 0;
29 args.num_nodes = 100;
30 args.num_edges = 100;
31 args.num_checks = 10;
32
33 // Parse command line options
34 while ((opt = getopt(argc, argv, "n:s:N:E:c:h")) != -1) {
35 switch (opt) {
36 case 'n':
37 args.num_iter = atoi(optarg);
38 break;
39 case 's':
40 args.seed = atoi(optarg);
41 break;
42 case 'N':
43 args.num_nodes = atoi(optarg);
44 break;
45 case 'E':
46 args.num_edges = atoi(optarg);
47 break;
48 case 'c':
49 args.num_checks = atoi(optarg);
50 break;
51 case 'h':
52 print_usage(); // Print usage information
53 exit(0);
54 case '?':
55 // Handle unknown options
56 fprintf(stderr, "Unknown option: -%c\n", optopt);
57 print_usage();
58 exit(1);
59 default:
60 print_usage();
61 exit(1);
62 }
63 }
64 return args;
65}
66
67int main(int argc, char *argv[]) {
68 struct Arg args = process_args(argc, argv);
69 int total_paths = 0;
70 srand(args.seed);
71 for (int i=0; i<args.num_iter; ++i) {
72 int paths_found = 0;
73 struct graph$$Graph$$shell g = graph$$create_rand_graph(args.num_nodes, args.num_edges);
74 for (int j=0; j<args.num_checks; ++j) {
75 struct graph$node$$Node$$shell *n2 = graph$$grab_random_node(&g);
76 struct graph$node$$Node$$shell *n1 = graph$$grab_random_node(&g);
77 paths_found += graph$$path_found(n1, n2); // it's technically returning a bool
78 }
79 graph$$destroy_graph(&g);
80 printf("Graph %d: %d paths found over %d checks.\n", i, paths_found, args.num_checks);
81 }
82 printf("Summary: %d graphs * %d checks = %d total checks performed, %d paths found.\n", args.num_iter, args.num_checks, args.num_iter * args.num_checks, total_paths);
83}
Note: See TracBrowser for help on using the repository browser.