Star Edu
      • 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
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners 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
    • 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 Help
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
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- tags: Star Edu title: Star Edu, Graph Theory author: Vo Hoang Anh - SPyofgame license: Private Use --- <style> .markdown-body { max-width: 1800px; } .orange { color: orange; } .green { color: green; } .red { color: red; } section.present > div > pre > code { max-height: 700px; max-width: 2000px; font-size: 16px; } section.present > div > blockquote > pre > code { font-size: 16px; } section.present > div > blockquote > code { font-size: 16px; } section.present > div > blockquote { margin: calc(var(--r-block-margin) / 2) auto; width: 800px; } h3 > span { font-size: 60px; color: #ff88ff; } h4 > span { font-size: 60px; color: #ee4400; } h5 > span { font-size: 27px; color: orange; } section.present > div > blockquote > p { margin: 0; } .image-figure { margin-right: 100px; text-align: center; } .image-figure img { height: 500px; width: 500px; } .table { width: 100%; font-size: 20px; } .table td { height: 100px; width: 500px; } .table figure { position: relative; height: 150px; margin: 0; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; } .table img { height: 100px; margin: 0; top: 0; background: transparent; } figcaption { text-align: center; font-size: 27px; width: 100%; bottom: 0; } </style> ## Graph Traversal --- ## Graph Traversal Graph traversal algorithms are techniques used to visit all nodes (or vertices) within a graph in a specific order. These algorithms are fundamental for various computational tasks such as searching, pathfinding, and connectivity analysis. As they traverse the graph, they can gather and compute relevant information. --- ## Graph Traversal The most common graph traversal algorithms are depth first search (DFS) and breadth first search (BFS), each with its fundamental characteristics and applications. Their ideas are straightforward and simple to learn. --- ### I. Depth-First Search: DFS #### I.1 - Abstract Depth First Search (DFS) is a fundamental graph traversal algorithm that explores as deep into the graph as possible before backtracking. It can used to find the lexicographical smallest path from a source node to all reachable nodes by sorting the node's children beforehand (so-called topologically shortest path). --- ### I. Depth-First Search: DFS #### I.2 - Background Depth First Search (DFS) dates back to early graph theory studies and remains essential for modern applications in areas like AI, compilers, and network analysis. It follows a systematic approach of exploring as far as possible along a branch before retracing steps to explore unvisited paths. This characteristic makes DFS suitable for tasks requiring exhaustive exploration and backtracking, such as maze solving and topological sorting. --- ### I. Depth-First Search: DFS #### I.3 - Concepts > **Node**: Smallest individual unit of a graph abstractically represent a point or entity. > **Edge**: The simplest relation of two nodes that define a pathway between them. > **Stack**: Manages nodes need to be explored, can be used for the recursion. > **Visited**: Keeps track of explored nodes, to avoid revisiting one node indefinitely. > **Source**: The starting point for DFS traversal, can be considered as DFS root. > **Path**: The sequence of traversed nodes using edges of pairwisely adjacent nodes. > **Pre-order**: A parent node is processed before any of its child nodes is done. > **Discovery Time**: Timestamp of a node's first visited in a traversal order. > **Finish Time**: Timestamp of a fully visited nodes in a traversal order. > **Depth**: Indicates how far into the graph DFS has traversed from the source. > **Reachable Node**: A node that can be traversed via some paths starting from a source. > **Adjacency Node**: A node that is directly reachable from another node through an edge. --- ### I. Depth-First Search: DFS #### I.4 - Details: Ideas ```cpp= Init: Mark all nodes as unvisited. If you want lexicographically smallest order, sort the node adjacency list. Start at the root node or any arbitrary node (this is the DFS root). Algo: Mark the node as visited. For each unvisited adjacent node, recursively call DFS. Backtrack to the last visited node once all adjacent nodes are visited. ``` --- ### I. Depth-First Search: DFS #### I.4 - Details: Pseudo-code ```cpp= dfs(node u): mark u as visisted for each unvisited adjacency node v dfs(v) ``` --- ### I. Depth-First Search: DFS #### I.5 - Example: Simple Case <center> <figure style="margin-right: 100px; text-align: center;"> <img src="https://hackmd.io/_uploads/rkSXsqab1l.png" alt="" style="height:384px"> <figcaption>1. Given an example graph<br>This graph contains 9 nodes and 11 edges</figcaption> </figure> </center> --- ### I. Depth-First Search: DFS #### I.5 - Example: Simple Case <center> <figure style="margin-right: 100px; text-align: center;"> <img src="https://hackmd.io/_uploads/S1s9RcaZ1e.png" alt="" style="height:384px"> <figcaption>2. We can represent the graph data in three common representations<br>Either matrix, edge list or adjacency list can help us with DFS</figcaption> </figure> </center> --- ### I. Depth-First Search: DFS #### I.5 - Example: Simple Case <center> <figure style="margin-right: 100px; text-align: center;"> <img src="https://hackmd.io/_uploads/Hyx6JoTb1l.png" alt="" style="height:384px"> <figcaption>3. Let start with an arbitrary node, says node <b>(1)</b><br>This node will be our DFS root from now</figcaption> </figure> </center> --- ### I. Depth-First Search: DFS #### I.5 - Example: Simple Case <center> <figure style="margin-right: 100px; text-align: center;"> <img src="https://hackmd.io/_uploads/rkb82q6Zkg.png" alt="" style="height:384px"> <figcaption>4. From <b>(1)</b> we can either traverse to <b>(2)</b>, <b>(3)</b> or <b>(4)</b><br>For lexicographically smallest DFS, we pick <b>(2)</b></figcaption> </figure> </center> --- ### I. Depth-First Search: DFS #### I.5 - Example: Simple Case <center> <figure style="margin-right: 100px; text-align: center;"> <img src="https://hackmd.io/_uploads/SyE0A9TW1l.png" alt="" style="height:384px"> <figcaption>5. From <b>(2)</b> we can either traverse to the unvisited nodes <b>(3)</b> or <b>(4)</b><br>For lexicographically smallest DFS, we pick <b>(3)</b></figcaption> </figure> </center> --- ### I. Depth-First Search: DFS #### I.5 - Example: Simple Case <center> <figure style="margin-right: 100px; text-align: center;"> <img src="https://hackmd.io/_uploads/HyAlkj6b1g.png" alt="" style="height:384px"> <figcaption>6. From <b>(3)</b>, we can only traverse to the unvisited node <b>(5)</b><br>Since all its other adjacency nodes <b>(1)</b> and <b>(2)</b> are already visisted<br>Notice that two edges <b>(3-5)</b> and <b>(6-9)</b> are crossed but <b>NOT</b> connected</figcaption> </figure> </center> --- ### I. Depth-First Search: DFS #### I.5 - Example: Simple Case <center> <figure style="margin-right: 100px; text-align: center;"> <img src="https://hackmd.io/_uploads/H132xspZ1e.png" alt="" style="height:384px"> <figcaption>7. From <b>(5)</b>, we can traverse to either unvisited nodes <b>(7)</b> or <b>(8)</b><br>Again, we will choose <b>(7)</b> for obvious reason</figcaption> </figure> </center> --- ### I. Depth-First Search: DFS #### I.5 - Example: Simple Case <center> <figure style="margin-right: 100px; text-align: center;"> <img src="https://hackmd.io/_uploads/H12PZs6WJl.png" alt="" style="height:384px"> <figcaption>8. From <b>(7)</b>, we can only traverse to the unvisited <b>(4)</b><br>Notice that two edges <b>(5-8)</b> and <b>(4-7)</b> are crossed but <b>NOT</b> connected</figcaption> </figure> </center> --- ### I. Depth-First Search: DFS #### I.5 - Example: Simple Case <center> <figure style="margin-right: 100px; text-align: center;"> <img src="https://hackmd.io/_uploads/SJFnfs6Wyg.png" alt="" style="height:384px"> <figcaption>9. From <b>(4)</b>, we can only traverse to the unvisited <b>(7)</b><br>We can no longer visit any other nodes, so we stop the DFS</figcaption> </figure> </center> --- ### I. Depth-First Search: DFS #### I.5 - Example: Simple Case <center> <figure style="margin-right: 100px; text-align: center;"> <img src="https://hackmd.io/_uploads/BJNfjsTZ1g.png" alt="" style="height:384px"> <figcaption>10. Let add little information to our DFS traversal at root <b>(1)</b><br>Only two nodes aren't visisted are <b>(6)</b> and <b>(9)</b></figcaption> </figure> </center> --- ### I. Depth-First Search: DFS #### I.5 - Example: Simple Case <center> <figure style="margin-right: 100px; text-align: center;"> <img src="https://hackmd.io/_uploads/S1h58sTWyg.png" alt="" style="height:384px"> <figcaption>11. Here is our DFS Tree, with depth marked<br>Excepts the two nodes <b>(6)</b> and <b>(9)</b> are not reachable from (1)</figcaption> </figure> </center> --- ### I. Depth-First Search: DFS #### I.6 - Specials In some tree structures, DFS can find the shortest path as the path between two nodes is always unique, but in general graphs, it may not find the shortest path. --- ## Breadth-First Search: BFS

    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 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