SlideShare a Scribd company logo
1 of 42
Algorithm Design and
Complexity
Course 10
Overview







Shortest Paths
Single Source Shortest Paths
Dijkstra’s Algorithm
DAG SSSP Algorithm
Bellman-Ford Algorithm
Sample Applications
Shortest Paths


G(V, E) (un)directed, connected and weighted graph



The weight (cost) function w: E → R
w(u, v) = the weight of the edge (u, v)





Weight of a path u..v = w(u..v) = sum of the weights of all the
edges on the path






The weight must be additive

δ(u, v) = weight of the shortest path from u to v
δ(u, v) = INF if there is no path from u to v in G
δ(u, v) = min{w(p)} for all paths p = u..v
Shortest Paths are Not Unique



Example: from CLRS
Shortest paths from source node s to all the other
vertices
Shortest Paths Problems


The shortest paths from a source vertex s are organized
as a tree



Shortest paths tree
p[u] used to store the predecessor of any vertex in the tree



The weight can be any measure that is additive and we
would like to minimize: distance, cost, time, penalties,
etc.



Generalization of BFS for a weighted graph
Types of Shortest Paths Problems



Single-source shortest paths (SSSP): find the shortest paths
from a source vertex s to all the other vertices v∈V
Single-destination shortest paths:





Single-source single-destination shortest path:






It is similar to the SSSP problem so use the same algorithms
Just transpose the graph and solve the SSSP using the old destination
vertex as a the source for this new problem
At first sight, it seems to be simpler than the two previous problems
However, in the worst case, the destination vertex the destination
might be the most difficult vertex to be reached!
Still need to compute the SP to all the other intermediate vertices

All-pairs shortest paths (APSP): find the shortest paths
between all the pairs of vertices in the graph


More difficult than the previous problems
Directed Graphs?






Usually, the algorithms for shortest path are developed
for directed graphs
However, they also work for undirected graphs
But must be adapted!
The greatest problem: not go back on the same edge to
the predecessor:


If the last edge considered for building a SP is (p[u], u) then do
not allow to go back to p[u] from u!
Optimal Substructure?


Minimization problem



Lemma
Any subpath of a shortest path is always a shortest path!





p = u..v = u..x..y..v is a shortest path => u..x, x..y and y..v are
also shortest paths!
Proof by contradiction






Assume subpath x..y is not a shortest path
Then there is a better path from x..y
It means that p = u..v is not a short path either => contradiction!

Look for greedy or DP solutions
Negative Edges?


Are graphs with negative weights for the edges allowed?



Yes
But some algorithms do not compute the correct results if
negative weight edges are reachable from the source






E.g. Dijkstra

Makes the problem more difficult to solve, as the greedy
choice won’t work any more!

However, negative weight cycles are a problem:




If they are reachable from the source
No algorithm works correctly
They could be detected and maybe eliminated


Difficult! What edge to remove from the cycle? 
Cycles in a Shortest Path?


Cycles are not allowed in a shortest path between any two
vertices!






It also makes because otherwise the shortest paths from a
single source to all the vertices would not form a tree




Negative weight cycles: are not allowed!
Positive weight cycles: should not be part of a shortest path as they
could be removed and find a better path!
Zero weight cycles: prefer not to use them

As trees are not allowed to contain cycles

If a shortest path cannot contains any cycles, then any shortest
path must have at most |V| - 1 edges
SSSP: Single-Source Shortest Paths






G(V, E) and source s∈V
Find δ(s, v) for all v∈V
All the SSSP algorithms use two arrays:
d[u] = the cost of the best path s..u discovered by the
algorithm at the current moment







An estimation of the shortest path δ(s, u)
Initially d[u] = INF
Iteratively reduces d[u], but always d[u] >= δ(s, u)
We want d[u] = δ(s, u) when the algorithm finishes

p[u] = the predecessor of u from the best path s..u discovered
by the algorithm at the current moment


Initially p[u] = NIL
SSSP Algorithms


Dijkstra:





Bellman-Ford




Greedy algorithm
Only for positive weight edges
For any weights, also detects negative cycles if they exist

They use two procedures:


INIT-SSSP(G, s)




RELAX(u, v)





Call it once, when the algorithm starts
Relax along the edge (u, v)∈E
Called a number of times, depending on the algorithm

The algorithms depend on:



The order of relaxing the edges
The number of edges that need to be relaxed (also counting if the same
edge is relaxed more than once)
Initialization


Pessimistic: assume no path exits to any other vertex
except the source

INIT-SSSP(G, s)
FOREACH (v∈V)
d[v] = INF
p[v] = NIL
d[s] = 0
Relaxing an Edge



Try to improve the estimation for vertex v, d[v], by
relaxing the edge (u, v)
Maybe (u, v) is the last edge on a better path from the
source (d[u]+w(u,v)) than the one that has been
computed until now by the algorithm for v (d[v])

RELAX(u, v)
IF (d[v] > d[u] + w(u, v))
d[v] = d[u] + w(u, v)
p[v] = u
Relaxing an Edge – Examples


d[v] changes after relaxing (u,v)
s

d=10

s

v

w(u,v)=2

d=5

d=7
v

u



w(u,v)=2

d=5
u

d[v] does not change after relaxing (u,v)
s

d=10

s

v

w(u,v)=6

d=5
u

d=10
v

w(u,v)=6

d=5
u
Triangle Inequality







