Showing posts with label minimum spanning tree. Show all posts
Showing posts with label minimum spanning tree. Show all posts

Tuesday, February 20, 2018

Hackerank: Road in hacker land

Feb. 20, 2018

Introduction

It is my favorite algorithm called union find algorithm. It can be Kruskal algorithm. I did have a lot of practice recently. It is so good to come back to work on the algorithm again.

Algorithm study 

Plan to review the algorithm by studying the blog.

题意
给一个联通的无向图,求所有点对之间的距离和。其中每条边的距离都是 2 的幂且互不相同。
题解

  • 每条边距离为 2 的幂且不相同,意味着选择长的边很可能是不好的。
  • 这道题跑最短路显然不大可能,因为需要求所有点对的距离和。最优解是最小生成树,接下来来证明。
  • 假设最优解不是最小生成树,那么去掉其中长度最长的边(长度为 k)后再增加若干条长度小于 k 的边仍能使其联通。先去掉最长的边,那么有一些点对会无法联通,这些点对的距离至少为 2^k,现在增加一些边使其联通,那么这些点对的距离至多是 2^0+2^1+2^2++2^k−1<2^k,从而得到一个更优解,所以假设不成立。
  • 既然是最小生成树,那么需要求一下这棵树上每条边的贡献,这个问题就比较简单了,跑一边 dfs 即可。

Coding


Please use the code for Union find algorithm Island count II for the template, and apply the same technique on this algorithm. Here is the link. 

Quick union algorithm

I like to review the practice on quick union algorithm. Here is the link: 

Quick find algorithm

I reviewed the quick find algorithm more than one month ago. Here is the link: 


A mock interview discussion

I remembered that the peer gave me the question and test my disjoint set data structure. I chose to use depth first search and then had some idea to use hashset only data structure. Here is the discussion.


Sunday, July 2, 2017

Super Mancunian - HourRank 22

July 2, 2017

Introduction



It is a minimum spanning tree algorithm. But there is additional work to remove max cost edge in the tree. Julia started to work on the algorithm last 30 minutes, she spent 10 minutes to go over the problem statement and then figured out the whole requirement. She only had 20 minutes, she fumbled, and she looked at the leaderboard, checked players from Google's performance, she likes to write a simple code to score partial points to make her a medal player.

Too short time, it just reminds her that she needs to work on and review what she works on. She just needs to look into past work and then find a solution to score the points.

An algorithm makes her Sunday morning so challenging.

Algorithm 



Previous practice on Kruskal's algorithm is here. Julia spent wrong on the name, she spelled Krusal. In order to pay respect the scientist, Julia decided to review his wiki page and get more detail on his work.

Read wiki page as well. Plan to spend one hour to read this Sunday morning.





Friday, April 14, 2017

Kruskal's algorithm workout (I)

April 14, 2017


Introduction


It is a good workout to work on Kruskal's algorithm based on the geeksforgeeks.com article.  Julia likes to spend 2 - 3 hours to play with Kruskal's algorithm using C#. 

Later on, she likes to work on the algorithm on Hackerrank week code 31 - Spanning Tree Fraction. 


Kruskal's algorithm

Greedy algorithm to get minimum spanning tree using Kruskal's algorithm. 

Spent near 2 hours to work on the C# code. C# code is here. 

Wednesday, July 20, 2016

HackerRank: World codesprint #4 - Roads in HackerLand - II - code study

July 20, 2016

First blog on this algorithm:

HackerRank: World codesprint #4 - Roads in HackerLand

http://juliachencoding.blogspot.ca/2016/06/hackerrank-world-codesprint-4-roads-in.html

Come back to work on this problem. Study editorial solution provided by HackerRank, and then,
review union find, minimum spanning tree algorithm with code first:

3 steps:
step 1: union find algorithm
step 2: minimum spanning tree
step 3: roads in hackerLand

 Blogs to read:
1.
https://en.wikipedia.org/wiki/Minimum_spanning_tree

2.
http://www.ics.uci.edu/~eppstein/161/960206.html

Work on union find algorithm first:  (step 1)
July 21, 2016

1. Union find algorithm - detect cycle

http://www.geeksforgeeks.org/union-find/

