![Page 1: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week...Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms Greedy Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022042016/5e74d3c1de122b43000cb7ea/html5/thumbnails/1.jpg)
CSE101: Design and Analysis of Algorithms
Ragesh Jaiswal, CSE, UCSD
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
![Page 2: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week...Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms Greedy Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022042016/5e74d3c1de122b43000cb7ea/html5/thumbnails/2.jpg)
Greedy AlgorithmsMinimum Spanning Tree
Spanning Tree: Given a strongly connected graphG = (V ,E ), a spanning tree of G is a subgraph G ′ = (V ,E ′)such that G ′ is a tree.
Minimum Spanning Tree (MST): Given a strongly connectedweighted graph G = (V ,E ), a Minimum Spanning Tree of Gis a spanning tree of G of minimum total weight (i.e., sum ofweight of edges in the tree).
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
![Page 3: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week...Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms Greedy Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022042016/5e74d3c1de122b43000cb7ea/html5/thumbnails/3.jpg)
Greedy AlgorithmsMinimum Spanning Tree
Spanning Tree: Given a strongly connected graphG = (V ,E ), a spanning tree of G is a subgraph G ′ = (V ,E ′)such that G ′ is a tree.
Minimum Spanning Tree (MST): Given a strongly connectedweighted graph G = (V ,E ), a Minimum Spanning Tree of Gis a spanning tree of G of minimum total weight (i.e., sum ofweight of edges in the tree).
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
![Page 4: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week...Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms Greedy Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022042016/5e74d3c1de122b43000cb7ea/html5/thumbnails/4.jpg)
Greedy AlgorithmsMinimum Spanning Tree
Problem
Given a strongly connected weighted graph G where all the edgeweights are distinct, give an algorithm for finding the MST of G .
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
![Page 5: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week...Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms Greedy Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022042016/5e74d3c1de122b43000cb7ea/html5/thumbnails/5.jpg)
Greedy AlgorithmsMinimum Spanning Tree
Theorem
Cut property: Given a strongly connected weighted graphG = (V ,E ) where all the edge weights are distinct. Consider anon-empty proper subset S of V and S ′ = V \ S . Let e be theleast weighted edge between any pair of vertices (u, v), where u isin S and v is in S ′. Then e is necessarily present in all MSTs of G .
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
![Page 6: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week...Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms Greedy Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022042016/5e74d3c1de122b43000cb7ea/html5/thumbnails/6.jpg)
Greedy AlgorithmsMinimum Spanning Tree
Theorem
Cut property: Given a strongly connected weighted graph G = (V ,E )where all the edge weights are distinct. Consider a non-empty propersubset S of V and S ′ = V \ S . Let e be the least weighted edge betweenany pair of vertices (u, v), where u is in S and v is in S ′. Then e isnecessarily present in all MSTs of G .
Proof sketch
For the sake of contradiction, assume that there is a MST T thatdoes not contain egde e = (u, v).Claim 1: Any path from u to v in tree T will contain a cut-edge.Consider any path from u to v in tree T and let e ′ be the firstcut-egde in this path. Consider graph T ′ obtained by removing e ′
from T and adding e.Claim 2: The sum total weight of edges of T ′ is smaller than that ofT .Claim 3: T ′ is strongly connected.
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
![Page 7: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week...Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms Greedy Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022042016/5e74d3c1de122b43000cb7ea/html5/thumbnails/7.jpg)
Greedy AlgorithmsMinimum Spanning Tree
Algorithm
Prim’s Algorithm(G)
- S ← {u} //u is an arbitrary vertex in the graph- T ← {}- While S does not contain all vertices
- Let e = (v ,w) be the minimum weight edge betweenS and V \ S
- T ← T ∪ {e}- S ← S ∪ {w}
Algorithm
Kruskal’s Algorithm(G)
- S ← E ; T ← {}- While the edge set T does not connect all the vertices
- Let e be the minimum weight edge in the set S- If e does not create a cycle in T
- T ← T ∪ {e}- S ← S \ {e}
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
![Page 8: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week...Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms Greedy Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022042016/5e74d3c1de122b43000cb7ea/html5/thumbnails/8.jpg)
Greedy AlgorithmsMinimum Spanning Tree
Algorithm
Prim’s Algorithm(G)
- S ← {u} //u is an arbitrary vertex in the graph- T ← {}- While S does not contain all vertices
- Let e = (v ,w) be the minimum weight edge betweenS and V \ S
- T ← T ∪ {e}- S ← S ∪ {w}
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
![Page 9: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week...Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms Greedy Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022042016/5e74d3c1de122b43000cb7ea/html5/thumbnails/9.jpg)
Greedy AlgorithmsMinimum Spanning Tree
Algorithm
Prim’s Algorithm(G)
- S ← {u} //u is an arbitrary vertex in the graph- T ← {}- While S does not contain all vertices
- Let e = (v ,w) be the minimum weight edge betweenS and V \ S
- T ← T ∪ {e}- S ← S ∪ {w}
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
![Page 10: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week...Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms Greedy Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022042016/5e74d3c1de122b43000cb7ea/html5/thumbnails/10.jpg)
Greedy AlgorithmsMinimum Spanning Tree
Algorithm
Prim’s Algorithm(G)
- S ← {u} //u is an arbitrary vertex in the graph- T ← {}- While S does not contain all vertices
- Let e = (v ,w) be the minimum weight edge betweenS and V \ S
- T ← T ∪ {e}- S ← S ∪ {w}
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
![Page 11: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week...Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms Greedy Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022042016/5e74d3c1de122b43000cb7ea/html5/thumbnails/11.jpg)
Greedy AlgorithmsMinimum Spanning Tree
Algorithm
Prim’s Algorithm(G)
- S ← {u} //u is an arbitrary vertex in the graph- T ← {}- While S does not contain all vertices
- Let e = (v ,w) be the minimum weight edge betweenS and V \ S
- T ← T ∪ {e}- S ← S ∪ {w}
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
![Page 12: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week...Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms Greedy Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022042016/5e74d3c1de122b43000cb7ea/html5/thumbnails/12.jpg)
Greedy AlgorithmsMinimum Spanning Tree
Algorithm
Prim’s Algorithm(G)
- S ← {u} //u is an arbitrary vertex in the graph- T ← {}- While S does not contain all vertices
- Let e = (v ,w) be the minimum weight edge betweenS and V \ S
- T ← T ∪ {e}- S ← S ∪ {w}
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
![Page 13: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week...Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms Greedy Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022042016/5e74d3c1de122b43000cb7ea/html5/thumbnails/13.jpg)
Greedy AlgorithmsMinimum Spanning Tree
Algorithm
Prim’s Algorithm(G)
- S ← {u} //u is an arbitrary vertex in the graph- T ← {}- While S does not contain all vertices
- Let e = (v ,w) be the minimum weight edge betweenS and V \ S
- T ← T ∪ {e}- S ← S ∪ {w}
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
![Page 14: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week...Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms Greedy Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022042016/5e74d3c1de122b43000cb7ea/html5/thumbnails/14.jpg)
Greedy AlgorithmsMinimum Spanning Tree
Algorithm
Kruskal’s Algorithm(G)
- S ← E ; T ← {}- While the edge set T does not connect all the vertices
- Let e be the minimum weight edge in the set S- If e does not create a cycle in T
- T ← T ∪ {e}- S ← S \ {e}
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
![Page 15: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week...Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms Greedy Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022042016/5e74d3c1de122b43000cb7ea/html5/thumbnails/15.jpg)
Greedy AlgorithmsMinimum Spanning Tree
Algorithm
Kruskal’s Algorithm(G)
- S ← E ; T ← {}- While the edge set T does not connect all the vertices
- Let e be the minimum weight edge in the set S- If e does not create a cycle in T
- T ← T ∪ {e}- S ← S \ {e}
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
![Page 16: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week...Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms Greedy Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022042016/5e74d3c1de122b43000cb7ea/html5/thumbnails/16.jpg)
Greedy AlgorithmsMinimum Spanning Tree
Algorithm
Kruskal’s Algorithm(G)
- S ← E ; T ← {}- While the edge set T does not connect all the vertices
- Let e be the minimum weight edge in the set S- If e does not create a cycle in T
- T ← T ∪ {e}- S ← S \ {e}
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
![Page 17: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week...Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms Greedy Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022042016/5e74d3c1de122b43000cb7ea/html5/thumbnails/17.jpg)
Greedy AlgorithmsMinimum Spanning Tree
Algorithm
Kruskal’s Algorithm(G)
- S ← E ; T ← {}- While the edge set T does not connect all the vertices
- Let e be the minimum weight edge in the set S- If e does not create a cycle in T
- T ← T ∪ {e}- S ← S \ {e}
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
![Page 18: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week...Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms Greedy Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022042016/5e74d3c1de122b43000cb7ea/html5/thumbnails/18.jpg)
Greedy AlgorithmsMinimum Spanning Tree
Algorithm
Kruskal’s Algorithm(G)
- S ← E ; T ← {}- While the edge set T does not connect all the vertices
- Let e be the minimum weight edge in the set S- If e does not create a cycle in T
- T ← T ∪ {e}- S ← S \ {e}
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
![Page 19: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week...Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms Greedy Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022042016/5e74d3c1de122b43000cb7ea/html5/thumbnails/19.jpg)
Greedy AlgorithmsMinimum Spanning Tree
Algorithm
Kruskal’s Algorithm(G)
- S ← E ; T ← {}- While the edge set T does not connect all the vertices
- Let e be the minimum weight edge in the set S- If e does not create a cycle in T
- T ← T ∪ {e}- S ← S \ {e}
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
![Page 20: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week...Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms Greedy Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022042016/5e74d3c1de122b43000cb7ea/html5/thumbnails/20.jpg)
Greedy AlgorithmsMinimum Spanning Tree
Algorithm
Prim’s Algorithm(G)
- S ← {u} //u is an arbitrary vertex in the graph- T ← {}- While S does not contain all vertices
- Let e = (v ,w) be the minimum weight edge betweenS and V \ S
- T ← T ∪ {e}- S ← S ∪ {w}
What is the running time of Prim’s algorithm?
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
![Page 21: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week...Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms Greedy Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022042016/5e74d3c1de122b43000cb7ea/html5/thumbnails/21.jpg)
Greedy AlgorithmsMinimum Spanning Tree
Algorithm
Prim’s Algorithm(G)
- S ← {u} //u is an arbitrary vertex in the graph- T ← {}- While S does not contain all vertices
- Let e = (v ,w) be the minimum weight edge betweenS and V \ S
- T ← T ∪ {e}- S ← S ∪ {w}
What is the running time of Prim’s algorithm?O(|E | · log |V |)
Using a priority queue.
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
![Page 22: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week...Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms Greedy Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022042016/5e74d3c1de122b43000cb7ea/html5/thumbnails/22.jpg)
End
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms