1 week 9-10 graphs ii unweighted shortest paths dijkstra’s algorithm graphs with negative costs...

23
1 WEEK 9-10 Graphs II Unweighted Shortest Paths Dijkstra’s Algorithm Graphs with negative costs Acyclic Graphs Izmir University of Economics

Upload: arlene-freeman

Post on 27-Dec-2015

231 views

Category:

Documents


0 download

TRANSCRIPT

1

WEEK 9-10 Graphs II

Unweighted Shortest PathsDijkstra’s Algorithm

Graphs with negative costsAcyclic Graphs

Izmir University of Economics

2

Shortest-Path Algorithms

Izmir University of Economics

• The input is a weighted graph: associated with each edge (vi, vj) is a cost ci,j.

• The cost of a path v1v2...vN is ∑ci,i+1 for i in [1..N-1] weighted path length

• The unweighted path length is merely the number of edges on the path, namely, N-1.

3

Shortest-Path Algorithms

Izmir University of Economics

Single-Source Shortest-Path Problem:

Given as input a weighted graph G=(V, E), and a distinguished vertex, s, find the shortest weighted path from s to every other vertex in G.

Negative Cost Cycles

Izmir University of Economics 4

• In the graph to the left, the shortest path from v1 to v6 has a cost of 6 and the path itself is v1v4v7v6. The shortest unweighted path has 2 edges.

• In the graph to the right, we have a negative cost. The path from v5 to v4 has cost 1, but a shorter path exists by following the loop v5v4v2v5v4 which has cost -5. This path is still not the shortest, because we could stay in the loop arbitrarily long.

Shortest Path Lenght: ProblemsWe will examine 4 algorithms to solve four versions of

the problem

1.Unweighted shortest path O(|E|+|V|)

2.Weighted shortest path without negative edges O(|E|log|V|) using queues

3.Weighted shortest path with negative edges O(|E| . |V|)

4.Weighted shortest path of acyclic graphs linear time

Izmir University of Economics 5

Unweighted Shortest Paths• Using some vertex, s, which is an input

parameter, find the shortest path from s to all other vertices in an unweighted graph. Assume s=v3.

Izmir University of Economics 6

Unweighted Shortest Paths

• Algorithm: find vertices that are at distance 1, 2, ... N-1 by processing vertices in layers (breadth-first search)

Izmir University of Economics 7

Unweighted Shortest Paths

Izmir University of Economics 8

Unweighted Shortest Paths-ImplementationFor each vertex v

1. The distance from s in the entry dv

2. The bookkeeping variable (path) which will allow us to print the actual path pv

3. A boolean variable to check is the vertex is processed or not known

9

class Vertex {

List adj;

bool known;

int dist;

Vertex path; }

Unweighted Shortest Paths

• Complexity O(|V|2)Izmir University of Economics 10

Unweighted Shortest Paths - Improvement

11

Unweighted Shortest Paths - Improvement

• At any point in time there are only two types of unknown vertices that have dv≠∞. Some have dv

= currDist and the rest have dv = currDist +1.

• We can make use of a queue data structure.

• O(|E|+|V|)12

Weighted Shortest Path Dijkstra’s Algorithm

• With weighted shortest path,distance dv is tentative. It turns out to be the shortest path length from s to v using only known vertices as intermediates.

• Greedy algorithm: proceeds in stages doing the best at each stage.

• Dijkstra’s algorithm selects a vertex v with smallest dv among all unknown vertices and declares it known. Remainder of the stage consists of updating the values dw for all edges (v, w).

Izmir University of Economics 13

Dijkstra’s Algorithm - Example

Izmir University of Economics 14

Dijkstra’s Algorithm - Example

• A proof by contradiction will show that this algorithm always works as long as no edge has a negative cost.

Izmir University of Economics 15

►►

Dijkstra’s Algorithm Example – Stages I

Izmir University of Economics 16

Dijkstra’s Algorithm Example – Stages II

Izmir University of Economics 17

Dijkstra’s Algorithm - Pseudocode• If the vertices are

sequentially scanned to find minimum dv, each phase will take O(|V|) to find the minimum, thus O(|V|2) over the course of the algorithm.

• The time for updates is constant and at most one update per edge for a total of O(|E|).

• Therefore the total time spent is O(|V|2+|E|).

• If the graph is dense, OPTIMAL.

18

Dijkstra’s Algorithm-What if the graph is sparse?

• If the graph is sparse |E|=θ(|V|), algorithm is too slow. The distances of vertices need to be kept in a priority queue.

• Selection of vertex with minimum distance via deleteMin, and updates via decreaseKey operation. Hence; O(|E|log|V|+|V|log|V|)

• find operations are not supported, so you need to be able to maintain locations of di in the heap and update them as they change.

• Alternative: insert w and dw with every update.Izmir University of Economics 19

Graphs with negative edge costs

• Dijkstra’s algorithm does not work with negative edge costs. Once a vertex u is known, it is possible that from some other unknown vertex v, there is a path back to u that is very negative.

• Algorithm: A combination of weighted and unweighted algorithms. Forget about the concept of known vertices.

Izmir University of Economics 20

Graphs with negative edge costs - I• O(|E|*|V|) • Each vertex can dequeue at

most O(|V|) times.

(Why? Algorithm computes shortest paths with at most 0, 1, ..., |V|-1 edges in this order). Hence, the result!

• If negative cost cycles, then each vertex should be checked to have been dequeued at most |V| times.

Izmir University of Economics 21

Acyclic Graphs• If the graph is known to be acyclic, the

order in which vertices are declared known, can be set to be the topological order.

• Running time = O(|V|+|E|)

• This selection rule works because when a vertex is selected, its distance can no longer be lowered, since by topological ordering rule it has no incoming edges emanating from unknown nodes.

Izmir University of Economics 22

Acyclic Graphs - Example

Izmir University of Economics 23