Skip to content

Commit

Permalink
graph completed
Browse files Browse the repository at this point in the history
  • Loading branch information
viren-sureja committed Jul 12, 2021
1 parent 2c0a8e8 commit af6bad0
Show file tree
Hide file tree
Showing 48 changed files with 4,451 additions and 0 deletions.
59 changes: 59 additions & 0 deletions 12_graph/01_create_and_print_graph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
link: https://www.javatpoint.com/graph-theory-graph-representations#:~:text=In%20graph%20theory%2C%20a%20graph,to%20it%20by%20an%20edge).
link: https://onedrive.live.com/?authkey=%21AJrTq%5FU8BPKIWDk&cid=842AECBB531CCEA4&id=842AECBB531CCEA4%211179&parId=842AECBB531CCEA4%211164&o=OneUp
*/


// ----------------------------------------------------------------------------------------------------------------------- //
/*
using adjacency matrix:
TC: O(1) => for searching
SC: O(N^2)
*/

int adjMatrix() {
int rows, cols;
cin >> rows >> cols;

int adj[rows + 1][rows + 1];

for (int i = 0;i < rows;i++) {
int u, v;
cin >> u >> v;
adj[u][v] = 1;
adj[v][u] = 1;
}
}





// ----------------------------------------------------------------------------------------------------------------------- //
/*
using adjacency list
TC: O(Edges of that vertex)
SC: O(N + (2*E))
*/

int adjList() {
int rows, cols;
cin >> rows >> cols;

vector<int> adj(rows + 1);

for (int i = 0;i < m;i++) {
int u, v;
cin >> u >> v;

adj[u].push_back(v);
adj[v].push_back(u); // comment this line for directed graph

// for weighted graph use: adj[u].push_back({v, wt});
// adj[v].push_back({u, wt});
}
}
90 changes: 90 additions & 0 deletions 12_graph/02_implement_BFS_algorithm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
link: https://practice.geeksforgeeks.org/problems/bfs-traversal-of-graph/1
sol: https://www.geeksforgeeks.org/breadth-first-search-or-bfs-for-a-graph/
video: https://youtu.be/UeE67iCK2lQ?list=PLgUwDviBIf0rGEWe64KWas0Nryn7SCRWw
travel adjacent node first
than move to the next node
The Time complexity of BFS is O(V + E) when Adjacency List is used
and O(V^2) when Adjacency Matrix is used,
where V stands for vertices and E stands for edges.
as it will go to every vertex as well as it will check for corresponding edges
hence TC becomes V+E
*/



// ----------------------------------------------------------------------------------------------------------------------- //
/*
for connected component
*/
vector<int> bfsOfGraph(int V, vector<int> adj[])
{
vector<bool> vis(V, false);
vector<int> ans;

queue<int> q;
q.push(0);
vis[0] = 1;

while (!q.empty()) {
int curr = q.front();
q.pop();
ans.push_back(curr);

for (auto j : adj[curr]) {
if (!vis[j]) {
q.push(j);
vis[j] = 1;
}
}
}

return ans;
}





// ----------------------------------------------------------------------------------------------------------------------- //
/*
for disconnected component
TC: O(N + E) => visiting every node once
SC: O(N + E) => space for every node and vice-verca for both edge
*/

vector<int> bfsOfGraph(int V, vector<int> adj[])
{
vector<bool> vis(V, false);
vector<int> ans;

for (int i = 0;i < V;i++) {
if (!vis[i]) {
int i = 0;
queue<int> q;
q.push(i);
vis[i] = 1;

while (!q.empty()) {
int curr = q.front();
q.pop();
ans.push_back(curr);

for (auto j : adj[curr]) {
if (!vis[j]) {
q.push(j);
vis[j] = 1;
}
}
}
}

return ans;
}
}
174 changes: 174 additions & 0 deletions 12_graph/03_implement_DFS_algorithm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
link: https://practice.geeksforgeeks.org/problems/depth-first-traversal-for-a-graph/1
sol: https://www.geeksforgeeks.org/depth-first-search-or-dfs-for-a-graph/
video: https://youtu.be/uDWljP2PGmU?list=PLgUwDviBIf0rGEWe64KWas0Nryn7SCRWw
*/


// ----------------------------------------------------------------------------------------------------------------------- //
/*
[Accepted]
TC: O(V + E)
SC: O(V + E)
*/
void dfs(int curr, vector<int> adj[], vector<int>& vis, vector<int>& ans) {
ans.push_back(curr);
vis[curr] = 1;

for (auto i : adj[curr]) {
if (!vis[i]) dfs(i, adj, vis, ans);
}
}

vector<int>dfsOfGraph(int V, vector<int> adj[])
{
vector<int> vis(V, 0);
vector<int> ans;

for (int i = 0;i < V;i++) {
if (!vis[i]) {
dfs(i, adj, vis, ans);
}
}
return ans;
}




// ----------------------------------------------------------------------------------------------------------------------- //
/*
for connected component
TC: O(V + E)
SC: O(V + E)
*/

// Graph class represents a directed graph
// using adjacency list representation
class Graph
{
public:
map<int, bool> visited;
map<int, list<int>> adj;

// function to add an edge to graph
void addEdge(int v, int w);

// DFS traversal of the vertices
// reachable from v
void DFS(int v);
};

void Graph::addEdge(int v, int w)
{
adj[v].push_back(w); // Add w to v’s list.
}

void Graph::DFS(int v)
{
// Mark the current node as visited and
// print it
visited[v] = true;
cout << v << " ";

// Recur for all the vertices adjacent
// to this vertex
list<int>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i)
if (!visited[*i])
DFS(*i);
}

// Driver code
int main()
{
// Create a graph given in the above diagram
Graph g;
g.addEdge(0, 1);
g.addEdge(0, 9);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(9, 3);

cout << "Following is Depth First Traversal"
" (starting from vertex 2) \n";
g.DFS(2);

return 0;
}



// ----------------------------------------------------------------------------------------------------------------------- //
/*
for disconnected component
TC: O(V + E)
SC: O(V + E)
*/
class Graph {

// A function used by DFS
void DFSUtil(int v);

public:
map<int, bool> visited;
map<int, list<int>> adj;
// function to add an edge to graph
void addEdge(int v, int w);

// prints DFS traversal of the complete graph
void DFS();
};

void Graph::addEdge(int v, int w)
{
adj[v].push_back(w); // Add w to v’s list.
}

void Graph::DFSUtil(int v)
{
// Mark the current node as visited and print it
visited[v] = true;
cout << v << " ";

// Recur for all the vertices adjacent to this vertex
list<int>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i)
if (!visited[*i])
DFSUtil(*i);
}

// The function to do DFS traversal. It uses recursive
// DFSUtil()
void Graph::DFS()
{
// Call the recursive helper function to print DFS
// traversal starting from all vertices one by one
for (auto i : adj)
if (visited[i.first] == false)
DFSUtil(i.first);
}

// Driver Code
int main()
{
// Create a graph given in the above diagram
Graph g;
g.addEdge(0, 1);
g.addEdge(0, 9);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(9, 3);

cout << "Following is Depth First Traversal \n";
g.DFS();

return 0;
}
Loading

0 comments on commit af6bad0

Please sign in to comment.