120 likes | 418 Views
Greedy approach. Chapter 4. Agenda. Greedy approach Prim’s Algorithm (Alg. 4.1) Kruskal’s Algorithm (Alg 4.2) Dijkstra’s Algorithm (Alg 4.3) Greedy 0-1 Knapsack Algorithm (fig. 4.10). Greedy approach. Take local best possible alternative each time. Problem.
E N D
Greedy approach Chapter 4
Agenda • Greedy approach • Prim’s Algorithm (Alg. 4.1) • Kruskal’s Algorithm (Alg 4.2) • Dijkstra’s Algorithm (Alg 4.3) • Greedy 0-1 Knapsack Algorithm (fig. 4.10)
Greedy approach • Take local best possible alternative • each time
Problem • Problem: Determine a minimum spanning tree • Starting sets, F (edges) and Y (vertices) • F -- empty set of edges or specific sets • Y-- single arbitrary starting vertex, v1, Y={v1} or a specific set of vertices.
Prim (vertex method) Start empty set of edges arbitraryvertex Kruskal (edge method) Start shortest edge (e1) (All edges are sorted by weight (wi): w1 w2 w3 w4 ….. wn all vertices considered disjoint Prim’s and Kruskal’s Algorithms
Prim [1957] (Yarnik 1930) Loop add shortest edge to the current k vertices Method Search in the set of all possible edges from the current k vertices to the rest m-k vertices: max = k(n-k) single arbitrary starting vertex Kruskal (1956) Loop add shortest edge to the set of all n vertices Method sorting all m edges by comparison of weights (log(m!) for binary insertion sort) selecting the next edge using this sorted array testing for circles Algorithms
Theorem 4.1. Prim’s algorithm always produce a minimum spanning tree. Theorem 4.2 Kruskal’s algorithm always produce a minimum spanning Optimal spanning tree?
Prim (Average case) # of passes through the 3 loops for selecting the next edge (code in the book) (n2)=2(n-1)(n-1), where n is the number of vertices In connected graph n-1 m n(n-1)/2 (relation between m and n) Kruskal (Worst-case) # of comparisons for sorting m edges: mergesort (mlgm) binary insertion (lg(m!)) # of passes through the loop for selecting the next edge (code in the book) (mlgm) # of steps to initialize n disjoint vertices (n) linear time A and W complexity of algorithms
Prim (Average case) (mlgn) (using heap) sparse graph: m<<n!/2!(n-2)! (combinations of 2 elements from n) (nlgn) Dense graph (n2lgn) (m+nlgn) (Fibonacci heap) sparse graph- (lgn) dense graph - (n2) In connected graph n-1 m n(n-1)/2 (relation between m and n) Kruskal (Worst-case) # of comparisons for sorting m edges: mergesort (mlgm) binary insertion (lg(m!)) # of passes through the loop for selecting the next edge (code in the book) (mlgm) # of steps to initialize n disjoint vertices (n) linear time Complexity and implementation
Dijkstra’s Algorithm (Alg. 4.3) • Problem 1 ((n3) -- Floid’s Alg. 3.3-- dynamic programming) • to find the shortest passes from each vertex to all other vertices in a weighted, directed graph • Problem 2 ((n2) (Dijkstra, 1959) • to find the shortest passes from one particular vertex (v1) to all other vertices in a weighted, directed graph • similar to Prim’s Algorithm • begins from v1
0-1 Knapsack problem • S={item1, item2,…, itemn} • wi=weight of itemi (non-negative) • pi= profit of itemi (non-negative) • W=maximum weight the knapsack can hold • Problem: • Determine a subset A of S such that itemiA pi ismaximized subject to itemiA wi W