For all edges (u,v)∈E:
δ(s, v) <= δ(s, u) + w(u, v)
δ(s, v) = δ(s, u) + w(u, v) if and only if s..u→v is a
shortest path from s..v => s..u is also a shortest path by
optimal substructure
Else, δ(s, v) < δ(s, u) + w(u, v)
Upper-Bound Property












After any number of edge relaxations, d[v] >= δ(s, v) for all
v∈V
Proof by contradiction!
Initially, this is true as d[v] = INF for all v∈V
Let v be the first vertex for which d[v] < δ(s, v)
If u = p[v] that caused d[v] to change
d[v] < δ(s, v) <= δ(s, u) + w(u,v) <= d[u] +w(u,v)
=> d[v] < d[u] +w(u,v)
Contradicts, d[v] = d[u] +w(u,v)
Once d[v] = δ(s, v), it never changes!
True, since relaxing an edge can only decrease d[v]
No-Path Property



If u is not reachable from s, then d[u] = δ(s, u) = INF
Use previous property!
Convergence Property


Used by all algorithms





If s..u→v is a shortest path and d[u] = δ(s, u) when we
call RELAX(u, v), then d[v] = δ(s, v) afterwards
d[v] <= d[u] + w(u,v) = δ(s, u) + w(u,v) = δ(s, v)
But d[v] >= δ(s, v)



It means d[v] = δ(s, v)


Path Relaxation Property


Let p = <v0 = s, v1, … , vk> be a shortest path v0..vk



If we relax in order (v0, v1), (v1, v2), … , (vk-1, vk)

even intermixed with other relaxations
then d[vk] = δ(s, vk) afterwards



Proof by induction
Base case: k = 0 => p = <v0>
d[v0] = d[s] = 0 = δ(s, s)



Inductive step: Basis d[vk-1] = δ(s, vk-1) and RELAX(vk-1, vk)
=> convergence property => d[vk] = δ(s, vk)
Dijkstra’s Algorithm









G(V, E) with no negative weight edges reachable from the
source
The algorithm does not always compute the correct shortest
paths if negative weight edges are reachable from the source
It is a generalization of BFS
Greedy algorithm
Uses a priority queue Q to extract the min(d[v]) of the
“unvisited” vertices
It is also similar to Prim’s algorithm
The set of vertices for which the shortest path has already
been determined – S
Dijkstra – Pseudocode
Dijkstra(G, w, s)
INIT-SSSP(G, s)
S=∅
Q = PRIORITY-QUEUE(V, d)

// build a priority queue indexed by the vertices V
// with priorities in d[u] for each vertex

WHILE (!Q.EMPTY())
u = Q.EXTRACT-MIN()

// greedy choice

S = S U {u}
FOREACH (v∈Adj[u])
RELAX(u, v)
// may need to heapify-up the element if d[v] has changed after this relaxation!
// Q.DECREASE-KEY(v, d[v])
Dijkstra – Remarks








Dijkstra is greedy because it always chooses a vertex u
that is not in S, but has the minimum path s..u from all
the remaining vertices in Q
Any vertex is either in Q or in S
Loop invariant: d[v] = δ(s, v) for all v∈S
Termination: S = V => d[v] = δ(s, v) for all v∈V
Need to prove the maintenance of the invariant!
Dijkstra – Complexity


Depends how we implement the priority queue:
Θ(n * EXTRACT-MIN + m * DECREASE-KEY)



If the priority queue is a simple array:









EXTRACT-MIN: O(n)
DECREASE-KEY: O(1)
Dijkstra: Θ(n2 +m)  good for dense graphs

If the priority queue is a binary heap:




EXTRACT-MIN: O(logn)
DECREASE-KEY: O(logn)
Dijkstra: Θ(nlogn +mlogn)=Θ(mlogn)  good for sparse graphs
Dijkstra & Fibonacci Heaps



Best solution: use Fibonacci heaps
http://en.wikipedia.org/wiki/Fibonacci_heap




EXTRACT-MIN: O(logn)
DECREASE-KEY: O(1)
Dijkstra: Θ(nlogn + m) = Θ(nlogn+m)  good for sparse and
dense graphs
Dijkstra – Example
Dijkstra – Example (2)





d[1] = 0;
(1): d[2] = 1; d[3] = 2; d[6] = 3;
(2): d[4] = 7; d[5] = 10;
(3): d[5] = 7;
Dijkstra and Negative Edges


Usually, Dijkstra does not compute the correct shortest path
if there is a negative weight edge reachable from s



However, there are cases when it does work correctly




E.g. when all the negative weight edges are leaving from s to some
other vertices

Can we determine when Dijkstra works correctly on a graph
with negative edges reachable from s?



There is a condition that can easily be verified when relaxing a
negative edge!
Taking this into account, we can adapt Dijkstra to work for negative
edges, but it becomes unefficient
Bellman-Ford Algorithm








Works for graphs with both positive and negative edges
It also detects negative weight cycles reachable from the
source
Uses the path relaxation property
And the fact that any shortest path must have at most |V|
- 1 edges
It returns FALSE if it discovers a negative cycle in order
to let you know that the values in d[v] are not correct!
Bellman-Ford – Pseudocode
Bellman-Ford(G, s)
INIT-SSSP(G, s)
FOR (i = 1..|V|-1)
FOREACH ((u,v)∈E)
RELAX(u,v)
FOREACH ((u,v)∈E)
IF (d[v] > d[u] + w(u,v))
RETURN FALSE
RETURN TRUE



