1 / 11

Greedy approach

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.

nicki
Télécharger la présentation

Greedy approach

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Greedy approach Chapter 4

  2. 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)

  3. Greedy approach • Take local best possible alternative • each time

  4. 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.

  5. 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

  6. 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

  7. 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?

  8. 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

  9. 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

  10. 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

  11. 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 itemiA pi ismaximized subject to itemiA wi W

More Related