From 43ae223ace538944ae4f0563a7455e8182758d73 Mon Sep 17 00:00:00 2001 From: Aarushi11H <67260600+Aarushi11H@users.noreply.github.com> Date: Sun, 13 Dec 2020 21:36:02 +0530 Subject: [PATCH 1/5] ford fulkerson --- cpp/Graphs/Ford_Fulkerson_Algorithm.cpp | 112 ++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 cpp/Graphs/Ford_Fulkerson_Algorithm.cpp diff --git a/cpp/Graphs/Ford_Fulkerson_Algorithm.cpp b/cpp/Graphs/Ford_Fulkerson_Algorithm.cpp new file mode 100644 index 000000000..f9d61eb02 --- /dev/null +++ b/cpp/Graphs/Ford_Fulkerson_Algorithm.cpp @@ -0,0 +1,112 @@ + +#include +#include +#include +#include +using namespace std; + +// Number of vertices in given graph +#define V 6 + +/* Returns true if there is a path from source 's' to sink 't' in + residual graph. Also fills parent[] to store the path */ +bool bfs(int rGraph[V][V], int s, int t, int parent[]) +{ + // Create a visited array and mark all vertices as not visited + bool visited[V]; + memset(visited, 0, sizeof(visited)); + + // Create a queue, enqueue source vertex and mark source vertex + // as visited + queue q; + q.push(s); + visited[s] = true; + parent[s] = -1; + + // Standard BFS Loop + while (!q.empty()) + { + int u = q.front(); + q.pop(); + + for (int v=0; v 0) + { + q.push(v); + parent[v] = u; + visited[v] = true; + } + } + } + + // If we reached sink in BFS starting from source, then return + // true, else false + return (visited[t] == true); +} + +// Returns the maximum flow from s to t in the given graph +int fordFulkerson(int graph[V][V], int s, int t) +{ + int u, v; + + int rGraph[V][V]; // Residual graph where rGraph[i][j] indicates + // residual capacity of edge from i to j (if there + // is an edge. If rGraph[i][j] is 0, then there is not) + for (u = 0; u < V; u++) + for (v = 0; v < V; v++) + rGraph[u][v] = graph[u][v]; + + int parent[V]; // This array is filled by BFS and to store path + + int max_flow = 0; // There is no flow initially + + // Augment the flow while there is path from source to sink + while (bfs(rGraph, s, t, parent)) + { + // Find minimum residual capacity of the edges along the + // path filled by BFS. Or we can say find the maximum flow + // through the path found. + int path_flow = INT_MAX; + for (v=t; v!=s; v=parent[v]) + { + u = parent[v]; + path_flow = min(path_flow, rGraph[u][v]); + } + + // update residual capacities of the edges and reverse edges + // along the path + for (v=t; v != s; v=parent[v]) + { + u = parent[v]; + rGraph[u][v] -= path_flow; + rGraph[v][u] += path_flow; + } + + // Add path flow to overall flow + max_flow += path_flow; + } + + // Return the overall flow + return max_flow; +} + + +int main() +{ + // Creating a graph with 6 vertices as defined earlier. + //to change the vertices value, change it in the #define V + //may change the edge values here + int graph[V][V] = { {0, 16, 13, 0, 0, 0}, + {0, 0, 10, 12, 0, 0}, + {0, 4, 0, 0, 14, 0}, + {0, 0, 9, 0, 0, 20}, + {0, 0, 0, 7, 0, 4}, + {0, 0, 0, 0, 0, 0} + }; + + + cout << "The maximum possible flow is " << fordFulkerson(graph, 0, 5); + + return 0; +} From 2dcea7233be9b4bc2fff47e7aa7629f9dea3a360 Mon Sep 17 00:00:00 2001 From: Aarushi11H <67260600+Aarushi11H@users.noreply.github.com> Date: Sun, 13 Dec 2020 21:36:51 +0530 Subject: [PATCH 2/5] Johnson's algo --- cpp/Graphs/Johnson's_Algorithm.cpp | 38 ++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 cpp/Graphs/Johnson's_Algorithm.cpp diff --git a/cpp/Graphs/Johnson's_Algorithm.cpp b/cpp/Graphs/Johnson's_Algorithm.cpp new file mode 100644 index 000000000..a6379708c --- /dev/null +++ b/cpp/Graphs/Johnson's_Algorithm.cpp @@ -0,0 +1,38 @@ +#include +#define INF 9999 +using namespace std; +int min(int a, int b); +int cost[10][10], adj[10][10]; +inline int min(int a, int b){ + return (a> vert; + cout << "Enter no of edges: "; + cin >> edge; + cout << "Enter the EDGE Costs:\n"; + for (k = 1; k <= edge; k++) { + cin >> i >> j >> c; + adj[i][j] = cost[i][j] = c; + } + for (i = 1; i <= vert; i++) + for (j = 1; j <= vert; j++) { + if (adj[i][j] == 0 && i != j) + adj[i][j] = INF; //if there is no edge, put infinity + } + for (k = 1; k <= vert; k++) + for (i = 1; i <= vert; i++) + for (j = 1; j <= vert; j++) + adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]); + cout << "Resultant adj matrix\n"; + for (i = 1; i <= vert; i++) { + for (j = 1; j <= vert; j++) { + if (adj[i][j] != INF) + cout << adj[i][j] << " "; + } + cout << "\n"; + } +} + From 89a61f998dcdc5999520c5914611b8cd8a80054a Mon Sep 17 00:00:00 2001 From: Aarushi11H <67260600+Aarushi11H@users.noreply.github.com> Date: Sun, 13 Dec 2020 21:37:23 +0530 Subject: [PATCH 3/5] johnson --- cpp/Graphs/Johnson's_Algorithm.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/cpp/Graphs/Johnson's_Algorithm.cpp b/cpp/Graphs/Johnson's_Algorithm.cpp index a6379708c..b160ca8bc 100644 --- a/cpp/Graphs/Johnson's_Algorithm.cpp +++ b/cpp/Graphs/Johnson's_Algorithm.cpp @@ -35,4 +35,19 @@ main() { cout << "\n"; } } +/* Example input and output +Enter no of vertices: 3 +Enter no of edges: 5 +Enter the EDGE Cost: +1 2 4 +2 1 6 +1 3 11 +3 1 3 +2 3 2 +Resultant adj matrix +0 4 6 +5 0 2 +3 7 0 +*/ + From f8b7c8f44ff0ca2e2a9c38f96498d43c69537821 Mon Sep 17 00:00:00 2001 From: Aarushi11H <67260600+Aarushi11H@users.noreply.github.com> Date: Sun, 13 Dec 2020 21:37:50 +0530 Subject: [PATCH 4/5] Cycle detection --- cpp/Graphs/Cycle_Detection_in_DAG.cpp | 66 +++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 cpp/Graphs/Cycle_Detection_in_DAG.cpp diff --git a/cpp/Graphs/Cycle_Detection_in_DAG.cpp b/cpp/Graphs/Cycle_Detection_in_DAG.cpp new file mode 100644 index 000000000..9fe305812 --- /dev/null +++ b/cpp/Graphs/Cycle_Detection_in_DAG.cpp @@ -0,0 +1,66 @@ +#include +#include +#define NODE 5 +using namespace std; + +//DIRECTED ACYCLIC GRAPH ( A directed graph with no self cycle) +int graph[NODE][NODE] = { + {0, 1, 1, 0, 0}, + {0, 0, 0, 0, 0}, + {0, 0, 0, 1, 0}, + {0, 1, 0, 0, 0}, + {0, 1, 0, 1, 0} +}; + +/* + 1 -> 2 ,3 + 3 -> 4 + 4 -> 2 + 5 -> 2,4 +*/ + +bool dfs(int curr, set&wSet, set&gSet, set&bSet) { + //moving curr to white set to grey set. + wSet.erase(wSet.find(curr)); + gSet.insert(curr); + + for(int v = 0; v < NODE; v++) { + if(graph[curr][v] != 0) { //for all neighbour vertices + if(bSet.find(v) != bSet.end()) + continue; //if the vertices are in the black set + if(gSet.find(v) != gSet.end()) + return true; //it is a cycle + if(dfs(v, wSet, gSet, bSet)) + return true; //cycle found + } + } + + //moving v to grey set to black set. + gSet.erase(gSet.find(curr)); + bSet.insert(curr); + return false; +} + +bool hasCycle() { + set wSet, gSet, bSet; //three set as white, grey and black + for(int i = 0; i 0) { + for(int current = 0; current < NODE; current++) { + if(wSet.find(current) != wSet.end()) + if(dfs(current, wSet, gSet, bSet)) + return true; + } + } + return false; +} + +int main() { + bool res; + res = hasCycle(); + if(res) + cout << "The graph has cycle." << endl; + else + cout << "The graph has no cycle." << endl; +} From 4e61e4387e71e8f58e4fd18353a625665464b28f Mon Sep 17 00:00:00 2001 From: Aarushi11H <67260600+Aarushi11H@users.noreply.github.com> Date: Sun, 13 Dec 2020 21:38:55 +0530 Subject: [PATCH 5/5] Kosaraju's algo --- cpp/Graphs/Kosaraju's_Algorithm.cpp | 113 ++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 cpp/Graphs/Kosaraju's_Algorithm.cpp diff --git a/cpp/Graphs/Kosaraju's_Algorithm.cpp b/cpp/Graphs/Kosaraju's_Algorithm.cpp new file mode 100644 index 000000000..59c041ad9 --- /dev/null +++ b/cpp/Graphs/Kosaraju's_Algorithm.cpp @@ -0,0 +1,113 @@ +// Kosaraju's algorithm to find strongly connected components in C++ + +#include +#include +#include + +using namespace std; + +class Graph { + int V; + list *adj; + /* Fills Stack with vertices (in increasing order of finishing + times). The top element of stack has the maximum finishing + */ + void fillOrder(int s, bool visitedV[], stack &Stack); + // A recursive function to print DFS starting from v + void DFS(int s, bool visitedV[]); + + public: + Graph(int V); + void addEdge(int s, int d); + void printSCC(); + Graph transpose(); +}; + +Graph::Graph(int V) { + this->V = V; + adj = new list[V]; +} + +// DFS +void Graph::DFS(int s, bool visitedV[]) { + visitedV[s] = true; + cout << s << " "; + + list::iterator i; + for (i = adj[s].begin(); i != adj[s].end(); ++i) + if (!visitedV[*i]) + DFS(*i, visitedV); +} + +// Transpose +Graph Graph::transpose() { + Graph g(V); + for (int s = 0; s < V; s++) { + // Recur for all the vertices adjacent to this vertex + list::iterator i; + for (i = adj[s].begin(); i != adj[s].end(); ++i) { + g.adj[*i].push_back(s); + } + } + return g; +} + +// Add edge into the graph +void Graph::addEdge(int s, int d) { + adj[s].push_back(d); +} + +void Graph::fillOrder(int s, bool visitedV[], stack &Stack) { + visitedV[s] = true; + // Recur for all the vertices adjacent to this vertex + list::iterator i; + for (i = adj[s].begin(); i != adj[s].end(); ++i) + if (!visitedV[*i]) + fillOrder(*i, visitedV, Stack); + + Stack.push(s); +} + +// Print strongly connected component +void Graph::printSCC() { + stack Stack; + + bool *visitedV = new bool[V]; + for (int i = 0; i < V; i++) + visitedV[i] = false; + + for (int i = 0; i < V; i++) + if (visitedV[i] == false) + fillOrder(i, visitedV, Stack); + + Graph gr = transpose(); + + for (int i = 0; i < V; i++) + visitedV[i] = false; + + while (Stack.empty() == false) { + int s = Stack.top(); + Stack.pop(); + + if (visitedV[s] == false) { + gr.DFS(s, visitedV); + cout << endl; + } + } +} + +int main() { + Graph g(8); + g.addEdge(0, 1); + g.addEdge(1, 2); + g.addEdge(2, 3); + g.addEdge(2, 4); + g.addEdge(3, 0); + g.addEdge(4, 5); + g.addEdge(5, 6); + g.addEdge(6, 4); + g.addEdge(6, 7); + + cout << "Strongly Connected Components through Kosaraju's Algorithm:\n"; + g.printSCC(); +}