Download presentation
Presentation is loading. Please wait.
1
Greedy Algorithms: Chapter 7
2
3 Algorithms to know 1. Prim’s Minimum Spanning Tree Minimum Spanning Tree 2. Dijkstra’s Shortest path – weighted graph Shortest path – weighted graph 3. Knapsack Problem
3
Prim’s Algorithm prim( adj, start, parent) { for i = 1 to n key[i] = ; key[start] = 0; parent[start] = 0; h.copy(key); for i = 1 to n { v = h.delmin(); ref = adj[v]; while (ref != null) { w = ref.ver if (h.isin(w) && ref.weight < h.keyval(w)) { parent[w] = v; h.decrease(w, ref.weight); } ref = ref.next; }}}
4
Prim’s Algorithm prim( adj, start, parent) { for i = 1 to n O(n) key[i] = ; key[start] = 0; parent[start] = 0; h.copy(key); O(n) for i = 1 to n { v = h.delmin(); ref = adj[v]; while (ref != null) { w = ref.ver if (h.isin(w) && ref.weight < h.keyval(w)) { O(1) parent[w] = v; h.decrease(w, ref.weight); O(log n) } ref = ref.next; }}} O(m), m = #of edges
5
Dijkstra’s Algorithm Finds the shortest path from a start vertex to all the other vertices Finds the shortest path from a start vertex to all the other vertices Single-source shortest path Single-source shortest path
6
Dijkstra’s Algorithm prim( adj, start, parent) { for i = 1 to n key[i] = ; key[start] = 0; parent[start] = 0; h.copy(key); for i = 1 to n { v = h.delmin(); ref = adj[v]; while (ref != null) { w = ref.ver if (h.isin(w) && ref.weight < h.keyval(w)) { parent[w] = v; h.decrease(w, ref.weight); } ref = ref.next; }}}
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.