K-DA
  • 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    [toc] ## Các khái niệm Đồ thị là một cấu trúc rời rạc gồm các đỉnh và các cạnh nối với các đỉnh đó. Được mô tả hình thức: $$ G = (V, E) $$ Trong đó: - $V$ gọi là tập các đỉnh (Vertices) và $E$ gọi là tập các cạnh (Edges). Có thể coi $E$ là tập các cặp $(u,v)$ với $u, v \in V$. ### Đồ thị vô hướng Đồ thị $G$ là đồ thị vô hướng khi $u$ nối với $v$ cũng là $v$ nối với $u$, $(u, v) = (v,u)$. ### Đồ thị có hướng Đồ thị $G$ là đồ thị có hướng khi $u$ nối với $v$ khác $v$ nối với $u$, $(u, v) \neq (v,u)$. ## Biểu diễn đồ thị ### Ma trận kề - Ma trận kề của đồ thị $𝐺$, ký hiệu $𝐵(𝐺)$, là một ma trận nhị phân cấp $𝑁 \times 𝑁$: $𝐵 = (𝐵_{𝑖,𝑗})$ với $𝐵_{𝑖,𝑗}$ được định nghĩa: + Với đồ thị không có trọng số + $𝐵_{𝑖,𝑗} = 1$ nếu có cạnh nối $𝑖$ tới $𝑗$ + $𝐵_{𝑖,𝑗} = 0$ trong trường hợp ngược lại + Với đồ thị có trọng số + $𝐵_{𝑖,𝑗} = x$ nếu có cạnh nối $𝑖$ tới $𝑗$ với trọng số $x$ + $𝐵_{𝑖,𝑗} = 0$ nếu không có cạnh nối $i$ tới $j$ - Trong c++ ta dùng mảng 2 chiều để biểu diễn ma trận kề. ![](https://hackmd.io/_uploads/S1_kbHku3.png) ### Danh sách kề - Là danh sách các mảng hoặc danh sách liên kết, trong đó mỗi phần tử tương ứng với một đỉnh trong đồ thị. Mỗi phần tử trong danh sách này chứa thông tin về các đỉnh kề của đỉnh tương ứng đó. - Trong C++ ta sử dụng một mảng `vector` để biểu diễn danh sách kề. ![](https://hackmd.io/_uploads/SkgjVrJuh.png) ### Danh sách cạnh - Là danh sách các cặp đỉnh, mỗi cặp đỉnh tương ứng với một cạnh trong đồ thị. Mỗi cạnh được biểu diễn bằng hai đỉnh mà nó kết nối. ![](https://hackmd.io/_uploads/SJzsrBJdn.png) ## Duyệt đồ thị ### Theo chiều sâu (Depth-First Search - DFS) Dựa trên ý tưởng quay lui. Ta sẽ đi toàn bộ đường, sau đó khi không thể đi được nữa ta sẽ quay lui để tìm đường khác. ```cpp= vector <int> adj[MAX_N]; // danh sách kề bool lab[MAX_N]; // mảng xác định một đỉnh đã được xét nếu là 1, chưa nếu là 0 void DFS(int u){ // xử lý đỉnh u (tùy theo yêu cầu đề) lab[u] = 1; // đánh dấu đỉnh u đã được xét for (int i=0; i<adj[u].size(); i++){ // duyệt qua những đỉnh kề với đỉnh u đang xét int v = adj[u][i]; // v là đỉnh kề u if (lab[v] == 0){ // nếu đỉnh v chưa được xét DFS(v); // xét tiếp đỉnh v tiếp theo } } } ``` ### Theo chiều rộng (Breadth-First Search - BFS) Ta sẽ tìm tất cả các đỉnh kề $s$, sau đó tiếp tục từ các đỉnh đó sẽ đến các đỉnh kề. Sử dụng `queue` để lưu các đỉnh sẽ được thăm. ```cpp= vector <int> adj[MAX_N]; // danh sách kề bool lab[MAX_N]; // mảng xác định một đỉnh đã được xét nếu là 1, chưa nếu là 0 void BFS(int s){ queue <int> q; q.push(s); // đưa đỉnh bắt đầu vào hàng đợi // xử lý đỉnh s lab[s] = 1; // đánh dấu s đã được xét while(s.size() > 0){ // lặp khi hàng đợi khác rỗng int u = q.front(); // lấy ra phần tử đầu hàng đợi for (int i=0; i<adj[u].size(); i++){ int v = adj[u][i]; if (lab[v] == 0){ // nếu v chưa được xét // xử lý đỉnh v lab[v] = 1; // đánh dẫu đỉnh v đã được xét q.push(v); // đưa đỉnh kề chưa được xét vào hàng đợi để xét tiếp trong tương lai } } } } ``` ![](https://hackmd.io/_uploads/B1RU8cx_2.gif) [Minh họa thuật toán ở đây](https://visualgo.net/en/dfsbfs) ## Một số bài toán về đồ thị ### Tìm đường đi ngắn nhất #### Dijkstra Thuật toán này đã được học ở môn Cấu Trúc Rời Rạc, chúng ta đã làm nhiều nên mình không cần nói chi tiết ở đây :v. Thuật toán tìm đường đi ngắn nhất từ $1$ đỉnh đến các đỉnh còn lại trong đồ thị. Độ phức tạp: $O(N\log(N))$ ```cpp= const int mN = 2e5 + 10; // khởi tạo giới hạn ban đầu const long long oo = 1e18 + 10ll; // định nghĩa dương vô cùng typedef pair<long long, int> pli; // định nghĩa lại kiểu dữ liệu để tiện hơn int numNode, numEdge; // khai báo số đỉnh, số cạnh long long d[mN]; // khởi tạo mảng khoảng cách vector <pli> adj[mN]; // khai báo danh sách kề void dijkstra(int start){ // gọi hàm dijkstra với đỉnh bắt đầu là start priority_queue <pli, vector<pli>, greater<pli>> pq; // khai báo hàng đợi ưu tiên sắp xếp với ưu tiên trọng số giảm dần int u, v; long long du, uv; for (int i = 1; i <= numNode; ++i) d[i] = oo; // khởi tạo khoảng cách ban đầu là dương vô cùng d[start] = 0; // cho đỉnh bắt đầu có khoảng cách là 0 pq.push(pli(0ll, start)); // đưa trọng số khoảng cách của đỉnh bắt đầu vào trong while (pq.size()) { // trong khi vẫn còn đỉnh trong hàng đợi u = pq.top().second; // lấy ra đỉnh u để xét tiếp các cạnh kế tiếp du = pq.top().first; // lấy ra khoảng cách ngắn nhất từ start đên u pq.pop(); // xóa ra khỏi hàng đợi ưu tiên if (du != d[u]) continue; // nếu đã xét từ trước thì không cần xét tiếp for (int i = 0; v=adj[u][i].second; i++) { // lấy đỉnh kề của u là v uv = adj[u][i].first; // lấy trọng số kề của u if (d[v] > du + uv) { // nếu trọng số khoảng cách của đỉnh v lớn hơn tổng khoảng cách từ đỉnh nguồn đến u và trọng số cạnh u đến v thì cập nhật lại trọng số khoảng cách mới cho đỉnh v d[v] = du + uv; pq.push(pli(d[v], v)); // đưa vào hàng đợi để xét tiếp các đỉnh kế với v } } } } ``` ![](https://hackmd.io/_uploads/rk06H9gun.gif) ### Tìm cây khung nhỏ nhất/lớn nhất 2 thuật toán này cũng đã quá quen với chúng ta ở môn Cấu trúc rời rạc. #### Kruskal Ta sẽ sử dụng cấu trúc dữ liệu [Disjoint set union](https://vnoi.info/wiki/algo/data-structures/disjoint-set-union.md) để có thể nối các cạnh vào thành cây khung và các cây khung với nhau. ```cpp= int par[maxN]; //Lưu node gốc của tập hợp //Nếu node đó là gốc thì mảng thể hiện số lượng node có trong tập hợp. int root(int x){ //tìm gốc của node đồng thời tối ưu cho những lần sau if(par[x] < 0? x:(par[x] = root(par[x]))) } void union(int x, int y){ //nếu đã chung gốc => chung tập hợp, không cần nối if((x = root(x) == (y = root(y)) return; //nếu số lượng ở tập x nhỏ hơn y, ta nối x vào y và ngược lại if(par[x] < par[y]) swap(x, y); par[x] += par[y]; par[y] = x; } struct edge{ //danh sách cạnh int u, v, w; edge(int uu = 0, int vv = 0, int ww = 0){ u = uu; v = vv; w = ww; } //overload bool operator < (edge& x){ return this -> w < x.w; } bool operator <= (edge& x){ return this -> w <= x.w; } bool operator > (edge& x){ return this -> w > x.w; } bool operator >= (edge& x){ return this -> w >= x.w; } }; pair<vector<edge>, int> kruskal(vector<edge>& el){ sort(el.begin(), el.end()); vector<edge> spanningTree; for(int i = 1; i <= n; i++){ par[i] = -1; } int edges = 0; //số cạnh trong cây khung = n - 1, chỉ cần chạy tới đó int totalWeight = 0; for(auto e: el){ //nếu chung tập hợp, bỏ qua if(root(e.u) == root(e.v)) continue; totalWeight += e.w; union(e.u, e.v); spanningTree.push_back(e); edges++; if(edges == n - 1) break; } return make_pair(spanningTree, totalWeight); } ``` ![](https://hackmd.io/_uploads/B1Ewvcxdn.gif) #### Prim ```cpp= typedef pair<int, int> ii; // định nghĩa kiểu dữ liệu để thuận tiện gọi hơn const int mN = 2e5 + 10; // khởi tạo giới hạn const int oo = 1e9 + 10ll; // khởi tạo dương vô cùng int numNode, numEdge, d[mN]; // số đỉnh, số cạnh, trọng số khoảng cách vector <ii> adj[mN]; // khai báo danh sách cạnh kề int prim(int u) { // bắt đầu random ở một đỉnh bất kỳ int totalWeight = 0, v, du, uv; // khai báo priority_queue<ii, vector<ii>, greater<ii>> pq; // tạo một hàng đợi ưu tiên đề lấy trọng số nhỏ nhất để ưu tiên lấy cạnh đó ra for (int i = 1; i <= numNode; i++) d[i] = oo; // khởi tạo mọi đỉnh có khoảng cách dương vô cùng pq.push(ii(0, u)); // đưa đỉnh bắt đầu vào hàng đợi d[u] = 0; // cho đỉnh bắt đầu khoảng cách là 0 while (pq.size()) { // khi còn đỉnh để xét u = pq.top().second; // lấy ra đỉnh u để xét tiếp các cạnh kế tiếp du = pq.top().first; // lấy ra trọng số cạnh hiện tại pq.pop(); // xóa ra khỏi hàng đợi ưu tiên if (du != d[u]) continue; // nếu đã xét từ trước thì không cần xét tiếp totalWeight += du; // lấy cạnh thêm vào tăng đúng 1 lượng trọng số cạnh d[u] = 0; // xóa đỉnh u bằng cách gán trọng số bằng 0 for (int i = 0; i < (int) adj[u].size(); i++) { v = adj[u][i].second; // xét đỉnh kế tiếp uv = adj[u][i].first; // xét cạnh kề if (d[v] > uv) { // nếu cạnh trước đó gán cho v chưa phải cạnh nhỏ nhất d[v] = uv; // cập nhật cạnh nhỏ nhất (u, v) cho tập cạnh pq.push({d[v], v}); // đưa vào hàng đợi để xét tiếp } } } return totalWeight; // trả về tổng số cạnh } ``` ![](https://hackmd.io/_uploads/HysiPceun.gif)

    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