col106: data structures and algorithmsrjaiswal/2017/col106/slides/week-12/lec-2.pdf · minimum...

30
COL106: Data Structures and Algorithms Ragesh Jaiswal, IIT Delhi Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

Upload: others

Post on 08-Aug-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COL106: Data Structures and Algorithmsrjaiswal/2017/COL106/Slides/Week-12/lec-2.pdf · Minimum Spanning Tree Kruskal’s algorithm using Union-Find. Algorithm Kruskal’s Algorithm(G)

COL106: Data Structures and Algorithms

Ragesh Jaiswal, IIT Delhi

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

Page 2: COL106: Data Structures and Algorithmsrjaiswal/2017/COL106/Slides/Week-12/lec-2.pdf · Minimum Spanning Tree Kruskal’s algorithm using Union-Find. Algorithm Kruskal’s Algorithm(G)

Greedy Algorithms: Minimum Spanning Tree (MST)

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

Page 3: COL106: Data Structures and Algorithmsrjaiswal/2017/COL106/Slides/Week-12/lec-2.pdf · Minimum Spanning Tree Kruskal’s algorithm using Union-Find. Algorithm Kruskal’s Algorithm(G)

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, IIT Delhi COL106: Data Structures and Algorithms

Page 4: COL106: Data Structures and Algorithmsrjaiswal/2017/COL106/Slides/Week-12/lec-2.pdf · Minimum Spanning Tree Kruskal’s algorithm using Union-Find. Algorithm Kruskal’s Algorithm(G)

Greedy AlgorithmsMinimum Spanning Tree

Problem

Given a weighted graph G where all the edge weights are distinct,give an algorithm for finding the MST of G .

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

Page 5: COL106: Data Structures and Algorithmsrjaiswal/2017/COL106/Slides/Week-12/lec-2.pdf · Minimum Spanning Tree Kruskal’s algorithm using Union-Find. Algorithm Kruskal’s Algorithm(G)

Greedy AlgorithmsMinimum Spanning Tree

Theorem

Cut property: Given a weighted graph G = (V ,E ) where all theedge weights are distinct. Consider a non-empty proper subset S ofV and S ′ = V \ S . Let e be the least weighted edge between anypair of vertices (u, v), where u is in S and v is in S ′. Then e isnecessarily present in all MSTs of G .

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

Page 6: COL106: Data Structures and Algorithmsrjaiswal/2017/COL106/Slides/Week-12/lec-2.pdf · Minimum Spanning Tree Kruskal’s algorithm using Union-Find. Algorithm Kruskal’s Algorithm(G)

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, IIT Delhi COL106: Data Structures and Algorithms

Page 7: COL106: Data Structures and Algorithmsrjaiswal/2017/COL106/Slides/Week-12/lec-2.pdf · Minimum Spanning Tree Kruskal’s algorithm using Union-Find. Algorithm Kruskal’s Algorithm(G)

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, IIT Delhi COL106: Data Structures and Algorithms

Page 8: COL106: Data Structures and Algorithmsrjaiswal/2017/COL106/Slides/Week-12/lec-2.pdf · Minimum Spanning Tree Kruskal’s algorithm using Union-Find. Algorithm Kruskal’s Algorithm(G)

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, IIT Delhi COL106: Data Structures and Algorithms

Page 9: COL106: Data Structures and Algorithmsrjaiswal/2017/COL106/Slides/Week-12/lec-2.pdf · Minimum Spanning Tree Kruskal’s algorithm using Union-Find. Algorithm Kruskal’s Algorithm(G)

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, IIT Delhi COL106: Data Structures and Algorithms

Page 10: COL106: Data Structures and Algorithmsrjaiswal/2017/COL106/Slides/Week-12/lec-2.pdf · Minimum Spanning Tree Kruskal’s algorithm using Union-Find. Algorithm Kruskal’s Algorithm(G)

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, IIT Delhi COL106: Data Structures and Algorithms

Page 11: COL106: Data Structures and Algorithmsrjaiswal/2017/COL106/Slides/Week-12/lec-2.pdf · Minimum Spanning Tree Kruskal’s algorithm using Union-Find. Algorithm Kruskal’s Algorithm(G)

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, IIT Delhi COL106: Data Structures and Algorithms

