Skip to content

Commit 4225bcd

Browse files
authored
Add files via upload
1 parent 477f15b commit 4225bcd

File tree

4 files changed

+390
-0
lines changed

4 files changed

+390
-0
lines changed

Graphs/BFS Graph.cpp

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Program to print BFS traversal from a given
2+
// source vertex. BFS(int s) traverses vertices
3+
// reachable from s.
4+
#include<iostream>
5+
#include <list>
6+
7+
using namespace std;
8+
9+
// This class represents a directed graph using
10+
// adjacency list representation
11+
class Graph
12+
{
13+
int V; // No. of vertices
14+
15+
// Pointer to an array containing adjacency
16+
// lists
17+
list<int> *adj;
18+
public:
19+
Graph(int V); // Constructor
20+
21+
// function to add an edge to graph
22+
void addEdge(int v, int w);
23+
24+
// prints BFS traversal from a given source s
25+
void BFS(int s);
26+
};
27+
28+
Graph::Graph(int V)
29+
{
30+
this->V = V;
31+
adj = new list<int>[V];
32+
}
33+
34+
void Graph::addEdge(int v, int w)
35+
{
36+
adj[v].push_back(w); // Add w to v’s list.
37+
}
38+
39+
void Graph::BFS(int s)
40+
{
41+
// Mark all the vertices as not visited
42+
bool *visited = new bool[V];
43+
for(int i = 0; i < V; i++)
44+
visited[i] = false;
45+
46+
// Create a queue for BFS
47+
list<int> queue;
48+
49+
// Mark the current node as visited and enqueue it
50+
visited[s] = true;
51+
queue.push_back(s);
52+
53+
// 'i' will be used to get all adjacent
54+
// vertices of a vertex
55+
list<int>::iterator i;
56+
57+
while(!queue.empty())
58+
{
59+
// Dequeue a vertex from queue and print it
60+
s = queue.front();
61+
cout << s << " ";
62+
queue.pop_front();
63+
64+
// Get all adjacent vertices of the dequeued
65+
// vertex s. If a adjacent has not been visited,
66+
// then mark it visited and enqueue it
67+
for (i = adj[s].begin(); i != adj[s].end(); ++i)
68+
{
69+
if (!visited[*i])
70+
{
71+
visited[*i] = true;
72+
queue.push_back(*i);
73+
}
74+
}
75+
}
76+
}
77+
78+
// Driver program to test methods of graph class
79+
int main()
80+
{
81+
// Create a graph given in the above diagram
82+
Graph g(4);
83+
g.addEdge(0, 1);
84+
g.addEdge(0, 2);
85+
g.addEdge(1, 2);
86+
g.addEdge(2, 0);
87+
g.addEdge(2, 3);
88+
g.addEdge(3, 3);
89+
90+
cout << "Following is Breadth First Traversal "
91+
<< "(starting from vertex 2) \n";
92+
g.BFS(2);
93+
94+
return 0;
95+
}
96+

