Dijkstra

文章探讨了如何结合Dijkstra算法和不同的数据结构(如未排序数组、有序数组和二叉堆)来优化路径搜索。重点在于找到插入和删除最大值操作之间的权衡,最终选择二叉堆以实现在O(logN)时间内完成操作。
摘要由CSDN通过智能技术生成

Dijkstra

01.Set dist(s,v)=INF for any v \in V;

     Set dist(s,s)=0;

02.Now we build our CPQ(changeable priority queue): Q,with an item < v,dist(s,v) > for any v \in V;

      item x=<x.id , x.key>

03.while Q not empty:

         delete <u,dist(s,u)> from Q  ;    (which has minumun distance from s)

         For v \in Adj^{+}(u) : if dist(s,v)>=dist(s,u)+weight(u,v),then

                    Relax edge (u,v);

                   //and tell Q that we changed the dist(s,,v)------

                    Decrease key of v in Q to new dist(s,v) 

Priority Queue Interface 

build(x):

          build by starting empty and repeatly insert

Insert(x):

         add item x;

Delete_Max():

         delete & return max-key item;

Find_Max():

        return max-key item;


1.Unsorted Array:

  • insert takes O(1),//放尾巴就行
  • but delete_max() takes O(n);//cuz have to scan through the whole Array to find where to insert

2.Sorted Array:

  • insert takes O(n)

//cuz have to scan through the whole Array to find where to insert

  • delete_max() takes O(1)

//cuz max is always the last


we want the best of these two world,which delete_max() is faster with unsorted array,and insert is faster with sorted array.

:(

we can't get  constant time both 

:)   

but we can get logN time both

-------->which lead us to a datastructure Binary Heap

3.Binary Heap(is also Array)

  • insert takes O(logN),
  • delete_max() takes O(logN),too;

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值