Implement Dijkstra by Python

from HERE:github/zippera/Projects

# input: graph, node
# output: shortest path and the length
from collections import defaultdict as dt
from copy import deepcopy as dc


def get_path(path,node,node_path):
    tmp = path[node].pop()
    if tmp == -1:
        return
    else:
        node_path.append(tmp)
        return get_path(path,tmp,node_path)


def dijkstra(graph,node):
    known = [node]
    cost = {}
    inf = float('inf')
    path = dt(list)
    for k in graph.keys():
        cost[k] = inf
        path[k].append(-1)
    cost[node] = 0
    while len(known) != len(graph.keys()):
        cur = known[-1]
        for k,v in (graph[cur]).items():
            if (k not in known) and (v+cost[cur] < cost[k]):
                cost[k] = v + cost[cur]
                path[k].append(cur)
        min_node = min(filter(lambda x: x not in known,cost),key=lambda x: cost[x])
        known.append(min_node)
    shortest_path = {}
    for k in graph.keys():
        node_path = []
        tmp_path = dc(path)
        get_path(tmp_path,k,node_path)
        node_path.reverse()
        shortest_path[k] = node_path
    return shortest_path

graph = {
        0:{2:3, 3:4, 4:1},
        1:{2:4, 5:6, 6:4},
        2:{0:3, 1:4, 5:2},
        3:{0:4},
        4:{0:1, 7:9},
        5:{1:6, 2:2, 6:6, 7:4},
        6:{1:4, 5:6},
        7:{4:9, 5:4}
        }
graph1={ 
    0:{1:3, 2:8, 4:7},
    1:{0:3, 2:6, 5:8},
    2:{0:8, 1:6, 6:7},
    3:{7:3},
    4:{0:7, 7:9},
    5:{1:8, 6:9},
    6:{2:7, 5:9},
    7:{3:3, 4:9}
}

s_path = dijkstra(graph,2)

print s_path

Using Markdown Syntax

脚步慢一些,等一下你的灵魂。

h1


文字超链:Inline方式,Tooltips可省略

h2

  • italic
  • bold




My First Header

This is some content in my first section.

First Header Second Header
Content Cell Content Cell
Content Cell Content Cell

This is true markdown text using p tag.

My Second Header

This is some content in my second section.


代码:行内代码

在第一行后指定编程语言,也可以不指定

Func:读文本内容

#include <string.h>
#include<stdlib.h>
#include<stdio.h>
void tst_fun(int val)
{
      printf("%d\n",val);
}
int main(int argc, char** argv)
{
     FILE* pf = fopen("e:\\a.txt","rb");
        if(pf == NULL)
        {
                //err_handle();
                return (1);
        }
        const int BUFFLEN=200;
        char buffer[BUFFLEN];
        while(!feof(pf)) //判断是否到了文件末尾
        {
                //读取一行文件内容
                fgets(buffer,BUFFLEN,pf);
                //handle stream
                char* pindex = strstr(buffer,"、");
                if(pindex == NULL)
                {
                        continue;
                }else
                {
                        pindex+=1;
                }
                int iVal = atoi(pindex);
                //call function
                tst_fun(iVal);
        }
        fclose(pf);
        return 0;
}

代码:段落代码 每行文字前加4个空格或者1个Tab

<!-- 注释 -->
val s = "hello Markdown"
println( s )

代码:hexo 可指定编程语言,『』代表左右大括号

『% codeblock [title] [lang:language] [url] [link text] %』
	code snippet
『% endcodeblock %』

转义字符 Markdown中的转义字符为\,转义的有:

\\ 反斜杠
\` 反引号
\* 星号
\_ 下划线
\{\} 大括号
\[\] 中括号
\(\) 小括号
\# 井号
\+ 加号
\- 减号
\. 英文句号
\! 感叹号

其它

  文本中可直接用html标签,但是要前后加上空行。

one more thing:表格 Markdown的扩展语法,hexo不支持

|| *Year* || *Temperature (low)* || *Temperature (high)* ||
|| 1900 || -10 || 25 ||
|| 1910 || -15 || 30 ||
|| 1920 || -10 || 32 ||

The Normal Distribution

The normal distribution is defined as follows:

<!--latex公式-->
$$latex
f(x;\mu,\sigma^2) = \frac{1}{\sigma\sqrt{2\pi}} e^{ -\frac{1}{2}\left(\frac{x-\mu}{\sigma}\right)^2 }
$$

To generate random draws from a normal distribution we use the rnorm function:

{r block1}
output <- rnorm(1000, 100, 15);

The normal distribution has the typical bell shape:

{r block2, fig.width=8, fig.height=5}
ggplot2::qplot(output)

结束语

以上基本够用,更详尽的请参考文献10,另外Markdown+R可以干大事,请参考文献7。

参考文献
1.不如

My Third Header

This is some content in my three section.

  • Bird
  • Magic

My Header

This is some content in my first section.

This is h1

This is h2

This is h3

This is h4

This is h5
This is h6

自动链接:尖括号

索引超链:Reference方式,索引1 2可以是任意字符

图片超链
多个感叹号,Tooltips可省略,要设置大小只能借助HTML标记
GitHub Octocat

柴静专访剑桥大学校长

《看见》2010年12月6日起,中央电视台一套综合频道推出全新专题栏目《看见》。­作为一档记录现实题材的专题节目,《看见》观察变化中的时代生活,用影像记录事件中的­人,努力刻画这个飞速转型的时代中,人的冷暖、感知、思想与渴望,期待和观众一起,了­解陌生,认识彼此;端详相似,审视自我。

第一篇文章

Hello, I am Jason, a web/andorid/windows developer. Hope I can gain more friends that share same interests and spread new thoughts and ideas here. :)