This repository contains files and code related to graph operations. Below is an overview of the files and their contents:
-
dfs.py
- This file contains a Python script for performing Depth-First Search (DFS) on a graph. It reads graph data from a user-specified file, and then allows the user to specify a starting node for DFS. It prints the DFS traversal result. -
graph_operations.py
- This file defines aDirectedGraph
class that provides various operations on a directed graph. It includes methods for adding vertices, adding edges, calculating in-degree and out-degree, and finding successors and predecessors of a vertex. -
graph-test-100.txt
- This file represents a graph in a text format with two columns, indicating the edges of the graph. -
graph-test-50000.txt
- This file is another representation of a graph in a text format with two columns.
The dfs.py
script includes the following functions:
dfs(node, graph, visited, component)
: This function performs Depth-First Search on the given graph.main()
: The main section of the script reads graph data, takes user input for the starting node, and prints the DFS traversal.
The graph_operations.py
script includes the DirectedGraph
class with the following methods:
add_vertex(vertex)
: Adds a new vertex to the graph.add_edge(source, destination)
: Adds an edge between two vertices.forward_star(vertex)
: Returns the list of successor vertices of a given vertex.reverse_star(vertex)
: Returns the list of predecessor vertices of a given vertex.in_degree(vertex)
: Calculates the in-degree of a vertex.out_degree(vertex)
: Calculates the out-degree of a vertex.- Other helper functions for file validation and reading graph data.
To use these scripts, follow these steps:
-
Run the
dfs.py
script by providing the name of a file containing graph data. It will prompt you to enter a starting node, and then it will print the DFS traversal result. -
Run the
directed_graph_analyzer.py
script by providing the name of a file containing graph data. It will prompt you to enter a starting node, and then it will print the analysis results.