Esempio n. 1
0
int main (int argc, const char * argv[]) 
{
	if (argc < 3)
	{
		cout << "Usage: script <graph-file-name> <script-file-name>" << endl;
		exit(1);
	}
	char filename[256];
	strcpy (filename, argv[1]);
	char script_filename[256];
	strcpy (script_filename, argv[2]);

  	// ---------------------------------------------------------	
  	// Read graph	

 	MyGraph G;
	
	GML_error err  = G.load (filename);
	if (err.err_num != GML_OK)
	{
		cerr << "Error (" << err.err_num << ") loading graph from file \"" << filename << "\"";
		switch (err.err_num)
		{
			case GML_FILE_NOT_FOUND: cerr << "A file with that name doesn't exist."; break;
			case GML_TOO_MANY_BRACKETS: cerr << "A mismatch of brackets was detected, i.e. there were too many closing brackets (])."; break;
			case GML_OPEN_BRACKET: cerr << "Now, there were too many opening brackets ([)";  break;
			case GML_TOO_MANY_DIGITS: cerr << "The number of digits a integer or floating point value can have is limited to 1024, this should be enough :-)";  break;
			case GML_PREMATURE_EOF: cerr << "An EOF occured, where it wasn't expected, e.g. while scanning a string."; break;
			case GML_SYNTAX: cerr << "The file isn't a valid GML file, e.g. a mismatch in the key-value pairs."; break;
			case GML_UNEXPECTED: cerr << "A character occured, where it makes no sense, e.g. non-numerical characters"; break;
			case GML_OK: break;
		}
		cerr << endl;
		exit(1);
	}
	else
	{
		cout << "Graph read from file \"" << filename << "\" has " << G.number_of_nodes() << " nodes and " << G.number_of_edges() << " edges" << endl;
	}
	
	
	// Output starting graph
	G.save_dot ("start.dot");


	// 1. Get map between labels and nodes
	std::map < std::string, GTL::node, std::less<std::string> > l; 
	node n;
	forall_nodes (n, G)
	{
		l[G.get_node_label(n)] = n;
	}
	
	// Read edit script
	{
		ifstream f (script_filename);
		char buf[512];
		
		while (f.good())
		{
			f.getline (buf, sizeof(buf));
			
			if (f.good())
			{
				
				// Break line into tokens
				std::vector<std::string> tokens;
				std::string s = buf;
				std::string delimiters = "|";
				Tokenise (s, delimiters, tokens);
				
				if (tokens[0] == "delete")
				{
					if (tokens[1] == "node")
					{
						string name = tokens[2];
						if (l.find(name) == l.end())
						{
							cout << "Node labelled \"" << name << "\" not found" << endl;
							exit(1);
						}
						else
						{
							cout << "Node \"" << name << "\" deleted" << endl;
							G.del_node(l[name]);
							l.erase(name);
						}
					}
					else if (tokens[1] == "branch")
					{
						string source = tokens[2];
						string target = tokens[3];
						if (l.find(source) == l.end())
						{
							cout << "Node labelled \"" << source << "\" not found" << endl;
							exit(1);
						}
						if (l.find(target) == l.end())
						{
							cout << "Node labelled \"" << target << "\" not found" << endl;
							exit(1);
						}
						cout << "Edge \"" << source << "\"-->\"" << target << "\" deleted" << endl;
						G.delete_edge(l[source], l[target]);
					}
				}
				if (tokens[0] == "insert")
				{
					if (tokens[1] == "node")
					{
						string name = tokens[2];
						node n = G.new_node();
						G.set_node_label (n, name);
						G.set_node_colour (n, "green");
						l[name] = n;
						cout << "Node \"" << name << "\" inserted" << endl;
					}
					else if (tokens[1] == "branch")
					{
						string source = tokens[2];
						string target = tokens[3];
						if (l.find(source) == l.end())
						{
							cout << "Node labelled \"" << source << "\" not found" << endl;
							exit(1);
						}
						if (l.find(target) == l.end())
						{
							cout << "Node labelled \"" << target << "\" not found" << endl;
							exit(1);
						}
						
						edge e = G.new_edge (l[source], l[target]);
						G.set_edge_colour (e, "green");
						
						cout << "Edge \"" << source << "\"-->\"" << target << "\" added" << endl;
					}
				}

			}			

		}
		f.close();
	}

	G.save_dot ("end.dot");


	
   return 0;
}
Esempio n. 2
0
/**
 * Kompilace: $ g++ -std=c++11 -Wall -pedantic main.cpp mygraph.cpp edge.cpp node.cpp dijkstra.cpp -o graph
 * ./graph [string] ... Spusti program, nacte ze souboru.
 * ./graph [string] [unsigned int] [unsigned int] ... Generuje graf do souboru,
 * druhy parametr je pocet uzlu a treti parametr pocet hran, ktere v prumeru
 * vedou z nejakeho uzlu.
 */
