ASC Program Example Part 3 of Associative Computing Examining the MST code in ASC Primer.

Slides:



Advertisements
Similar presentations
Unit-iv.
Advertisements

Algorithm Design Methods Spring 2007 CSE, POSTECH.
Marking Schema question1: 40 marks question2: 40 marks question3: 20 marks total: 100 marks.
Introduction to Algorithms Greedy Algorithms
Chapter 5 Fundamental Algorithm Design Techniques.
Greed is good. (Some of the time)
Greedy Algorithms Be greedy! always make the choice that looks best at the moment. Local optimization. Not always yielding a globally optimal solution.
Chapter 4 The Greedy Approach. Minimum Spanning Tree A tree is an acyclic, connected, undirected graph. A spanning tree for a given graph G=, where E.
Merge Sort 4/15/2017 6:09 PM The Greedy Method The Greedy Method.
Lecture 3: Greedy Method Greedy Matching Coin Changing Minimum Spanning Tree Fractional Knapsack Dijkstra's Single-Source Shortest-Path.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
1 Greedy Algorithms. 2 2 A short list of categories Algorithm types we will consider include: Simple recursive algorithms Backtracking algorithms Divide.
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
© 2004 Goodrich, Tamassia Greedy Method and Compression1 The Greedy Method and Text Compression.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Chapter 11: Limitations of Algorithmic Power
An Associative Program for the MST Problem Part 2 of Associative Computing.
Complexity Issues Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Fundamental Techniques
1 Shortest Path Algorithms. 2 Routing Algorithms Shortest path routing What is a shortest path? –Minimum number of hops? –Minimum distance? There is a.
Backtracking.
1 The Greedy Method CSC401 – Analysis of Algorithms Lecture Notes 10 The Greedy Method Objectives Introduce the Greedy Method Use the greedy method to.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
Minimum Spanning Trees. Subgraph A graph G is a subgraph of graph H if –The vertices of G are a subset of the vertices of H, and –The edges of G are a.
TECH Computer Science Graph Optimization Problems and Greedy Algorithms Greedy Algorithms  // Make the best choice now! Optimization Problems  Minimizing.
Lecture 23. Greedy Algorithms
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Greedy methods Prudence Wong
Copyright © Cengage Learning. All rights reserved.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Dr. Naveed Ahmad Assistant Professor Department of Computer Science University of Peshawar.
Dijkstra’s Algorithm. Announcements Assignment #2 Due Tonight Exams Graded Assignment #3 Posted.
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
Binary Search From solving a problem to verifying an answer.
알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap4. Greedy Approach Grabs data items in sequence, each time with “best” choice, without.
5-1-1 CSC401 – Analysis of Algorithms Chapter 5--1 The Greedy Method Objectives Introduce the Brute Force method and the Greedy Method Compare the solutions.
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
GREEDY ALGORITHMS UNIT IV. TOPICS TO BE COVERED Fractional Knapsack problem Huffman Coding Single source shortest paths Minimum Spanning Trees Task Scheduling.
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
The Greedy Method. The Greedy Method Technique The greedy method is a general algorithm design paradigm, built on the following elements: configurations:
The ASC Language Part 2 of Associative Computing.
CSE 589 Part VI. Reading Skiena, Sections 5.5 and 6.8 CLR, chapter 37.
Greedy Algorithms. What is “Greedy Algorithm” Optimization problem usually goes through a sequence of steps A greedy algorithm makes the choice looks.
December 14, 2015 Design and Analysis of Computer Algorithm Pradondet Nilagupta Department of Computer Engineering.
Graphs A ‘Graph’ is a diagram that shows how things are connected together. It makes no attempt to draw actual paths or routes and scale is generally inconsequential.
1 Dynamic Programming Topic 07 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology.
SPANNING TREES Lecture 21 CS2110 – Fall Nate Foster is out of town. NO 3-4pm office hours today!
Algorithm Design Methods 황승원 Fall 2011 CSE, POSTECH.
Greedy Algorithms Analysis of Algorithms.
Spring 2008The Greedy Method1. Spring 2008The Greedy Method2 Outline and Reading The Greedy Method Technique (§5.1) Fractional Knapsack Problem (§5.1.1)
Divide and Conquer. Problem Solution 4 Example.
1 Ch18. The Greedy Methods. 2 BIRD’S-EYE VIEW Enter the world of algorithm-design methods In the remainder of this book, we study the methods for the.
Greedy Algorithms.
COMP108 Algorithmic Foundations Greedy methods
Lecture on Design and Analysis of Computer Algorithm
Greedy Technique.
Greedy Method 6/22/2018 6:57 PM Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015.
Merge Sort 7/29/ :21 PM The Greedy Method The Greedy Method.
The Greedy Method and Text Compression
The Greedy Method and Text Compression
Greedy Method     Greedy Matching     Coin Changing     Minimum Spanning Tree     Fractional Knapsack     Dijkstra's Single-Source Shortest-Path.
Visualizing Prim’s MST Algorithm Used to Trace the Algorithm in Class
Merge Sort 11/28/2018 2:18 AM The Greedy Method The Greedy Method.
Merge Sort 11/28/2018 2:21 AM The Greedy Method The Greedy Method.
Merge Sort 11/28/2018 8:16 AM The Greedy Method The Greedy Method.
Advanced Analysis of Algorithms
Merge Sort 1/17/2019 3:11 AM The Greedy Method The Greedy Method.
Graph Searching.
Greedy Algorithms TOPICS Greedy Strategy Activity Selection
Merge Sort 5/2/2019 7:53 PM The Greedy Method The Greedy Method.
Presentation transcript:

