SlideShare a Scribd company logo
1 of 33
最短経路問題
立命館大学理工学部数理科学科3回生谷口泰地
ダイクストラ法で行く
二部グラフ
「ノードに色を与える。その際、隣接するノード
は同じ色を与えてはいけない。」
二部グラフ
「ノードに色を与える。その際、隣接するノード
は同じ色を与えてはいけない。」
二色だけで塗り切れるグラフを二部グラフをいう。
二部グラフ 二部グラフの例
10
2 3
二部グラフ 二部グラフの例
10
2 3
0に黄色を塗る
二部グラフ 二部グラフの例
10
2 3
0と隣接している
1,3に別の色を塗る
二部グラフ 二部グラフの例
10
2 3
1に隣接している2に1
とは別の色を塗る
2と3も違う色になっ
ている!!
二部グラフ
今の例のように一つのノードの色が決ま
ればそれに隣接しているノードの色が決
定するので、二部グラフ判定アルゴリズ
ムはそれほど難しくない。(はず...)
二部グラフ
10
2
3
4
二部グラフ
10
2
3
4
実行結果
二部グラフでしたっ!
二部グラフ
10
2
3
4
二部グラフでしたっ!
最短経路問題
今回のセミナーの主目的
ダイクストラ法
ダイクストラ法
• 次のような重み付きグラフの最短経路を考える。
A B
G
FD
E
C
5
2
4
2
6
1
3
10
9
5
ダイクストラ法
• 次のような重み付きグラフの最短経路を考える。
A B
G
FD
E
C
5
2
4
2
6
1
3
10
9
5
ダイクストラ法
• アルゴリズム
①各ノードiにd_i = ∞を振る
A B
G
FD
E
C
5
2
4
2
6
1
3
10
9
5
d = ∞
d = ∞
d = ∞
d = ∞
d = ∞
d = ∞
d = ∞
ダイクストラ法
• アルゴリズム
②出発点であるAだけd=0とする。
A B
G
FD
E
C
5
2
4
2
6
1
3
10
9
5
d = 0
d = ∞
d = ∞
d = ∞
d = ∞
d = ∞
d = ∞
ダイクストラ法
• アルゴリズム
③Aに隣接しているノードを更新。
A B
G
FD
E
C
5
2
4
2
6
1
3
10
9
5
d = 0
d =5 < ∞
d = ∞
d =2< ∞
d = ∞
d = ∞
d = ∞
ダイクストラ法
• アルゴリズム
②Aに隣接しているノードを更新。
A B
G
FD
E
C
5
2
4
2
6
1
3
10
9
5
d = 0
d =5
d = ∞
d =2
d = ∞
d = ∞
d = ∞
ダイクストラ法
• アルゴリズム
④Aを除外
A B
G
FD
E
C
5
2
4
2
6
1
3
10
9
5
d = 0
d =5
d = ∞
d =2
d = ∞
d = ∞
d = ∞
ダイクストラ法
• アルゴリズム
④Bのdは決定
A B
G
FD
E
C
5
2
4
2
6
1
3
10
9
5
d = 0
d =5
d = ∞
d =2
d = ∞
d = ∞
d = ∞
ダイクストラ法
• アルゴリズム
⑤Bの隣接しているノードを更新
A B
G
FD
E
C
5
2
4
2
6
1
3
10
9
5
d = 0
d=5 < 6
d = ∞ > 𝟖
d =2
d = ∞
d = ∞
d = ∞ > 𝟏𝟐
ダイクストラ法
• アルゴリズム
⑤Bの隣接しているノードを更新
A B
G
FD
E
C
5
2
4
2
6
1
3
10
9
5
d = 0
d=5
d = 𝟖
d =2
d = ∞
d = ∞
d = 𝟏𝟐
ダイクストラ法
• アルゴリズム
⑤Bのを除外
A B
G
FD
E
C
5
2
4
2
6
1
3
10
9
5
d = 0
d=5
d = 𝟖
d =2
d = ∞
d = ∞
d = 𝟏𝟐
ダイクストラ法
• アルゴリズム
⑤Cのdは決定
A B
G
FD
E
C
5
2
4
2
6
1
3
10
9
5
d = 0
d=5
d = 𝟖
d =2
d = ∞
d = ∞
d = 𝟏𝟐
ダイクストラ法
• アルゴリズム
⑤Cに隣接しているdを更新
A B
G
FD
E
C
5
2
4
2
6
1
3
10
9
5
d = 0
d=5
d = 𝟖 < 𝟕
d =2
d = ∞
d = ∞
d = 𝟏𝟐
ダイクストラ法
• アルゴリズム
⑤Cに隣接しているdを更新
A B
G
FD
E
C
5
2
4
2
6
1
3
10
9
5
d = 0
d=5
d = 𝟕
d =2
d = ∞
d = ∞
d = 𝟏𝟐
ダイクストラ法
• アルゴリズム
⑤Cを削除
A B
G
FD
E
C
5
2
4
2
6
1
3
10
9
5
d = 0
d=5
d = 𝟕
d =2
d = ∞
d = ∞
d = 𝟏𝟐
ダイクストラ法
•これを繰り返す!!!!
ダイクストラ法
隣接行列では𝑂 𝑉 2
隣接リストでは𝑂( 𝐸 )と見せかけて、𝑂( 𝑉2
)
最小のdを持っているノードを探
すのが時間がかかる。
ダイクストラ法
隣接行列では𝑂 𝑉 2
隣接リストでは𝑂( 𝐸 )と見せかけて、𝑂( 𝑉2
)
最小のdを持っているノードを探
すのが時間がかかる。
????
ダイクストラ法
ヒープの出番では???
隣接リストでは𝑂( 𝐸 )と見せかけて、𝑂( 𝑉2
)
最小のdを持っているノードを探
すのが時間がかかる。
????
ダイクストラ法
最小のdを持っているノー
ドを探すのが時間がかかる。
ヒープ構造で各ノードのdを
保存しておけばいいだろ??
ダイクストラ法
こうすることで計算時間は
𝑂(𝑙𝑜𝑔 𝐸 )

More Related Content

Featured

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

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

Dijkstra