SlideShare a Scribd company logo
⽣成樹
lancer
相信⼤家都知道
什麼是⽣成樹
圖
• 如果沒有特別說明,那都是在說無向圖
• G = ( V , E )
• n = | V | , m = | E |
•
•
• etc.











3
4
5
12
7
7
7
6
3
4
5
12
7
7
7
6
最⼩⽣成樹 (MST)
Minimal Spanning Tree
• 如果全部的權重都是正數

得到的會是⼀棵樹 ( Why?
• 可能會有很多個







3
4
5
12
7
7
7
6
3
4
5
12
7
7
7
6
3
4
5
12
7
7
7
6
Kruskal’s Algorithm
• 作法 :
• 按權重從⼩到⼤排好序
• 從權重⼩的邊開始放
• 不要產⽣環
Kruskal’s Algorithm
3
4
5
12
7
7
7
6
3
4
5
12
7
7
7
6
3
4
5
12
7
7
7
6
3
4
5
12
7
7
7
6
3
4
5
12
7
7
7
6
3
4
5
12
7
7
7
6
Kruskal’s Algorithm
• 複雜度 :
• 排序 O( E log E )
• 加邊 O( E α(E,V) )
瓶頸:排序
Borůvka's Algorithm
• 作法:
• 每個分量找權重最⼩的邊連出去
• 權重⼀樣的呢?
Borůvka's Algorithm
3
4
5
12
7
7
7
6
3
4
5
12
7
7
7
6
Borůvka's Algorithm
• 複雜度 :
• 每回合 O( E + V )
• 回合數 最差 O( log V )
• 在 Kruskal 裡,我們需要先排序
• 但有時輸⼊已經排好序了
• 所以在看複雜度的時候

排序的時間另外考慮
應⽤
最⼩瓶頸⽣成樹 (MBST)
Minimum Bottleneck Spanning Tree
• XX的瓶頸: XX上最⼤/最⼩權重
• 最⼩⽣成樹 是⼀棵 最⼩瓶頸⽣成樹











3
4
5
12
7
7
7
6
瓶頸 : 7
例題 1

UVa 12655 Trucks 改
• 給你⼀張圖
• 多筆詢問 Q a b

問 a, b 兩點所有路徑中,瓶頸的最⼩值









a
b
3
4
5
12
7
7
7
6
a
b
3
4
5
12
7
7
7
6
如果是單筆詢問呢?
⼆分搜?
a
b
3
4
5
12
7
7
7
6
a
b
3
4
5
12
7
7
7
6
a
b
3
4
5
12
7
7
7
6
a
b
3
4
5
12
7
7
7
6
再看 Kruskal’s Algorithm
• 作法 :
• 按權重從⼩到⼤排好序
• 從權重⼩的邊開始放
• 不要產⽣環
• ⼀直放邊直到 a, b 連通

則最後放的邊就是瓶頸
改進
• 採⽤ O( N ) 找中位數的⽅法
• 可以改進到 O( E )
沒有講的 Prim
• 類似 Dijkstra
• ⼤家都知道,就不講了
原題轉化
• 預處理: 最⼩⽣成樹
• 詢問: 樹上兩點路徑上最⼤權重
• 複雜度:
• 預處理: O( E log E ) + O( V log V )
• 詢問: O( log V )
例題 2

UVa 11733 Airports
• 現在有 N 個城市
• 要建機場及道路,讓每個城市都可以到機場
• 給你每個城市建機場的代價

以及⼀些可以建的道路與其代價
• 問最⼩的代價是多少?
45
8
14
2
3
4
5
12
7
7
7
6
45
8
14
2
3
4
5
12
7
7
7
6
45
8
14
2
3
4
5
12
7
7
7
6
cost=35
cost=31
cost=21
45
8
14
2
3
4
5
12
7
7
7
6
新增節點:天空
45
8
14
2
3
4
5
12
7
7
7
6
連邊
45
8
14
2
3
4
5
12
7
7
7
6
2
5
4
8
1
4
最⼩⽣成樹
3
4
5
12
7
7
7
6
2
5
4
8
1
4
例題 3

POJ 3522 Slim Span
• 給你⼀張圖
• 問你所有⽣成樹中

最⼤邊與最⼩邊的差最⼩值為何?









3
4
5
12
7
7
7
6
3
4
5
12
7
7
7
6
3
4
5
12
7
7
7
6
max-min=4 max-min=2 max-min=9
• 枚舉最⼩值,找瓶頸
• 複雜度: O( E log E ) + O( E * E )
3
4
5
12
7
7
7
6
3
4
5
12
7
7
7
6
4
5
12
7
7
7
6
5
12
7
7
7
6
min=3
min=4
min=5
想法⼆
• 先找⼀棵最⼤⽣成樹
• 從權重⼤的開始”換邊”
• 複雜度: O( E log E ) + O( E * V )
3
4
5
12
7
7
7
6
3
4
5
12
7
7
7
6
3
4
5
12
7
7
7
6
3
4
5
12
7
7
7
6
max-min=7
max-min=2
max-min=3
max-min=4
想法⼆’
• 改善換邊的複雜度
• 複雜度: O( E log E ) + O( E log V )
延伸:次⼩⽣成樹
• 先找出⼀個最⼩⽣成樹
• 換邊
• 複雜度: O( E log E ) + O( E log V )
例題 4

CF#111 Edges in MST
• 給你⼀張圖
• 請輸出每條邊是哪種邊
1. 不在任何⼀個MST裡
2. 在所有MST裡
3. 在⾄少⼀個MST裡
3
4
5
12
7
7
7
6
1. 不在任何⼀個MST裡
2. 在所有MST裡
3. 在⾄少⼀個MST裡
想法⼀
• 對每個邊測試看看
• 先放,看可不可以變成MST
• 可以: 在⾄少⼀個MST裡
• 不⾏: 不在任何⼀個MST裡
• 不放,看有沒有MST
• 沒有: 在所有MST裡
• 複雜度: O( E log E ) + O( E * E )
想法⼆
• 先找⼀個最⼩⽣成樹
• 換邊
• 複雜度: O( E log E ) + O( E * V )
再看 Kruskal’s Algorithm
• 作法 :
• 按權重從⼩到⼤排好序
• 從權重⼩的邊開始放
• 權重⼀樣的⼀起放
• 不要產⽣環
權重⼀樣的邊產⽣環
不唯⼀
3
3
5
12
7
7
7
6
3
3
5
12
7
7
7
6
12
7
7
76
12
7
7
76
7
7
7
7
7
7
產⽣環
想法三
• 同樣權重的邊⼀起放
• 放下去的邊可以在連通塊上⽣成⼦圖
• ⼦圖上的邊代表有可能在MST裡
• ⼦圖上的橋代表⼀定在MST裡
• 複雜度: O( E log E ) + O( E )
3
3
5
12
7
7
7
8
7
3
3
5
12
7
7
7
8
7 12
7
7
78
7 12
7
7
78
7
3
3
5
12
7
7
7
8
7
3
3
5
12
7
7
7
8
7
3
3
5
12
7
7
7
8
7
3
3
5
12
7
7
7
8
7
1. 不在任何⼀個MST裡
2. 在所有MST裡
3. 在⾄少⼀個MST裡
3
3
5
12
7
7
7
8
7
延伸:有幾個MST
3
3
5
12
7
7
7
8
7
總共三個
3
3
5
12
7
7
7
8
7
3
3
5
12
7
7
7
8
7
延伸:有幾個MST
• 根據乘法原理
• 總個數就是每種權重選擇數的乘積
• 每種權重的選擇數?









3
3
5
12
7
7
7
8
7 總共三個
平⾯MST
• 常常會看到給你平⾯上 N 個點

要你找出最⼩⽣成樹
• 找出Delaunay triangulation,會有O( N )條邊

複雜度: O( N log N )
• 總複雜度: O( N log N )

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

生成樹專題