Graphs/DFS Graph.cpp

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// C++ program to print DFS traversal from
2+
// a given vertex in a given graph
3+
#include<bits/stdc++.h>
4+
using namespace std;
5+
6+
// Graph class represents a directed graph
7+
// using adjacency list representation
8+
class Graph
9+
{
10+
int V; // No. of vertices
11+
12+
// Pointer to an array containing
13+
// adjacency lists
14+
list<int> *adj;
15+
16+
// A recursive function used by DFS
17+
void DFSUtil(int v, bool visited[]);
18+
public:
19+
Graph(int V); // Constructor
20+
21+
// function to add an edge to graph
22+
void addEdge(int v, int w);
23+
24+
// DFS traversal of the vertices
25+
// reachable from v
26+
void DFS(int v);
27+
};
28+
29+
Graph::Graph(int V)
30+
{
31+
this->V = V;
32+
adj = new list<int>[V];
33+
}
34+
35+
void Graph::addEdge(int v, int w)
36+
{
37+
adj[v].push_back(w); // Add w to v’s list.
38+
}
39+
40+
void Graph::DFSUtil(int v, bool visited[])
41+
{
42+
// Mark the current node as visited and
43+
// print it
44+
visited[v] = true;
45+
cout << v << " ";
46+
47+
// Recur for all the vertices adjacent
48+
// to this vertex
49+
list<int>::iterator i;
50+
for (i = adj[v].begin(); i != adj[v].end(); ++i)
51+
if (!visited[*i])
52+
DFSUtil(*i, visited);
53+
}
54+
55+
// DFS traversal of the vertices reachable from v.
56+
// It uses recursive DFSUtil()
57+
void Graph::DFS(int v)
58+
{
59+
// Mark all the vertices as not visited
60+
bool *visited = new bool[V];
61+
for (int i = 0; i < V; i++)
62+
visited[i] = false;
63+
64+
// Call the recursive helper function
65+
// to print DFS traversal
66+
DFSUtil(v, visited);
67+
}
68+
69+
// Driver code
70+
int main()
71+
{
72+
// Create a graph given in the above diagram
73+
Graph g(4);
74+
g.addEdge(0, 1);
75+
g.addEdge(0, 2);
76+
g.addEdge(1, 2);
77+
g.addEdge(2, 0);
78+
g.addEdge(2, 3);
79+
g.addEdge(3, 3);
80+
81+
cout << "Following is Depth First Traversal"
82+
" (starting from vertex 2) \n";
83+
g.DFS(2);
84+
85+
return 0;
86+
}
87+
+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#include <iostream>
2+
#include <vector>
3+
using namespace std;
4+
5+
// data structure to store graph edges
6+
struct Edge {
7+
int src, dest;
8+
};
9+
10+
// class to represent a graph object
11+
class Graph
12+
{
13+
public:
14+
// An array of vectors to represent adjacency list
15+
vector<int> *adjList;
16+
17+
// Constructor
18+
Graph(vector<Edge> const &edges, int N)
19+
{
20+
// allocate memory
21+
adjList = new vector<int>[N];
22+
23+
// add edges to the undirected graph
24+
for (unsigned i = 0; i < edges.size(); i++)
25+
{
26+
int src = edges[i].src;
27+
int dest = edges[i].dest;
28+
29+
adjList[src].push_back(dest);
30+
adjList[dest].push_back(src);
31+
}
32+
}
33+
};
34+
35+
void printAllHamiltonianPaths(Graph const& g, int v, vector<bool>
36+
visited, vector<int> &path, int N)
37+
{
38+
// if all the vertices are visited, then
39+
// Hamiltonian path exists
40+
if (path.size() == N)
41+
{
42+
// print Hamiltonian path
43+
for (int i : path)
44+
cout << i << " ";
45+
cout << endl;
46+
47+
return;
48+
}
49+
50+
// Check if every edge starting from vertex v leads
51+
// to a solution or not
52+
for (int w : g.adjList[v])
53+
{
54+
// process only unvisited vertices as Hamiltonian
55+
// path visits each vertex exactly once
56+
if (!visited[w])
57+
{
58+
visited[w] = true;
59+
path.push_back(w);
60+
61+
// check if adding vertex w to the path leads
62+
// to solution or not
63+
printAllHamiltonianPaths(g, w, visited, path, N);
64+
65+
// Backtrack
66+
visited[w] = false;
67+
path.pop_back();
68+
}
69+
}
70+
}
71+
72+
// main function
73+
int main()
74+
{
75+
// consider complete graph having 4 vertices
76+
vector<Edge> edges =
77+
{
78+
{0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 3}, {2, 3}
79+
};
80+
81+
// starting node
82+
int start = 0;
83+
84+
// Number of vertices in the graph
85+
int N = 4;
86+
87+
// create a graph from edges
88+
Graph g(edges, N);
89+
90+
// add starting node to the path
91+
vector<int> path;
92+
path.push_back(start);
93+
94+
// mark start node as visited
95+
vector<bool> visited(N);
96+
visited[start] = true;
97+
98+
printAllHamiltonianPaths(g, start, visited, path, N);
99+
100+
return 0;
101+
}