Complexity: Θ((n-1)*m) = Θ(n*m)  very bad for dense
graphs
Bellman-Ford – Remarks


The algorithm tries to relax all the possible paths with |V| - 1 edges
This is why it relaxes each edge |V| - 1 times, in order for it to be in
any position from 1 .. |V|-1 in a possible shortest path



The algorithm can definitely be improved









By considering less edges each time!
E.g. the first edge should always start from s

As, no path can have more than |V|-1 edges, all the shortest path
should be determined after the first two nested loops are over
If we can improve the estimate of any vertex, d[v], by relaxing any
edge one more time than we have reached a negative cycle! This is
exactly what the last cycle does!
Bellman-Ford and Negative Cycles


Proof that Bellman-Ford returns FALSE if a negative
weight cycle reachable from s is present in the graph



If BF returns TRUE it means that:
d[u] = δ(s, u) <= δ(s, v) + w(u,v) = d[v] + w(u,v)
for all edges (u,v)∈E



If a negative cycle exists (BF should return FALSE):
Proof by contradiction!
Let p = <v0, … , vk> be the negative cycle
Suppose BF returns TRUE  see blackboard
Questions


Given a directed graph



How many relaxations does Dijkstra make ?



How many relaxations does Bellman-Ford make ?



How can you find the negative weight cycle(s) discovered
by Bellman-Ford ?



How would you improve Bellman-Ford ?
SSSP in a DAG


Can we build an algorithm that is more efficient for a
special case of graphs: DAGs ?



Any ideas ?
SSSP in a DAG (2)


Compute the topological sorting of the DAG
Relax the edges in the DAG by considering the start
vertex according to the topological sorting order



No need for priority queue!



Uses path relaxation property as we are relaxing the
edges in the right order!


SSSP in DAG – Example


From CLRS
Problem 1: Currency Conversion
Problem 2: Rolling Dice on Board


Given a dice that has on each side an integer value in
1..100, find the minimum sum that can be reached by
rotating the dice on a NxN chess board, knowing the
start position and the end position of the dice. The dice
may rotate on any of its edges!
Conclusions


Shortest paths problems are frequently met in reality
A lot of applications



A lot of algorithms for solving SP problems







Taking into account problem and graph constraints

Lots of applications in networking


Routing algorithms
References


CLRS – Chapter 24



R. Sedgewick, K Wayne – Algorithms and Data Structures –
Princeton 2007 www.cs.princeton.edu/~rs/AlgsDS07/




Problem 1 and the corresponding images are taken from these
slides!

MIT OCW – Introduction to Algorithms – video lectures 17 &
18

More Related Content

What's hot

04 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x204 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x2MuradAmn
 
Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14Naor Ami
 
Prim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning treePrim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning treeoneous
 
Algorithm Design and Complexity - Course 5
Algorithm Design and Complexity - Course 5Algorithm Design and Complexity - Course 5
Algorithm Design and Complexity - Course 5Traian Rebedea
 
Minimum spanning tree algorithms by ibrahim_alfayoumi
Minimum spanning tree algorithms by ibrahim_alfayoumiMinimum spanning tree algorithms by ibrahim_alfayoumi
Minimum spanning tree algorithms by ibrahim_alfayoumiIbrahim Alfayoumi
 
Floyd warshall algo {dynamic approach}
Floyd warshall algo {dynamic approach}Floyd warshall algo {dynamic approach}
Floyd warshall algo {dynamic approach}Shubham Shukla
 
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)Mohanlal Sukhadia University (MLSU)
 
Definition ofvectorspace
Definition ofvectorspaceDefinition ofvectorspace
Definition ofvectorspaceTanuj Parikh
 
Algorithm Design and Complexity - Course 12
Algorithm Design and Complexity - Course 12Algorithm Design and Complexity - Course 12
Algorithm Design and Complexity - Course 12Traian Rebedea
 
Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithmsSaga Valsalan
 

What's hot (20)

Chapter 23 aoa
Chapter 23 aoaChapter 23 aoa
Chapter 23 aoa
 
Chapter 25 aoa
Chapter 25 aoaChapter 25 aoa
Chapter 25 aoa
 
Chapter 26 aoa
Chapter 26 aoaChapter 26 aoa
Chapter 26 aoa
 
04 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x204 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x2
 
20 Single Source Shorthest Path
20 Single Source Shorthest Path20 Single Source Shorthest Path
20 Single Source Shorthest Path
 
Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14
 
Prim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning treePrim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning tree
 
Topological sorting
Topological sortingTopological sorting
Topological sorting
 
Algorithm Design and Complexity - Course 5
Algorithm Design and Complexity - Course 5Algorithm Design and Complexity - Course 5
Algorithm Design and Complexity - Course 5
 
Minimum spanning tree algorithms by ibrahim_alfayoumi
Minimum spanning tree algorithms by ibrahim_alfayoumiMinimum spanning tree algorithms by ibrahim_alfayoumi
Minimum spanning tree algorithms by ibrahim_alfayoumi
 
Chapter 24 aoa
Chapter 24 aoaChapter 24 aoa
Chapter 24 aoa
 
Floyd warshall algo {dynamic approach}
Floyd warshall algo {dynamic approach}Floyd warshall algo {dynamic approach}
Floyd warshall algo {dynamic approach}
 
Dijkstra c
Dijkstra cDijkstra c
Dijkstra c
 
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
 
