Skip to content

Commit 6032297

Browse files
committed
Day-36: BFS- Graph and bit manipulation prob
1 parent 649bc93 commit 6032297

File tree

5 files changed

+251
-92
lines changed

5 files changed

+251
-92
lines changed

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
| Current Status| Stats |
66
| :------------: | :----------: |
7-
| Total Problems | 54 |
8-
| Current Streak | 35 |
9-
| Longest Streak | 35 ( August 17, 2015 - September 20, 2015 ) |
7+
| Total Problems | 56 |
8+
| Current Streak | 36 |
9+
| Longest Streak | 36 ( August 17, 2015 - September 21, 2015 ) |
1010

1111
</center>
1212

@@ -60,6 +60,7 @@ Include contains single header implementation of data structures and some algori
6060
|Given a vector of numbers, only one number occurs odd number of times, find the number.| [find_odd_one_out.cpp](bit_manipulation/find_odd_one_out.cpp)|
6161
| Given two integers, determine if their sum would be interger overflow.| [integerOverflow.cpp](bit_manipulation/integerOverflow.cpp)|
6262
| How many bit flip operation would require to convert number A to B. | [countNumberOfBitFlips.cpp](bit_manipulation/countNumberOfBitFlips.cpp)|
63+
| Given a number x and two positions (from right side) in binary representation of x, write a function that swaps n right bits at given two positions and returns the result. It is also given that the two sets of bits do not overlap.|[swapSetOfBits.cpp](bit_manipulation/swapSetOfBits.cpp)|
6364

6465
### Cracking the coding interview problems
6566
| Problem | Solution |
@@ -109,3 +110,4 @@ Include contains single header implementation of data structures and some algori
109110
| Problem | Solution |
110111
| :------------ | :----------: |
111112
| Depth First Traversal of a Graph | [dfsDemo.cpp](graph_problems/dfsDemo.cpp) |
113+
| Breadth First Traversal of a Graph | [bfsDemo.cpp](graph_problems/bfsDemo.cpp) |

bit_manipulation/swapSetOfBits.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Problem - Given a number 'num' and two positions (p1 and p2 from right side) in binary representation of num,
3+
* write a function that swaps n bits at given two positions and returns the result.
4+
* It is also given that the two sets of bits do not overlap.
5+
*/
6+
7+
#include <iostream>
8+
9+
int swapBits(unsigned int num, unsigned int p1,
10+
unsigned int p2, unsigned int n )
11+
{
12+
//Step1 lets form a number set1 by moving n bits at position p1 to the rightmost
13+
unsigned int set1 = (num >> p1 ) & ((1U << n) - 1);
14+
// Lets understand what we just did.
15+
// Part1 : (num >> p1), we just right shifted num so that bit at p1 position takes 0th postion.
16+
// Part2 : Remember we needed n bits from position p1(which has become position 0)
17+
// So, (1U << n) moves 1 to nth bit, i.e. it the value formed by this movement is 2^n
18+
// Now, If we substract 1 from the (2^n), it will give us all 1's for n positions.
19+
// For example (1U << 3) = 1000
20+
// and (1U << 3) - 1 = 0111
21+
// Part3 : Thus Part1 & Part2 will give as n bits which were at P1 (now moved to 0)
22+
23+
//similarly for p2
24+
unsigned int set2 = (num >> p2) & ((1U << n) - 1);
25+
26+
// xor two sets ( we are doing similar to xor swap algorithm )
27+
// https://en.wikipedia.org/wiki/XOR_swap_algorithm
28+
unsigned int xorSets = set1 ^ set2;
29+
30+
// now moving back the xor'd sets to p1 and p2
31+
xorSets = (xorSets << p1) | (xorSets << p2);
32+
33+
unsigned int finalVal = xorSets ^ num;
34+
return finalVal;
35+
}
36+
37+
int main()
38+
{
39+
std::cout << "Swaping bits in number 28, such that 2 bits starting from 0th bit and 2 bits "
40+
"starting from 3rd bit are swapped, 28 becomes " << swapBits(28, 0, 3, 2)
41+
<< std::endl;
42+
43+
std::cout << "Swaping bits in number 47, such that 3 bits starting from 1st bit(0 based counting) and 3 bits "
44+
"starting from 5th bit(0 based counting) are swapped, 47 becomes " << swapBits(47, 1, 5, 3)
45+
<< std::endl;
46+
}

graph_problems/bfsDemo.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <iostream>
2+
#include "graph.h"
3+
4+
/**
5+
* Implementation of BFS algorithm is in ../include/graph.h
6+
*/
7+
8+
int main()
9+
{
10+
std::vector<int> vec{ 0, 1, 2, 3, 4, 5, 6, 7 };
11+
algo::Graph<int> g(vec);
12+
g.setEdge(0, 1);
13+
g.setEdge(0, 2);
14+
g.setEdge(1, 0);
15+
g.setEdge(1, 2);
16+
g.setEdge(1, 3);
17+
g.setEdge(2, 4);
18+
g.setEdge(2, 6);
19+
g.setEdge(3, 1);
20+
g.setEdge(3, 7);
21+
g.setEdge(4, 6);
22+
g.setEdge(5, 1);
23+
g.setEdge(5, 3);
24+
g.setEdge(6, 5);
25+
g.setEdge(7, 5);
26+
g.display();
27+
g.breadth_first_search(0);
28+
return 0;
29+
}

graph_problems/dfsDemo.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#include <iostream>
2-
#include <graph.h>
2+
#include "graph.h"
3+
4+
5+
/**
6+
* Implementation of BFS algorithm is in ../include/graph.h
7+
*/
38

49
int main()
510
{

0 commit comments

Comments
 (0)