劉鎧源
    • 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

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

    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.
    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
    # K-core Decomposition and Finding the Maximum Clique in Massive Graphs(Graph theory) # 簡介 社交網絡分析的主要關注點之一是識別網絡中具有凝聚力的子群。有凝聚力的子群體是網路的子集,他們之間存在相對強大、直接、強烈、頻繁或積極的聯繫。 k-Core演算法通常用來對一個圖進行子圖劃分,通過去除不重要的節點,將符合逾期的子圖暴露出來進行進一步分析。 Maximum Clique 演算法可以找出最大完全子圖,找出最大完全子圖在計算上是困難的,這是一個np-complete問題。 本次作業要在限定時間內完成在82168個節點的網路中找出節點的coreness與Maximum Clique。 ## 定義k-core子圖 k-core子圖即要求每個節點至少與該子圖中的其他k個節點相關聯。 ## 定義節點的degree 節點的degree即一個節點在網路中和它相關聯的節點的數目。 ## 定義節點的coreness 節點的coreness為k即代表包含這個節點最大的core子圖為k-core子圖。 ![](https://i.imgur.com/0E00qez.png) 圖1,圖中藍色的節點的coreness為3、綠色的節點的coreness為2、黃色的節點的coreness為1。 k-core子圖可以由以下psuedo-code得到, ``` Input:圖G, for(核心度k=1,2,3,...) Step 1:將圖G中度數小於k的頂點全部移除,得到子圖G'。 Step 2:將圖G'中度數小於k的頂點全部移除,得到新子圖G''。該子圖G''就是最終k-Core劃分的結果子圖。 ``` ![](https://i.imgur.com/76OPY4N.png) 圖2,求解3-core的過程。 根據[An O(m) Algorithm for Cores Decomposition of Networks](https://arxiv.org/pdf/cs/0310049.pdf?fbclid=IwAR02bLrnHrf4jOZkMef0DB2Q-CjCNMbI2bvoO2I5NghP7K-W8q02b4HsdDU)這篇論文 k-core算法可以表示成以下pseudo-code ![](https://i.imgur.com/TjMdbKp.jpg) 圖3,k-core算法pseudo-code。 演算法中有兩個地方要做排序,分別是第一種:一開始(1.2),第二種:挑選完節點u之後(2.2.1.2), 尤其是第二種情況可能要執行非常多次(至少在兩個for迴圈內),[An O(m) Algorithm for Cores Decomposition of Networks](https://arxiv.org/pdf/cs/0310049.pdf?fbclid=IwAR02bLrnHrf4jOZkMef0DB2Q-CjCNMbI2bvoO2I5NghP7K-W8q02b4HsdDU)給出一種有效率的方法來減少(1.2)與(2.2.1.2)的時間複雜度,完成k-core演算法。 ![](https://i.imgur.com/lEhDTUO.png) 圖4.高效率k-core演算法。 8~12行 計算出所有節點的max degree為多少, ```c= for (int v = 0; v < n; v++) { d = 0; vector<int>::iterator u; //計算所有node的degree並存在Degree[v]中 for (u = adj[v].begin(); u != adj[v].end(); ++u) { d++; } Degree[v] = d; if (d > md) { md = d; } } ``` 13~27行 有了max degree後就可以用counting sort完成一開始(1.2)的排序,時間複雜度為O(n)。 ```c= //宣告一個array:bin 存degree = index的點有幾個 int* bin = new int[md + 1]; for (int d = 0; d <= md; d++) { bin[d] = 0; } for (int v = 0; v < n; v++) { bin[Degree[v]]++; } //用counting sort排序,bin[i]紀錄degree為i的節點應在vert[]第幾個位置排起 //pos[i]紀錄ID為i的節點在vert[]的第幾個位置,vert[i]紀錄第i個位置的節點的ID。 start = 0; for (int d = 0; d < md + 1; d++) { num = bin[d]; bin[d] = start; start = start + num; } //使用bin[]幫忙計算節點的位置。 for (int v = 0; v < n; v++) { pos[v] = bin[Degree[v]]; vert[pos[v]] = v; bin[Degree[v]]++; } //還原bin[]的值 for (int d = md; d >= 1; d--) { bin[d] = bin[d - 1]; } bin[0] = 0; ``` ![](https://i.imgur.com/c8p3I85.png) 圖5.各種陣列的關係 28~41行 原本圖3中挑完節點u之後Vert要重新依照degree做排序,實際上只要做一次交換就可以完成排序。 ```c= for (int i = 0; i < n; i++) { v = vert[i]; vector<int>::iterator u; for (u = adj[v].begin(); u != adj[v].end(); ++u) { if (Degree[*u] > Degree[v]) { //交換節點u與節點w //節點w是在矩陣Vert[]中第一個與節點u有一樣degree的節點 int pu, du, pw, w; du = Degree[*u]; pu = pos[*u]; pw = bin[du]; w = vert[pw]; if (*u != w) { pos[*u] = pw; vert[pu] = w; pos[w] = pu; vert[pw] = *u; } bin[du]++; Degree[*u]--; } } } ``` ![](https://i.imgur.com/CBzvwr6.png) 圖6. 假設原本u的節點為Degree 3,Degree-1為2,把u放在Degree 3的節點的開頭位置(w位置),u就會在Degree 2的節點的最後面,就能以時間複雜度O(1)完成排序。(之後記得更新bin[ ]) ![](https://i.imgur.com/xiClzUN.png) 圖7. 最後就能以時間複雜度O(n)完成所有節點coreness的計算。 # Finding the Maximum Clique ## 定義Maximum Clique Maximum Clique subgraph即子圖中的所有節點都與其他節點相關聯。 ![](https://i.imgur.com/rxEzpX1.png) 圖8.Maximum Clique示意圖,節點ABCDE為Maximum Clique。 在維基百科中[Bron–Kerbosch algorithm](https://en.wikipedia.org/wiki/Bron%E2%80%93Kerbosch_algorithm) ``` algorithm BronKerbosch1(R, P, X) is if P and X are both empty then report R as a maximal clique for each vertex v in P do BronKerbosch1(R ⋃ {v}, P ⋂ N(v), X ⋂ N(v)) P := P \ {v} X := X ⋃ {v} ``` 此演算法採用窮舉所有可能的子圖(當然可以找到解)。時間複雜度 O(n⋅3ⁿᐟ³)。 此算法枚舉了大量不是Maximum Clique的集合,浪費許多時間。 我觀察演算法,發現到一開始就挑到對的節點,後面不是答案的窮舉就可以用[Branch and bound](https://en.wikipedia.org/wiki/Branch_and_bound)省略。 那要怎麼樣找到對的節點呢? 於是我就想到了可以用coreness先將節點做排序,節點coreness為k代表至少該節點有一個subgraph內所有節點都至少是k degree。一個Maximum Clique內若有n個節點,則所有節點的coreness至少都為n,(相反不一定成立,n core subgraph不一定是clique),所以優先挑選coreness最大的節點,會最有可能找到Maximum Clique。 ```c= //Q為已經挑選的節點的集合 , R為可以挑選的節點的集合已經按照coreness小到大排序 void get_max_clique(vector<Rnode> R, vector<Rnode> Q) { //若找到的集合Q>已知的最大Clique集合,更新已知的最大Clique集合 if (Q.size() > curmax) { Qmax = Q; curmax = Qmax.size(); } if (R.size() > 0) { vector<Rnode> newR; Rnode u; int maxcoreness; while (!R.empty()) { maxcoreness = count_max_coreness(R); //branch and bound //若R集合中的節點coreness都不大於curmax,則不必再計算, if (maxcoreness + 1 > curmax) { //pick a node which has max coreness in set R u = R.back(); newR.clear(); Q.push_back(u); vector<Rnode>::iterator a; for (a = R.begin(); a != R.end(); ++a) { if (connected(a->id, u.id)) { newR.push_back(*a); } } get_max_clique(newR, Q); Q.pop_back(); R.pop_back(); //} } else return; } } return; } ``` 後來順利在時間內完成。 ## 論文參考 寫這篇文章的當下發現說使用coreness與[Bron–Kerbosch algorithm](https://en.wikipedia.org/wiki/Bron%E2%80%93Kerbosch_algorithm)中的提到 With vertex ordering是一樣的方法, degeneracy的定義: >In graph theory, a k-degenerate graph is an undirected graph in which every subgraph has a vertex of degree at most k: that is, some vertex in the subgraph touches k or fewer of the subgraph's edges. The degeneracy of a graph is the smallest value of k for which it is k-degenerate. The degeneracy of a graph is a measure of how sparse it is, and is within a constant factor of other sparsity measures such as the arboricity of a graph. > 與coreness定義不同但結果相同。 Bron–Kerbosch with degeneracy演算法時間負責度為O(dn^(3d/3)),d為coreness(degeneracy)。 我的演算法結合了[Branch and bound](https://en.wikipedia.org/wiki/Branch_and_bound)與Bron–Kerbosch with degeneracy,與論文[Fast Maximum Clique Algorithms for Large Graphs](https://www.cs.purdue.edu/homes/dgleich/publications/Rossi%202014%20-%20max-clique.pdf)有相似的psuedo-code ![](https://i.imgur.com/aW0tcpc.png) 該論文表示在真實世界的網路中執行時間在log scale下有接近線性時間。 ![](https://i.imgur.com/hnUHEE8.png) 完整的code: ```c= #include <iostream> #include<vector> #include<list> #include<set> #include <algorithm> #include<signal.h> #include<fstream> #include<string> #define publicEdge 2925046 #define privateEdge 2110828 #define publicvertices 82168 #include<time.h> using namespace std; vector<int> adj[82168]; int coreness[82168]; bool connected(int i, int j); int curmax; int max_v = 0; struct Rnode { int id; int degree; int core; }; vector<Rnode>Q; vector<Rnode> Qmax; struct Result { int id; int color; }; bool compareBydegree(const Rnode& a, const Rnode& b) { return a.degree < b.degree; } bool compareBycore(const Rnode& a, const Rnode& b) { return a.core < b.core; } bool compareByid(const Rnode& a, const Rnode& b) { return a.id < b.id; } bool comp(const int& num1, const int& num2) { return num1 > num2; } void signalHandler(int signum) { //cout << "Get signal " << signum << endl; //cout << "MAXQ: " << endl; sort(Qmax.begin(), Qmax.end(), compareByid); vector<Rnode>::iterator c; fstream out; out.open("clique.txt", ios::out); for (c = Qmax.begin(); c != Qmax.end(); ++c) { out << c->id << endl; } out.close(); exit(signum); } // A utility function to add an edge in an // undirected graph. void addEdge(vector<int> adj[], int u, int v, int Degree[]) { Degree[u]++; Degree[v]++; adj[u].push_back(v); adj[v].push_back(u); } void printGraph(vector<int > adj[], int V) { int v; vector<int>::iterator h; for (int u = 0; u < V; u++) { cout << "Node " << u << " makes an edge with \n"; for (h = adj[u].begin(); h != adj[u].end(); h++) { v = *h; cout << "\tNode " << v << " with edge"; } cout << "\n"; } } void kcore(int k) { int* Degree = new int[max_v]; int n = max_v; int* vert = new int[max_v]; int* pos = new int[max_v]; int md = 0; //maxdegree int d = 0; //degree int num; int start; fstream out2; int v; for (int v = 0; v < n; v++) { d = 0; vector<int>::iterator u; //計算所有node的max degree並存在Degree[v]中 for (u = adj[v].begin(); u != adj[v].end(); ++u) { d++; } Degree[v] = d; if (d > md) { md = d; } } //宣告一個array:bin 存maxdegree = index的點有幾個 int* bin = new int[md + 1]; for (int d = 0; d <= md; d++) { bin[d] = 0; } for (int v = 0; v < n; v++) { bin[Degree[v]]++; } start = 0; for (int d = 0; d < md + 1; d++) { num = bin[d]; bin[d] = start; start = start + num; } for (int v = 0; v < n; v++) { pos[v] = bin[Degree[v]]; vert[pos[v]] = v; bin[Degree[v]]++; } for (int d = md; d >= 1; d--) { bin[d] = bin[d - 1]; } bin[0] = 0; for (int i = 0; i < n; i++) { v = vert[i]; vector<int>::iterator u; for (u = adj[v].begin(); u != adj[v].end(); ++u) { if (Degree[*u] > Degree[v]) { //交換節點u與節點w //節點w是在矩陣Vert[]中第一個與節點u有一樣degree的節點 int pu, du, pw, w; du = Degree[*u]; pu = pos[*u]; pw = bin[du]; w = vert[pw]; if (*u != w) { pos[*u] = pw; vert[pu] = w; pos[w] = pu; vert[pw] = *u; } bin[du]++; Degree[*u]--; } } } out2.open("kcore.txt", ios::out); for (int i = 0; i < max_v; i++) { if (Degree[i] >= k) { out2 << i << " " << Degree[i] << endl; } coreness[i] = Degree[i]; } out2.close(); } bool connected(int i, int j) { vector<int>::iterator a; for (a = adj[i].begin(); a != adj[i].end(); ++a) { if (*a == j) return true; } return false; } int count_max_coreness(vector<Rnode> R) { vector<Rnode>::iterator a; int maxcoreness = 0; for (a = R.begin(); a != R.end(); ++a) { if (coreness[a->id] > maxcoreness) { maxcoreness = coreness[a->id]; } } return maxcoreness; } void get_max_clique(vector<Rnode> R, vector<Rnode> Q) { if (Q.size() > curmax) { Qmax = Q; curmax = Qmax.size(); } if (R.size() > 0) { vector<Rnode> newR; Rnode u; int maxcoreness; while (!R.empty()) { maxcoreness = count_max_coreness(R); //branch and bound //curmax是節點數量 coreness是Degree故需加一 if (maxcoreness + 1 > curmax) { //pick a node which has max coreness in set R u = R.back(); newR.clear(); Q.push_back(u); vector<Rnode>::iterator a; for (a = R.begin(); a != R.end(); ++a) { if (connected(a->id, u.id)) { newR.push_back(*a); } } get_max_clique(newR, Q); Q.pop_back(); R.pop_back(); //} } else return; } } return; } int main(int argc, char* argv[]) { fstream out3; signal(SIGINT, signalHandler); double START, END; START = clock(); string file_name = string(argv[1]); string K = string(argv[2]); int k = stoi(K); int Degree[82168]; int V = 82168; vector<Rnode> R; ifstream in; in.open(file_name); int a, b; while (in >> a >> b) { addEdge(adj, a, b, Degree); if (b > max_v) { max_v = b; } } in.close(); //初始化R Rnode Relement; max_v++; for (int i = 0; i < max_v; i++) { Relement.degree = Degree[i]; Relement.id = i; R.push_back(Relement); } kcore(k); vector<Rnode>::iterator e; for (e = R.begin(); e != R.end(); ++e) { //cout<<" e id:"<<e->id; e->core = coreness[e->id]; } out3.open("clique.txt", ios::out); sort(R.begin(), R.end(), compareBycore); get_max_clique(R, Q); //cout << "MAXQ: " << endl; sort(Qmax.begin(), Qmax.end(), compareByid); vector<Rnode>::iterator c; for (c = Qmax.begin(); c != Qmax.end(); ++c) { out3 << c->id << endl; } out3.close(); cout << endl; END = clock(); //cout << "Execution time:" << (END - START) / CLOCKS_PER_SEC << " sec" << endl; } ``` ## 還可以改善的地方 在`get_max_clique`中的`count_max_coreness`時間複雜度為O(n),因為在集合R中的節點已經按照coreness作排列,故要取得max_coreness只要檢查集合R中的最後一個節點即可,可以降低時間複雜度至O(1)。 再增加Backtracking演算法,若Q集合的數目加上R集合的數目<已知Maximum Clique的數目,可以直接return。 程式碼100~110行計算節點的Degree時可以直接使用STL vector.size()降低時間複雜度。

    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

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    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