Vous êtes sur la page 1sur 8

Exercise No.

13
Dijkstra

Finding single source shortest-path for a graph G(V,E), with non-negative weights on the edges
Dijkstra(G, w, s) { Initialize-Single-Source(G, s) S Q V[G] while Q do u Extract-Min(Q) S S {u} for each vertex v Adj[u] do Relax(u, v) } Initialize-Single-Source(G, s) { for each vertex v V(G) d[v] [v] NIL d[s] 0 } Relax(u, v) { //update only if we found a strictly shortest path if d[v] > d[u] + w(u,v) d[v] d[u] + w(u,v) [v] u Update(Q, v) }

Complexity: O((E+V)logV) , Assuming binary-heap is used O(ElogV) , Assuming every vertex can be reached from the source.

Minimum Spanning Tree Kruskal Finding minimum spanning tree The algorithm starts with V different trees (the vertices in V) and each time unions the trees by adding the next minimal weight edge that doesnt creates a cycle.
MST-Kruskal(G, w) A for each vertex v V(G) Make-Set(v) sort the edges of E by non-decreasing weight w for each edge (u,v) E(G) in order by non-decreasing weight if Find-Set(u) Find-Set(v) A A {(u,v)} Union(u,v) return A

Complexity: O(ElogE) , Assuming that the optimized union-find was used. Prim Finding minimum spanning tree. The algorithm starts in the tree T=r (single vertex) and extends it until it spans all the vertices in V(G). In each iteration the minimal weight edge of all the edges connecting T to the vertices not in T is added to T. key[v] - the minimal weight of all the edges connecting v to the tree.
MST-Prim (G, w, r) Q V(G) for each v Q key[v] key[r] 0 [r] NIL while Q v Extract-Min(Q) for each u Adj[v] if u Q and w(v,u) < key[v] [u] v key[u] w(v,u) Update (Q, u)

Complexity: O(E + VlogV) when using fibonacci heap O(VlogV + ElogV) when using binary heap

Example 1
Execute the dijsktra algorithm on the following graph, with single-source s.

solution:

Example 2

Why does dikstra don't work on graphs with negative weights? The following is an algorithm for finding single source shortest-paths for a graph G with weights on the edges, that can be negative. Does it solve the problem? a. b. c. Find the minimal weight in G, Wmin To each weight in G, add |Wmin| Execute dijkstra algorithm

Solution: The following figure shows a simple example where Dijkstra's algorithm would find an incorrect shortest path if negative edge weights were allowed:

In this example, the algorithm finds a shortest path of length 3 from s to c, when the actual shortest path from s to c goes through node b and has length 1. Consider a path from S to v that includes a circular path with a negative total weight, with each iteration, the algorithm will always find a better path from S to v.

The algorithm doesnt solve the problem, it adds more weight to paths with more edges. consider the following example: The upper path is the shortest. The lower path is the shortest

Add 2 to each path

Question 1
Given a DAG G =(V,E), each edge (u,v) E(G) has a weight w(u,v). For a given path p = (v1v2...vn), w(p) = w(vi,vi+1). Find the path p and w(p) weight such as w(p) is maximal in O(|V| + |E|). Solution: 1. 2. 3. 4. Topologically sort G For each vertex vi, d[i] = the weight of the maximal path that ends in vi d[i] = max{d[j] + w(j,i) : (j,i) E(G)} For finding the path, mark for each vertex v what is the predecessor p(v) in the path

Question 2
Give an algorithm for computing the maximum-weight spanning tree in a graph and analyze its complexity. Solution : Given a graph, construct a new graph with same nodes and edges, weight of an edge = negative of weight of the corresponding edge in the given graph. Minimum spanning tree in this new graph is maximum weight spanning tree in the

original one. Example:

Question 3
Given an undirected graph G(V,E) with weights on the edges. e is a minimal weight edge in G and there is no other edge with this weight. Prove that every MST of G includes the edge e. Solution

Assume that T is an MST of G that doesn't include the edge e Add the edge e to T, this will create a cycle in T For each edge (u,v) : w(u,v) > w(e) since e has minimal weight and is single in G When removing any edge (u,v) on that cycle we get a new MST T' with smaller weight That contradicts the assumption that T is an MST

Question 4
Given a directed graph G (V, E), where each edge (u,v) E has a value r, where 0 r(u,v) 1, indicates the reliability of a communication channel, meaning, the probability that a channel is in order. Suggest an algorithm for finding the most reliable channel between two given vertices s and t. Solution: The most reliable channel from vertex s to vertex t is the channel with the highest probability of being in order, meaning the channel: sv1v2...vkt

where r(s, v1)r(v1, v2)...r(vk, t) is maximal (independent probabilities). The modifications in dijkstra algorithm are as follows: 1. 2. 3. 4. Change Extract-Min(Q), to Extract-Max(Q) Change '+' to '*' Init d[s] in 1 (identity for *) instead of 0 (identity for +) Init d[v] in - (identity for max) instead of (identity for min)

Dijkstra(G, w, s) Initialize-Single-Source(G, s) S Q V[G] while Q do u Extract-Max(Q) S S {u} for each vertex v Adj[u] do Relax(u, v) Initialize-Single-Source(G, s) for each vertex v V(G) d[v] - [v] NIL d[s] 1 Relax(u, v) if d[v] < d[u]*w(u,v) d[v] d[u]*w(u,v) [v] u Update(Q, v)

Vous aimerez peut-être aussi