Definition ofvectorspace
Definition ofvectorspaceDefinition ofvectorspace
Definition ofvectorspace
 
Shortest Path in Graph
Shortest Path in GraphShortest Path in Graph
Shortest Path in Graph
 
Scribed lec8
Scribed lec8Scribed lec8
Scribed lec8
 
Shortest path
Shortest pathShortest path
Shortest path
 
Algorithm Design and Complexity - Course 12
Algorithm Design and Complexity - Course 12Algorithm Design and Complexity - Course 12
Algorithm Design and Complexity - Course 12
 
Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithms
 

Similar to Algorithm Design and Complexity - Course 10

Single source shortes path in dag
Single source shortes path in dagSingle source shortes path in dag
Single source shortes path in dagKiran K
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithmRuchika Sinha
 
lecture 21
lecture 21lecture 21
lecture 21sajinsc
 
Single source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstraSingle source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstraRoshan Tailor
 
Shortest Paths Part 2: Negative Weights and All-pairs
Shortest Paths Part 2: Negative Weights and All-pairsShortest Paths Part 2: Negative Weights and All-pairs
Shortest Paths Part 2: Negative Weights and All-pairsBenjamin Sach
 
Shortest Path Problem.docx
Shortest Path Problem.docxShortest Path Problem.docx
Shortest Path Problem.docxSeethaDinesh
 
Bellman ford
Bellman fordBellman ford
Bellman fordKiran K
 
Lecture_10_Parallel_Algorithms_Part_II.ppt
Lecture_10_Parallel_Algorithms_Part_II.pptLecture_10_Parallel_Algorithms_Part_II.ppt
Lecture_10_Parallel_Algorithms_Part_II.pptWahyuAde4
 
2.6 all pairsshortestpath
2.6 all pairsshortestpath2.6 all pairsshortestpath
2.6 all pairsshortestpathKrish_ver2
 
bellman-ford Theorem.ppt
bellman-ford Theorem.pptbellman-ford Theorem.ppt
bellman-ford Theorem.pptSaimaShaheen14
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithmSrikrishnan Suresh
 

Similar to Algorithm Design and Complexity - Course 10 (20)

Single source shortes path in dag
Single source shortes path in dagSingle source shortes path in dag
Single source shortes path in dag
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
 
lecture 21
lecture 21lecture 21
lecture 21
 
Single source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstraSingle source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstra
 
Shortest Paths Part 2: Negative Weights and All-pairs
Shortest Paths Part 2: Negative Weights and All-pairsShortest Paths Part 2: Negative Weights and All-pairs
Shortest Paths Part 2: Negative Weights and All-pairs
 
04 greedyalgorithmsii
04 greedyalgorithmsii04 greedyalgorithmsii
04 greedyalgorithmsii
 
Shortest path algorithms
Shortest path algorithmsShortest path algorithms
Shortest path algorithms
 
Shortest Path Problem.docx
Shortest Path Problem.docxShortest Path Problem.docx
Shortest Path Problem.docx
 
dijkstraC.ppt
dijkstraC.pptdijkstraC.ppt
dijkstraC.ppt
 
Daa chpater14
Daa chpater14Daa chpater14
Daa chpater14
 
Bellman ford
Bellman fordBellman ford
Bellman ford
 
Dijkstra.ppt
Dijkstra.pptDijkstra.ppt
Dijkstra.ppt
 
Shortest path
Shortest pathShortest path
Shortest path
 
Lecture_10_Parallel_Algorithms_Part_II.ppt
Lecture_10_Parallel_Algorithms_Part_II.pptLecture_10_Parallel_Algorithms_Part_II.ppt
Lecture_10_Parallel_Algorithms_Part_II.ppt
 
2.6 all pairsshortestpath
2.6 all pairsshortestpath2.6 all pairsshortestpath
2.6 all pairsshortestpath
 
Compactrouting
CompactroutingCompactrouting
Compactrouting
 
bellman-ford Theorem.ppt
bellman-ford Theorem.pptbellman-ford Theorem.ppt
bellman-ford Theorem.ppt
 
Weighted graphs
Weighted graphsWeighted graphs
Weighted graphs
 
(148065320) dijistra algo
(148065320) dijistra algo(148065320) dijistra algo
(148065320) dijistra algo
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithm
 

More from Traian Rebedea

An Evolution of Deep Learning Models for AI2 Reasoning Challenge
An Evolution of Deep Learning Models for AI2 Reasoning ChallengeAn Evolution of Deep Learning Models for AI2 Reasoning Challenge
An Evolution of Deep Learning Models for AI2 Reasoning ChallengeTraian Rebedea
 
AI @ Wholi - Bucharest.AI Meetup #5
AI @ Wholi - Bucharest.AI Meetup #5AI @ Wholi - Bucharest.AI Meetup #5
AI @ Wholi - Bucharest.AI Meetup #5Traian Rebedea
 
Deep neural networks for matching online social networking profiles
Deep neural networks for matching online social networking profilesDeep neural networks for matching online social networking profiles
Deep neural networks for matching online social networking profilesTraian Rebedea
 
Intro to Deep Learning for Question Answering
Intro to Deep Learning for Question AnsweringIntro to Deep Learning for Question Answering
Intro to Deep Learning for Question AnsweringTraian Rebedea
 
