-
Notifications
You must be signed in to change notification settings - Fork 0
/
Algorithms.hpp
80 lines (69 loc) · 3.07 KB
/
Algorithms.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* ID: 53036281
* Email: [email protected]
* @author Samuel Lazareanu
*/
#pragma once
#include "Graph.hpp"
#include <string>
using namespace std;
namespace ariel{
class Algorithms{
public:
/*
* Gets a graph,
* returns 1 if the graph is connected (otherwise returns 0).
*/
static bool isConnected(Graph);
/*
* The function receives a graph, a start vertex and an end vertex.
* It returns the most light-weighted path (in case the graph is not weighted - the shortest) between the two vertices.
* If there is no such route, the function will return -1.
*/
static string shortestPath(Graph, size_t, size_t);
/*
* Receives a graph and tried to print a cycle in it (if it succeeds, it will return 1).
* If there are no circles in the graph, will return 0.
*/
static string isContainsCycle(Graph);
/*
* Gets a graph and returns the partition of the graph into a bipartite graph (if it succeeds, it will return 1).
* If the graph cannot be partitioned, will return 0.
*
* Using sort of BFS, splitting vertices into 2 sets by "coloring" them.
* Adjacent vertices with the same color = the graph is not bipartite -> returns "0".
* Otherwise, the graph is bipartite -> return a string with the 2 sets.
*/
static string isBipartite(Graph);
/*
* Gets a graph and tries to find a negative cycle (that is, a cycle whose sum of the weights of the sides is negative).
* If such circle exists, will print it, and return 1. otherwise, there are no negative circles, return 0.
*/
static string negativeCycle(Graph);
private:
/*
* Gets a graph and a start vertice, and runs the Bellman Ford Algorithm on it.
* Updates the Graph object if negative/positive cycles are found.
* Returns a path vector, allowing us to track extract the shortest path from it.
*/
static vector<size_t> bellmanFord(Graph&, size_t);
/*
* Gets a graph, distances vector and paths vector (pointers).
* Tries to realx all the edges one time and update the distance and path vectors.
* Returns the number of relaxations performed.
*/
static int relaxEdges(Graph, vector<int>&, vector<size_t>&);
/*
* Gets a graph and a vertice, and runs a DFS visit on it.
* Returns true if a cycle was found, false otherwise.
* Updates visited and path vectors, allowing us to track the cycle.
*/
static bool dfsVisit(Graph, size_t, vector<bool>&, vector<size_t>&, vector<size_t>&);
/*
* Gets a graph and a start vertice, and runs a BFS on it.
* Used to check if the graph is bipartite - Updates setA and setB with the vertices.
* Returns "1" if the graph is bipartite, "0" otherwise.
*/
static bool bfsBipartite(Graph&, vector<string>&, vector<size_t>&, vector<size_t>&, size_t);
};
}