Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "ASC Program Example Part 3 of Associative Computing Examining the MST code in ASC Primer."— Presentation transcript:

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

2 2 The Graph and Its Data File DE HI FC G BA 8 6 5 3 3 2 2 2 1 6 1 4 2 47 1 2 2 1 6 7 1 7 3 2 1 2 2 3 4 2 7 6 3 2 4 3 8 2 3 4 2 4 5 1 4 8 8 4 3 2 5 4 1 5 9 2 5 6 6 6 1 7 6 9 5 6 5 6 7 2 6 7 1 3 7 9 1 7 8 3 8 7 3 8 9 4 8 4 8 8 3 2 9 7 1 9 6 5 9 8 4 9 5 2

3 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 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 4 5 1. So node would be set to 4.

5 5 The Graph and Its Data File DE HI FC G BA 8 6 5 3 3 2 2 2 1 6 1 4 2 47 1 2 2 1 6 7 1 7 3 2 1 2 2 3 4 2 7 6 3 2 4 3 8 2 3 4 2 4 5 1 4 8 8 4 3 2 5 4 1 5 9 2 5 6 6 6 1 7 6 9 5 6 5 6 7 2 6 7 1 3 7 9 1 7 8 3 8 7 3 8 9 4 8 4 8 8 3 2 9 7 1 9 6 5 9 8 4 9 5 2

6 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 V2. 4 5 1 4 8 8 4 3 2

7 7 The Graph and Its Data File DE HI FC G BA 8 6 5 3 3 2 2 2 1 6 1 4 2 47 1 2 2 1 6 7 1 7 3 2 1 2 2 3 4 2 7 6 3 2 4 3 8 2 3 4 2 4 5 1 4 8 8 4 3 2 5 4 1 5 9 2 5 6 6 6 1 7 6 9 5 6 5 6 7 2 6 7 1 3 7 9 1 7 8 3 8 7 3 8 9 4 8 4 8 8 3 2 9 7 1 9 6 5 9 8 4 9 5 2

8 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: 4 5 1 4 8 8 4 3 2 so first one is selected and node is set to 5. state[nxtnod[$]] = 1; The edge 4 5 receives a state of 1.

9 9 The Graph and Its Data File DE HI FC G BA 8 6 5 3 3 2 2 2 1 6 1 4 2 47 1 2 2 1 6 7 1 7 3 2 1 2 2 3 4 2 7 6 3 2 4 3 8 2 3 4 2 4 5 1 4 8 8 4 3 2 5 4 1 5 9 2 5 6 6 6 1 7 6 9 5 6 5 6 7 2 6 7 1 3 7 9 1 7 8 3 8 7 3 8 9 4 8 4 8 8 3 2 9 7 1 9 6 5 9 8 4 9 5 2

10 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 11 The Graph and Its Data File DE HI FC G BA 8 6 5 3 3 2 2 2 1 6 1 4 2 47 1 2 2 1 6 7 1 7 3 2 1 2 2 3 4 2 7 6 3 2 4 3 8 2 3 4 2 4 5 1 4 8 8 4 3 2 5 4 1 5 9 2 5 6 6 6 1 7 6 9 5 6 5 6 7 2 6 7 1 3 7 9 1 7 8 3 8 7 3 8 9 4 8 4 8 8 3 2 9 7 1 9 6 5 9 8 4 9 5 2 Green entries are edges thrown out.

12 12 if (state[$] == 3 && node == tail[$]) then state[$] = 2; endif; The edges turned to a state of 2 are then: 5 4 5 9 5 6 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 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 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 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 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 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 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 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 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 21 Example of the Greedy-choice – only part of the graph is shown set S s a b 1 2 e c x 3 1 2 4 2 2 3 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.


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

Similar presentations


Ads by Google