How useful are semantic links for the detection of implicit references in csc...
How useful are semantic links for the detection of implicit references in csc...How useful are semantic links for the detection of implicit references in csc...
How useful are semantic links for the detection of implicit references in csc...Traian Rebedea
 
A focused crawler for romanian words discovery
A focused crawler for romanian words discoveryA focused crawler for romanian words discovery
A focused crawler for romanian words discoveryTraian Rebedea
 
Detecting and Describing Historical Periods in a Large Corpora
Detecting and Describing Historical Periods in a Large CorporaDetecting and Describing Historical Periods in a Large Corpora
Detecting and Describing Historical Periods in a Large CorporaTraian Rebedea
 
Practical machine learning - Part 1
Practical machine learning - Part 1Practical machine learning - Part 1
Practical machine learning - Part 1Traian Rebedea
 
Propunere de dezvoltare a carierei universitare
Propunere de dezvoltare a carierei universitarePropunere de dezvoltare a carierei universitare
Propunere de dezvoltare a carierei universitareTraian Rebedea
 
Automatic plagiarism detection system for specialized corpora
Automatic plagiarism detection system for specialized corporaAutomatic plagiarism detection system for specialized corpora
Automatic plagiarism detection system for specialized corporaTraian Rebedea
 
Relevance based ranking of video comments on YouTube
Relevance based ranking of video comments on YouTubeRelevance based ranking of video comments on YouTube
Relevance based ranking of video comments on YouTubeTraian Rebedea
 
Opinion mining for social media and news items in Romanian
Opinion mining for social media and news items in RomanianOpinion mining for social media and news items in Romanian
Opinion mining for social media and news items in RomanianTraian Rebedea
 
PhD Defense: Computer-Based Support and Feedback for Collaborative Chat Conve...
PhD Defense: Computer-Based Support and Feedback for Collaborative Chat Conve...PhD Defense: Computer-Based Support and Feedback for Collaborative Chat Conve...
PhD Defense: Computer-Based Support and Feedback for Collaborative Chat Conve...Traian Rebedea
 
Importanța algoritmilor pentru problemele de la interviuri
Importanța algoritmilor pentru problemele de la interviuriImportanța algoritmilor pentru problemele de la interviuri
Importanța algoritmilor pentru problemele de la interviuriTraian Rebedea
 
Web services for supporting the interactions of learners in the social web - ...
Web services for supporting the interactions of learners in the social web - ...Web services for supporting the interactions of learners in the social web - ...
Web services for supporting the interactions of learners in the social web - ...Traian Rebedea
 
Automatic assessment of collaborative chat conversations with PolyCAFe - EC-T...
Automatic assessment of collaborative chat conversations with PolyCAFe - EC-T...Automatic assessment of collaborative chat conversations with PolyCAFe - EC-T...
Automatic assessment of collaborative chat conversations with PolyCAFe - EC-T...Traian Rebedea
 
Conclusions and Recommendations of the Romanian ICT RTD Survey
Conclusions and Recommendations of the Romanian ICT RTD SurveyConclusions and Recommendations of the Romanian ICT RTD Survey
Conclusions and Recommendations of the Romanian ICT RTD SurveyTraian Rebedea
 
Istoria Web-ului - part 2 - tentativ How to Web 2009
Istoria Web-ului - part 2 - tentativ How to Web 2009Istoria Web-ului - part 2 - tentativ How to Web 2009
Istoria Web-ului - part 2 - tentativ How to Web 2009Traian Rebedea
 
Istoria Web-ului - part 1 (2) - tentativ How to Web 2009
Istoria Web-ului - part 1 (2) - tentativ How to Web 2009Istoria Web-ului - part 1 (2) - tentativ How to Web 2009
Istoria Web-ului - part 1 (2) - tentativ How to Web 2009Traian Rebedea
 

More from Traian Rebedea (20)

An Evolution of Deep Learning Models for AI2 Reasoning Challenge
An Evolution of Deep Learning Models for AI2 Reasoning ChallengeAn Evolution of Deep Learning Models for AI2 Reasoning Challenge
An Evolution of Deep Learning Models for AI2 Reasoning Challenge
 
AI @ Wholi - Bucharest.AI Meetup #5
AI @ Wholi - Bucharest.AI Meetup #5AI @ Wholi - Bucharest.AI Meetup #5
AI @ Wholi - Bucharest.AI Meetup #5
 
Deep neural networks for matching online social networking profiles
Deep neural networks for matching online social networking profilesDeep neural networks for matching online social networking profiles
Deep neural networks for matching online social networking profiles
 
Intro to Deep Learning for Question Answering
Intro to Deep Learning for Question AnsweringIntro to Deep Learning for Question Answering
Intro to Deep Learning for Question Answering
 
What is word2vec?
What is word2vec?What is word2vec?
What is word2vec?
 
How useful are semantic links for the detection of implicit references in csc...
How useful are semantic links for the detection of implicit references in csc...How useful are semantic links for the detection of implicit references in csc...
How useful are semantic links for the detection of implicit references in csc...
 
A focused crawler for romanian words discovery
A focused crawler for romanian words discoveryA focused crawler for romanian words discovery
A focused crawler for romanian words discovery
 
Detecting and Describing Historical Periods in a Large Corpora
Detecting and Describing Historical Periods in a Large CorporaDetecting and Describing Historical Periods in a Large Corpora
Detecting and Describing Historical Periods in a Large Corpora
 
Practical machine learning - Part 1
Practical machine learning - Part 1Practical machine learning - Part 1
Practical machine learning - Part 1
 
