Presentation is loading. Please wait.

Presentation is loading. Please wait.

Proof of correctness of Dijkstra’s algorithm: Basically, we need to prove two claims. (1)Let S be the set of vertices for which the shortest path from.

Similar presentations


Presentation on theme: "Proof of correctness of Dijkstra’s algorithm: Basically, we need to prove two claims. (1)Let S be the set of vertices for which the shortest path from."— Presentation transcript:

1 Proof of correctness of Dijkstra’s algorithm: Basically, we need to prove two claims. (1)Let S be the set of vertices for which the shortest path from the source s has been determined. Let v be the vertex in S’ with the smallest S-path distance from s and let C[v] be this distance. Then, C[v] is shortest path length from s to v. Proof: Suppose there is a shorter path from s to v. Then, it will include at least one vertex in S’ (besides v). Let u be the first such vertex. But the length of the sub-path from s to u is C[u] which is at least as large as C[v]. The total path length is therefore larger than C[v] since the edge weights are positive. This is a contradiction!

2 Lecture 5 March 5 Goals: Dijkstra’s Algorithm – Proof of correctness, complexity analysis. All-pairs shortest path problem. Prim’s algorithm Maximum matching in a bipartite graph

3 (2) The rule used by Dijkstra’s algorithm for updating the (new) S-path length of vertices in (the new) S’ is correct. Proof: s u p q v Could the path s  … v  p  …q  u be shorter than s  … v  u?

