Murugan k
  • NEW!
    NEW!  Connect Ideas Across Notes
    Save time and share insights. With Paragraph Citation, you can quote others’ work with source info built in. If someone cites your note, you’ll see a card showing where it’s used—bringing notes closer together.
    Got it
      • Create new note
      • Create a note from template
        • Sharing URL Link copied
        • /edit
        • View mode
          • Edit mode
          • View mode
          • Book mode
          • Slide mode
          Edit mode View mode Book mode Slide mode
        • Customize slides
        • Note Permission
        • Read
          • Only me
          • Signed-in users
          • Everyone
          Only me Signed-in users Everyone
        • Write
          • Only me
          • Signed-in users
          • Everyone
          Only me Signed-in users Everyone
        • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invite by email
        Invitee

        This note has no invitees

      • Publish Note

        Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

        Your note will be visible on your profile and discoverable by anyone.
        Your note is now live.
        This note is visible on your profile and discoverable online.
        Everyone on the web can find and read all notes of this public team.

        Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

        Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

        Explore these features while you wait
        Complete general settings
        Bookmark and like published notes
        Write a few more notes
        Complete general settings
        Write a few more notes
        See published notes
        Unpublish note
        Please check the box to agree to the Community Guidelines.
        View profile
      • Commenting
        Permission
        Disabled Forbidden Owners Signed-in users Everyone
      • Enable
      • Permission
        • Forbidden
        • Owners
        • Signed-in users
        • Everyone
      • Suggest edit
        Permission
        Disabled Forbidden Owners Signed-in users Everyone
      • Enable
      • Permission
        • Forbidden
        • Owners
        • Signed-in users
      • Emoji Reply
      • Enable
      • Versions and GitHub Sync
      • Note settings
      • Note Insights New
      • Engagement control
      • Make a copy
      • Transfer ownership
      • Delete this note
      • Save as template
      • Insert from template
      • Import from
        • Dropbox
        • Google Drive
        • Gist
        • Clipboard
      • Export to
        • Dropbox
        • Google Drive
        • Gist
      • Download
        • Markdown
        • HTML
        • Raw HTML
    Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
    Create Create new note Create a note from template
    Menu
    Options
    Engagement control Make a copy Transfer ownership Delete this note
    Import from
    Dropbox Google Drive Gist Clipboard
    Export to
    Dropbox Google Drive Gist
    Download
    Markdown HTML Raw HTML
    Back
    Sharing URL Link copied
    /edit
    View mode
    • Edit mode
    • View mode
    • Book mode
    • Slide mode
    Edit mode View mode Book mode Slide mode
    Customize slides
    Note Permission
    Read
    Only me
    • Only me
    • Signed-in users
    • Everyone
    Only me Signed-in users Everyone
    Write
    Only me
    • Only me
    • Signed-in users
    • Everyone
    Only me Signed-in users Everyone
    Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    1
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Graphs 1: DSU, Kruskal Algorithm & Bipartite Graph --- title: Problem 1 Is Knight reachable description: duration: 600 card_type: cue_card --- ### Problem Statement Given a N * N chessboard with K Knights placed on it. If a Knight is reachable from the other Knight, they can swap their positions Find the number of ways the Knights can rearrange themselves ### Observations Lets see the positions in which the Knight can move from (i, j). <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/086/764/original/graph.png?1724188843" width = 500 /> | | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | | --- | --- | --- | --- | --- | --- | --- | --- | --- | | row | -2 | -2 | -1 | -1 | 1 | 1 | 2 | 2 | | col | -1 | 1 | -2 | 2 | 2 | -2 | 1 | -1 | ### Example <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/086/765/original/graph.png?1724188884" width = 300 /> ### Solution Approach Lets see what are all the Knights that are reachable. As we can see that, the Black, Blue, Light Green, Pink, and Purple Knights are reachable, whereas Orange and Dark Green can swap themselves. <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/086/766/original/graph.png?1724188921" width = 500 /> So the answer for the above grid is, **5! x 2!**. Assume N connected components, then the solution will be `Ways = (count(CC1)!) * (count(CC2)!)....(count(CCN)!)` --- title: Problem 2 Nearest Hospital description: duration: 600 card_type: cue_card --- ### Problem Statement Given a N * M Matrix, the cells of the matrix is either marked as R (Residence) or H (Hospital) For every residence, find the distance to the nearest Hospital. From a particular cell, you can move to any adjacent cell (diagonal moves not allowed). **Example:** <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/086/767/original/graph.png?1724188958" width = 300 /> **Answer:** For the above matrix, lets calculate the distance to the nearest hospital. The distance matrix (solution) will be <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/086/768/original/graph.png?1724188990" width = 300 /> ### Solution Approach If we start from each Resident and find the Nearest Hospital, is an very unoptimal way, since we traverse the entire graph repeatedly for each resident. Alternately, what we do here is, we start from the hospital because all the hospitals are similar. If we mark all the source nodes initially and take one-one step outer and mark the distances of those nodes. Here we achieve a very optimal way to solve the problem. The Algorithm we are going to use here is a BFS but here it is know as **MultiSource BFS**. Why we call it as MultiSource ? It simply means, "When we have Multiple Start points" in the graph. <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/086/769/original/graph.png?1724189022" width = 500 /> <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/086/770/original/graph.png?1724189049" width = 500 /> --- title: Bipartite Graph description: duration: 600 card_type: cue_card --- ### Problem Statement Given a graph. Check if it is bipartite? **Bipartite:** A graph whose can be divided into 2 disjoint and independent sets (u & v) such that every edge of graph connects a node in set u to a node in the set v. **Example:** <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/086/771/original/graph.png?1724189091" width = 500 /> Lets mark the alternate levels with the same set. <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/086/772/original/graph.png?1724189123" width = 500 /> If we carefully see the graph, we can say that, for any node in the set u, it will always have an edge with the set v and vice-versa. This graph is known as Bipartite. The moment, if you had an edge between 9-10, the graph is no longer an Bipartite Graph. <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/086/773/original/graph.png?1724189195" width = 500 /> Here the node 9 (from set u) has an edge with the node 10 (with is also from set u). --- title: Graph Coloring description: duration: 600 card_type: cue_card --- ### Problem Statement Color the nodes of a graph such that no two adjacent nodes have the same color using minimum number of colors. **Example:** <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/086/774/original/graph.png?1724189233" width = 300 /> Answer: 2 Minimum number of Colors used is 2. Before moving forward, Lets see a new concept, Chromatic Number. ### Chromatic Number Chromatic Number is the Minimum number of colors required to color a graph, such that any pair of adjacent nodes have the same color! Example: <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/086/775/original/graph.png?1724189272" width = 300 /> > What is the chromatic number of the above graph ? For Now, Lets start coloring the nodes. <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/086/776/original/graph.png?1724189309" width = 300 /> On the marked node, Can you color with red color ? or with yellow color ? No right! Now lets introduce a new color, blue for that node. <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/086/777/original/graph.png?1724189345" width = 300 /> So the chromatic number of the graph is **3**. Example 2: <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/086/778/original/graph.png?1724189371" width = 300 /> What is the chromatic number of the above graph ? <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/086/779/original/graph.png?1724189407" width = 300 /> For a Fully Connected Graph with N nodes, the Chromatic number is N. --- title: Problem 4 Find Chromatic Number description: duration: 600 card_type: cue_card --- ### Problem Statement Given a graph find the chromatic number. ### Solution Approach There is one possible way to solve this question is Backtracking. The idea is very simple, you will start from a node, assign a color to the current node. Then to all the neighbour, color with a different color. <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/086/780/original/graph.png?1724189434" width = 300 /> ### Observation Lets look into different types of Graphs. <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/086/781/original/graph.png?1724189490" width = 300 /> Think, which graph type is a Bipartite graph? > Any Graph, which has a Odd Length cycle cannot be a Bipartite Graph. Is the Given Graph is a Bipartite Graph ? <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/086/782/original/graph.png?1724189518" width = 500 /> > The Graph is Not Bipartite, Since there is a Odd length Cycle. ### Pseudo Code ```java char color[N + 1] = {'w'}; for(i = 1; i <= N; i++){ if(color[i] == 'w'){ if(!isBipartite(i, 'G')){ return False; } } } return True; bool isBipartite(int Source, char C){ color[Source] = C; char temp; if(C == 'B'){ temp = 'G'; } else{ temp = 'B'; } for(all u connected to Source){ if(color[u] == C){ return False; } if(color[u] == 'w'){ if(!isBipartite(u,temp)){ return False; } } } return True; } ``` --- title: Problem 5 Maximum Number of Edges in a Bipartite graph description: duration: 600 card_type: cue_card --- ### Problem Statement Given a graph which is Bipartite. The Graph is Divided into 2 disjoint and independent sets u and v with N and M nodes respectively. What is the maximum number of edges we can have ? Example: Input: <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/086/783/original/graph.png?1724189550" width = 300 /> ### Dry Run Can we say the any node of the set u, can have form an edge between all the nodes of the set v. <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/086/784/original/graph.png?1724189577" width = 300 /> If we do the same thing for each node in the set u. <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/086/785/original/graph.png?1724189605" width = 300 /> The Total Number of Edges formed is **N x M**, which is the maximum possible. --- title: Problem 6 Maximum Edges Without Breaking Bipartite description: duration: 600 card_type: cue_card --- ### Problem Statement Given a tree with N nodes find the maximum number of edges that can be added to the tree so that it is still bi-partite graph. **Example:** <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/086/786/original/graph.png?1724189637" width = 500 /> ### Approach Lets Apply level order traversal here, in which the even levels belongs to a set u and the odd levels nodes belongs to another set v. <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/086/787/original/graph.png?1724189664" width = 500 /> Lets Separate the nodes based on the levels. <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/086/788/original/upload_9545b3e72199830e871d4b647cf82ea4.png?1724189702" width = 300 /> Lets say, we have X nodes on set u, and Y nodes on set v. Maximum number of Edges = (x * y) Current number of Edges (Since it is a Tree) = (N-1) ### Extra Edges = [(x * y) - (N - 1)] --- title: Problem 7 Friends Graph description: duration: 600 card_type: cue_card --- ### Problem Statement Given a friendship graph of N persons You can only attend the party if you have minimum K number of friends attending the party. Find total number of people that can attend the party. **Example:** K = 2 Graph: <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/086/789/original/graph.png?1724189748" width = 500 /> ### Observation As we can see that the straight forward appraoch will be, Finding the counting the friends of each node and at last count the nodes which has friends >= K. > But It is Wrong. Lets see why the previous one is wrong! ### Dry Run Can Person 1 can attend the party ? > No, Since they have only one friend. Can Person 6 can attend the party ? > No, Since they have only one friend. Can Person 2 attend the party ? > If we see the Person 2, he has two friends. But the question here is, "There should be Minimum of K friends **attending** the party". Person 1 cannot attend the party for sure. <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/086/790/original/graph.png?1724189795" width = 500 /> Rest of the persons have enough friends to attend the part. <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/086/791/original/graph.png?1724189824" width = 500 /> ### Solution Approach For a given graph, instead of creating a adjacency graph. Along with that, lets create a Hashmap to keep track of the neighbour of each node. <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/086/792/original/graph.png?1724189859" width = 500 /> Insert all the edges in a Min heap, which is used to pop the node with the minimum of edges which has. If the current node has less than K friends. The for all the neightbours reduce by 1. <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/086/793/original/graph.png?1724189892" width = 500 />

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully