Sunday, December 25, 2016

Greatest Common Divisor

Greatest common divisor:
       দুটি সংখ্যা a ও b যদি non-zero হয় তাহলে তাদের gcd হল largest common divisor a ও b হয়।যদি a এবং b non-zero হয় তাহলে,
                      1 ≤ gcd(a, b) ≤ min(|a|)


Gcd function এর কিছু elementary property:


gcd(a, 0)= |a|

gcd(a, ka)=|a|

gcd(0, 0)=0

gcd(an, bn)=n*gcd(a, b) where n ≥ 0

যদি n|ab এবং gcd(a, n)= 1 তাহলে n|b

যদি gcd(a, p) = 1 এবং gcd(b, p) = 1 হয় তাহলে gcd(ab, p) = 1


Euclidian algorithm:
    Euclid algorithm এর যে principal এর উপর কাজ করে তাহলো
       Gcd(a, b) = gcd (b, a%b)
         Base case হল: gcd(a, 0) = a


Algorithm implementation:




আমরা এটি প্রমান করতে পারি। কিন্তু এটি প্রমান করার আগে প্রথমে division theorem সম্পর্কে idea নিতে হবে।

Division theorem:
          যদি a একটি পূর্ণ সংখ্যা ও b একটি ধনাতম্ক পূর্ণ সংখ্যা হয় তাহলে আমরা এমন ২টি unique পূর্ণ সংখ্যা পাব k এবং r যেখানে,
                    a = k*b+r
            যেখানে k = a/b = ভাগফল
                       r = a mod b
     আমরা b|a লিখতে পারি যদি এবং কেবল যদি a mod b = 0 হয়।

Euclid algorithm এর প্রমানঃ

   ধরি, g = gcd(a, b)

         a = k*b+r [k হল k=a/b অধনাত্মক সংখ্যা ও r হল ভাগশেষ।]
যেহেতু g, a কে ভাগ করতে পারে তার মানে g, k*b+r নিঃশেষে ভাগ করতে পারে।

যেহেতু g, b কে নিঃশেষে ভাগ করতে পারে g, k*b কেও নিঃশেষে ভাগ করতে পারে।তার ফলে g r কেও ভাগ করতে পারে, তা না হলে k*b+r, b দ্বারা ভাগ করা যাবে না।

তাহলে আমরা প্রমান করলাম যে, g, b এবং r কে নিঃশেষে ভাগ করতে পারে।

          এখন ধরি,
             g’ = gcd(b, r)

     যেহেতু g’, b ও r উভয় কে নিঃশেষে ভাগ করতে পারে ত্রার মানে এটি k*b+r কে নিঃশেষে ভাগ করতে পারবে।

    তার মানে, g’,a কে নিঃশেষে ভাগ করতে পারবে।

আমরা contradiction এর মাধ্যমে প্রমান করতে 
             পারি যে, g = g’

অর্থাৎ gcd(a, b)= gcd(b, r) = gcd(b, a%b)


 কিছু Property of GCD function:


1) Communicative Law : GCD(a,b) = GCD(b,a)

2) Associative Law : GCD(a,GCD(b,c)) = GCD(GCD(a,b),c) 

3)  GCD(a,b,c) = GCD(GCD(a,b),c) 


Things to remember:

1) GCD(4,-2) returns -2 but correct answer should be 2 . To get the correct value we need to send the absolute value of the inputs to the algorithm or use the absolute value of the return value.

.2) GCD(0,0)=0 but the algo will try to do 0%0 for that we might 

get RTE. We need to take care of this manually.

Common Divisors



Common divisor: 

         যদি  d, a এর একটি divisor হয় এবং d যদি b এর ও একটি divisor হয় তাহলে আমরা বলতে পারি যে d হল a এবং b এর common divisor।


         6 এর divisor হল 1, 2, 3, 6
         12 এর divisor হল 1, 2, 3, 4, 6, 12

         Common divisor (6, 12) = 1, 2, 3, 6


কিছু দরকারি তথ্য common divisor এরঃ

        1)   d | a এবং d | b implies  d | (a+b) এবং d | (a-b)

                         
        আরও সাধারনভাবে বলা যায় যে, d | a এবং d | b implies d | (ax+by) [সকল x এবং y এর জন্য] এবং যদি  a|b তবে,|a|≤|b| অথবা b=0

        2)  Number of Common Divisor(a, b) = Number Of Divisor (gcd(a, b))

             প্রমানঃ

             GCD(24, 30) = 6
             অর্থাৎ 24 এবং 30 এর মধ্যে এমন কোনো common divisor      নেই যা 6 এর চেয়ে বড়। 6 এর divisor হল 1, 2, 3, and 6।  24, 30     এর জন্য 6 এর চেয়ে ছোট বা সমান কোনো সংখ্যা নেই যা 24, 30     কে ভাগ করতে পারে এবং{1, 2, 3, 6} এর সেটে এ পরে না।


Problem : COMDIV - Number of common divisors

Editorial : Editorial Link

Tuesday, November 22, 2016

Friday, November 18, 2016

List of algorithmic problems uva


Topological Sorts using indegree (Kahn's algorithm)

Tutorial - Kahn's Algorithm for Topological Sorting
             
                Shafaetsplanet

                Tutorial


ALGORITHM:


The usual algorithms for topological sorting have running time linear in the number of nodes plus the number of edges, asymptotically, .

Implementation:

Sunday, October 30, 2016

Codeforces problem B Two Buttons analysis

Problem Type : BFS , math , greedy , shortest path

Problem link: Two Buttons


this was a very interesting for me.


This problem asks us what is the minimum steps we need to reach from one given number to another given number by multiplying by 2 or  subtracting 1


i have found 2 ways to solve this problem 

       1) using bfs (hard way)
       2) simple mathematics

The hard way:


let us try to analyze 2 given test cases 


1)  4 ->6  minimum steps: 2

2) 10 ->1 minimum steps: 9

let us make a graph using the 2 test cases



Wednesday, October 26, 2016

UVA 383 - Shipping Routes analysis

Problem Type : Single-Source Shortest Paths On Unweighted Graph  (BFS/DFS)

Its a basic SSSP problem.

"The cost of a shipment is equal to the size of the shipment times the number of shipping legs required times $100."

we just have to do a bfs or dfs from given source if we have't done bfs or dfs on the source before.We can get the cost of the shipment using the above statement using the cost of the destination :D


Tuesday, October 25, 2016

UVA 260 - Il Gioco dell'X analysis

Problem Type : Finding Connected Components (BFS/DFS)

Let us try to understand the problem

"The game ‘Il Gioco dell’ X’ is played on a N by N board (N ≥ 2). The object of both players, say Black and White, is to join opposite sides of the board by placing in turn their pawns on the board in such a way that a path is made from one side to the other by adjacent (neighboring) pawns of their own color."

Though the statement is not very clear how Black and White will win but we can get the idea from the given picture



Saturday, October 22, 2016

Sifat Shishir's Blog: UVA 11254 : Consecutive Integers

Sifat Shishir's Blog: UVA 11254 : Consecutive Integers: Prob link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=22...

Friday, October 14, 2016

Depth-first search Implementation

1 thing i always remember about dfs is "Using dfs We go to the depth of a node and then we come back"

I learned dfs from my friend and from this blog shafaetsplanet . In shafaetsplanet the algorithm is explained very easily.


Algorithm :


Basic DFS Traversal of a Graph:


Here starting node is 0.



Some good beginner problems


IMPLEMENTATION:

01.Beautiful Year (L1)
Solution

02.Queue at the School (L1)
Solution

03.Lights Out (L1)
Solution

04.Arrival of the General (L1)
Solution

05.Twins (L1)
Solution

06.Effective Approach (L2)
Solution

07.Even Odds (L2)
Solution

08. Airport (L2)
Solution

EASY GREEDY:

01.Dragons (L2)
Solution

Monday, October 10, 2016