4 Update( ) { v = DeleteMin(Heap); //v is the next vertex to enter S. for every vertex u in S’ such that is in E if (Cost[v] + w < Cost[u]) UpdateHeap(u, Cost[v] + w ) } UpdateHeap takes as input the index of a node in a heap and new key value. It changes the value of this node to the new key (and restores the heap property).

5 Complete algorithm Input: A weighted directed graph G = as an adjacency list. A special vertex s in V. |V| = n, |E| = e. Output: cost[ ]. cost[v] is the shortest path length from s to v. Step 1. for every vertex (u != s) Insert w[s,u] into the heap H. Step 2. while the heap is not empty update( );

6 Time complexity of Dijkstra’s algorithm: Step 1. Requires inserting n keys into a heap. Cost is clearly O(n log n). Step 2. The cost accounting is a little tricky. 2.1. First consider the deleteMin ( ) operations. This is easy to estimate since there are exactly n-1 of these. Total cose = O(n log n). 2.2. What about update( ) operations? It should be obvious that each of them costs O(log n). But how many of calls do we make to update( )? Update is included in a nested loop. Inner loop is executed out-degree(v) times when vertex v is deleted from the heap. The outer loop is once for each vertex (except s). Thus the total number of calls to update is  degree(v) = e. Thus cost = O(e log n). Total cost = O((e+n) log n).

7 Shortest path tree When you highlight all the edges involved in the shortest path from the source to all vertices, you will get a tree rooted at s. This is called the shortest path tree. Proof? How to construct this tree? As in DF-spanning tree, the easiest data structure to represent the shortest path tree is a one-dimensional array. When the cost associated with a vertex in S’ is reduced because of update ( ), we should revise the parent pointer for this node. Details are left as an exercise.

8 All pairs shortest path problem Obviously, we can iterate Dijkstra’s algorithm starting with each vertex as source. This would be OK except when the graph is very dense, i.e., if it has close to n 2 edges. (What is the cost of Dijkstra’s in this case?) There is no reason to avoid adjacency matrix representation which significantly simplifies the solution. Floyd-Warshall algorithm is better when the graph is very dense. (Cost is  (n 3 ).) The other advantage is that this algorithm does not assume positive edge weights. It is also conceptually simpler and easier to implement since it just involves a triple nested loop structure.

9 Floyd-Warshall algorithm The idea behind the algorithm is as follows: During the j-th iteration, the algorithm computes the length of the shortest S-path between every pair of vertices i and j where S = {1, 2, …, k}. Let d i, j (k) denote the length of the shortest path between i and j that only go through the vertices {1, 2, …, k}. Then, d i, j (k) = min (d i, j (k-1), d i, k (k-1) + d k, j (k-1) ) The algorithm simply iterates the above update (called relaxation) n times for each pair of vertices. The resulting algorithm is shown in page 560 of the text.

10 Minimum Spanning Tree Problem Example: 12 9 21 14 42 7 17 29 8 34 26 27 Given a set of towns and cost of constructing roads between them. Suppose we want every town to be connected to each other by road. How to create a road system that minimizes the cost?

11 Prim’s algorithm. Similar to Dijkstra’s algorithm. Start at some vertex s and grow the spanning tree. At stage i, spanning tree of size i of minimal cost that includes vertex s is created. Let S be the set of vertices included in the tree so far. For the next stage, we need to decide which vertex from S’ should be moved to S, and which edge should be included. We maintain for each vertex in S’, the edge of minimum weight connecting it to some vertex in S. Rule for updating.

12 Maximum Matching in Bipartite Graphs A bipartite graph is a graph in which vertices are divided into two groups V1 and V2. An edge e = {u,v} can only connect a vertex in V1 to a vertex in V2. This is a natural model to represent a number of situations such as job/people, student/advisor, resident/town etc. An example of a bipartite graph is shown in the next slide.

13 Example: 1 2 3 4 5 a b c d e N(1) = {a,c} N(2) = {a,c} N(3) = {a,b,c,d} N(4) = {a,c} N(5) = {a,b,c,d,e}

14 Example: 1 2 3 4 5 a b c d e A matching in a graph is a set of edges such that no two of them share a common edge. The colored edges form a matching in the graph. A matching is maximum if there is no larger matching in the graph. If |V1|= |V2| = n, then no matching can be larger than n. A matching of size n will be called perfect matching.

15 1 2 3 4 5 a b c d e Claim: G does not have a perfect matching, i.e., a matching of size 5. Proof: Define N(S) as the set of neighbors of vertices in S. N({1,2,4}) = {a,c}. Since |N({1,2,4}| < |{1,2,4}|, the vertices 1,2 and 4 can’t all be matched. So no perfect matching.

16 Now generalize: Let G= be a bipartite graph where |A| = |B|. For some subset S of A, if |N(S)| <|S|, then G can’t have a perfect matching. Hall’s theorem: The above condition is both necessary and sufficient for a bipartite graph to have a perfect matching. i.e., the converse is also true, namely: if for every subset S of A, |N(S)|>=|S| then, G has a perfect matching.

17 Corollary: A bipartite graph G= where |A|=|B| in which the degree of every vertex is the same has a perfect matching. Proof of corollary: (by contradiction) Assume the claim is not true. Then, by Hall’s theorem, there is a subset S of A such that |N(S)|< |S|. Let N(S)={a,b,c,…}. Clearly, the total number of edges incident on N(S) is k|S| where k=degree of every vertex of G. Since d(a)+d(b)+… is k|S|, if |N(S)| is less than |S|, then some vertex of N(S) must be more than k, a contradiction! Application to the card trick...

18 Algorithm for finding a maximum matching The basic idea behind the algorithm is very simple. Suppose G is a bipartite graph and M is a matching in G. (M is a set of edges so that no two share a vertex.) We have a simple criterion for finding a matching whose size is one more than that of M, if M is not a maximum matching. Call this process “augmenting a matching”. We can also quickly test if M is a maximum matching.

19 algorithm MaxMatch(Graph G) { M = { } ; // M is the empty set. while (M is not a max matching) M = augment(M); output(M); } Augmenting a matching: We need to introduce some basic terminology. Let M be a matching in a bipartite graph G=. A vertex v of G is called exposed if v is not an end vertex of any edge in M.

20 An alternating path P w.r. to M is a path v 0 - v 1 - v 2 -…-v k in which the two end vertices are exposed, and alternating edges of the path belong to M, i.e., the second, fourth, sixth etc. edges are in M. Alternating path: a-1-c-3-d-5 a b c d e 1 2 3 4 5 Once you find an alternating path, augmenting the matching M is easy. Simple trade the edges (in the path) not in M with the ones in M.

21 How do you find an alternating path? There is a simple algorithm based on breadth- first search to find an augmenting path. Cost of augmenting is O(e+n). The number of iterations is at most n. Why? The total cost of algorithm = O(n(e+n)).


Download ppt "Proof of correctness of Dijkstra’s algorithm: Basically, we need to prove two claims. (1)Let S be the set of vertices for which the shortest path from."

Similar presentations


Ads by Google