迪杰斯特拉(Dijkstra)算法是典型最短路径算法,用于计算一个节点到其他节点的最短路径。它的主要特点是以起始点为中心向外层层扩展(广度优先搜索思想),直到扩展到终点为止。
C++语言实现:
#include<bits/stdc++.h>
using namespace std;
int tot = 0; //记录边数
int head[100]; //记录每个几点对应的起始边,顺时针方向数,最右为其实边
int dis[100]; //记录到各点的距离
int N; //总节点数
int visit[100]; //是否访问过
struct arc{ //边的表示
int end; //边的终止节点
int next; //同起点邻边
int weight; //权重
arc(int x = 0, int y = -1, int w = -1){ //结构体默认是public的构造函数
end = x;
next = y;
weight = w;
}
}e[100];
void addE(int u, int v, int w){ //增加一条从u 到 v的边
e[tot] = arc(v, head[u], w);
head[u] = tot;
tot++;
}
int getMin(){
int min = 99999;
int pos = -1;
for(int i = 0; i < N; i++){
if(dis[i] >0 && dis[i] < min && !visit[i]){
min = dis[i];
pos = i;
}
}
return pos;
}
/*
* dijkstra算法是单源最短路径算法,所以给定参数u为起点
* u:单源中的起点
*/
void dijkstra(int u){
for(int i = head[u]; i != -1; i = e[i].next)
dis[e[i].end] = e[i].weight; //初始化权重
int count = 1;
visit[u] = 1;
dis[u] = 0;
while(count < N){
int pos = getMin(); //找到最小值
visit[pos] = 1;
count++;
for(int i = head[pos]; i != -1; i = e[i].next) //以pos节点为中间节点,更新单源最短路径
if((dis[pos] + e[i].weight < dis[e[i].end]) || dis[e[i].end] == -1)
dis[e[i].end] = dis[pos] + e[i].weight;
}
for(int i = 0; i < N; i++){
if(dis[i] >= 0)
cout<<u<<" 到 "<<i<<" 的最短路径为: "<<dis[i]<<endl;
else
cout<<u<<", "<<i<<" 不可达!"<<endl;
}
}
int main()
{
N = 6;
memset(head, -1, sizeof(head));
memset(dis, -1, sizeof(dis)); //初始化dis为-1,意为不可达
memset(visit, 0, sizeof(visit)); //初始化visit为0,表示都未访问过
addE(0, 2, 10);
addE(0, 4, 30);
addE(0, 5, 100);
addE(1, 2, 5);
addE(2, 3, 50);
addE(3, 5, 10);
addE(4, 3, 20);
addE(4, 5, 60);
dijkstra(0);
return 0;
}
python语言实现:
import math
import sys
class PriorityQueue:
# Based on Min Heap
def __init__(self):
self.cur_size = 0
self.array = []
self.pos = {} # To store the pos of node in array
def isEmpty(self):
return self.cur_size == 0
def min_heapify(self, idx):
lc = self.left(idx)
rc = self.right(idx)
if lc < self.cur_size and self.array(lc)[0] < self.array(idx)[0]:
smallest = lc
else:
smallest = idx
if rc < self.cur_size and self.array(rc)[0] < self.array(smallest)[0]:
smallest = rc
if smallest != idx:
self.swap(idx, smallest)
self.min_heapify(smallest)
def insert(self, tup):
# Inserts a node into the Priority Queue
self.pos[tup[1]] = self.cur_size
self.cur_size += 1
self.array.append((sys.maxsize, tup[1]))
self.decrease_key((sys.maxsize, tup[1]), tup[0])
def extract_min(self):
# Removes and returns the min element at top of priority queue
min_node = self.array[0][1]
self.array[0] = self.array[self.cur_size - 1]
self.cur_size -= 1
self.min_heapify(1)
del self.pos[min_node]
return min_node
def left(self, i):
# returns the index of left child
return 2 * i + 1
def right(self, i):
# returns the index of right child
return 2 * i + 2
def par(self, i):
# returns the index of parent
return math.floor(i / 2)
def swap(self, i, j):
# swaps array elements at indices i and j
# update the pos{}
self.pos[self.array[i][1]] = j
self.pos[self.array[j][1]] = i
temp = self.array[i]
self.array[i] = self.array[j]
self.array[j] = temp
def decrease_key(self, tup, new_d):
idx = self.pos[tup[1]]
# assuming the new_d is atmost old_d
self.array[idx] = (new_d, tup[1])
while idx > 0 and self.array[self.par(idx)][0] > self.array[idx][0]:
self.swap(idx, self.par(idx))
idx = self.par(idx)
class Graph:
def __init__(self, num):
self.adjList = {} # To store graph: u -> (v,w)
self.num_nodes = num # Number of nodes in graph
# To store the distance from source vertex
self.dist = [0] * self.num_nodes
self.par = [-1] * self.num_nodes # To store the path
def add_edge(self, u, v, w):
# Edge going from node u to v and v to u with weight w
# u (w)-> v, v (w) -> u
# Check if u already in graph
if u in self.adjList.keys():
self.adjList[u].append((v, w))
else:
self.adjList[u] = [(v, w)]
# Assuming undirected graph
if v in self.adjList.keys():
self.adjList[v].append((u, w))
else:
self.adjList[v] = [(u, w)]
def show_graph(self):
# u -> v(w)
for u in self.adjList:
print(u, '->', ' -> '.join(str("{}({})".format(v, w))
for v, w in self.adjList[u]))
def dijkstra(self, src):
# Flush old junk values in par[]
self.par = [-1] * self.num_nodes
# src is the source node
self.dist[src] = 0
Q = PriorityQueue()
Q.insert((0, src)) # (dist from src, node)
for u in self.adjList.keys():
if u != src:
self.dist[u] = sys.maxsize # Infinity
self.par[u] = -1
while not Q.isEmpty():
u = Q.extract_min() # Returns node with the min dist from source
# Update the distance of all the neighbours of u and
# if their prev dist was INFINITY then push them in Q
for v, w in self.adjList[u]:
new_dist = self.dist[u] + w
if self.dist[v] > new_dist:
if self.dist[v] == sys.maxsize:
Q.insert((new_dist, v))
else:
Q.decrease_key((self.dist[v], v), new_dist)
self.dist[v] = new_dist
self.par[v] = u
# Show the shortest distances from src
self.show_distances(src)
def show_distances(self, src):
print("Distance from node: {}".format(src))
for u in range(self.num_nodes):
print('Node {} has distance: {}'.format(u, self.dist[u]))
def show_path(self, src, dest):
# To show the shortest path from src to dest
# WARNING: Use it *after* calling dijkstra
path = []
cost = 0
temp = dest
# Backtracking from dest to src
while self.par[temp] != -1:
path.append(temp)
if temp != src:
for v, w in self.adjList[temp]:
if v == self.par[temp]:
cost += w
break
temp = self.par[temp]
path.append(src)
path.reverse()
print('----Path to reach {} from {}----'.format(dest, src))
for u in path:
print('{}'.format(u), end=' ')
if u != dest:
print('-> ', end='')
print('\nTotal cost of path: ', cost)
if __name__ == '__main__':
graph = Graph(9)
graph.add_edge(0, 1, 4)
graph.add_edge(0, 7, 8)
graph.add_edge(1, 2, 8)
graph.add_edge(1, 7, 11)
graph.add_edge(2, 3, 7)
graph.add_edge(2, 8, 2)
graph.add_edge(2, 5, 4)
graph.add_edge(3, 4, 9)
graph.add_edge(3, 5, 14)
graph.add_edge(4, 5, 10)
graph.add_edge(5, 6, 2)
graph.add_edge(6, 7, 1)
graph.add_edge(6, 8, 6)
graph.add_edge(7, 8, 7)
graph.show_graph()
graph.dijkstra(0)
graph.show_path(0, 4)