スキップしてメイン コンテンツに移動

dijkstra method

Introduction

Today, I will write about the dijkstra method. This method is algorithm which find the shortest distance. The map is expressed by graph. If you never see this page, look at its page. This page explain the heap structure and definition of graph. The dijkstra method used heap structure, Because heap structure reduce the amout of calculation of dijkstra method.
I use this slide to explain dijkstra.

Overview

  • Algorithm
  • Implementation

Algorithm

This algorithm is 
  1. Decide start node, and this node named A.
  2. Allocate $d=\infty$ for each node, but d=0 for start node.
  3. Adjacent node of A named adj_list.
  4.  For adj in adj_list:  If d of adj > d of A + weight to adj -> d = A + weight to adj.
  5. Remove A from graph network.
  6. Find node which have the smallest d and it named A, and if network have node, back to 4.

I explain this algorithm by drawing.

 I explain algorithm by using this graph.
 Fistly, Decede start node and allocate $d=\infty$ for each node.

 Begin from node A. If d of B is smaller than d of A + weight(2), update d of B. Next, If d of C is smaller than d of A + weight(2), update d of C.

 Next remove A from graph network.
 Next, Begin from B. I do the same thing .


The dijkstra algorithm repeat this operation graph network do not have node.


Implementation

This algorithm take $O(log(|V|^2))$. Because this algorithm take time to search the node which have d of shortest distance.
However the calculation time is reduced to O((E+V)log(V)) by heap structure. Select node which have d of shortest distance by picking up node from heap structure.


I will implement the dijkstra by Python 3.
This implementation is published this github page.

I make random 100 node and , number selected by random from 0~30 edge.



Result...



This gif is expressed to update d of each node.












コメント

このブログの人気の投稿

MAP推定

Introduction English ver 今日はMAP推定(事後確率最大化法)について書きました。MAP推定ではベイズの定理を使います。データが少ないとき、最尤推定の結果をあまり信用できない話は、最尤推定の時に書きました。この時、MAP推定では自分の事前に持っている情報を取り入れることができます。 概要 ベイズの定理 MAP推定 共役分布 MAP推定の例 ベイズの定理 ベイズの定理は $$P(A|B) = \frac{P(B|A)P(A)}{P(B)}$$ です。 ただし、 $P(A|B)$ はBが起こった時のAの起こる確率です。 詳しくは  http://takutori.blogspot.com/2018/04/bayes-theorem.html  を見てください。 Map推定 MAP推定ではベイズの定理を使います。MAP推定は事後確率が最大になるようなパラメータを選びます。 いま、$x_1,x_2,...,x_n$というデータを$\theta$というパラメータを持つ分布から得られたとする。この時$P(\theta|x_1,x_2,...,x_n)$を求めたい。 ここで、ベイズの定理を使う。 $$P(\theta|x_1,x_2,...,x_n) = \frac{P(x_1,x_2,...,x_n | \theta ) P(\theta)}{P(x_1,x_2,...,x_n)}$$ ここで、$P(\theta)$は$\theta$の事前分布である。 $x_1,x_2,...,x_n$はそれぞれ独立であるので、 $$P(x_1,x_2,...,x_n | \theta ) = \Pi_{i=1}^n P(x_i|\theta)$$. よって、マップ推定は $$\theta^{\star} = \arg \max_{\theta} \frac{\Pi_{i=1}^n P(x_i|\theta) P(\theta)}{P(x_1,x_2,...,x_n)}$$ となる。 $P(x_1,x_2,...,x_n)$という値は$\theta$には依存しない。よって、定数であり、最適化に定数は関係ないので、排除すると、MAP推定は次のようになる。 $$\th...

ヒープ構造

Introduction English ver 今日はヒープ構造について書きます。ヒープ構造はデータ構造の一種です。ちょうど大学の自主ゼミグループのセミナー合宿に参加させてもらい、そこでグラフ理論を勉強したので、メモをしておこうと思います。   slide  はこんなのを使いました。 Overview データ構造 二分木 ヒープ 実装 ヒープソート データ構造 ヒープ構造の前に、データ構造について、説明します。データ構造とは、データを保存する手法であります。データ構造は、そのデータについてどのような操作を行いたいかによって、最適なものを選ぶことになります。 ヒープ構造はプライオリティキューと呼ばれれるデータ構造を表す方法です。プライオリティキューで行いたい操作は以下の二つです。 データの追加 最小値の抽出 二分木 まず、グラフを定義します。E と V は集合とし、 $e \in E$、つまりEの要素をedge(枝)と呼びます。また、$v \in V$、つまりVの要素をnodeと呼びます。 g:E->V×V をEからV × Vへの写像とします。この時、.(E,V,g)をグラフを言います。 例えば、次のようなものがあります。 丸いのがそれぞれのnodeで、矢印がedgeになります。 各edgeに対して、始点v1と始点v2を対応させるのが写像gの役目です。 根付き木とは次のような木のことです。 これはnode1からnodeが二つずつどんどん派生していっています。 特に、次のような木を 二分木 といいます。 特徴は、ノードが上からなおかつ左から敷き詰められています。一番上のノードを根といいます。また、例えば2を基準にすると、1は2の親、4,5は2の子、3は2の兄弟、8,9,10,11,12は葉と呼ばれます。 ヒープ ヒープ構造はプライオリティキューを二分木で表現したものです。プライオリティキューでやりたいことは次のことでした。 データの追加 最小値の抽出 . では、どのようにこの二つの操作を実現するのでしょうか。 初めにデータの追加について説明します。 1. 二分木の最後に追加す...

変分法の可視化

Introduction English ver 今日は、変分法の可視化を実装しました。変分法は、汎関数を最小化させるために使われます。汎関数とは、関数の関数のようなものです。変分法については、  [1] , [2] , [3] , [5] ,  [6] などを参考にしてください。 概要 汎関数 実装 可視化 汎関数 今回は、次のような汎関数を使います。 $$F(x) = \sqrt{1+(\frac{du}{dx}(x))^2}$$ $$l(u) = \int_{0}^{1} \sqrt{1+(\frac{du}{dx}(x))^2} dx$$ l(u)はu(x)という曲線の長さです。.  $u(0)=a$ and $u(1)=b$という制約のもと、$l(u)$を最小化したいといます。 最適な$l(u)$は $$u(x) = (b-a)x+a$$ となります。 (0,a) から (1,b)への直線になっているのがわかります。 これは、$l(u)$は$u$の曲線の長さなので、これを最小化するためには直線が一番であることが直観的にわかります。 変分法での導出は、 [5] を参考にしてください。 実装 変分法における最適な曲線とそうでない曲線の違いを可視化する実装をしました。 $u_A$を $$u_A = (b-a)x+a + A sin(8t)$$ とします。 $A sin(8t)$ は$u$から話す役割を持ちます。. $A \in [0,0.5]$であり、もし$A=0$であれば、$u_A=u$です。 github でcodeを公開しています。 可視化 上側の画像は$u_A(x)$を表しています。下側の画像は$l(u_A)$の値を表しています。 $u_A(x)$が$u$に近づくほど、$l(u_A)$が小さくなることがわかります。 Reference [1] http://www2.kaiyodai.ac.jp/~takenawa/optimization/resume10-4.pdf [2] http://hooktail.sub.jp/mathInPhys/brach...