int main(int argc, char* const argv[]) {
    ios::sync_with_stdio(false);
    MyGraph * graph = new MyGraph();

    try {
        switch(argc) {
            case 2: {
                graph->load(argv[1]);

                //ofstream ofs;

                //ofs.open("dijkstra.out");
                //if(!ofs.is_open()) throw IOException();

                double ** matrix;
                CDijkstra dijkstra = CDijkstra(graph);
                clock_t begin = clock();
                //matrix = dijkstra.CalculateDistanceMatrix();
                clock_t end = clock();
                //printMatrix(ofs, matrix, graph->size());

                //ofs.close();

                double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;

                cout << "Dijkstra:\t" << elapsed_secs << " sec" << endl;

                //ofs.open("floydwarshall.out");
                //if(!ofs.is_open()) throw IOException();

                int blockSize = 36;
                while(graph->size() % blockSize != 0) ++blockSize;

                begin = clock();
                matrix = floydWarschall(graph, blockSize);
                end = clock();
                //printMatrix(ofs, matrix, graph->size());
                //ofs.close();
                elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;

                cout << "FloydWarshall:\t" << elapsed_secs << " sec" << endl;

                /*double ** matrix;

                for(unsigned i = 1; i < 1080; ++i) {
                    if(1080 % i == 0)
                    {
                        clock_t begin = clock();
                        matrix = floydWarschall(graph, i);
                        clock_t end = clock();
                        cout << i << " " << double(end - begin) / CLOCKS_PER_SEC << endl;
                    }
                }*/

                break;
            }
            case 4: {
                unsigned int n, pocetHran;
                string arg = argv[2];
                arg += " ";
                arg += argv[3];
                arg += " ";
                stringstream ss(arg);
                ss >> n;
                ss >> pocetHran;
                if(ss.eof() || ss.fail()) throw IOException();
                graph->graphGen(n, pocetHran, argv[1]);
                break;
            }
            default: {
                throw IllegalArgumentException();
                break;
            }
        }
    }
    catch(IOException) {}
    catch(IllegalArgumentException) {}
    catch(...) {
        cerr << "Neco se podelalo." << endl;
    }

    delete graph;

    return 0;
}
Esempio n. 3
0
/**
 * Kompilace: $ g++ -std=c++11 -Wall -pedantic main.cpp mygraph.cpp edge.cpp node.cpp dijkstra.cpp -o graph
 * ./graph [string] ... Spusti program, nacte ze souboru.
 * ./graph [string] [unsigned int] [unsigned int] ... Generuje graf do souboru,
 * druhy parametr je pocet uzlu a treti parametr pocet hran, ktere v prumeru
 * vedou z nejakeho uzlu.
 */
int main(int argc, char* const argv[]) {
    ios::sync_with_stdio(false);
    MyGraph * graph = new MyGraph();

    try {
        switch(argc) {
            case 2: {
                graph->load(argv[1]);

                ofstream ofs;

                //ofs.open("dijkstra.out");
                //if(!ofs.is_open()) throw IOException();
                
                double ** matrix;
                CDijkstra dijkstra = CDijkstra(graph);

                double time1 = omp_get_wtime();
                matrix = dijkstra.CalculateDistanceMatrix();
                double time2 = omp_get_wtime();

                //printMatrix(ofs, matrix, graph->size());
                //ofs.close();

                cout << "omptime: " << time2 - time1 << endl;
                //ofs.open("floydwarshall.out");
                //if(!ofs.is_open()) throw IOException();
                //begin = clock();
                //matrix = floydWarschall(graph);
                //end = clock();
                //printMatrix(ofs, matrix, graph->size());
                //ofs.close();
                //elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;

                //cout << "FloydWarshall:\t" << elapsed_secs << " sec" << endl;

                break;
            }
            case 4: {
                unsigned int n, pocetHran;
                string arg = argv[2];
                arg += " ";
                arg += argv[3];
                arg += " ";
                stringstream ss(arg);
                ss >> n;
                ss >> pocetHran;
                if(ss.eof() || ss.fail()) throw IOException();
                graph->graphGen(n, pocetHran, argv[1]);
                break;
            }
            default: {
                throw IllegalArgumentException();
                break;
            }
        }
    }
    catch(IOException) {}
    catch(IllegalArgumentException) {}
    catch(...) {
        cerr << "Neco se podelalo." << endl;
    }

    delete graph;

    return 0;
}