Problems on Data structures

Some Graph Problems

Here are some Graph problems i have solved over time 


Flood Fill/Finding Connected Components using dfs/bfs:



BFS:



DFS:




Tropological sort:


Strongly connected component:



Graph coloring:



MST:

908 - Re-connecting Computer Sites
Solution

11631 - Dark roads
Solution

11747 - Heavy Cycle Edges
Solution

10034 - Freckles
Solution

11733 - Airports
Solution

1174 - IP-TV
Solution

11710 - Expensive subway
Solution

11857 - Driving Range
Solution

10048 - Audiophobia (mst+bfs)
Solution

544 - Heavy Cargo
Solution

10099 - Tourist Guide
Solution

10147 - Highways
Solution

10397 - Connect the Campus
Solution

10600 - ACM Contest and Blackout
Solution

11228 - Transportation system.
Solution

11733 - Airports
Solution

11857 - Driving Range
Solution


Dijkstra:

10986 - Sending email
Solution

929 - Number Maze
Solution


bellman ford:

558 - Wormholes
Solution


Thursday, September 29, 2016

Improve my skills in competitive programming



What is the connection between computer science and algorithms?

Thomas Cormen:
"I’ll tell you a little story. A true story.
In the late 1970s and early 1980s, I worked at a startup that made systems for computer-aided design. Users could define parts and store them in a library of parts. Each part could include another part by reference, so that if you changed the definition of a part, then all of its uses would update automatically. Part A could include a reference to part B, which could include a reference to part C, and so on. Circular references were not allowed, as a part could not include itself.
We had a customer that wanted the library of parts written out to tape so that each part appeared on the tape before any other part that used it. I was the only person at the company who knew that what this customer wanted was a topological sort of a directed acyclic graph. I knew that there was an efficient algorithm for this problem, and I knew where I’d seen it (in Knuth). I didn’t remember the details of the algorithm, and so I went to the library, got a copy of Knuth, and implemented the algorithm.
People at the company thought I was a god for knowing how to solve the problem, and how to solve it efficiently.
That’s why you want to know about algorithms."

Sieve of Eratosthenes




Prime generator with sieve Algo:

Tutorial (ENG)
shafaet tutorial (bangla)


Prime Factors Finding



Algorithm:

1) divide n by 2 till n is divided by 2 and save 2 in a list each time
2) divide n by (3 to  root(n) and only the odd numbers) if divisible save it in the list
3)if n is greater than 2 save n in the list

all the prime factors are in the list.


Monday, September 19, 2016

From Demi Guo

Programming contest is a kind of sports, no different from running. Online speed programming contests are especially exciting and competitive. You have to prepare and be ready to finish reading a problem and coding a 20-lines program in 2-3 minutes in order to beat your opponents. Your heart beats faster, when you wait in front of your computer, staring at the countdown. It has a sync scoreboard. You can also hack other people's codes to win points!!!
If I realize that I'm not among the smartest kids in the classroom, I'll definitely be more hard-working, I won't feel depressed but feel excited. I love realizing my weakness and confronting difficulties, because I know they make me conscious and tell me that I can become a even better person. I always believe that the most powerful ability we have is the ability to improve ourselves. Jack Ma (Alibaba CEO) once said, “I am very excited if I see my opponents are stronger than me in present, because I know that I will beat them and become the stronger in the future.”

Something I found on quora

From Mostafa Hany Gomaa :