Page 12: COL106: Data Structures and Algorithmsrjaiswal/2017/COL106/Slides/Week-12/lec-2.pdf · Minimum Spanning Tree Kruskal’s algorithm using Union-Find. Algorithm Kruskal’s Algorithm(G)

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, IIT Delhi COL106: Data Structures and Algorithms

Page 13: COL106: Data Structures and Algorithmsrjaiswal/2017/COL106/Slides/Week-12/lec-2.pdf · Minimum Spanning Tree Kruskal’s algorithm using Union-Find. Algorithm Kruskal’s Algorithm(G)

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, IIT Delhi COL106: Data Structures and Algorithms

Page 14: COL106: Data Structures and Algorithmsrjaiswal/2017/COL106/Slides/Week-12/lec-2.pdf · Minimum Spanning Tree Kruskal’s algorithm using Union-Find. Algorithm Kruskal’s Algorithm(G)

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, IIT Delhi COL106: Data Structures and Algorithms

Page 15: COL106: Data Structures and Algorithmsrjaiswal/2017/COL106/Slides/Week-12/lec-2.pdf · Minimum Spanning Tree Kruskal’s algorithm using Union-Find. Algorithm Kruskal’s Algorithm(G)

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, IIT Delhi COL106: Data Structures and Algorithms

Page 16: COL106: Data Structures and Algorithmsrjaiswal/2017/COL106/Slides/Week-12/lec-2.pdf · Minimum Spanning Tree Kruskal’s algorithm using Union-Find. Algorithm Kruskal’s Algorithm(G)

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, IIT Delhi COL106: Data Structures and Algorithms

Page 17: COL106: Data Structures and Algorithmsrjaiswal/2017/COL106/Slides/Week-12/lec-2.pdf · Minimum Spanning Tree Kruskal’s algorithm using Union-Find. Algorithm Kruskal’s Algorithm(G)

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, IIT Delhi COL106: Data Structures and Algorithms

Page 18: COL106: Data Structures and Algorithmsrjaiswal/2017/COL106/Slides/Week-12/lec-2.pdf · Minimum Spanning Tree Kruskal’s algorithm using Union-Find. Algorithm Kruskal’s Algorithm(G)

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, IIT Delhi COL106: Data Structures and Algorithms

Page 19: COL106: Data Structures and Algorithmsrjaiswal/2017/COL106/Slides/Week-12/lec-2.pdf · Minimum Spanning Tree Kruskal’s algorithm using Union-Find. Algorithm Kruskal’s Algorithm(G)

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, IIT Delhi COL106: Data Structures and Algorithms

Page 20: COL106: Data Structures and Algorithmsrjaiswal/2017/COL106/Slides/Week-12/lec-2.pdf · Minimum Spanning Tree Kruskal’s algorithm using Union-Find. Algorithm Kruskal’s Algorithm(G)

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, IIT Delhi COL106: Data Structures and Algorithms

Page 21: COL106: Data Structures and Algorithmsrjaiswal/2017/COL106/Slides/Week-12/lec-2.pdf · Minimum Spanning Tree Kruskal’s algorithm using Union-Find. Algorithm Kruskal’s Algorithm(G)

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}

Algorithm

Kruskal’s Algorithm(G)

- S ← E ; T ← {}- While the edge set T does not connect all the vertices

- //Note that G ′ = (V ,T ) contains dicsonnected components- Let e = (u, v) be the minimum weight edge in the set S- If e does not create a cycle in T- If u and v are in different components of G ′

- T ← T ∪ {e}- S ← S \ {e}

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

Page 22: COL106: Data Structures and Algorithmsrjaiswal/2017/COL106/Slides/Week-12/lec-2.pdf · Minimum Spanning Tree Kruskal’s algorithm using Union-Find. Algorithm Kruskal’s Algorithm(G)

Greedy AlgorithmsMinimum Spanning Tree

Union-Find: Used for storing partition of a set of elements.The following two operations are supported:

1 Find(v): Find the partition to which the element v belongs.2 Union(u, v): Merge the partition to which u belongs with the

partition to which v belongs.

Consider the following data structure.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

Page 23: COL106: Data Structures and Algorithmsrjaiswal/2017/COL106/Slides/Week-12/lec-2.pdf · Minimum Spanning Tree Kruskal’s algorithm using Union-Find. Algorithm Kruskal’s Algorithm(G)

Greedy AlgorithmsMinimum Spanning Tree

Suppose we start from a full partition (i.e., each partitioncontains one element).

How much time does the following operation take:

