Tzohar Lary
This project deals with graph algorithms, including finding the shortest path, detecting negative cycles, and checking if a graph is bipartite.
To run the project, you need to use the clang++ compiler and run the following commands:
-
Clone the Repository: First, you need to clone the repository to your local machine. Open a terminal and run the following command:
git clone https://github.com/TzoharLary/Systems-Programming-2-Task-1.git
-
Navigate to the Project Directory: Change your current directory to the project directory:
cd Systems-Programming-2-Task-1
-
Build the Project: You can build the project using the
make
command:make
-
Clean the Project: If you want to clean the project (remove all the build files), you can use the following command:
make clean
The project includes 54 different test cases to verify the functionality of the algorithms, divided into 6 different types of test cases. To run the tests, use the following commands:
make test
./test
To run the demo, use the following command in the terminal:
make demo
The Graph
class represents a graph using an adjacency matrix. It provides methods to load a graph, add or remove nodes, set edges, and retrieve the adjacency matrix. Key methods include:
1.void loadGraph(const vector<vector<int>>& matrix)
: Loads a graph from an adjacency matrix.
-
bool isEmpty() const
: Checks if the graph is empty. -
void addNode()
: Adds a node to the graph. -
void removeNode()
: Removes a node from the graph. -
void setEdge(int i, int j, int val)
: Sets the value of an edge in the graph. -
const vector<vector<int>>& getAdjacencyMatrix() const
: Returns the adjacency matrix. -
void printGraph() const
: Prints the adjacency matrix of the graph.
The Algorithms
class provides various static methods to perform graph algorithms. Key methods include:
-
string Algorithms::shortestPath(const Graph& g, int start, int end)
: This function calculates the shortest path between two nodes in a graph using the Bellman-Ford algorithm. If no path is found, it returns "-1". -
bool Algorithms::isContainsCycle(const Graph& g)
: This function checks if the graph contains a cycle using Depth-First Search (DFS). -
std::string Algorithms::isBipartite(const Graph& g)
: This function checks if the graph is bipartite and returns a string representing the two sets if it is. -
bool Algorithms::isDirected(const Graph& g)
: This function checks if the graph is directed by comparing the values in the adjacency matrix. -
string Algorithms::negativeCycle(const Graph& g)
: This function checks for a negative cycle in the graph using the Bellman-Ford algorithm. It uses thehasNegativeEdge
function to determine if there are negative edges in the graph. -
bool Algorithms::bellmanFord(const Graph& g, vector<int>& dist)
: This function runs the Bellman-Ford algorithm on the graph and returns whether a negative cycle was found. -
bool Algorithms::hasNegativeEdge(const Graph& g, vector<int>& dist)
: This function checks if the graph contains any negative edges. -
bool Algorithms::hasNegativeCycle(const Graph& g, const vector<int>& dist)
: This function checks for a negative cycle in the graph after running the Bellman-Ford algorithm. -
void Algorithms::relax(const Graph& g, vector<int>& dist, vector<int>& parent)
: This function performs edge relaxation in the graph as part of the Bellman-Ford algorithm. -
bool Algorithms::isConnected(const Graph& g)
: This function checks if the graph is connected, meaning there is a path between every pair of nodes. -
void Algorithms::dfs(const Graph& g, size_t node, vector<bool>& visited, size_t n)
: This function performs Depth-First Search (DFS) on the graph to check connectivity.
The code uses several data structures like vectors and queues, and it also uses concepts like graph theory and algorithms like DFS (Depth-First Search) and the Bellman-Ford algorithm.