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

Create connected-components.md #269

Closed
wants to merge 8 commits into from

Conversation

adityabisht02
Copy link

@adityabisht02 adityabisht02 commented Oct 14, 2022

Added a tutorial for connected components in a graph

Change Summary

Issue #107

Checklist

Ideally all of them should be checked. If you haven't fulfilled the below requirements or even delete the entire checklist, your PR won't be reviewed.

General

  • Make sure you've read CONTRIBUTING.md
  • Make sure you've followed the template format
  • Make sure you've started the app locally and verified all the content and all links (if applicable) must be accessible correctly
  • Make sure you've applied LaTex for variables, formulas and time / space complexity instead of using backticks
  • Make sure you've formatted your code well (Code Block is rendered using <Tabs/>)
    Below is a C++ example. You can include multiple langs using <TabItem/> if necessary
<Tabs>

<TabItem value="cpp" label="C++">
<SolutionAuthor name="@YOUR_ALIAS"/>

```cpp
// Your code goes here
// Prefer K&R Coding style
// See https://gist.github.com/jesseschalken/0f47a2b5a738ced9c845#why-kr for example
```

</TabItem>

</Tabs>
  • Make sure you've explained your code well (write comments above each line - not inline)
  • Make sure you've written your explanation well and it is easy to understand for beginners
  • Make sure you've included Time & Space Complexity Analysis
  • Make sure you've included a meaningful approach name for your solution. Don't leave it blank. e.g. ## Approach 1: Two Pointers.

For Tutorial Articles

  • Make sure you've explained your topic using 1 - 3 LC problems rather than using external problems. Solutions must be taken with explanations as well.
  • Make sure you've provided suggested problems at the end using the given format. See here as an example. If the target solution is not available, leave solutionLink blank.
  • Make sure you've given credits / references if you use external resources. For an image, give credit under it. Otherwise, add a new section called References at the end (after Suggested Problems).

Added a tutorial for connected components in a graph
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 look at the checklist first as well as other tutorials for reference.

@wingkwong wingkwong added the Status: Pending for Changes The PR is reviewed with some changes requested. Waiting for contributors' actions. label Oct 14, 2022
@adityabisht02
Copy link
Author

I have made the required changes

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.

read it again.

@wingkwong
Copy link
Owner

any update?

@adityabisht02 adityabisht02 reopened this Nov 1, 2022
@adityabisht02
Copy link
Author

@wingkwong I have made changes to make it similar to other tutorials. Also I or someone else will need to write the solution for 0547 (Number of provinces). Should I raise an issue for that as well?

@wingkwong
Copy link
Owner

@adityabisht02 Please look at the latest checklist and this tutorial

@adityabisht02 adityabisht02 reopened this Nov 5, 2022
@adityabisht02
Copy link
Author

@wingkwong I have made some changes according to the tutorial and checklist u referred ,it should be good now

In a graph, sometimes all nodes might not be connected with each other. Let's take an example of a graph with 5 nodes-


![image](https://user-images.githubusercontent.com/89146189/195919108-2e06dbbe-717a-4f86-9ba1-90cbdf5c119c.png)
Copy link
Owner

Choose a reason for hiding this comment

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

prefer a better image

<TutorialAuthors names="Aditya Bisht"/>

## Overview
In a graph, sometimes all nodes might not be connected with each other. Let's take an example of a graph with 5 nodes-
Copy link
Owner

Choose a reason for hiding this comment

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

Let's take an example of a graph with 5 nodes.

3. If you are unable to reach a particular node or a group of nodes, then that group of nodes might be a different connected component.

## How to find connected components?
1. Let's an adjacency list based graph is given.
Copy link
Owner

Choose a reason for hiding this comment

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

Let's create an adjacency list based on the given graph

## How to find connected components?
1. Let's an adjacency list based graph is given.
2. Traverse the adjacency list and for every node do a BFS or DFS on the node and mark all the nodes you visit in this way as visited.
3. While traversing the list do not do BFS on the nodes which have already been visited.
Copy link
Owner

Choose a reason for hiding this comment

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

or DFS

Return the total number of provinces.
```

Here, the given graph is in the form of an adjacency list. When we perform bfs/dfs starting from one particular node,
Copy link
Owner

Choose a reason for hiding this comment

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

please also show a DFS solution

<SolutionAuthor name="@adityabisht02"/>

```Java
class Solution {
Copy link
Owner

Choose a reason for hiding this comment

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

format the code following K&R Coding style

```
</TabItem>
</Tabs>
The time complexity of the above solution is O(n) where n is the number of elements in the isConnected list. We have also used an array with size equal to the number of nodes in the graph (not the number of elements in isConnected).
Copy link
Owner

Choose a reason for hiding this comment

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

  1. it's not O(N) ...
  2. missing space complexity
  3. apply LaTex on them

Copy link
Author

Choose a reason for hiding this comment

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

apply LaTex where ?

Copy link
Owner

Choose a reason for hiding this comment

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

Already stated in the checklist.

Make sure you've applied LaTex for variables, formulas and time / space complexity instead of using backticks

@wingkwong
Copy link
Owner

Closing due to inactivity.

@wingkwong wingkwong closed this Nov 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Pending for Changes The PR is reviewed with some changes requested. Waiting for contributors' actions.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants