Title: ASC Program Example Part 3 of Associative Computing
1ASC Program ExamplePart 3 of Associative
Computing
- Examining the MST code in ASC Primer
2The Graph and Its Data File
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
2
4
7
3
6
5
1
2
3
2
6
4
8
2
1
3Header 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
4Obtain 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 wasnt read in would be
used.) Find a tail whose weight is
minimal. setscope graph node
tailmindex(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.
5The Graph and Its Data File
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
2
4
7
3
6
5
1
2
3
2
6
4
8
2
1
6Mark 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
7The Graph and Its Data File
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
2
4
7
3
6
5
1
2
3
2
6
4
8
2
1
8while xx in (state 2) if (state
2) then nxtnod mindex(weight) endif
node headnxtnod 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. statenxtnod 1 The edge 4 5
receives a state of 1.
9The Graph and Its Data File
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
2
4
7
3
6
5
1
2
3
2
6
4
8
2
1
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 states to
0. This would be edges 6 5 and 9 5 in data file.
11The Graph and Its Data File
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
2
4
7
3
6
5
1
2
3
2
6
4
8
2
1
Green entries are edges thrown out.
12if (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? Isnt 4 5 already in? Solving the problem by
using a picture didnt 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?
13Try 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 nodes 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 tailmindex(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)
14Another 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.
15The 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 doesnt have the
greedy-choice property. In fact, a slight change
can produce an NP-complete problem.
16Some 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 bi
and a positive weight wi. 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.
17However, 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 doesnt 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 2n, then the pseudo-polynomial
algorithm for this problem is as bad as the brute
force method of trying all combinations.
18Some 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 si
and a finish time fi where si lt fi. - Task i must start at time si and it is guaranteed
to be finished by time fi. - 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 fi lt
sj or fj lt si. - 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?
19A 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
wont work for this problem. - Find one. (Homework)
20Dijkstras 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 doesnt 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. -
21Example of the Greedy-choice only part of the
graph is shown
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
3
c
a
1
2
1
4
s
x
2
b
2
2
3
e
set S
Therefore, let d(x) 3 and put x in S.