Prims+Algorithm
Prims+Algorithm
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Agenda
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Introduction to Greedy Programming
• An approach to programming where the first solution to a given problem is taken up as the
correct solution
• The benefit of using the Greedy approach is that It gives the best solution locally
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Examples of Greedy Programming
Examples
• Travelling Salesman Problem
• Prim's Minimal Spanning Tree Algorithm
• Kruskal's Minimal Spanning Tree Algorithm
• Dijkstra's Algorithm
• Graph – Vertex Cover
• Knapsack Problem
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Graph Theory
• Graphs
• Degree of Graph
• Cycle in a Graph
• Trees
• Types of Graph
1. Null Graph
2. Trivial Graph
3. Directed Graph
4. Undirected Graph
5. Complete Graph
6. Connected Graph
7. Disconnected Graph
8. Regular Graph
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Graph Theory
• Trees
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Prims minimal spanning tree
Spanning Tree
• A spanning tree has all vertices connected and is constructed from the graph, and a graph can have
multiple spanning trees
Spanning Tree
• Example
A 4
B
5
6
3
F C
9 1 7
E D
2
• A minimal spanning tree is a spanning tree whose sum of weights of edges is the minimum
• Example
A 4
B
5
6
3
F C
9 1
7
E D
2 DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Prims minimal spanning tree
Prim’s algorithm example with Solution
A 4
B
5
6
3
F 9 C
1 7
E D
2
A 4
B
5
6
3
F 9 C
1 7
E D
2
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Prims minimal spanning tree Codes-
implementation in python
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Prims Algorithm Time Complexity
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Prims Algorithm Time Complexity
We discussed
• Greedy approach to programming
• Spanning tree, Minimal spanning tree
• How to find minimal spanning tree using Prim’s and Kruskal’s method?
• Its implementation in Java and Python.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited