TEST(Topsort, speed){ clock_t start, finish; { int n = 1000; std::unique_ptr<Graph> graph = getFullGraph(n); start = clock(); topSort(graph.get()); finish = clock(); std::cout << "On full graph: " << (finish - start) << "\n"; } { int n = 1000; std::unique_ptr<Graph> graph = getFunctionalGraph(n, [](int x){return x*x; }); start = clock(); topSort(graph.get()); finish = clock(); std::cout << "On functional (x*x) graph: " << (finish - start) << "\n"; } { int n = 1000; std::unique_ptr<Graph> graph = getFunctionalGraph(n, [](int x){return x / 2; }); start = clock(); topSort(graph.get()); finish = clock(); std::cout << "On functional (x/2) graph: " << (finish - start) << "\n"; } }
string alienOrder(vector<string>& words) { string order = ""; if (words.empty()) return order; unordered_set<char> letters; vector<pair<char, char>> partialOrders; for (int i = 0; i < words.size(); i++) { if (words[i].empty()) continue; for (int k = 0; k < words[i].size(); k++) { letters.insert(words[i][k]); } for (int j = i+1; j < words.size(); j++) { if (words[j].empty() || words[j] == words[i]) continue; // find first miss-matching characters in word i and j pair<char, char> oneOrder = firstFirstUnMatchChars(words[i], words[j], letters); // If uppercase chars are returned, then words[i] is a prefix of // words[j], or vice versa. In this case no ordering information // can be extracted, thus we need to be able to filter it. if (oneOrder.first != oneOrder.second) { partialOrders.push_back(oneOrder); } } } topSort(letters, partialOrders, order); return order; }
TEST(Topsort, correctness){ /* test on all graphs of size <=5 */ for (int i = 1; i <= 5; ++i) for (auto &graph : getAllGraphs(i)) EXPECT_TRUE(isValidTopSort(topSort(graph.get()), graph.get())); /* test on big graphs with differen density */ for (int i = 0; i < 100; ++i){ int vertexNumber = 300; double density = 0.01*i; std::unique_ptr<Graph> graph = makeCompactGraph(getRandomEdges(vertexNumber, density), vertexNumber); EXPECT_TRUE(isValidTopSort(topSort(graph.get()), graph.get())); } }
int main(int argc, const char *argv[]) { int i, n, edge, k, vertex, duration; scanf("%d %d", &n, &edge); for (i = 0; i < edge && scanf("%d %d %d", &k, &vertex, &duration); ++i) insertNode(k, vertex, duration); topSort(n); printf("Critical Length: %d\n", CriticalPath(n)); return 0; }
void main () { G *g = initGraph (6); addEdge(g, 5, 2); addEdge(g, 5, 0); addEdge(g, 4, 0); addEdge(g, 4, 1); addEdge(g, 2, 3); addEdge(g, 3, 1); topSort(g); }
void Main() { vector<DirectedGraphNode*> graph = createGraph({ { 0, 1, 2, 3, 4 }, { 1, 3, 4 }, { 2, 1, 4 }, { 3, 4 }, { 4 } }); topSort(graph); }