ASC Program Example Part 3 of Associative Computing Examining the MST code in ASC Primer

2 The Graph and Its Data File DE HI FC G BA

3 Header and declarations: /* The ASC Minimum Spanning Tree - with slight modifications from ASC PRIMER */ main mst /* Note: Vertices were encoded as integers */ deflog (TRUE, 1); deflog (FALSE, 0); char parallel tail[$], head[$]; int parallel weight[$], state[$]; char scalar node; index parallel xx[$]; logical parallel nxtnod[$], graph[$], result[$];

4 Obtain input: associate head[$], tail[$], weight[$], state[$] with graph[$]; read tail[$], head[$], weight[$] in graph[$]; Mark the active PEs for the next command (otherwise the zeros in the fields where data wasn’t read in would be used.) Find a tail whose weight is minimal. setscope graph[$] node = tail[mindex(weight[$])]; endsetscope; Because of the layout of the data file, we would find the first PE containing the minimal weight (which is 1) to be the PE holding So node would be set to 4.

5 The Graph and Its Data File DE HI FC G BA

6 Mark as being in set V2, all edges that have tails equal to node, i.e. 4: if (node == tail[$]) then state[$] = 2; else state[$] = 3; endif; This would mark the following edges as having a state of 2, i.e. they are in V

7 The Graph and Its Data File DE HI FC G BA

8 while xx in (state[$] == 2) if (state[$] == 2) then nxtnod[$] = mindex(weight[$]); endif; node = head[nxtnod[$]]; In loop 0: Only edges with state of 2 are edges: so first one is selected and node is set to 5. state[nxtnod[$]] = 1; The edge 4 5 receives a state of 1.

9 The Graph and Its Data File DE HI FC G BA

10 if (head[$] == node && state[$] != 1) then state[$] = 0; endif; We no longer want edges with a head of 5 so we throw those out of consideration by setting their state’s to 0. This would be edges 6 5 and 9 5 in data file.

11 The Graph and Its Data File DE HI FC G BA Green entries are edges thrown out.

12 if (state[$] == 3 && node == tail[$]) then state[$] = 2; endif; The edges turned to a state of 2 are then: Recall these are possible candidates for the next round. Do we want 5 4? Isn’t 4 5 already in? Solving the problem by using a picture didn’t run into this problem because once 5 4 was in, the 4 5 was eliminated from consideration automatically. So- we need to correct this. When an edge is included like X Y, we need to set the state of Y X to 0 to keep it out of further consideration. Is anything else needed?

13 Try to Correct the MST Algorithm The algorithm as coded selects D first, while we selected A. The business at the beginning to select a minimal weight edge and use one of its node’s as the starting point was to avoid the need to assign a character to a variable. Since we are using integer nodes, we could eliminate setscope graph[$] node = tail[mindex(weight[$])]; endsetscope; and just set node to 1, i.e. node = 1; This might help you see what is going on. Try to trace the MST algorithm with this change and correct it. (Homework)

