Skip to content

Commit

Permalink
Merge pull request #430 from Aarushi11H/aarushi
Browse files Browse the repository at this point in the history
graph algos cpp
  • Loading branch information
geekquad authored Dec 13, 2020
2 parents f70de27 + 4e61e43 commit e7c54be
Show file tree
Hide file tree
Showing 4 changed files with 344 additions and 0 deletions.
66 changes: 66 additions & 0 deletions cpp/Graphs/Cycle_Detection_in_DAG.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include<iostream>
#include<set>
#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<int>&wSet, set<int>&gSet, set<int>&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<int> wSet, gSet, bSet; //three set as white, grey and black
for(int i = 0; i<NODE; i++)
wSet.insert(i); //initially add all node into the white set

while(wSet.size() > 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;
}
112 changes: 112 additions & 0 deletions cpp/Graphs/Ford_Fulkerson_Algorithm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@

#include <iostream>
#include <limits.h>
#include <string.h>
#include <queue>
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 <int> 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<V; v++)
{
if (visited[v]==false && rGraph[u][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 <your vertex value>
//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;
}
53 changes: 53 additions & 0 deletions cpp/Graphs/Johnson's_Algorithm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include<iostream>
#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<b)?a:b;
}
main() {
int vert, edge, i, j, k, c;
cout << "Enter no of vertices: ";
cin >> 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";
}
}
/* 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
*/


113 changes: 113 additions & 0 deletions cpp/Graphs/Kosaraju's_Algorithm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Kosaraju's algorithm to find strongly connected components in C++

#include <iostream>
#include <list>
#include <stack>

using namespace std;

class Graph {
int V;
list<int> *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<int> &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<int>[V];
}

// DFS
void Graph::DFS(int s, bool visitedV[]) {
visitedV[s] = true;
cout << s << " ";

list<int>::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<int>::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<int> &Stack) {
visitedV[s] = true;
// Recur for all the vertices adjacent to this vertex
list<int>::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<int> 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();
}

0 comments on commit e7c54be

Please sign in to comment.