Find(v):Union(u, v):

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

Page 24: COL106: Data Structures and Algorithmsrjaiswal/2017/COL106/Slides/Week-12/lec-2.pdf · Minimum Spanning Tree Kruskal’s algorithm using Union-Find. Algorithm Kruskal’s Algorithm(G)

Greedy AlgorithmsMinimum Spanning Tree

Suppose we start from a full partition (i.e., each partitioncontains one element).

How much time does the following operation take:

Find(v): O(1)Union(u, v):

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

Page 25: COL106: Data Structures and Algorithmsrjaiswal/2017/COL106/Slides/Week-12/lec-2.pdf · Minimum Spanning Tree Kruskal’s algorithm using Union-Find. Algorithm Kruskal’s Algorithm(G)

Greedy AlgorithmsMinimum Spanning Tree

Suppose we start from a full partition (i.e., each partitioncontains one element).

How much time does the following operation take:

Find(v): O(1)Union(u, v):

Claim: Performing k union operations takes O(k log k) time inthe worst case when starting from a full partition.Proof sketch: For any element u, every time its pointer needsto be changed, the size of the partition that it belongs to atleast doubles in size. This means that the pointer for u cannotchange more than O(log k) times.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

Page 26: COL106: Data Structures and Algorithmsrjaiswal/2017/COL106/Slides/Week-12/lec-2.pdf · Minimum Spanning Tree Kruskal’s algorithm using Union-Find. Algorithm Kruskal’s Algorithm(G)

Greedy AlgorithmsMinimum Spanning Tree

Kruskal’s algorithm using Union-Find.

Algorithm

Kruskal’s Algorithm(G)

- T ← {}- Let S be the list of edges sorted in increasing order of weight- While the edge set T does not connect all the vertices

- //Note that G ′ = (V ,T ) contains dicsonnected components- Let e = (u, v) be the next edge in the list S- If e does not create a cycle in T- If u and v are in different components of G ′

- If (Find(u) 6= Find(v))- T ← T ∪ {e}- Union(u, v)

What is the running time of the above algorithm?

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

Page 27: COL106: Data Structures and Algorithmsrjaiswal/2017/COL106/Slides/Week-12/lec-2.pdf · Minimum Spanning Tree Kruskal’s algorithm using Union-Find. Algorithm Kruskal’s Algorithm(G)

Greedy AlgorithmsMinimum Spanning Tree

Kruskal’s algorithm using Union-Find.

Algorithm

Kruskal’s Algorithm(G)

- T ← {}- Let S be the list of edges sorted in increasing order of weight- While the edge set T does not connect all the vertices

- //Note that G ′ = (V ,T ) contains dicsonnected components- Let e = (u, v) be the next edge in the list S- If e does not create a cycle in T- If u and v are in different components of G ′

- If (Find(u) 6= Find(v))- T ← T ∪ {e}- Union(u, v)

What is the running time of the above algorithm? O(|E | · log |V |)

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

Page 28: COL106: Data Structures and Algorithmsrjaiswal/2017/COL106/Slides/Week-12/lec-2.pdf · Minimum Spanning Tree Kruskal’s algorithm using Union-Find. Algorithm Kruskal’s Algorithm(G)

Greedy AlgorithmsShortest path

Path length: Let G = (V ,E ) be a weighted directed graph.Given a path in G , the length of a path is defined to be thesum of lengths of the edges in the path.

Shortest path: The shortest path from u to v is the path withminimum length.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

Page 29: COL106: Data Structures and Algorithmsrjaiswal/2017/COL106/Slides/Week-12/lec-2.pdf · Minimum Spanning Tree Kruskal’s algorithm using Union-Find. Algorithm Kruskal’s Algorithm(G)

Greedy AlgorithmsShortest path

Path length: Let G = (V ,E ) be a weighted directed graph.Given a path in G , the length of a path is defined to be the sumof lengths of the edges in the path.Shortest path: The shortest path from u to v is the path withminimum length.

Problem

Single source shortest path: Given a weighted, directed graphG = (V ,E ) with positive edge weights and a source vertex s, find theshortest path from s to all other vertices in the graph.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

Page 30: COL106: Data Structures and Algorithmsrjaiswal/2017/COL106/Slides/Week-12/lec-2.pdf · Minimum Spanning Tree Kruskal’s algorithm using Union-Find. Algorithm Kruskal’s Algorithm(G)

End

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms