diff --git a/C++/topological_search_using_dfs/Makefile b/C++/topological_search_using_dfs/Makefile new file mode 100644 index 000000000..124c88ff4 --- /dev/null +++ b/C++/topological_search_using_dfs/Makefile @@ -0,0 +1,7 @@ +tp1: main.cpp Makefile + $(CXX) -O2 -std=c++11 main.cpp -o tp1 + +clean: + rm tp1 + +all: tp1 diff --git a/C++/topological_search_using_dfs/main.cpp b/C++/topological_search_using_dfs/main.cpp new file mode 100644 index 000000000..67b63edb0 --- /dev/null +++ b/C++/topological_search_using_dfs/main.cpp @@ -0,0 +1,103 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +int DFS(unordered_map>& graph, int key, unordered_map& marked, int *time, unordered_map& finalTime) { + *time = *time + 1; + marked[key] = 1; + for (int neighbor : graph[key]) { + if (marked[neighbor] == 0) { + if (DFS(graph, neighbor, marked, time, finalTime)) return 1; + } + else if (marked[neighbor] == 1) { + marked[neighbor] = 2; + return 1; + } + } + marked[key] = 2; + *time = *time + 1; + finalTime[key] = *time; + return 0; +} + +int main(int argc, char const *argv[]) { + + FILE* inputFile = fopen(argv[1], "r"); + + int numberVertices, numberEdges; + fscanf(inputFile, "%d %d\n", &numberVertices, &numberEdges); + + //creates and fills a graph using inputFile data + unordered_map> graph; + //creates a vector that will hold the keys of DFS walking order + set order; + //creates a vector that will check if the graph is of type 1 or 2 + set defineType; + + int vertexA, vertexB; + for (int i = 0; i < numberEdges; ++i) { + fscanf(inputFile, "%d %d\n", &vertexA, &vertexB); + //insert vertices on a set to make sure they'll be unique + order.insert(vertexA); + order.insert(vertexB); + //insert vertices on graph + graph[vertexA].push_back(vertexB); + defineType.insert(vertexA); + } + fclose(inputFile); + + //marks vertices as unvisiteds + unordered_map marked; + for (auto vertex : graph) { + marked[vertex.first] = 0; + } + + //creates a final time hashtable for graph + unordered_map finalTime; + + //inserts content of set on a vector so it can be ordered + vector vertices; + for (int v : order) { + vertices.push_back(v); + } + sort(vertices.begin(), vertices.end()); + + FILE* outputFile = fopen(argv[2], "w"); + + //runs DFS + int isCycle = 0; + int time = 0; + for (int vertex : vertices) { + if (marked[vertex] != 2) { + if (DFS(graph, vertex, marked, &time, finalTime) == 1) { + fprintf(outputFile, "0 -1\n"); + return 0; + } + } + } + + //define the type of graph + if (defineType.size() < numberVertices - 1) + fprintf(outputFile, "2 "); + else fprintf(outputFile, "1 "); + + //sort vertices according to finalTime in growing order + sort(vertices.begin(), vertices.end(), [&finalTime](int a, int b) { + return finalTime[a] < finalTime[b]; + }); + + //prints output + for (int v : vertices) { + if (v == vertices.back()) fprintf(outputFile, "%d\n", v); + else fprintf(outputFile, "%d, ", v); + } + + fclose(outputFile); + + return 0; +}