Skip to content

Conversation

@ErdemT09
Copy link
Collaborator

@ErdemT09 ErdemT09 commented Jul 6, 2021

Resolves: #211

Before talking about this problem, it's best to review previous problems on Union-Find:
#332, Union-Find Solution
#329
#200
These two problems also provide us great templates for efficient directed/undirected graph creation:
#330
#332, DFS Solution

Algorithm:

What does it mean to have a well here? A well from a house is just a weighted edge to some water-source vertex, a weighted edge like all pipes between the houses, which are also vertices.
What we should do is then connect all these vertices, the water source and the houses, with minimum cost. This is nearly the exact same problem we had in #24. We have to find the minimum spanning tree (MST) for this graph of wells as edges from a water source and houses with pipes.
image
There are two ways of doing this:

Approach 1-Union-Find/Kruskal

A visualization of this from Wikipedia
This classical Union-Find approach begins by adding all edges of this graph to a priority queue (using sorting is also possible). We poll the shortest edge from it each time. If the root of two vertices of this edge are equal, they are already connected and we don't need to connect them again. Else, we connect them and assign parents using the rank system. We add the edge's cost to the total and decrement the number of unconnected components until we are left with 1, when we have "connected" all the vertices.

Approach 2-Prim's Algorithm

A visualization of this from Wikipedia
Instead of picking the smallest edge from the entire graph, we can begin with some node (preferably the water source) and add nodes to this tree by selecting the shortest edge connected to this tree. If we add some node a to the tree, we also add all of its unvisited nodes to the priority queue to visit them. When the number of elements in the tree equal to n (we don't count the water source) that means that all houses are connected and we can return the costs.

@ErdemT09
Copy link
Collaborator Author

ErdemT09 commented Jul 6, 2021

Union-Find is faster than the Prim's algorithm. Prim's algorithm might be slower because of the extensive graph creation and the number of edges being processed in the heap.

@ErdemT09 ErdemT09 closed this Jul 6, 2021
@ErdemT09 ErdemT09 reopened this Jul 6, 2021
@altay9
Copy link
Collaborator

altay9 commented Jul 6, 2021

Thanks for drawing Erdem. It'll help a lot.

Copy link
Collaborator

@altay9 altay9 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Erdem. Nothing to add more. I especially like Union Find approach.

@ErdemT09 ErdemT09 merged commit b82a6e2 into master Jul 9, 2021
@ErdemT09 ErdemT09 deleted the 1168.-Optimize-Water-Distribution-in-a-Village branch July 9, 2021 04:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1168. Optimize Water Distribution in a Village

3 participants