14 Another Graph Problem – Shortest Path in a Graph The minimal spanning tree algorithm by Prim is called a greedy algorithm. Greedy algorithms are usually applied to optimization problems – i.e. a set of configurations is searched to find one that minimizes or maximizes some objective function defined on these configurations. The approach is to proceed with a sequence of choices. –The sequence starts from some well-understood starting configuration. –Then we iteratively make decisions that seem the best from all of those that are currently possible. This approach does not always lead to a solution, but if it does, the problem is said to possess the greedy-choice property.

15 The Greedy-choice Property. This property says a global optimal configuration can be reached by a series of locally optimal choices – i.e. choices that are best from among the possibilities available at a time. This allows us to avoid the exponential timing that would result if, for example, we had to generate all trees in a graph and then find the minimal one. Many other problems are known to have the greedy choice problem. However, you need to be careful. Sometimes just a slight change in the wording of the problem turns it into a problem that doesn’t have the greedy-choice property. In fact, a slight change can produce an NP-complete problem.

16 Some Problems Known to Have the Greedy- choice Property (Minimal Spanning Tree) – just discussed (Shortest Path) Find the shortest path between two nodes on a connected, weighted graph where the weights are positive and represent distances. (Fractional Knapsack) Given a set of n items, such that each item i has a positive benefit b i and a positive weight w i. Find the maximum benefit subset that does not exceed a given weight W provided we can take fractional values for the items, –Think of this as a knapsack being filled to not exceed the weight you can carry. Each item has benefit to you, but it can be split up into fractional parts, as is possible with granola bars, popcorn, water, etc.

17 However, The Wording is Delicate The Fractional Knapsack Problem is one that must be carefully stated. If, for the n items, you only allow an item to be taken or rejected, you have the 0-1 Knapsack Problem which is known to be NP-complete – i.e. it doesn’t have the greedy choice property. This has a pseudo-polynomial algorithm – i.e. one that runs in O(nW) time, where W is the weight. So the timing is not proportional just to the input size of the problem, n, but to a function involving in the problem statement. In fact, if W = 2 n, then the pseudo-polynomial algorithm for this problem is as bad as the brute force method of trying all combinations.

18 Some Problems Known to Have the Greedy- choice Property (Task Scheduling Problem) We are given a set T of n tasks such that each task i has a start time s i and a finish time f i where s i < f i. –Task i must start at time s i and it is guaranteed to be finished by time f i. –Each task has to be performed on a machine and each machine can execute only one task at a time. –Two tasks i and j are non-conflicting if f i <= s j or f j <= s i. –Two tasks can be scheduled to be executed on the same machine only if they are non-conflicting. What is the minimum number of machines needed to schedule all the tasks?

19 A Greedy-choice Algorithm for the Shortest Path Problem Given a connected graph with positive weights and two nodes s, the start node, and d, the destination node. Find a shortest path from s to d. A greedy choice algorithm is due to Dijkstra. Unlike the MST algorithm, more must be considered than just the minimum weight on edge leading out of a node. It is easy to find examples where that approach won’t work for this problem. Find one. (Homework)

20 Dijkstra’s Algorithm for Shortest Path Problem Let S be the set of nodes already explored and V all the nodes in the graph For each u in S, we store a distance value d(u) which will be defined below. Initially, only s, the starting point, is in S and d(s) =0. While S doesn’t include d, the destination, –Select a node v not in S with at least one edge from S for which the following is minimal: d’(v) = min d(u) + wgt(u,v) where wgt(u,v) is the weight from u to v and the min is taken over all edges e=(u,v) with u in S. Add v to S and define d(v) = d’(v). This v can never be chosen again. Stop when d, the destination point, is placed in S.

21 Example of the Greedy-choice – only part of the graph is shown set S s a b 1 2 e c x d(a) = 1 d(b) = 2 d(s) = 0 Choose minimal from: d’(c) = d(a) + 3 = 4 d’(x) = min {d(a) + 2, d(s) + 4, d(b) +2} = 3 d’(e) = d(b) + 3 = 5 Therefore, let d(x) =3 and put x in S.