dijkstra's algorithm

18
Dijkstra's algorithm Shortest paths in edge-weighted digraph Krasin Georgiev Technical University of Sofia g.krasin at gmail com Assistant Professor

Upload: yoko

Post on 12-Feb-2016

96 views

Category:

Documents


0 download

DESCRIPTION

Dijkstra's algorithm. Shortest paths in edge-weighted digraph. Krasin Georgiev. Technical University of Sofia. g.krasin at gmail com. Assistant Professor. Table of Contents. Background The problem Properties and assumptions Applications Dijkstra's algorithm and Pseudocode - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Dijkstra's  algorithm

Dijkstra's algorithmShortest paths in edge-weighted digraph

Krasin Georgiev

Technical University of Sofiag.krasin at gmail com

Assistant Professor

Page 2: Dijkstra's  algorithm

Table of Contents1. Background2. The problem3. Properties and assumptions4. Applications5. Dijkstra's algorithm and

Pseudocode6. C# Demo7. Related problems and algorithms8. Resources

2

Page 3: Dijkstra's  algorithm

Background Let G=(V,E) be a (di)graph. In directed graphs, edges areone-way

An edge-weighted graph is a graphwhere we associate weights or costs with each edge (or other attributes).

A shortest path from vertex s to vertex t is a directed path from s to t with the property that no other such path has a lower weight. 3

Page 4: Dijkstra's  algorithm

The ProblemSingle-Source Shortest Path

Problem

Find the shortest paths from a source vertex

to all other vertices in the graph

4

Page 5: Dijkstra's  algorithm

Properties and Assumptions

Paths are directed. The weights are not necessarily distances (could be time or cost).

Edge weights are positive (or zero)

Shortest paths are not necessarily unique

Not all vertices need be reachable

Parallel edges may be present

5

Page 6: Dijkstra's  algorithm

Algorithm ApplicationsApplication

Vertex Edge Solutions

Map Intersection

Road Find the shortest routeFind the fastest routeFind the best route

Network Router Connection Internet routingFlight Agenda

Airports Flights Find earliest time to reach destination

Epidemiology

Individuals Possible contacts

Model the spread of infectious diseases

Arbitrage Currency Exchange rate

6

Page 7: Dijkstra's  algorithm

Algorithm Description

7

Dijkstra's algorithm first initiates all vertex distances with preliminary values and puts all vertexes in a priority queue. Then picks the unvisited vertex with the lowest-distance, calculates the distance through it to each unvisited neighbor, and updates the neighbor's distance if smaller. Mark visited when done with neighbors.

Page 8: Dijkstra's  algorithm

Algorithm Trace Trough

8

Page 9: Dijkstra's  algorithm

Algorithm Trace Trough

9

Page 10: Dijkstra's  algorithm

Algorithm Pseudocodefunction Dijkstra(Graph, source): for each vertex v in Graph: // Initializations dist[v] := infinity ; // Unknown distance function from // source to v previous[v] := undefined ; // Previous node in optimal path end for // from source dist[source] := 0 ; // Distance from source to source Q := the set of all nodes in Graph ; // All nodes in the graph are put in a // Priority Queue Q while Q is not empty: // The main loop u := vertex in Q with smallest distance in dist[] ; // Start node in first case remove u from Q ; if dist[u] = infinity: break ; // all remaining vertices are end if // inaccessible from source for each neighbor v of u in Q: // where v has not yet been // removed from Q. alt := dist[u] + dist_between(u, v) ; if alt < dist[v]: // Relax (u,v,a) dist[v] := alt ; previous[v] := u ; decrease-key v in Q; // Reorder v in the Queue end if end for end whilereturn dist;

10

Page 11: Dijkstra's  algorithm

C# Demo0

1

2

3

54

6

Page 12: Dijkstra's  algorithm

Modifications We can solve different problems using modified Dijkstra algorithm elements: Graph –vertices, edges and weights

meanings Distance – definition Priority Queue Relaxation and Distance

Initialization

12

Page 13: Dijkstra's  algorithm

Some Theory Dijkstra algorithm is based on the following Lemmas: Shortest paths are composed of

shortest paths. It is based on the fact that if there was a shorter path than any sub-path, then the shorter path should replace that sub-path to make the whole path shorter.

The sum of the lengths of any two sides of a triangle is greater than the length of the third side.

13

Page 14: Dijkstra's  algorithm

Some Theory Analysis of Dijkstra’s Algorithm:

The initialization uses only O(n) time. Each vertex is processed exactly once. The inner loop is

called once for each edge in the graph. Each call of the inner loop does O(1) work plus, possibly, one Decrease-Key operation.

All of the priority queue operations require time

Finally

and we get time If unsorted sequence is used instead of priority queue

we get O(n2 + e) 14

Page 15: Dijkstra's  algorithm

Related algorithms Breadth-first search - special-case of

Dijkstra's algorithm on unweighted graphs when all edge costs are positive and identical. The priority queue degenerates into a FIFO queue.

Uniform-cost search - the shortest path to a particular node

The A* algorithm - generalization of Dijkstra's algorithm that cuts down on the size of the subgraph that must be explored, if additional information is available that provides a lower bound on the "distance" to the target.

15

Page 17: Dijkstra's  algorithm

форум програмиране, форум уеб дизайнкурсове и уроци по програмиране, уеб дизайн – безплатно

програмиране за деца – безплатни курсове и уроцибезплатен SEO курс - оптимизация за търсачки

уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop

уроци по програмиране и уеб дизайн за ученициASP.NET MVC курс – HTML, SQL, C#, .NET, ASP.NET MVC

безплатен курс "Разработка на софтуер в cloud среда"

BG Coder - онлайн състезателна система - online judge

курсове и уроци по програмиране, книги – безплатно от Наков

безплатен курс "Качествен програмен код"

алго академия – състезателно програмиране, състезанияASP.NET курс - уеб програмиране, бази данни, C#, .NET, ASP.NET

курсове и уроци по програмиране – Телерик академия

курс мобилни приложения с iPhone, Android, WP7, PhoneGapfree C# book, безплатна книга C#, книга Java, книга C# Дончо Минков - сайт за програмиране

Николай Костов - блог за програмиранеC# курс, програмиране, безплатно

?? ? ?

??? ?

?

? ?

??

?

?

? ?

Questions?

?

Dijkstra's algorithm

http://algoacademy.telerik.com

Page 18: Dijkstra's  algorithm

Free Trainings @ Telerik Academy

“C# Programming @ Telerik Academy csharpfundamentals.telerik.com

Telerik Software Academy academy.telerik.com

Telerik Academy @ Facebook facebook.com/TelerikAcademy

Telerik Software Academy Forums forums.academy.telerik.com