Graphs/TwoCliqueProblem.cpp

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// C++ program to find out whether a given graph can be
2+
// converted to two Cliques or not.
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
const int V = 5;
7+
8+
// This function returns true if subgraph reachable from
9+
// src is Bipartite or not.
10+
bool isBipartiteUtil(int G[][V], int src, int colorArr[])
11+
{
12+
colorArr[src] = 1;
13+
14+
// Create a queue (FIFO) of vertex numbers and enqueue
15+
// source vertex for BFS traversal
16+
queue <int> q;
17+
q.push(src);
18+
19+
// Run while there are vertices in queue (Similar to BFS)
20+
while (!q.empty())
21+
{
22+
// Dequeue a vertex from queue
23+
int u = q.front();
24+
q.pop();
25+
26+
// Find all non-colored adjacent vertices
27+
for (int v = 0; v < V; ++v)
28+
{
29+
// An edge from u to v exists and destination
30+
// v is not colored
31+
if (G[u][v] && colorArr[v] == -1)
32+
{
33+
// Assign alternate color to this adjacent
34+
// v of u
35+
colorArr[v] = 1 - colorArr[u];
36+
q.push(v);
37+
}
38+
39+
// An edge from u to v exists and destination
40+
// v is colored with same color as u
41+
else if (G[u][v] && colorArr[v] == colorArr[u])
42+
return false;
43+
}
44+
}
45+
46+
// If we reach here, then all adjacent vertices can
47+
// be colored with alternate color
48+
return true;
49+
}
50+
51+
// Returns true if a Graph G[][] is Bipartite or not. Note
52+
// that G may not be connected.
53+
bool isBipartite(int G[][V])
54+
{
55+
// Create a color array to store colors assigned
56+
// to all veritces. Vertex number is used as index in
57+
// this array. The value '-1' of colorArr[i]
58+
// is used to indicate that no color is assigned to
59+
// vertex 'i'. The value 1 is used to indicate first
60+
// color is assigned and value 0 indicates
61+
// second color is assigned.
62+
int colorArr[V];
63+
for (int i = 0; i < V; ++i)
64+
colorArr[i] = -1;
65+
66+
// One by one check all not yet colored vertices.
67+
for (int i = 0; i < V; i++)
68+
if (colorArr[i] == -1)
69+
if (isBipartiteUtil(G, i, colorArr) == false)
70+
return false;
71+
72+
return true;
73+
}
74+
75+
// Returns true if G can be divided into
76+
// two Cliques, else false.
77+
bool canBeDividedinTwoCliques(int G[][V])
78+
{
79+
// Find complement of G[][]
80+
// All values are complemented except
81+
// diagonal ones
82+
int GC[V][V];
83+
for (int i=0; i<V; i++)
84+
for (int j=0; j<V; j++)
85+
GC[i][j] = (i != j)? !G[i][j] : 0;
86+
87+
// Return true if complement is Bipartite
88+
// else false.
89+
return isBipartite(GC);
90+
}
91+
92+
// Driver program to test above function
93+
int main()
94+
{
95+
int G[][V] = {{0, 1, 1, 1, 0},
96+
{1, 0, 1, 0, 0},
97+
{1, 1, 0, 0, 0},
98+
{0, 1, 0, 0, 1},
99+
{0, 0, 0, 1, 0}
100+
};
101+
102+
canBeDividedinTwoCliques(G) ? cout << "Yes" :
103+
cout << "No";
104+
return 0;
105+
}
106+

0 commit comments

Comments
 (0)