2. Union by rank and path compression

http://www.geeksforgeeks.org/union-find-algorithm-set-2-union-by-rank/

Then, work on minimum spanning tree algorithm: (step II)

3.
http://www.geeksforgeeks.org/greedy-algorithms-set-2-kruskals-minimum-spanning-tree-mst/

4.
http://www.geeksforgeeks.org/greedy-algorithms-set-5-prims-minimum-spanning-tree-mst-2/


And then, study 10 code submission scoring 60/60 in Java, C++, C#. Practice one by one. (step III)

A better way to learn an algorithm - minimum spanning tree - work on a concrete example, make it fun learning experience.

1. Java implementation:
https://gist.github.com/jianminchen/20775a7ac1eeb83fa141e92633ea7d78

2. C++ 14
https://gist.github.com/jianminchen/28b5c3bf191a27250b67ce4e7656166b

3. C#
https://gist.github.com/jianminchen/7a21dfe2a62f1bcf4bc305480ef2f51e

4. C#
https://gist.github.com/jianminchen/8a15be7b83770d22647f34371fb48a97

5. C++
https://gist.github.com/jianminchen/5a3e680b86d2c5ffc0f090f29f074089


6. C++
https://gist.github.com/jianminchen/dcac67099aa7ddcb4497565f0732fe5e

7. C++
https://gist.github.com/jianminchen/10983fdcbbe15fef37bd73a60fe87430

8.

9.

10.

Follow up after 8 months


March 9, 2017
Read the tutotial first. And then write a C# solution, post a question on code review.

Wednesday, June 29, 2016

HackerRank: World codesprint #4 - Roads in HackerLand

June 29, 2016

Problem statement:


John lives in HackerLand, a country with  cities and  bidirectional roads. Each of the roads has a distinct length, and each length is a power of two (i.e.,  raised to some exponent). It's possible for John to reach any city from any other city.
Given a map of HackerLand, can you help John determine the sum of the minimum distances between each pair of cities? Print your answer in binary representation.

Max Score: 60
Difficulty: Moderate
Submissions: 1319

The problem is a graph problem, so here is her analysis:

Spent over 1 hour to try to figure out the graph problem, what is my best bet to solve the problem. 
Find simple problems to work on first:
Algorithm 1. Any two directed connected nodes, find shortest distance
distance (n1, n3)  = 10, go through node 1 -> node 2 -> node 3, instead of directly connected edge - 32

2. Any two nodes in the graph, find the shortest distance
based on graph in the step 3, find a route from one node to another node, using BFS or DFS, for example, node 1 to node 4, node 1 -> node 2 -> node 4. 
   min distance (node1, node4) 

Spend a lot of time here to try to design the algorithm: 
1. using DFS/recursive/memorization solution: 
    evaluate the solution, problems in the design cannot be solved: 
    problem and its subproblems are overlapping. 

   min distance (node2, node4)
   Not working 
2. using BFS/queue, the design concerns:
   1. Avoid loops
   2. Stop early - 

3. using DFS/stack, the design concerns:
  
Also, each edge's length is power of 2, different length. 
    
1. Step 1-> Step 2: 
Try to work on this graph, for each directed connected graph, use BFS algorithm, try to find a shorter route. For example, 
edge(1, 3) = 32, 
edge(1, 2) = 8, 
edge(2, 3) = 2, 
edge(1,3)  > edge(1, 2) + edge(2,3), since 32 > 8 + 2, 
So, the edge(1, 3) can be marked removable, this direct route will never be travelled. 

Work out the above case: 
start from node 1, add node 1 to the queue, and then, go through the queue, remove node from queue, add all neighbors to the queue, if it is not visited previously, and if it is the node 3, then only allow twice , as long as the sum is less than edge(1,3) = 32, continue; Make a single linked list, n2.prev = n1, n3.prev = n2, 

2. To work on graph in step 3,
For each node in the graph, for example, n1, using DFS or BFS to find a route to node j, j<>i.
So, with those removed edges, only one route will be found through any two nodes, i<>j.

To be continued.

Follow up 

April 26, 2017
Need to write down C# version to solve this minimum spanning tree algorithm.

---