Skip to content

Commit

Permalink
Day-36: BFS- Graph and bit manipulation prob
Browse files Browse the repository at this point in the history
  • Loading branch information
mandliya committed Sep 21, 2015
1 parent 649bc93 commit 6032297
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 92 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

| Current Status| Stats |
| :------------: | :----------: |
| Total Problems | 54 |
| Current Streak | 35 |
| Longest Streak | 35 ( August 17, 2015 - September 20, 2015 ) |
| Total Problems | 56 |
| Current Streak | 36 |
| Longest Streak | 36 ( August 17, 2015 - September 21, 2015 ) |

</center>

Expand Down Expand Up @@ -60,6 +60,7 @@ Include contains single header implementation of data structures and some algori
|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)|
| Given two integers, determine if their sum would be interger overflow.| [integerOverflow.cpp](bit_manipulation/integerOverflow.cpp)|
| How many bit flip operation would require to convert number A to B. | [countNumberOfBitFlips.cpp](bit_manipulation/countNumberOfBitFlips.cpp)|
| 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)|

### Cracking the coding interview problems
| Problem | Solution |
Expand Down Expand Up @@ -109,3 +110,4 @@ Include contains single header implementation of data structures and some algori
| Problem | Solution |
| :------------ | :----------: |
| Depth First Traversal of a Graph | [dfsDemo.cpp](graph_problems/dfsDemo.cpp) |
| Breadth First Traversal of a Graph | [bfsDemo.cpp](graph_problems/bfsDemo.cpp) |
46 changes: 46 additions & 0 deletions bit_manipulation/swapSetOfBits.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Problem - Given a number 'num' and two positions (p1 and p2 from right side) in binary representation of num,
* write a function that swaps n bits at given two positions and returns the result.
* It is also given that the two sets of bits do not overlap.
*/

#include <iostream>

int swapBits(unsigned int num, unsigned int p1,
unsigned int p2, unsigned int n )
{
//Step1 lets form a number set1 by moving n bits at position p1 to the rightmost
unsigned int set1 = (num >> p1 ) & ((1U << n) - 1);
// Lets understand what we just did.
// Part1 : (num >> p1), we just right shifted num so that bit at p1 position takes 0th postion.
// Part2 : Remember we needed n bits from position p1(which has become position 0)
// So, (1U << n) moves 1 to nth bit, i.e. it the value formed by this movement is 2^n
// Now, If we substract 1 from the (2^n), it will give us all 1's for n positions.
// For example (1U << 3) = 1000
// and (1U << 3) - 1 = 0111
// Part3 : Thus Part1 & Part2 will give as n bits which were at P1 (now moved to 0)

//similarly for p2
unsigned int set2 = (num >> p2) & ((1U << n) - 1);

// xor two sets ( we are doing similar to xor swap algorithm )
// https://en.wikipedia.org/wiki/XOR_swap_algorithm
unsigned int xorSets = set1 ^ set2;

// now moving back the xor'd sets to p1 and p2
xorSets = (xorSets << p1) | (xorSets << p2);

unsigned int finalVal = xorSets ^ num;
return finalVal;
}

int main()
{
std::cout << "Swaping bits in number 28, such that 2 bits starting from 0th bit and 2 bits "
"starting from 3rd bit are swapped, 28 becomes " << swapBits(28, 0, 3, 2)
<< std::endl;

std::cout << "Swaping bits in number 47, such that 3 bits starting from 1st bit(0 based counting) and 3 bits "
"starting from 5th bit(0 based counting) are swapped, 47 becomes " << swapBits(47, 1, 5, 3)
<< std::endl;
}
29 changes: 29 additions & 0 deletions graph_problems/bfsDemo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <iostream>
#include "graph.h"

/**
* Implementation of BFS algorithm is in ../include/graph.h
*/

int main()
{
std::vector<int> vec{ 0, 1, 2, 3, 4, 5, 6, 7 };
algo::Graph<int> g(vec);
g.setEdge(0, 1);
g.setEdge(0, 2);
g.setEdge(1, 0);
g.setEdge(1, 2);
g.setEdge(1, 3);
g.setEdge(2, 4);
g.setEdge(2, 6);
g.setEdge(3, 1);
g.setEdge(3, 7);
g.setEdge(4, 6);
g.setEdge(5, 1);
g.setEdge(5, 3);
g.setEdge(6, 5);
g.setEdge(7, 5);
g.display();
g.breadth_first_search(0);
return 0;
}
7 changes: 6 additions & 1 deletion graph_problems/dfsDemo.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#include <iostream>
#include <graph.h>
#include "graph.h"


/**
* Implementation of BFS algorithm is in ../include/graph.h
*/

int main()
{
Expand Down
Loading

0 comments on commit 6032297

Please sign in to comment.