Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prims Algorithm Tutorial #711

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

Infonioknight
Copy link

Change Summary

  • Included an overview, the complexities of the algorithm
  • Explained one leetcode problem in depth (I couldn't find too many and I wanted to leave some for the suggested problems that were simple and similar)
  • Included two suggested problems.
  • Highlighted any references used.

Checklist

If you haven't fulfilled the below requirements or even delete the entire checklist, your PR won't be reviewed and will be closed without notice. Regular contributors (with 10+ PRs) can skip this part.

General

  • [/ ] This Pull Request is all my own work. (You'll be blacklisted if you are caught for plagiarism.)
  • [ /] I've read CONTRIBUTING.md
  • [ /] I've applied LaTex for all variables, formulas and time / space complexity instead of using backticks
  • [/ ] I've started the app locally and verified all the content and all links (if applicable) are accessible correctly
  • [ /] I've included Complexity Analysis (Time Complexity & Space Complexity).
  • [ /] I've written my explanation well and it is easy to understand for beginners

Tutorial

  • [ /] I've read and followed the Tutorial Template
  • [ x] I've explained my topic well with 2 - 3 LC problems and no external problems are used.
  • [/ ] I've provided the full working solutions to the problems used in this tutorial.
  • [ /] I've provided suggested problems at the end with the given format. See here as an example. If the target solution is not available, leave solutionLink blank.
  • [/ ] I've given credits / references if I use external resources. (For an image, give credit under it. Otherwise, add a new section called References at the end (after Suggested Problems).)

Solutions

  • [x ] I've read and followed the Solution Template
  • [ /] I've formatted my code well with K&R Coding style
  • [ /] I've confirmed that comments are put above each line rather than writing on the same line.
  • [ /] I've included a meaningful approach name for my solution. e.g. ## Approach 1: Two Pointers.

- Included an overview, the complexities of the algorithm
- Explained one leetcode problem in depth (I couldn't find too many and I wanted to leave some for the suggested problems that were simple and similar)
- Included two suggested problems.
- Highlighted any references used.
tutorials/graph-theory/prims-algorithm.md Outdated Show resolved Hide resolved
tutorials/graph-theory/prims-algorithm.md Outdated Show resolved Hide resolved
tutorials/graph-theory/prims-algorithm.md Outdated Show resolved Hide resolved
tutorials/graph-theory/prims-algorithm.md Outdated Show resolved Hide resolved
tutorials/graph-theory/prims-algorithm.md Outdated Show resolved Hide resolved
tutorials/graph-theory/prims-algorithm.md Outdated Show resolved Hide resolved
tutorials/graph-theory/prims-algorithm.md Outdated Show resolved Hide resolved
tutorials/graph-theory/prims-algorithm.md Outdated Show resolved Hide resolved
Incorporated the requested changes.
@Infonioknight
Copy link
Author

Incorporated the requested changes. Do let me know if they're okay.

Copy link
Owner

@wingkwong wingkwong left a comment

Choose a reason for hiding this comment

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

the structure is fine. please add one more example with the same structure.

tutorials/graph-theory/prims-algorithm.md Show resolved Hide resolved
tutorials/graph-theory/prims-algorithm.md Outdated Show resolved Hide resolved
tutorials/graph-theory/prims-algorithm.md Outdated Show resolved Hide resolved
tutorials/graph-theory/prims-algorithm.md Show resolved Hide resolved
tutorials/graph-theory/prims-algorithm.md Outdated Show resolved Hide resolved
- Added a second example problem for the same.
@Infonioknight
Copy link
Author

Added a second example problem, also highlighted the solutions for the problems that had one.

Made a few modifications to the code, indentation to make it copy-pastable in leetcode
@Infonioknight
Copy link
Author

I made a second commit changing a little bit of the indentation and layout of the code to make it directly copy-pastable in leetcode. Do let me know if it's okay!

@Infonioknight
Copy link
Author

Hello! I just wanted to follow up on the PR.

@wingkwong
Copy link
Owner

hey next time please click re-request review when it's ready.

image

@Infonioknight
Copy link
Author

Ah, got it. Will make sure to do that next time!

Copy link
Owner

@wingkwong wingkwong left a comment

Choose a reason for hiding this comment

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

please verify locally once first.

image

Made necessary changes to run locally
@Infonioknight
Copy link
Author

Apologies for the delay, was a bit caught up with work. Made the changes and tested it locally. Seems to be working fine now.

@Infonioknight
Copy link
Author

Hi there! Just wanted to follow up on the PR!

Copy link
Owner

@wingkwong wingkwong left a comment

Choose a reason for hiding this comment

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

  • please apply LaTex instead of using backticks.
  • for the content, i will review in depth in weekend.

tutorials/graph-theory/prims-algorithm.md Outdated Show resolved Hide resolved
tutorials/graph-theory/prims-algorithm.md Outdated Show resolved Hide resolved
tutorials/graph-theory/prims-algorithm.md Outdated Show resolved Hide resolved
- Changed all backtick highlighting to LaTeX
- Modified the introduction to the problems (made it an explanation instead of just listing input and output)
- Changed value for TabItems to 'py'
@Infonioknight
Copy link
Author

Made the requested changes. My apologies for not checking with the format of things. I think it should be compliant now.

@Infonioknight
Copy link
Author

Hi, just wanted to follow up on the pull request.

@Infonioknight
Copy link
Author

Hi! Just wanted to know if I needed to make any other changes in this.

@wingkwong
Copy link
Owner

@Infonioknight hey sorry for the late update. been a bit busy these months. will try to review on weekend. thanks.

@Infonioknight
Copy link
Author

@wingkwong no worries at all! Do take care.

Copy link
Owner

@wingkwong wingkwong left a comment

Choose a reason for hiding this comment

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

The subheader for examples should be consistent. Also subheader shouldn't include :.

image

## Suggested Pre-requisites
Before moving on to learn the Prims algorithm, it is suggested that you know what a [Minimum Spanning Tree (MST)](../graph-theory/minimum-spanning-tree.md) is.
Copy link
Owner

Choose a reason for hiding this comment

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

can you also work on MST page?


### Complexity and Use-Cases
When implemented using a priority queue (usually a binary heap), the time complexity of Prim’s algorithm is $O(E + V log V)$, where $E$ is the number of edges and $V$ is the number of vertices. This makes Prim's algorithm highly efficient for **dense** graphs, where the number of edges is close to the maximum possible. For sparse graphs (a graph with a relatively small number of edges compared to the maximum number of possible edges), other algorithms like [Kruskal’s](kruskals-algorithm.md) may perform better.
Copy link
Owner

Choose a reason for hiding this comment

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

may elaborate how to get $O(E + V log V)$


### Complexity and Use-Cases
When implemented using a priority queue (usually a binary heap), the time complexity of Prim’s algorithm is $O(E + V log V)$, where $E$ is the number of edges and $V$ is the number of vertices. This makes Prim's algorithm highly efficient for **dense** graphs, where the number of edges is close to the maximum possible. For sparse graphs (a graph with a relatively small number of edges compared to the maximum number of possible edges), other algorithms like [Kruskal’s](kruskals-algorithm.md) may perform better.
Copy link
Owner

Choose a reason for hiding this comment

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

This makes Prim's algorithm highly efficient for dense graphs,

may explain why it is efficient for dense graphs but not sparse ones.

![Prims Algorithm Image](https://miro.medium.com/max/700/1*7kpPIPcmXr38Juh0umM6fA.jpeg)

_Source: https://miro.medium.com/max/700/1*7kpPIPcmXr38Juh0umM6fA.jpeg_
Copy link
Owner

Choose a reason for hiding this comment

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

refer to the post title instead.

3: [(6, 1), (1, 2)]
```
Where each tuple consists of '(cost, destination)'.
Copy link
Owner

Choose a reason for hiding this comment

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

'(cost, destination)' -> $(cost, destination)$

## Example 2: [1584 - Min Cost to Connect All Points](https://leetcode.com/problems/min-cost-to-connect-all-points)
### Instructions:
You are given an array $points$ representing integer coordinates of some points on a 2D-plane, where $points[i]$ = $[xi, yi]$.
Copy link
Owner

Choose a reason for hiding this comment

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

xi -> x_i
yi -> y_i

apply to j as well.

for i in range(len(points)):
current = tuple(points[i])
for j in range(i+1, len(points)):
Copy link
Owner

Choose a reason for hiding this comment

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

i+1 -> i + 1


adjacency_list = {}
for i in range(len(points)):
Copy link
Owner

Choose a reason for hiding this comment

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

len(points) -> n since it will be used more than one time

Comment on lines +283 to +298
heapq.heappush(connection_queue, connection)

while len(seen) < n and connection_queue:
cost, current = heapq.heappop(connection_queue)
if current in seen:
continue

res += cost
seen.add(current)

for connection in adjacency_list[current]:
if connection[1] not in seen:
heapq.heappush(connection_queue, connection)

return res
Copy link
Owner

Choose a reason for hiding this comment

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

fix indentation

seen = set([start])

for connection in adjacency_list[start]:
Copy link
Owner

Choose a reason for hiding this comment

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

include comments for the below code like L314 - L323. (apply to other code block)

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.

2 participants