This C++ program generates Directed Acyclic Graphs (DAGs) and general directed graphs (which may contain cycles). It provides options for topological sorting, cycle detection, and file handling for saving/loading graphs. The program also logs operations in graph_log.txt.
- Generates a random Directed Acyclic Graph (DAG).
- Generates a random directed graph that may contain cycles.
- Cycle detection to check if the graph is a DAG.
- Topological sorting for DAGs.
- File handling: Save and load graphs.
- Simulation log: Records graph operations.
- Interactive menu for user-friendly operations.
- Clears the console screen.
- Displays a formatted banner with a title.
- Displays a message with a symbol decoration.
- Deletes the contents of
graph_log.txt.
- Implements a stack for Depth-First Search (DFS) operations.
- Graph initialization: Stores adjacency list representation.
void addEdge(int src, int dest): Adds an edge fromsrctodest.bool generateRandomDAG(int n, int m): Generates a random DAG withnvertices andmedges.bool generateRandomGraphWithPossibleCycles(int n, int m): Generates a random directed graph with possible cycles.bool detectCycle(): Checks for cycles using DFS.bool topologicalSort(vector<int>& result): Performs topological sorting.void printGraph(): Displays the graph as an adjacency list.bool saveGraph(const string& filename): Saves the graph to a file.bool loadGraph(const string& filename): Loads a graph from a file.
- Displays the main menu and handles user choices.
- Compile the program using a C++ compiler:
g++ -o graph_generator GraphGenerator.cpp
- Run the executable:
./graph_generator
- Follow the prompts to generate, analyze, or save/load graphs.
- The program prints graph details and results to the console.
- Logs are stored in
graph_log.txt.
Enter number of tasks (vertices): 5
Enter number of dependencies (edges): 6
Generating graph... Done!
Successfully generated a random DAG
Graph Adjacency List
Task 1 |-> Task 3, Task 5
Task 2 |-> Task 4
Task 3 |-> Task 5
Task 4 |-> None
Task 5 |-> None
Topological Order of Tasks
Execution sequence: Task 1 -> Task 3 -> Task 5 -> Task 2 -> Task 4
This project is open-source and free to use.