+ All Categories
Home > Documents > CSE101: Design and Analysis of...

CSE101: Design and Analysis of...

Date post: 16-Mar-2020
Category:
Upload: others
View: 2 times
Download: 0 times
Share this document with a friend
22
CSE101: Design and Analysis of Algorithms Ragesh Jaiswal, CSE, UCSD Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
Transcript
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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

End

Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms


Recommended