{"id":16214638,"url":"https://github.com/lfeq/dfs","last_synced_at":"2025-04-07T22:29:59.668Z","repository":{"id":193130079,"uuid":"688181180","full_name":"lfeq/DFS","owner":"lfeq","description":"Non-Recursive Depth-First Search (DFS) in C++","archived":false,"fork":false,"pushed_at":"2023-10-02T21:06:00.000Z","size":41488,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-13T23:45:06.471Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lfeq.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-09-06T20:21:16.000Z","updated_at":"2023-09-19T02:54:10.000Z","dependencies_parsed_at":"2023-10-03T02:44:37.253Z","dependency_job_id":null,"html_url":"https://github.com/lfeq/DFS","commit_stats":null,"previous_names":["lfeq/dfs"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lfeq%2FDFS","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lfeq%2FDFS/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lfeq%2FDFS/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lfeq%2FDFS/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lfeq","download_url":"https://codeload.github.com/lfeq/DFS/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247740294,"owners_count":20988187,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-10T11:11:51.980Z","updated_at":"2025-04-07T22:29:59.652Z","avatar_url":"https://github.com/lfeq.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Búsqueda en Profundidad No Recursiva (DFS) en C++\n## Introducción\n\nEn teoría de grafos, la Búsqueda en Profundidad (DFS, por sus siglas en inglés) es un algoritmo utilizado para recorrer un grafo. Comienza en un vértice dado y explora todos sus vértices adyacentes, luego todos los vértices adyacentes de esos vértices, y así sucesivamente. Este proceso se repite hasta que se hayan visitado todos los vértices en el grafo.\n\nDFS se puede implementar de forma recursiva o no recursiva. En este artículo, discutiremos la implementación no recursiva de DFS en C++.\n\n## Explicación del Código\n\nEl código para el algoritmo DFS no recursivo se muestra a continuación:\n\n```cpp\n#include \u003ciostream\u003e\n#include \u003cvector\u003e\n#include \u003cstack\u003e\n\nusing namespace std;\n\nclass Graph {\nprivate:\n    int n; // Number of vertices\n    vector\u003cvector\u003cint\u003e\u003e adjacency_list; // Adjacency list\n\npublic:\n    Graph(int n) : n(n), adjacency_list(n) {}\n\n    // Add an edge from u to v\n    void add_edge(int u, int v) {\n        adjacency_list[u].push_back(v);\n    }\n\n    // Implement the non-recursive DFS algorithm\n    void dfs_non_recursive(int s) {\n        vector\u003cbool\u003e visited(n, false); // Create a vector to mark visited vertices\n        stack\u003cint\u003e stack; // Create a stack\n        stack.push(s); // Put the initial vertex into the stack\n        while (!stack.empty()) { // While the stack is not empty\n            int u = stack.top(); \n            stack.pop();\n            if (!visited[u]) {  // If the vertex has not been visited\n                visited[u] = true; // Mark it as visited\n                cout \u003c\u003c u \u003c\u003c \" \"; // Print the vertex\n                for (int v : adjacency_list[u]) { // Traverse vertices adjacent to u\n                    stack.push(v); // Put them into the stack\n                }\n            }\n        }\n    }\n};\n\nint main() {\n  Graph g(6);\n  g.add_edge(0, 1);\n  g.add_edge(0, 2);\n  g.add_edge(0, 3);\n  g.add_edge(1, 3);\n  g.\n}\n```\n\n## Pasos para Ejecutar\n\nCompile el código C++ utilizando su compilador preferido. Puede usar el siguiente comando en la línea de comandos si tiene g++ instalado:\n```bash\ng++ main.cpp Demonstrations.cpp -o nombre_del_ejecutable\n```\n\nEjecute el programa compilado:\n```bash\nnombre_del_ejecutable\n```\n\nEjecutar en Replit\nhttps://replit.com/@LorenzEquihua/DFS#main.cpp\n\n\n\n# Non-Recursive Depth-First Search (DFS) in C++\n## Introduction\n\nIn graph theory, Depth-First Search (DFS) is an algorithm used to traverse a graph. It starts at a given vertex and explores all its adjacent vertices, then all the adjacent vertices of those vertices, and so on. This process continues until all vertices in the graph have been visited.\n\nDFS can be implemented recursively or non-recursively. In this article, we will discuss the non-recursive implementation of DFS in C++.\n\n## Code Explanation\n\nThe code for the non-recursive DFS algorithm is shown below:\n\n```cpp\n#include \u003ciostream\u003e\n#include \u003cvector\u003e\n#include \u003cstack\u003e\n\nusing namespace std;\n\nclass Graph {\nprivate:\n    int n; // Number of vertices\n    vector\u003cvector\u003cint\u003e\u003e adjacency_list; // Adjacency list\n\npublic:\n    Graph(int n) : n(n), adjacency_list(n) {}\n\n    // Add an edge from u to v\n    void add_edge(int u, int v) {\n        adjacency_list[u].push_back(v);\n    }\n\n    // Implement the non-recursive DFS algorithm\n    void dfs_non_recursive(int s) {\n        vector\u003cbool\u003e visited(n, false); // Create a vector to mark visited vertices\n        stack\u003cint\u003e stack; // Create a stack\n        stack.push(s); // Put the initial vertex into the stack\n        while (!stack.empty()) { // While the stack is not empty\n            int u = stack.top(); \n            stack.pop();\n            if (!visited[u]) {  // If the vertex has not been visited\n                visited[u] = true; // Mark it as visited\n                cout \u003c\u003c u \u003c\u003c \" \"; // Print the vertex\n                for (int v : adjacency_list[u]) { // Traverse vertices adjacent to u\n                    stack.push(v); // Put them into the stack\n                }\n            }\n        }\n    }\n};\n\nint main() {\n  Graph g(6);\n  g.add_edge(0, 1);\n  g.add_edge(0, 2);\n  g.add_edge(0, 3);\n  g.add_edge(1, 3);\n  // Add more edges here if needed\n  g.dfs_non_recursive(0); // Start DFS from vertex 0\n  return 0;\n}\n```\n\n## Execution Steps\nCompile the C++ code using your preferred compiler. You can use the following command in the command line if you have g++ installed:\n\n```bash\ng++ main.cpp Demonstrations.cpp -o executable_name\n```\n\nRun the compiled program:\n\n```bash\nexecutable_name\n```\n\nRun on Replit https://replit.com/@LorenzEquihua/DFS#main.cpp","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flfeq%2Fdfs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flfeq%2Fdfs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flfeq%2Fdfs/lists"}