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

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.












コメント

このブログの人気の投稿

カーネルK-means 理論編

Introduction English ver 今日は、カーネルK-meansの理論について書きます。カーネルK-meansは通常のK-meansの欠点を補うことができます。通常のK-meansの欠点とカーネルK-meansの強みも説明します。もし、まだ御覧になられていなければ、通常の K-means 理論編 の記事を見ていただけるとよいのではないかと思います。 カーネルK-meansの実装編 も併せてご覧ください。 概要 K-meansの弱点 カーネルトリック カーネルK-means アルゴリズム K-meansの弱点 例えば、次のようなデータを用意します。 このデータはK-meansによってうまく分類することはできません。なぜなら通常のK-meansでは、データとプロトタイプのユークリッド距離に依存しているからです。そのため、このような円状に分布しているデータはうまく分類することができません。 プロトタイプとはそれぞれのクラスにあり、そのクラスを代表するようなもののことです。K-meansでは各クラスの平均ベクトルとなります。それゆえ、以下のような分類になってしまいます。 このようなデータではK-meansはうまくいきません。 K-meansで分類できるデータセットは次のように各クラスで固まっている必要があります。 カーネルK-meansはK-meansの弱点を補います。 カーネルトリック 初めに、カーネルトリックを説明します。 線形分離できないようなデータ$X$を例えば次のように線形分離できるように$\phi(x)$に送る写像$\phi$を考えます。 カーネルは次のように定義されます。 $$K(x,y) = \phi(x)^T \phi(y)$$ $\phi$を具体的に計算することは難しいですが、$K(x,y)$を計算することなら簡単です。 この手法をカーネルトリックと呼ばれます。 カーネルK means K-meansの目的関数を復習しておきます。 $$J = \sum_{n=1}^{N} \sum_{k=1}^{K} r_{nk} ||x_n-\mu_k||^2$$ ここで、 プロトタイプは$\mu_i ~\forall k \in K$としま...

Visualization of Variational method

Introduction Today, I will implement visualization of Variational method. Variational method is used when we want to minimize functional. functional is function of function. Please look at  [1] , [2] , [3] , [5]  and  [6] . Overview formula  Implementation Visualization Formula I used following formula. $$F(x) = \sqrt{1+(\frac{du}{dx}(x))^2}$$ $$l(u) = \int_{0}^{1} \sqrt{1+(\frac{du}{dx}(x))^2} dx$$ l(u) is the length of the u(x).  I want to minimize l(u) subject to $u(0)=a$ and $u(1)=b$. u minimizing I(u) is  $$u(x) = (b-a)x+a$$ This u is line from (0,a) to (1,b). Because l(u) is the length of the u(x), We found out that u minimizing l(u) is line. Please look  [5]  to calculate of variational method. Implementation I implement visualization of variational method to check difference of optimize curve and other curve.  Let $u_A$ is  $$u_A = (b-a)x+a + A sin(8t)$$ $A sin(8t)$ increase the di...

Kullback-Leibler divergence

Introduction sorry, this page is Japanese only.   今日がダイバージェンスについて書いていきます。 ちなみにエントロピーの知識を使うのでエントロピーの記事も見てあげてください。 エントロピーの記事はこちら Kullback-Leibler Divergence 二つの確率分布の平均エントロピーの差を表す値をKLダイバージェンスといいます。 式では次のように定義されます。 $$KL(P||Q) = \int_{-\infty}^{\infty} P(X) log \frac{P(X)}{Q(X)}$$ 離散の場合は $$KL(P||Q) = \sum_{i} P(X_i) log \frac{P(X_i)}{Q(X)}$$ なぜ二つの分布間の距離をこのように定義できるのでしょうか。 式の解釈 真の分布P(X)が存在するとします。しかし、有限のデータから真の分布P(X)を求めるのは難しいです。そこで、有限のデータから推定して得られた確率分布をQ(X)とします。では真の分布P(X)と推定した分布Q(X)はどれだけ違っているのでしょうか。 ここで登場するのがエントロピーです。エントロピーはその分布の不確実性を示す値でした。 エントロピーが高いほど不確かなことが起こるとゆうことです。 P(X)のエントロピーとは$-\int_{-\infty}^{\infty} logP(X)$でした。 では推定した確率分布Q(X)は確率分布P(X)に対してどれだけ不確実性を持っているのでしょうか。エントロピーとは情報量の期待値でした。確率分布Q(X)が持つ情報量は$-logQ(X)$です。この情報量を確率P(X)で期待値をとります。 式は以下のようになります。 $$-\int_{-\infty}^{\infty} P(X) logQ(X)$$ この値と真の分布のエントロピーとの差を二つの分布間の差として定義します。式では以下のようになります。 $$-\int_{-\infty}^{\infty} P(X) logQ(X) - (--\int_{-\infty}^{\infty} P(X) logP(X)))$$ これを式変形すると $$-\int_{-\infty}^...