Once ei is added it is never removed and is included

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: lo) CSE 431/531 Algorithm Analysis and Design 7 / 49 Kruskal's Algorithm The algorithm goes through a sequence of steps. At each step, we consider the edge ei , and decide whether add ei into T. Since we are building a spanning tree T, T can not contain any cycle. So if adding ei into T introduces a cycle in T, we do not add it into T. Otherwise, we add ei into T. We are processing the edges in the order of increasing edge weight. So when ei is added into T, it looks the best to achieve the goal (minimum total weight). Once ei is added, it is never removed and is included into the final tree T. This is a perfect example of greedy algorithms. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 7 / 49 An Example 4 2 3(4) 4 1 3 3 4 5 3 (5) 5 5 4(6) 7 2 (3) 1 (2) 4 1 (1) 6 The number near an edge is its weight. The blue edges are in the MST constructed by Kruskal's algorithm. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 8 / 49 An Example 4 2 3(4) 4 1 3 3 4 5 3 (5) 5 5 4(6) 7 2 (3) 1 (2) 4 1 (1) 6 The number near an edge is its weight. The blue edges are in the MST constructed by Kruskal's algorithm. The blue numbers in () indicate the order in which the edges are added into MST. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 8 / 49 Kruskal's Algorithm For a given graph G = (V, E), its MST is not unique. However, the weight of any two MSTs of G must be the same. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 9 / 49 Kruskal's Algorithm For a given graph G = (V, E), its MST is not unique. However, the weight of any two MSTs of G must be the same. In Kruskal's algorithm, two edges ei and ei+1 may have the same weight. If we process ei+1 before ei , we may get a different MST. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 9 / 49 Kruskal's Algorithm For a given graph G = (V, E), its MST is not unique. However, the weight of any two MSTs of G must be the same. In Kruskal's algorithm, two edges ei and ei+1 may have the same weight. If we process ei+1 before ei , we may get a different MST. Runtime of Kruskal's algorithm: Sorting of edge list takes (m log m) time. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 9 / 49 Kruskal's Algorithm For a given graph G = (V, E), its MST is not unique. However, the weight of any two MSTs of G must be the same. In Kruskal's algorithm, two edges ei and ei+1 may have the same weight. If we process ei+1 before ei , we may get a different MST. Runtime of Kruskal's algorithm: Sorting of edge list takes (m log m) time. Then we process the edges one by one. So the loop iterates m time. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 9 / 49 Kruskal's Algorithm For a given graph G = (V, E), its MST is not unique. However, the weight of any two MSTs of G must be the same. In Kruskal's algorithm, two edges ei and ei+1 may have the same weight. If we process ei+1 before ei , we may get a different MST. Runtime of Kruskal's algorithm: Sorting of edge list takes (m log m) time. Then we process the edges one by one. So the loop iterates m time. When processing an edge ei , we check if T {ei } contains a cycle or not. If not, add ei into T. If yes, do nothing. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 9 / 49 Kruskal's Algorithm For a given graph G = (V, E), its MST is not unique. However, the weight of any two MSTs of G must be the same. In Kruskal's algorithm, two edges ei and ei+1 may have the same weight. If we process ei+1 before ei , we may get a different MST. Runtime of Kruskal's algorithm: Sorting of edge list takes (m log m) time. Then we process the edges one by one. So the loop iterates m time. When processing an edge ei , we check if T {ei } contains a cycle or not. If not, add ei into T. If yes, do nothing. By using proper data structures, the processing of an edge ei can be done in O(log n) time. (The detail was discussed in CSE250). c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 9 / 49 Kruskal's Algorithm For a given graph G = (V, E), its MST is not unique. However, the weight of any two MSTs of G must be the same. In Kruskal's algorithm, two edges ei and ei+1 may have the same weight. If we process ei+1 before ei , we may get a different MST. Runtime of Kruskal's algorithm: Sorting of edge list takes (m log m) time. Then we process the edges one by one. So the loop iterates m time. When processing an edge ei , we check if T {ei } contains a cycle or not. If not, add ei into T. If yes, do nothing. By using proper data structures, the processing of an edge ei can be done in O(log n) time. (The detail was discussed in CSE250). So the loop takes O(m log n) time. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 9 / 49 Kruskal's Algorithm For a given graph G = (V, E), its MST is not unique. However, the weight of any two MSTs of G must be the same. In Kruskal's algorithm, two edges ei and ei+1 may have the same weight. If we process ei+1 befor...
View Full Document

Ask a homework question - tutors are online