Propunere de dezvoltare a carierei universitare
Propunere de dezvoltare a carierei universitarePropunere de dezvoltare a carierei universitare
Propunere de dezvoltare a carierei universitare
 
Automatic plagiarism detection system for specialized corpora
Automatic plagiarism detection system for specialized corporaAutomatic plagiarism detection system for specialized corpora
Automatic plagiarism detection system for specialized corpora
 
Relevance based ranking of video comments on YouTube
Relevance based ranking of video comments on YouTubeRelevance based ranking of video comments on YouTube
Relevance based ranking of video comments on YouTube
 
Opinion mining for social media and news items in Romanian
Opinion mining for social media and news items in RomanianOpinion mining for social media and news items in Romanian
Opinion mining for social media and news items in Romanian
 
PhD Defense: Computer-Based Support and Feedback for Collaborative Chat Conve...
PhD Defense: Computer-Based Support and Feedback for Collaborative Chat Conve...PhD Defense: Computer-Based Support and Feedback for Collaborative Chat Conve...
PhD Defense: Computer-Based Support and Feedback for Collaborative Chat Conve...
 
Importanța algoritmilor pentru problemele de la interviuri
Importanța algoritmilor pentru problemele de la interviuriImportanța algoritmilor pentru problemele de la interviuri
Importanța algoritmilor pentru problemele de la interviuri
 
Web services for supporting the interactions of learners in the social web - ...
Web services for supporting the interactions of learners in the social web - ...Web services for supporting the interactions of learners in the social web - ...
Web services for supporting the interactions of learners in the social web - ...
 
Automatic assessment of collaborative chat conversations with PolyCAFe - EC-T...
Automatic assessment of collaborative chat conversations with PolyCAFe - EC-T...Automatic assessment of collaborative chat conversations with PolyCAFe - EC-T...
Automatic assessment of collaborative chat conversations with PolyCAFe - EC-T...
 
Conclusions and Recommendations of the Romanian ICT RTD Survey
Conclusions and Recommendations of the Romanian ICT RTD SurveyConclusions and Recommendations of the Romanian ICT RTD Survey
Conclusions and Recommendations of the Romanian ICT RTD Survey
 
Istoria Web-ului - part 2 - tentativ How to Web 2009
Istoria Web-ului - part 2 - tentativ How to Web 2009Istoria Web-ului - part 2 - tentativ How to Web 2009
Istoria Web-ului - part 2 - tentativ How to Web 2009
 
Istoria Web-ului - part 1 (2) - tentativ How to Web 2009
Istoria Web-ului - part 1 (2) - tentativ How to Web 2009Istoria Web-ului - part 1 (2) - tentativ How to Web 2009
Istoria Web-ului - part 1 (2) - tentativ How to Web 2009
 

Recently uploaded

fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 

Recently uploaded (20)

fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 

