Skip to content

Commit 1c01fbc

Browse files
committed
Add BFS DFS notes
Added CLIQUED Solution Signed-off-by: inishchith <inishchith@gmail.com>
2 parents 5a75dde + eb65afc commit 1c01fbc

File tree

3 files changed

+152
-2
lines changed

3 files changed

+152
-2
lines changed

Algorithms/DFS_BFS.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
## Graph traversals
2+
3+
Graph traversal means visiting every vertex and edge exactly once in a well-defined order. While using certain graph algorithms, you must ensure that each vertex of the graph is visited exactly once.
4+
5+
During a traversal, it is important that you track which vertices have been visited. The most common way of tracking vertices is to mark them , most commonly done by mainting a `boolean array` .
6+
7+
|Nodes| A | B | C | D | E | F |
8+
|----------|:-------------:|------:|----------|:-------------:|------:|-----:|
9+
| Array | 0 | 1 | 1 | 0 | 0 | 0 |
10+
11+
> `BFS` and `DFS` are the two most commonly used graph traversal approach .
12+
13+
### Breadth First Search (BFS)
14+
15+
>As the name BFS suggests, you are required to traverse the graph breadthwise .
16+
17+
BFS is a traversing algorithm where you should start traversing from a selected node (source or starting node) and traverse the graph `layerwise` (i.e breadthwise) thus exploring the neighbour nodes (nodes which are directly connected to source node). You must then move towards the next-level neighbour nodes.
18+
19+
1. First move horizontally and visit all the nodes of the
20+
2. current layer and then Move to the next layer .
21+
22+
In this case , we use `queue` data structure to maintain the order of traversal of nodes so as to traverse breadthwise we add it's `unvisited` neighbours(vertices that are directly connected to it) to the queue and move on to the next node in the current layer (i.e breadth) .
23+
24+
```cpp
25+
BFS (G, s) //Where G is the graph and s is the source node
26+
let Q be queue.
27+
Q.enqueue( s ) //Inserting s in queue until all its neighbour vertices are marked.
28+
29+
mark s as visited.
30+
while ( Q is not empty)
31+
//Removing that vertex from queue,whose neighbour will be visited now
32+
v = Q.dequeue( )
33+
34+
//processing all the neighbours of v
35+
for all neighbours w of v in Graph G
36+
if w is not visited
37+
Q.enqueue( w ) //Stores w in Q to further visit its neighbour
38+
mark w as visited.
39+
```
40+
41+
### Depth First Search (DFS)
42+
43+
> DEPTH first search , as the name suggests ; we explore a particular node untill it's depth and then it's breadthwise neighbours .
44+
45+
The DFS algorithm is a recursive algorithm that uses the idea of backtracking. It involves exhaustive searches of all the nodes by going ahead, if possible, else by backtracking.
46+
47+
Here, the word backtrack means that when you are moving forward and there are no more nodes along the current path, you move backwards on the same path to find nodes to traverse. All the nodes will be visited on the current path till all the unvisited nodes have been traversed after which the next path will be selected.
48+
49+
This recursive nature of DFS can be implemented using stacks. The basic idea is as follows:
50+
Pick a starting node and push all its adjacent nodes into a stack.
51+
Pop a node from stack to select the next node to visit and push all its adjacent nodes into a stack.
52+
Repeat this process until the stack is empty. However, ensure that the nodes that are visited are marked. This will prevent you from visiting the same node more than once. If you do not mark the nodes that are visited and you visit the same node more than once, you may end up in an infinite loop.
53+
54+
```cpp
55+
DFS-iterative (G, s): //Where G is graph and s is source vertex
56+
let S be stack
57+
S.push( s ) //Inserting s in stack
58+
mark s as visited.
59+
while ( S is not empty):
60+
//Pop a vertex from stack to visit next
61+
v = S.top( )
62+
S.pop( )
63+
//Push all the neighbours of v in stack that are not visited
64+
for all neighbours w of v in Graph G:
65+
if w is not visited :
66+
S.push( w )
67+
mark w as visited
68+
```
69+
70+
[DFS BFS Algorithm](./DFS_BFS.cpp)
71+
72+
<p align="center">
73+
<img src = "http://www.stoimen.com/blog/wp-content/uploads/2012/09/1.-DFS-vs.-BFS.png">
74+
</p>
75+
76+
[FOOTNOTES](https://www.hackerearth.com/practice/algorithms/graphs/breadth-first-search/tutorial/)

Problems/CLIQUED.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
Problem Link : https://www.codechef.com/problems/CLIQUED
3+
*/
4+
5+
#include<iostream>
6+
#include<vector>
7+
#include<set>
8+
#define pi pair< long long int, long long int >
9+
#define llu long long int
10+
#define INF 1e18
11+
using namespace std;
12+
13+
int main()
14+
{
15+
llu t,n,k,x,m,s,i,j,a,b,c,curr;
16+
cin>>t;
17+
while(t--)
18+
{
19+
cin>>n>>k>>x>>m>>s;s--;
20+
vector <pi> roads[n+1];
21+
vector <pi>::iterator it;
22+
23+
for(i=0;i<k;i++)
24+
{
25+
roads[i].push_back(make_pair(x,n));
26+
roads[n].push_back(make_pair(x,i));
27+
}
28+
29+
for(i=0;i<m;i++)
30+
{
31+
cin>>a>>b>>c;
32+
a--;b--;
33+
34+
roads[a].push_back(make_pair(2*c,b));
35+
roads[b].push_back(make_pair(2*c,a));
36+
}
37+
38+
llu dist[n+1];
39+
bool visited[n+1];
40+
for(i=0;i<=n;i++)
41+
{
42+
dist[i]=INF;
43+
visited[i] = false;
44+
}
45+
dist[s]=0;
46+
set <pi> spt;
47+
spt.insert(make_pair(0,s));
48+
49+
while(!spt.empty())
50+
{
51+
curr = spt.begin()->second;
52+
spt.erase(spt.begin());
53+
54+
if(visited[curr] == false)
55+
{
56+
for(it=roads[curr].begin();it!=roads[curr].end();it++)
57+
{
58+
if(dist[curr]+it->first < dist[it->second])
59+
{
60+
spt.erase(make_pair(dist[it->second],it->second));
61+
dist[it->second]=dist[curr]+it->first;
62+
spt.insert(make_pair(dist[it->second],it->second));
63+
}
64+
}
65+
}
66+
}
67+
68+
for(i=0;i<n;i++) cout<<dist[i]/2<<" ";
69+
cout<<endl;
70+
}
71+
return 0;
72+
}
73+
74+

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ We hope it will help you understand graphs better and make it possible for you t
2222
* [Problems](./Problems)
2323
* [Discount on crackers](https://www.codechef.com/problems/ACM14KG3) | [Solution](https://github.com/KJSCE-Codecell/Graph-notes/blob/master/Problems/ACM14KG3.cpp)
2424
* [Skiing](https://www.codechef.com/problems/SKIING) | [Solution](https://github.com/KJSCE-Codecell/Graph-notes/blob/master/Problems/SKIING.cpp)
25-
* [fillmtr](https://www.codechef.com/problems/FILLMTR) | [Solution](./Problems/fillmtr.cpp)
26-
25+
* [Bear and Clique Distances](https://www.codechef.com/problems/CLIQUED) | [Solution](./Problems/CLIQUED.cpp)
26+
* [Fill The Matrix](https://www.codechef.com/problems/FILLMTR) | [Solution](./Problems/fillmtr.cpp)

0 commit comments

Comments
 (0)