"it's normal to feel frustrated, and it's even normal to sometimes feel that you're just plain stupid when practicing algorithmic problems. I felt the same way when I first started practicing on codeforces. I could only do DIV 2 level A problems, and I even got stuck on some of them sometimes. I felt so frustrated that I asked one of my friends who is a pretty good competitive programmer,  what' the use if I can't even solve some DIV2 level A problems?. He told me it's normal, and just keep going. So, I will tell you the same thing, just keep trying. The biggest mistake you can make is get too frustrated and decide to quit. I know because I quit multiple times, but it is when I persevered that I started to see some improvement. I got faster, I was able to solve tougher problems, and it became more fun. I am still not that good, yet. Right now, I can consistently solve level A, B problems, and I am starting to build consistency on C problems, but I still have to make it to harder ones, and eventually to DIV1. So my advice is stick in there, if you get stuck on a problem, think about it as much as you can and give it a lot of effort, if you're still stuck after that, then ask someone for hints, if you still can't figure it out, then look up the solution. When you read the solution, make sure you understand it well, if you found out that you're missing some background knowledge, then study that. Also, it's very important to implement the solution after understanding it. Another important thing to consider is to save the links to the problems you couldn't figure out on your own so that you can revisit them later. If you revisit the problem at a later time, and you still couldn't solve it, then you didn't really understand its solution well enough. Finally, don't get too scared of harder problems. It's very easy to stay in the comfort zone of solving problems that are at your level. You should always keep pushing the limit and go for harder problems, otherwise you won't learn anything new and you won't see much improvement. Best of luck, and have fun :)."

Friday, September 9, 2016

Light OJ 1074 - Extended Traffic

Problem link - 1074 - Extended Traffic

In this problem we have to compute minimum earning from node 1. At first we have to do a dfs to see if destination is reachable from 1.Every node that can be reached with negative earning , all its adjacent nodes can be reached with negative earning too.

If a node if not reachable from 1 or or the earning is less than 3 then we have to print '?' else we have to print ta earning from 1. We can use bellman ford algorithm from this problem as there are negative earnings.

let us analyze some test cases:


1

5
6 7 8 9 10
6
1 2
2 3
3 4
1 5
5 4
4 5
2
4
5





here node 4 (D) can be reached with earning 3 and node 5(E) can be reached with earning 4


Thursday, September 8, 2016

Breadth-first search implementation

I learned bfs from my friend and from this blog shafaetsplanet . In shafaetsplanet the algorithm is explained very easily. There the bfs code was implemented using adjacent metrix. Here we will implement it using adj list.

from safaet's blog I will take few lines to show everyone how bfs works

     01. A node can't be visited more than once.
     02. The source node is always level 0.
     03. All nodes directly connected to level 0 or source node are level 1.
     04. All nodes directly connected to level 1 are level 2.And this is how level will keep on rising one by one.
     05. Level of each node is the shortest path distance from its source node


Basic Graph:

















Run Meter (pedometer app android) (2nd year,2nd semester)

A basic pedometer app which counts calorie burned while running or cycling  for fulfillment of my 2nd year,2nd semester Project Work.

Screenshot:

Unity Pos System Using Java Netbeans and Database (2nd year,1st semester)

This Project has been designed and developed for fulfillment of my 2nd year,1st semester Project Work.

Video:





Run Away using c++(1st year,2nd semester)

A 2D game on basic c and c++ for fulfillment of my 1st year,2nd semester Project Work.

Screenshot:

Second best mst (using Kruskal)

Sometimes we are asked to find Second best mst in some problems. We can do this with brute force.
At first we will find the mst and all the edges of mst. From all the edges of mst we will remove every edge once and and will create a new mst. In this way the minimum of all newly created mst is the Second best mst

mst:


yellow edges are edges of mst

sum of all weights is 110.













minimum spanning tree (Kruskal's algorithm) Implementation

There is an awesome kruskal algorithm tutorial  in - shafaetsplanet

here I will try to implement the algorithm in my way

a normal undirected graph















Light OJ 1041 - Road Construction

Problem link -  1041 - Road Construction

In this problem we are given m roads with this roads we have to create a mst. If there are n cities and if we can select n-1 edges from  all edges when running mst then we can say we have a solution as we got a tree :-)

For mapping the cities we can use STL map.


Light OJ 1040 - Donation



Problem link - 1040 - Donation

Its a very basic mst problem.

let us try to analyze some test case

2
27 26
1 52


Football Player Transfer Prediction

Football Player Transfer Prediction Using Different Classifiers Project Report :  Football Player Transfer Prediction Report ...