Algorithm Design and Complexity - Course 10

  • 2. Overview       Shortest Paths Single Source Shortest Paths Dijkstra’s Algorithm DAG SSSP Algorithm Bellman-Ford Algorithm Sample Applications
  • 3. Shortest Paths  G(V, E) (un)directed, connected and weighted graph  The weight (cost) function w: E → R w(u, v) = the weight of the edge (u, v)   Weight of a path u..v = w(u..v) = sum of the weights of all the edges on the path     The weight must be additive δ(u, v) = weight of the shortest path from u to v δ(u, v) = INF if there is no path from u to v in G δ(u, v) = min{w(p)} for all paths p = u..v
  • 4. Shortest Paths are Not Unique   Example: from CLRS Shortest paths from source node s to all the other vertices
  • 5. Shortest Paths Problems  The shortest paths from a source vertex s are organized as a tree   Shortest paths tree p[u] used to store the predecessor of any vertex in the tree  The weight can be any measure that is additive and we would like to minimize: distance, cost, time, penalties, etc.  Generalization of BFS for a weighted graph
  • 6. Types of Shortest Paths Problems   Single-source shortest paths (SSSP): find the shortest paths from a source vertex s to all the other vertices v∈V Single-destination shortest paths:    Single-source single-destination shortest path:     It is similar to the SSSP problem so use the same algorithms Just transpose the graph and solve the SSSP using the old destination vertex as a the source for this new problem At first sight, it seems to be simpler than the two previous problems However, in the worst case, the destination vertex the destination might be the most difficult vertex to be reached! Still need to compute the SP to all the other intermediate vertices All-pairs shortest paths (APSP): find the shortest paths between all the pairs of vertices in the graph  More difficult than the previous problems
  • 7. Directed Graphs?     Usually, the algorithms for shortest path are developed for directed graphs However, they also work for undirected graphs But must be adapted! The greatest problem: not go back on the same edge to the predecessor:  If the last edge considered for building a SP is (p[u], u) then do not allow to go back to p[u] from u!
  • 8. Optimal Substructure?  Minimization problem  Lemma Any subpath of a shortest path is always a shortest path!    p = u..v = u..x..y..v is a shortest path => u..x, x..y and y..v are also shortest paths! Proof by contradiction     Assume subpath x..y is not a shortest path Then there is a better path from x..y It means that p = u..v is not a short path either => contradiction! Look for greedy or DP solutions
  • 9. Negative Edges?  Are graphs with negative weights for the edges allowed?   Yes But some algorithms do not compute the correct results if negative weight edges are reachable from the source    E.g. Dijkstra Makes the problem more difficult to solve, as the greedy choice won’t work any more! However, negative weight cycles are a problem:    If they are reachable from the source No algorithm works correctly They could be detected and maybe eliminated  Difficult! What edge to remove from the cycle? 
  • 10. Cycles in a Shortest Path?  Cycles are not allowed in a shortest path between any two vertices!     It also makes because otherwise the shortest paths from a single source to all the vertices would not form a tree   Negative weight cycles: are not allowed! Positive weight cycles: should not be part of a shortest path as they could be removed and find a better path! Zero weight cycles: prefer not to use them As trees are not allowed to contain cycles If a shortest path cannot contains any cycles, then any shortest path must have at most |V| - 1 edges
  • 11. SSSP: Single-Source Shortest Paths     G(V, E) and source s∈V Find δ(s, v) for all v∈V All the SSSP algorithms use two arrays: d[u] = the cost of the best path s..u discovered by the algorithm at the current moment      An estimation of the shortest path δ(s, u) Initially d[u] = INF Iteratively reduces d[u], but always d[u] >= δ(s, u) We want d[u] = δ(s, u) when the algorithm finishes p[u] = the predecessor of u from the best path s..u discovered by the algorithm at the current moment  Initially p[u] = NIL
  • 12. SSSP Algorithms  Dijkstra:    Bellman-Ford   Greedy algorithm Only for positive weight edges For any weights, also detects negative cycles if they exist They use two procedures:  INIT-SSSP(G, s)   RELAX(u, v)    Call it once, when the algorithm starts Relax along the edge (u, v)∈E Called a number of times, depending on the algorithm The algorithms depend on:   The order of relaxing the edges The number of edges that need to be relaxed (also counting if the same edge is relaxed more than once)
  • 13. Initialization  Pessimistic: assume no path exits to any other vertex except the source INIT-SSSP(G, s) FOREACH (v∈V) d[v] = INF p[v] = NIL d[s] = 0
  • 14. Relaxing an Edge   Try to improve the estimation for vertex v, d[v], by relaxing the edge (u, v) Maybe (u, v) is the last edge on a better path from the source (d[u]+w(u,v)) than the one that has been computed until now by the algorithm for v (d[v]) RELAX(u, v) IF (d[v] > d[u] + w(u, v)) d[v] = d[u] + w(u, v) p[v] = u
  • 15. Relaxing an Edge – Examples  d[v] changes after relaxing (u,v) s d=10 s v w(u,v)=2 d=5 d=7 v u  w(u,v)=2 d=5 u d[v] does not change after relaxing (u,v) s d=10 s v w(u,v)=6 d=5 u d=10 v w(u,v)=6 d=5 u
  • 16. Triangle Inequality     For all edges (u,v)∈E: δ(s, v) <= δ(s, u) + w(u, v) δ(s, v) = δ(s, u) + w(u, v) if and only if s..u→v is a shortest path from s..v => s..u is also a shortest path by optimal substructure Else, δ(s, v) < δ(s, u) + w(u, v)
  • 17. Upper-Bound Property           After any number of edge relaxations, d[v] >= δ(s, v) for all v∈V Proof by contradiction! Initially, this is true as d[v] = INF for all v∈V Let v be the first vertex for which d[v] < δ(s, v) If u = p[v] that caused d[v] to change d[v] < δ(s, v) <= δ(s, u) + w(u,v) <= d[u] +w(u,v) => d[v] < d[u] +w(u,v) Contradicts, d[v] = d[u] +w(u,v) Once d[v] = δ(s, v), it never changes! True, since relaxing an edge can only decrease d[v]
  • 18. No-Path Property   If u is not reachable from s, then d[u] = δ(s, u) = INF Use previous property!
  • 19. Convergence Property  Used by all algorithms   If s..u→v is a shortest path and d[u] = δ(s, u) when we call RELAX(u, v), then d[v] = δ(s, v) afterwards d[v] <= d[u] + w(u,v) = δ(s, u) + w(u,v) = δ(s, v) But d[v] >= δ(s, v)  It means d[v] = δ(s, v) 
  • 20. Path Relaxation Property  Let p = <v0 = s, v1, … , vk> be a shortest path v0..vk  If we relax in order (v0, v1), (v1, v2), … , (vk-1, vk) even intermixed with other relaxations then d[vk] = δ(s, vk) afterwards   Proof by induction Base case: k = 0 => p = <v0> d[v0] = d[s] = 0 = δ(s, s)  Inductive step: Basis d[vk-1] = δ(s, vk-1) and RELAX(vk-1, vk) => convergence property => d[vk] = δ(s, vk)
  • 21. Dijkstra’s Algorithm        G(V, E) with no negative weight edges reachable from the source The algorithm does not always compute the correct shortest paths if negative weight edges are reachable from the source It is a generalization of BFS Greedy algorithm Uses a priority queue Q to extract the min(d[v]) of the “unvisited” vertices It is also similar to Prim’s algorithm The set of vertices for which the shortest path has already been determined – S
  • 22. Dijkstra – Pseudocode Dijkstra(G, w, s) INIT-SSSP(G, s) S=∅ Q = PRIORITY-QUEUE(V, d) // build a priority queue indexed by the vertices V // with priorities in d[u] for each vertex WHILE (!Q.EMPTY()) u = Q.EXTRACT-MIN() // greedy choice S = S U {u} FOREACH (v∈Adj[u]) RELAX(u, v) // may need to heapify-up the element if d[v] has changed after this relaxation! // Q.DECREASE-KEY(v, d[v])
  • 23. Dijkstra – Remarks      Dijkstra is greedy because it always chooses a vertex u that is not in S, but has the minimum path s..u from all the remaining vertices in Q Any vertex is either in Q or in S Loop invariant: d[v] = δ(s, v) for all v∈S Termination: S = V => d[v] = δ(s, v) for all v∈V Need to prove the maintenance of the invariant!
  • 24. Dijkstra – Complexity  Depends how we implement the priority queue: Θ(n * EXTRACT-MIN + m * DECREASE-KEY)  If the priority queue is a simple array:      EXTRACT-MIN: O(n) DECREASE-KEY: O(1) Dijkstra: Θ(n2 +m)  good for dense graphs If the priority queue is a binary heap:    EXTRACT-MIN: O(logn) DECREASE-KEY: O(logn) Dijkstra: Θ(nlogn +mlogn)=Θ(mlogn)  good for sparse graphs
  • 25. Dijkstra & Fibonacci Heaps   Best solution: use Fibonacci heaps http://en.wikipedia.org/wiki/Fibonacci_heap    EXTRACT-MIN: O(logn) DECREASE-KEY: O(1) Dijkstra: Θ(nlogn + m) = Θ(nlogn+m)  good for sparse and dense graphs
  • 27. Dijkstra – Example (2)     d[1] = 0; (1): d[2] = 1; d[3] = 2; d[6] = 3; (2): d[4] = 7; d[5] = 10; (3): d[5] = 7;
  • 28. Dijkstra and Negative Edges  Usually, Dijkstra does not compute the correct shortest path if there is a negative weight edge reachable from s  However, there are cases when it does work correctly   E.g. when all the negative weight edges are leaving from s to some other vertices Can we determine when Dijkstra works correctly on a graph with negative edges reachable from s?   There is a condition that can easily be verified when relaxing a negative edge! Taking this into account, we can adapt Dijkstra to work for negative edges, but it becomes unefficient
  • 29. Bellman-Ford Algorithm      Works for graphs with both positive and negative edges It also detects negative weight cycles reachable from the source Uses the path relaxation property And the fact that any shortest path must have at most |V| - 1 edges It returns FALSE if it discovers a negative cycle in order to let you know that the values in d[v] are not correct!
  • 30. Bellman-Ford – Pseudocode Bellman-Ford(G, s) INIT-SSSP(G, s) FOR (i = 1..|V|-1) FOREACH ((u,v)∈E) RELAX(u,v) FOREACH ((u,v)∈E) IF (d[v] > d[u] + w(u,v)) RETURN FALSE RETURN TRUE  Complexity: Θ((n-1)*m) = Θ(n*m)  very bad for dense graphs
  • 31. Bellman-Ford – Remarks  The algorithm tries to relax all the possible paths with |V| - 1 edges This is why it relaxes each edge |V| - 1 times, in order for it to be in any position from 1 .. |V|-1 in a possible shortest path  The algorithm can definitely be improved      By considering less edges each time! E.g. the first edge should always start from s As, no path can have more than |V|-1 edges, all the shortest path should be determined after the first two nested loops are over If we can improve the estimate of any vertex, d[v], by relaxing any edge one more time than we have reached a negative cycle! This is exactly what the last cycle does!
  • 32. Bellman-Ford and Negative Cycles  Proof that Bellman-Ford returns FALSE if a negative weight cycle reachable from s is present in the graph  If BF returns TRUE it means that: d[u] = δ(s, u) <= δ(s, v) + w(u,v) = d[v] + w(u,v) for all edges (u,v)∈E  If a negative cycle exists (BF should return FALSE): Proof by contradiction! Let p = <v0, … , vk> be the negative cycle Suppose BF returns TRUE  see blackboard
  • 33. Questions  Given a directed graph  How many relaxations does Dijkstra make ?  How many relaxations does Bellman-Ford make ?  How can you find the negative weight cycle(s) discovered by Bellman-Ford ?  How would you improve Bellman-Ford ?
  • 34. SSSP in a DAG  Can we build an algorithm that is more efficient for a special case of graphs: DAGs ?  Any ideas ?
  • 35. SSSP in a DAG (2)  Compute the topological sorting of the DAG Relax the edges in the DAG by considering the start vertex according to the topological sorting order  No need for priority queue!  Uses path relaxation property as we are relaxing the edges in the right order! 
  • 36. SSSP in DAG – Example  From CLRS
  • 37. Problem 1: Currency Conversion
  • 38.
  • 39.
  • 40. Problem 2: Rolling Dice on Board  Given a dice that has on each side an integer value in 1..100, find the minimum sum that can be reached by rotating the dice on a NxN chess board, knowing the start position and the end position of the dice. The dice may rotate on any of its edges!
  • 41. Conclusions  Shortest paths problems are frequently met in reality A lot of applications  A lot of algorithms for solving SP problems    Taking into account problem and graph constraints Lots of applications in networking  Routing algorithms
  • 42. References  CLRS – Chapter 24  R. Sedgewick, K Wayne – Algorithms and Data Structures – Princeton 2007 www.cs.princeton.edu/~rs/AlgsDS07/   Problem 1 and the corresponding images are taken from these slides! MIT OCW – Introduction to Algorithms – video lectures 17 & 18