SlideShare a Scribd company logo
1 of 32
Dijkstra法
2021/11/15 高橋
Strictly Confidential
Copyright©︎株式会社Acompany All rights reserved.
目次
・最短経路を求める
・Dijkstra法とは
・Dijkstra法の流れ
・Dijkstra法を用いる問題
おまけ
・Bellman-Ford法
Strictly Confidential
Copyright©︎株式会社Acompany All rights reserved.
最短経路を求める
S G
7
5
1
2
6
3
1
5
1
Strictly Confidential
Copyright©︎株式会社Acompany All rights reserved.
最短経路を求める
S G
7
5
1
2
6
3
1
5
1
5+6=11
Strictly Confidential
Copyright©︎株式会社Acompany All rights reserved.
最短経路を求める
S G
7
5
1
2
6
3
1
5
1
2+1+1+3=7
Strictly Confidential
Copyright©︎株式会社Acompany All rights reserved.
・全経路を探索すれば最短経路を求められる
最短経路を求める
Strictly Confidential
Copyright©︎株式会社Acompany All rights reserved.
・頂点数が増えると経路の総数は急激に増加する
最短経路を求める
引用:https://www.miraikan.jst.go.jp/sp/medialab/11.html
メディアラボ第11期展示「フカシギの数え方」© National Museum of Emerging Science and Innovation
・効率よく計算できる様にしたい
→Dijkstra法を使う
Strictly Confidential
Copyright©︎株式会社Acompany All rights reserved.
Dijkstra法とは
・1959年にエドガー・ダイクストラにより考案
・グラフ上のある一点を始点とする最短経路を求めるアルゴリズム
計算量はヒープなしでO(V2),ヒープありでO(|E|log|V|)
・辺のコストが全て0以上の時にしか使えない
・負の辺がある時はBellman-Ford法を用いる
Strictly Confidential
Copyright©︎株式会社Acompany All rights reserved.
Dijkstra法の流れ
1. 始点を決め、始点の距離を0にする
2. 始点と繋がっている全ての頂点においてその辺のコストを暫定の
最短距離とする.
3. まだ通っていない頂点のなかで距離が最小の頂点に移動する.
4. 今いる頂点と繋がっている,まだ通っていない頂点の最短距離を更
新する
5. 3と4を繰り返し行う
6. 更新がされなくなったら終了
Strictly Confidential
Copyright©︎株式会社Acompany All rights reserved.
Dijkstra法の流れ
S G
2
7
5
7
5
1
2
6
3
1
5
1
Strictly Confidential
Copyright©︎株式会社Acompany All rights reserved.
Dijkstra法の流れ
S G
2
7
3
7
5
1
2
6
3
1
7
5
1
Strictly Confidential
Copyright©︎株式会社Acompany All rights reserved.
Dijkstra法の流れ
S G(9)
2
4
3
7
5
1
2
6
3
1
7
5
1
Strictly Confidential
Copyright©︎株式会社Acompany All rights reserved.
Dijkstra法の流れ
S G(7)
2
4
3
7
5
1
2
6
3
1
7
5
1
Strictly Confidential
Copyright©︎株式会社Acompany All rights reserved.
Dijkstra法の流れ
S G(7)
2
4
3
7
5
1
2
6
3
1
7
5
1
Strictly Confidential
Copyright©︎株式会社Acompany All rights reserved.
Dijkstra法の流れ
S G(7)
2
4
3
7
5
1
2
6
3
1
7
5
1
Strictly Confidential
Copyright©︎株式会社Acompany All rights reserved.
Dijkstraの流れ
C++で書くとこんな感じになる
Strictly Confidential
Copyright©︎株式会社Acompany All rights reserved.
Dijkstraを用いる問題
引用:https://onlinejudge.u-aizu.ac.jp/beta/room.html#ACPC2018Day2/problems/D
Strictly Confidential
Copyright©︎株式会社Acompany All rights reserved.
最短経路の総数
・現時点での最短コストと等しい経路の総数
を保存しておくことと、二種類の操作を
追加するだけでいい
操作
1.dist[v]が更新されたらcnt[v]も更新
2.dist[v]と同じだったらcnt[v]に追加
Strictly Confidential
Copyright©︎株式会社Acompany All rights reserved.
Dijkstraを用いる問題
(x,y)から(x’,y’)に移動する
dist[x][y]+1<dist[x’][y’]
→cnt[x’][y’]=cnt[x][y]
dist[x][y]+1==dist[x’][y’]
→cnt[x’][y’]+=cnt[x][y]
https://onlinejudge.u-aizu.ac.jp/beta/room.html#ACPC2018Day2/problems/D
Strictly Confidential
Copyright©︎株式会社Acompany All rights reserved.
経路復元
頂点
S
(0,linf)
頂点
G
(7,1)
頂点
1
(4,2)
7
5
1
2
6
3
1
5
1
頂点
2
(3,3)
頂点
3
(2,0)
頂点
4
(7,3)
・最短経路の道のりを知りたい
→直前の頂点を覚えてるだけでいい
(コスト,前頂点)
経路
G→1→2→3→S
Strictly Confidential
Copyright©︎株式会社Acompany All rights reserved.
経路復元
実装
Strictly Confidential
Copyright©︎株式会社Acompany All rights reserved.
拡張ダイクストラ法
・拡張ダイクストラ法とは
→ 普通のDjkstra法とは異なり(頂点,状態)のように複数の状態を
1つの頂点として考える.
Strictly Confidential
Copyright©︎株式会社Acompany All rights reserved.
拡張ダイクストラ法を使う問題
引用:https://atcoder.jp/contests/abc164/tasks/abc164_e
Atcoder
Strictly Confidential
Copyright©︎株式会社Acompany All rights reserved.
拡張ダイクストラ
・1つの頂点を(駅,銀貨の枚数)とする
→制約より銀貨は最大で2500枚あればいいので頂点数は50*2500
となる
・各頂点に対して辺を張る
・金貨を銀貨に変える辺を張る
現在の頂点を(i,C)として駅vで金貨を銀貨ci枚にできるとすると
(i,C+ci),(i,C+2ci)...
と遷移できるが実際には(i,C+ci)だけで十分
・他の頂点への移動はいつも通り行う
あとは普通のDijkstraをするだけで解ける
Strictly Confidential
Copyright©︎株式会社Acompany All rights reserved.
おまけ
Bellman-Ford法
・グラフ上の二点間の最短経路を求めるアルゴリズム
・計算量はO(VE)
→辺が全て正ならDijkstraを使う方がいい
・負の辺があっても使える
・負閉路を検出できる
Strictly Confidential
Copyright©︎株式会社Acompany All rights reserved.
おまけ
Bellman-Ford法の流れ
1. 始点を決め、始点の距離を0にする
2. 全ての辺において最短経路の更新ができるか確認し、更新できる
な
ら更新する
5. 2を更新されなくなるまで繰り返す
6. V回繰り返しても更新されるのであれば閉路がある
Strictly Confidential
Copyright©︎株式会社Acompany All rights reserved.
おまけ
S
0
G
∞
∞
∞
∞
7
5
1
2
6
3
1
∞
2
1
-4
Strictly Confidential
Copyright©︎株式会社Acompany All rights reserved.
おまけ
S
0
G
∞
2
7
5
7
5
1
2
6
3
1
∞
2
1
-4
Strictly Confidential
Copyright©︎株式会社Acompany All rights reserved.
おまけ
S
0
G
10
2
6
5
7
5
1
2
6
3
1
4
2
1
-4
Strictly Confidential
Copyright©︎株式会社Acompany All rights reserved.
おまけ
S
0
G
5
2
6
0
7
5
1
2
6
3
1
4
2
1
-4
Strictly Confidential
Copyright©︎株式会社Acompany All rights reserved.
おまけ
S
0
G
5
1
6
0
7
5
1
2
6
3
1
4
2
1
-4
Strictly Confidential
Copyright©︎株式会社Acompany All rights reserved.
おまけ
C++で書くとこんな感じになる

More Related Content

Featured

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)
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Saba Software
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
Simplilearn
 

Featured (20)

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...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
 

dijkstra法