Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Dijkstra's Algorithm: A Greedy Approach to Finding Shortest Paths, Study notes of Algorithms and Programming

Dijkstra's algorithm is a popular greedy strategy used to find the shortest path between nodes in a weighted connected graph. The concept of greedy techniques, the applications of dijkstra's algorithm, and its working. The algorithm constructs a tree through the graph, where each node in the tree is a node for which the shortest path is already determined. It adds one node to the tree at a time by finding the vertex with the smallest sum of the length of the shortest path from the source and the weight of the edge connecting it to the already selected node. Applications of dijkstra's algorithm include finding optimal solutions for change making, minimum spanning trees, and single-source shortest paths, as well as approximations for the traveling salesman problem and knapsack problem.

Typology: Study notes

Pre 2010

Uploaded on 08/18/2009

koofers-user-djv
koofers-user-djv 🇺🇸

10 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Greedy
Strategies:
Dijkstra’s
Algorithm
COMP157
Nov19,2007
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Dijkstra's Algorithm: A Greedy Approach to Finding Shortest Paths and more Study notes Algorithms and Programming in PDF only on Docsity!

Greedy Strategies:Dijkstra’s Algorithm

COMP157Nov^ 19,

Greedy

Technique

Constructs

a^ solution

to^ an^ optimization

problem

piece^ by

piece^ through

a^ sequence

of^ choices

that^ are:

-^ feasible –^ locally

optimal

-^ irrevocable For^ some^

problems,

this^ yields

an^ optimal

solution

for^ every

instance.

For^ most,^ this^ yields

non‐optimal,

but^ good,

fast^ approximations.

Single

Source

Shortest

Paths

Single^ Source

Shortest

Paths^

Problem

For^ a^ given

vertex

(the^ source)

in^ a^ weighted

connected

graph,^

find^ shortest

to^ all^ other

vertices

C DB 2 (^6) A^2

Example: find shortest paths 1 from source node A. G

AÆB: 2AÆBÆ

C: 4

AÆBÆ

D: 4

note: Floyd’s algorithm solved All Pairs Shortest Path

SSSP:

Dijkstra’s

Algorithm

Dijkstra’s

algorithm:

-^ Builds

a^ tree^ through

the^ graph, where^ each

node^ in

the^ tree

is^ a^ node

for^ which

the^ shortest

path^ is^

already^

determined.

-^ Each^

pass^ through

the^ algorithm

adds^ one

node^ to

the^ tree,

by^ finding

vertex^ u

with^ the

smallest

sum^ dv^

+^ w ( v , u

where^ v^ is^ a^ vertex

for^ which

shortest

path^ has

been^ already

found

on^ preceding

iterations

(such^ vertices

form^ a^ tree)

d^ is^ the v^

length^ of

the^ shortest

path^ form

source^

to^ v

w ( v , u )^ is

the^ length

(weight)

of^ edge

from^ v^ to

u

Dijkstra’s

Algorithm:

AnalogyA B 2 CD

6 2 2 1 C DB 2 (^6) A^2

(^1) G

A

C

B

E

4 D

3

2

5

6 7

4

ExampleA 3

C

B

E

4 D

3

2

5

6 7

(^47) (^57) A

C

B

E

4 D

3

2

5

6 7

4 5 3

7 10

9 7 3

Dijkstra:

Outline

of^ Algorithm

-^ Identify

u*^ =^ fringe

vertex

with^ min

cost

-^ move

u*^ from

fringe

to^ tree

-^ for^ each

fringe

vertex

u^ connected

to^ u*

if^ d^ +w(u,u)u^

<^ d^ :u^ p^ =^ uu^ d^ =^ d^ u^ u

+w(u*,u)

Example

Tree^

Remaining vertices a(-,0)^

b(a,3) c(-,

∞) d(a,7) e(-,

b(a,3)^

c(b,3+4) d(b,3+2) e(-,

d(b,5)^

c(b,7)^

e(d,5+4) c(b,7)^

e(d,9) e(d,9)

b a 4 c d

e 3 7

(^62 ) b a 4 c d

e 3 7

(^652 ) b a 4 c d

e 3 7

(^4) d 62 5 4 b^ a 4

e 3 7

6 c 2 5 d (^4) bc ad e 3 7

(^652 )

Notes

on^ Dijkstra’s

algorithm

-^ Doesn’t

work^

for^ graphs

with^

negative

weights • Applicable

to^ both

undirected

and^ directed

graphs • Efficiency^ –^ O(|V|

2 )^ for^ graphs

represented

by^ weight

matrix

and^ array

implementation

of^ priority

queue

-^ O(|E|log|V|)

for^ graphs

represented

by^ adj.^ lists

and^ min

‐heap^ implementation

of^ priority

queue

Graph

Algorithms

•^ DFS,^

BFS :^ visit

all^ nodes

-^ adj^ matrix:

2 Θ(|V |),

adj^ list:^

Θ(|V|+|E|)

-^ TSP^ (Brute

Force):^

shortest

tour

-^ O(n!) • Warshall

(DP):^ transitive

closure

Floyd^ (DP):

all^ pairs

shortest

path (^3) – Θ(|V

-^ Prim^

(greedy):

minimum

spanning

tree

Dijkstra

(greedy):

single^ source

shortest

path

-^ matrix/array:

2 Θ(|V |),

list/heap:

O(|E|log|V|)

-^ Kruskal

(greedy):

minimum

spanning

tree

-^ O(|E|log|E|)